def test_toString(self): f = Formula( [ PyInequality([Term(2,-1)], 1) , PyInequality([Term(2, 2)], 2) ] ) stream = io.StringIO() f.write(stream) print(stream.getvalue()) assert stream.getvalue() == \ "* #variable= 42 #constraint= 2\n" \ "+2 ~x1 >= 1;\n" \ "+2 x2 >= 2;\n"
def compute(self, antecedents, context): checkSolution(context.propEngine, context.ineqFactory, self.partialAssignment) return [ context.ineqFactory.fromTerms( [Term(1, -lit) for lit in self.partialAssignment], 1) ]
def test_eq(self): res = OPBParser(self.ineqFactory).parseOPB("1 x2 -2 x1 = 2;".split()) if self.ineqFactory.freeNames: ineq1 = self.ineqFactory.fromTerms([Term(-2,2), Term(1,1)], 2) ineq2 = self.ineqFactory.fromTerms([Term(2,2), Term(-1,1)], -2) else: ineq1 = self.ineqFactory.fromTerms([Term(-2,1), Term(1,2)], 2) ineq2 = self.ineqFactory.fromTerms([Term(2,1), Term(-1,2)], -2) assert res == [ineq1, ineq2]
def write(self, stream): stream.write("* #variable= %i #constraint= %i\n" % (self.numVars, self.numConstraints)) for c in self.constraints: try: stream.write(c.toOPB()) except AttributeError: Inequality([Term(coeff, var) for coeff, var in c.terms], c.degree).toOPB() else: stream.write(";\n")
def parseCNF(self, words): words = iter(words) lits = list() try: nxt = int(next(words)) while (nxt != 0): lits.append(nxt) nxt = int(next(words)) except StopIteration: raise ValueError("Expected 0 at end of constraint.") return [ self.ineqFactory.fromTerms( [Term(1, self.ineqFactory.intlit2int(l)) for l in lits], 1) ]
def checkParsing(self, terms, degree, eq = False): termString = " ".join(["%i %s" % (coeff, lit2str(lit)) for coeff, lit in terms]) op = "=" if eq else ">=" string = "%s %s %i ;" % (termString, op, degree) terms = [Term(coeff, lit) for coeff, lit in terms] expect = [] expect.append(self.ineqFactory.fromTerms(terms, degree)) if eq: for term in terms: term.coefficient = -term.coefficient degree = -degree expect.append(self.ineqFactory.fromTerms(terms, degree)) res = self.ineqFactory.parseString(string, allowMultiple = True) assert res == expect
def compute(self, antecedents, context): checkSolution(context.propEngine, context.ineqFactory, self.partialAssignment) objValue = 0 numFoundValues = 0 for lit in self.partialAssignment: if abs(lit) in context.objective: numFoundValues += 1 if lit > 0: objValue += context.objective[lit] if len(context.objective) != numFoundValues: raise ObjectiveNotFullyAssigned() # obj <= objvalue - 1 lowerBound = context.ineqFactory.fromTerms( [Term(-coeff, lit) for lit, coeff in context.objective.items()], -(objValue - 1)) return [lowerBound]
def clause(literals): return Inequality([Term(1, l) for l in literals], 1)
def toTerms(terms): return list(map(lambda x: Term(x[0], x[1]), terms))
def test_implies(self): rule = ConstraintImplies.parse("42 3 x1 >= 2;", self.context) assert rule == ConstraintImplies(42, self.ineqFactory.fromTerms([Term(3,1)], 2))
def test_CNF_line(self): res = OPBParser(self.ineqFactory).parseCNF("-1 2 0".split()) assert res == [self.ineqFactory.fromTerms([Term(1,2), Term(1,-1)], 1)]
def test_OPB_line_2(self): res = OPBParser(self.ineqFactory).parseOPB("+2 x1 -4 x2 = -42;".split()) assert res == [ self.ineqFactory.fromTerms([Term(2,1), Term(-4,2)], -42), self.ineqFactory.fromTerms([Term(-2,1), Term(4,2)], 42)]
def test_OPB_line_1(self): res = OPBParser(self.ineqFactory).parseOPB("3 x1 >= 2;".split()) assert res == [self.ineqFactory.fromTerms([Term(3,1)], 2)]
def test_OPB_line_1(self): res = self.ineqFactory.parseString("3 x1 >= 2 ;", allowMultiple = True) assert res == [self.ineqFactory.fromTerms([Term(3,1)], 2)]