示例#1
0
 def __init__(self,variables):
     self.variables    = variables
     self.equalities   = Equalities(self.variables)
     self.inequalities = Inequalities(self.variables)
     
     self._container_map = {
         '=' : self.equalities ,
         '>' : self.inequalities,
         '<' : self.inequalities,
     }               
示例#2
0
class Constraints(Object):
    
    def __init__(self,variables):
        self.variables    = variables
        self.equalities   = Equalities(self.variables)
        self.inequalities = Inequalities(self.variables)
        
        self._container_map = {
            '=' : self.equalities ,
            '>' : self.inequalities,
            '<' : self.inequalities,
        }               
    
    def __set__(self,problem,arg_list):            
        self.clear()
        self.extend(arg_list)
        
    def clear(self):
        self.equalities.clear()
        self.inequalities.clear()
    
    def append(self, evaluator, 
               tag=None, sense='=', edge=0.0, 
               scale=1.0 ):
        
        if type(evaluator) is Constraint:
            constraint = evaluator
            evaluator = constraint.evaluator
            tag   = constraint.tag
            sense = constraint.sense
            edge  = constraint.edge
            scale = constraint.scale
            
        if tag is None and isinstance(evaluator,Constraint):
            constraint = evaluator
            constraint.variables = self.variables
        else:
            constraint = class_map[sense](evaluator,tag,sense,edge,scale,self.variables)
        
        constraint.__check__()
        
        sense = constraint.sense
        if sense not in class_map.keys():
            raise KeyError , 'invalid constraint sense "%s"' % sense        
        
        tag = constraint.tag
        self._container_map[sense][tag] = constraint
        
    def extend(self,arg_list):
        for args in arg_list:
            args = flatten_list(args)
            self.append(*args)
            
    def tags(self):
        return self.equalities.tags() + self.inequalities.tags()
    def senses(self):
        return self.equalities.senses() + self.inequalities.senses()
    def edges(self):
        return self.equalities.edges() + self.inequalities.edges()
    def scales(self):
        return self.equalities.scales() + self.inequalities.scales()
    def evaluators(self):
        return self.equalities.evaluators() + self.inequalities.evaluators()
    
    # no set(), modify constraints by type
    
    def __len__(self):
        return len(self.equalities) + len(self.inequalities)
    def items(self):
        return self.equalities.items() + self.inequalities.items()
    def __repr__(self):
        return repr(self.equalities) + '\n' + repr(self.inequalities)
    def __str__(self):
            return str(self.equalities) + '\n' + str(self.inequalities)