JobShopProblem is now a Mapping, not a list
- implementing all interface functions for JobShopProblem to behave like a Mapping (e.g. dict) with (int, int) keys this implements and closes #14
This commit is contained in:
		
							parent
							
								
									edd413db54
								
							
						
					
					
						commit
						252f3a824e
					
				
					 2 changed files with 33 additions and 4 deletions
				
			
		| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
from typing import List, Tuple
 | 
					from typing import List, Tuple, Generator
 | 
				
			||||||
import arpeggio
 | 
					import arpeggio
 | 
				
			||||||
from itertools import chain
 | 
					from itertools import chain
 | 
				
			||||||
 | 
					from collections.abc import Mapping
 | 
				
			||||||
 | 
					
 | 
				
			||||||
__all__ = ["js1_style", "js2_style"]
 | 
					__all__ = ["js1_style", "js2_style"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -43,7 +44,7 @@ class ParseError(Exception):
 | 
				
			||||||
        self.message = message
 | 
					        self.message = message
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class JobShopProblem(list):
 | 
					class JobShopProblem(Mapping):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(
 | 
					    def __init__(
 | 
				
			||||||
            self, jobs: int, machines: int,
 | 
					            self, jobs: int, machines: int,
 | 
				
			||||||
| 
						 | 
					@ -59,11 +60,39 @@ class JobShopProblem(list):
 | 
				
			||||||
        self.name = name
 | 
					        self.name = name
 | 
				
			||||||
        self.machines = machines
 | 
					        self.machines = machines
 | 
				
			||||||
        self.jobs = jobs
 | 
					        self.jobs = jobs
 | 
				
			||||||
        super().__init__(problem_data)
 | 
					        self.problem_data = problem_data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __str__(self) -> str:
 | 
					    def __str__(self) -> str:
 | 
				
			||||||
        return "JobShopProblem " + str(self.name)
 | 
					        return "JobShopProblem " + str(self.name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # make this behave like a mapping (e.g. a dict)
 | 
				
			||||||
 | 
					    def __getitem__(self, key: Tuple[int, int]) -> Tuple[int, int]:
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            # unpacking key
 | 
				
			||||||
 | 
					            (jobnum, tasknum) = key
 | 
				
			||||||
 | 
					            return self.problem_data[jobnum][tasknum]
 | 
				
			||||||
 | 
					        except ValueError as e:
 | 
				
			||||||
 | 
					            raise TypeError("Key must be a (int, int) tuple")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __iter__(self):
 | 
				
			||||||
 | 
					        self.iterpos1 = 0
 | 
				
			||||||
 | 
					        self.iterpos2 = 0
 | 
				
			||||||
 | 
					        return self
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __next__(self) -> Tuple[int, int]:
 | 
				
			||||||
 | 
					        if self.iterpos1 >= len(self.problem_data):
 | 
				
			||||||
 | 
					            raise StopIteration
 | 
				
			||||||
 | 
					        return_key = (self.iterpos1, self.iterpos2)
 | 
				
			||||||
 | 
					        self.iterpos2 += 1
 | 
				
			||||||
 | 
					        if self.iterpos2 >= len(self.problem_data[self.iterpos1]):
 | 
				
			||||||
 | 
					            self.iterpos2 = 0
 | 
				
			||||||
 | 
					            self.iterpos1 += 1
 | 
				
			||||||
 | 
					        return return_key
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __len__(self) -> int:
 | 
				
			||||||
 | 
					        return sum(map(len, self.problem_data))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class JobShopVisitor(arpeggio.PTNodeVisitor):
 | 
					class JobShopVisitor(arpeggio.PTNodeVisitor):
 | 
				
			||||||
    """contains visitor functions needed for both jobshop1 (list of instances)
 | 
					    """contains visitor functions needed for both jobshop1 (list of instances)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -45,7 +45,7 @@ class JobShop1Visitor(JobShopVisitor):
 | 
				
			||||||
        return None
 | 
					        return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def parse_string(inputdata: str) -> JobShopProblem:
 | 
					def parse_string(inputdata: str) -> List[JobShopProblem]:
 | 
				
			||||||
    """parse string with jobshop1-formatted data (multiple problem instances)
 | 
					    """parse string with jobshop1-formatted data (multiple problem instances)
 | 
				
			||||||
    and return list of JobShopProblem s"""
 | 
					    and return list of JobShopProblem s"""
 | 
				
			||||||
    parse_tree = parser.parse(inputdata)
 | 
					    parse_tree = parser.parse(inputdata)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue