def addConstraints(self, Solver): if self.parameters: if self.quantifier == 'All': for p in self.parameters: Solver.add(CPS.InSetConstraint(self.set), [p]) elif self.quantifier == 'Some': Solver.add(CPS.SomeInSetConstraint(self.set), self.parameters)
def addConstraints(self, Solver): if self.op_str=='!=': Solver.add(CPS.AllDifferentConstraint(), [self.LHS, self.RHS]) elif self.op_str=='=': Solver.add(CPS.AllEqualConstraint(), [self.LHS, self.RHS]) else: parameters = [self.LHS, self.RHS] Solver.add(CPS.FunctionConstraint(FuncWrapper(self.op, parameters)), parameters)
def initialize(self, Model): self.regulator = self.regulator.initialize(Model) self.target = self.target.initialize(Model) accepted = False for comp, thrs in self.regulator.targets: if comp == self.target: if self.thr in thrs: accepted = True break if not accepted: raise Exception( 'Non-existing threshold %i for interaction %s->%s' % (self.thr, self.regulator, self.target)) ParsLHS = [ p for p in self.target.parameters if self.thr in p.context[self.regulator] ] ParsRHS = [ p for p in self.target.parameters if self.thr - 1 in p.context[self.regulator] ] if self.formula: self.formula.initialize(Model) prob = CPS.Problem() pairs = [] for p1, p2 in zip(ParsLHS, ParsRHS): for comp in Model.components: domain = list(p1.context.get(comp, range(comp.max + 1))) prob.addVariable(comp.index, domain) prob.addConstraint( CPS.FunctionConstraint( Basics.FuncWrapper(self.formula, Model.components)), Model.components) if prob.getSolution(): pairs.append((p1, p2)) prob.reset() self.pairs = pairs else: self.pairs = zip(ParsLHS, ParsRHS) self.requirements = [] if self.label in ConjLabelEval: self.op_conj = ConjLabelEval[self.label] self.requirements.append(self.ConjEval) if self.label in DisjLabelEval: self.op_disj = DisjLabelEval[self.label] self.requirements.append(self.DisjEval)
def addConstraints(self, Solver): if self.quant == 'All': for p in self.parameters: activities = tuple( [i for i in p.range if self.op(i, self.act)]) Solver.add(CPS.InSetConstraint(activities), [p]) elif self.quant == 'Some': domain = set([]) for p in self.parameters: activities = set([i for i in p.range if self.op(i, self.act)]) domain.update(activities) Solver.add(CPS.SomeInSetConstraint(domain), self.parameters)
def addConstraints(self, Solver): if self.label in ConjLabelEval: parameters = [p for pair in self.pairs for p in pair] for pair in self.pairs: func = self.pair_func(pair, self.op_conj) Solver.add( CPS.FunctionConstraint(Basics.FuncWrapper(func, pair)), pair) if self.label in DisjLabelEval: parameters = [p for pair in self.pairs for p in pair] Solver.add( CPS.FunctionConstraint( Basics.FuncWrapper(self.DisjEval, parameters)), parameters)
def initialize(self, Model): self.formula.initialize(Model) self.comp = self.comp.initialize(Model) prob = CPS.Problem() self.ones = [] self.zeros = [] for p in self.comp.parameters: for comp in Model.components: domain = list(p.context.get(comp, range(comp.max + 1))) prob.addVariable(comp, domain) prob.addConstraint( CPS.FunctionConstraint( Basics.FuncWrapper(self.formula, Model.components)), Model.components) if prob.getSolution(): self.ones.append(p) else: self.zeros.append(p) prob.reset()
def initialize(self, Model): self.formula.initialize(Model) self.comp = self.comp.initialize(Model) prob = CPS.Problem() params = [] func = self.formula for p in self.comp.parameters: for comp in Model.components: domain = list(p.context.get(comp, range(comp.max+1))) prob.addVariable(comp.index, domain) prob.addConstraint(CPS.FunctionConstraint(FuncWrapper(self.formula, Model.components)), Model.components) if prob.getSolution(): params.append(p) prob.reset() if not params: raise Exception("Empty parameter reference '%s', please update."%self.formula) return params
def initialize(self, Model): for f in self.formulas: f.initialize(Model) self.comp = self.comp.initialize(Model) prob = CPS.Problem() classes = {} for p in self.comp.parameters: Id = [] for func in self.formulas: for comp in Model.components: domain = list(p.context.get(comp, range(comp.max + 1))) prob.addVariable(comp, domain) prob.addConstraint( CPS.FunctionConstraint( Basics.FuncWrapper(func, Model.components)), Model.components) if prob.getSolution(): Id.append(True) else: Id.append(False) prob.reset() Id = tuple(Id) if Id in classes: classes[Id].append(p) else: classes[Id] = [p] self.classes = [tuple(c) for c in classes.values() if len(c) > 1] if not self.classes: raise Exception("'%s' defines only empty classes, please remove." % self.s) self.parameters = set([p for c in classes.values() for p in c])
def addConstraints(self, Solver): parameters = tuple(self.used_variables()) Solver.add(CPS.FunctionConstraint(FuncWrapper(self.__call__, parameters)), parameters)
def addConstraints(self, Solver): domain = tuple([i for i in self.LHS.range if self.op(i, self.RHS)]) Solver.add(CPS.InSetConstraint(domain), [self.LHS])
def addConstraints(self, Solver): if isinstance(self.RHS, int): domain = tuple([i for i in range(self.LHS.max+1) if self.op(i, self.RHS)]) Solver.add(CPS.InSetConstraint(domain), [self.LHS]) else: Solver.add(CPS.FunctionConstraint(FuncWrapper(self.relative, [self.LHS, self.RHS])), [self.LHS, self.RHS])
def addConstraints(self, Solver): for p, op, op_str, val in self.requirements: domain = tuple([i for i in p.range if op(i, val)]) Solver.add(CPS.InSetConstraint(domain), [p])
def addConstraints(self, Solver): if self.parameters: Solver.add( CPS.FunctionConstraint( Basics.FuncWrapper(self.__call__, self.parameters)), self.parameters)
def addConstraints(self, Solver): if self.parameters: Solver.add(CPS.AllEqualConstraint(), self.parameters)
def addConstraints(self, Solver): if self.zeros: Solver.add(CPS.InSetConstraint([0]), self.zeros) if self.ones: Solver.add(CPS.InSetConstraint([1]), self.ones)
def addConstraints(self, Solver): for c in self.classes: Solver.add(CPS.AllEqualConstraint(), c)
def prepare_solutions(self, verbose=-1): free_variables = set(list(self.variables)) groups = [] nodes = self.constraints[:] while nodes: c1, v1 = nodes.pop() free_variables.difference_update(v1) hit = [] for i, group in enumerate(groups): for c2, v2 in group: if set(v1).intersection(set(v2)): group.append((c1, v1)) hit.append(i) break newgroup = [(c1, v1)] for i in hit: newgroup.extend(groups[i]) groups = [g for i, g in enumerate(groups) if i not in hit] groups.append(newgroup) bound_variables = set([]) problems = [] for group in groups: problem = CPS.Problem() test = CPS.Problem() variables = set([]) for c, v in group: problem.addConstraint(c, v) test.addConstraint(c, v) variables.update(set(v)) if verbose and variables: for v in variables: bound_variables.add(v.name) for v in variables: problem.addVariable(v, v.range) test.addVariable(v, v.range) if not test.getSolution(): print '!!No solutions to constraints involving the parameters:' print ' ' + ','.join([str(v) for v in variables]) problems.append(problem.getSolutionIter()) if free_variables: if verbose > -1: print print 'Split into', len(groups), 'problems.' print ' bound variables: ', ','.join( [str(n) for n in bound_variables]) print ' free variables: ', ','.join( [str(i) for i in free_variables]) for variable in free_variables: problems.append(FreeSolutions([variable])) #problems.append(FreeSolutions(free_variables)) self.problems = problems