def isOff(self, index): ''' Returns a Boolean Constraint stating whether or not the instance at the given index is *off*. An instance is off if it is set to self.parentInstances. ''' try: return SMTLib.SMT_EQ(self.instances[index], SMTLib.SMT_IntConst(self.parentInstances)) except: return SMTLib.SMT_EQ(index, SMTLib.SMT_IntConst(self.parentInstances))
def joinWithParent(arg): instances = arg.getInstances(nonsupered=True) newInstances = {} for (sort, index) in instances.keys(): (expr, pol) = instances[(sort, index)] if pol == Common.DEFINITELY_OFF: continue (lower, upper, _) = sort.instanceRanges[index] for i in range(lower, min(sort.parentInstances, upper + 1)): (old_expr, old_pol) = newInstances.get( (sort.parent, i), (SMTLib.SMT_BoolConst(False), Common.DEFINITELY_OFF)) if pol == Common.DEFINITELY_ON and lower == upper: new_pol = Common.DEFINITELY_ON new_expr = SMTLib.SMT_BoolConst(True) else: new_pol = Common.aggregate_polarity(old_pol, Common.UNKNOWN) new_expr = mOr( old_expr, mAnd( expr, SMTLib.SMT_EQ(sort.instances[index], SMTLib.SMT_IntConst(i)))) newInstances[(sort.parent, i)] = (new_expr, new_pol) return ExprArg(newInstances, nonsupered=True)
def joinWithClafer(left, right): newInstances = {} leftInstances = left.getInstances(nonsupered=True) rightInstances = right.getInstances(nonsupered=True) for (lsort, lindex) in leftInstances.keys(): (lexpr, lpolarity) = leftInstances[(lsort, lindex)] if lpolarity == Common.DEFINITELY_OFF: continue for (rsort, rindex) in rightInstances.keys(): (_rexpr, rpolarity) = rightInstances[(rsort, rindex)] if rpolarity == Common.DEFINITELY_OFF: continue noMatch = False while not (rsort in lsort.fields): if not lsort.superSort: noMatch = True break (lsort, lindex) = joinWithSuper(lsort, lindex) if noMatch: continue (lower, upper, _) = rsort.instanceRanges[rindex] (new_rexpr, new_rpol) = newInstances.get((rsort, rindex), ({}, {})) new_rpol[lindex] = lpolarity new_rexpr[lindex] = mAnd( lexpr, SMTLib.SMT_EQ(rsort.instances[rindex], SMTLib.SMT_IntConst(lindex))) if lower <= lindex and lindex <= upper: newInstances[(rsort, rindex)] = (new_rexpr, new_rpol) newInstances = flattenInstances(newInstances) return ExprArg(newInstances, nonsupered=True)
def int_set_in(leftIntSort, rightIntSort): (left_sort, left_mask) = leftIntSort (right_sort, right_mask) = rightIntSort cond = [] for i in left_mask.keys(): constraint = SMTLib.SMT_Or( SMTLib.SMT_EQ(left_sort.cardinalityMask.get(i), SMTLib.SMT_IntConst(0)), SMTLib.SMT_Or(*[ SMTLib.SMT_And( SMTLib.SMT_EQ(right_sort.cardinalityMask.get(j), SMTLib.SMT_IntConst(1)), SMTLib.SMT_EQ(right_mask.get(j), left_mask.get(i))) for j in right_mask.keys() ])) cond.append(constraint) return (SMTLib.SMT_And(*cond))
def ConstraintEqualToX(self, model): """ Returns a Constraint that a new instance, can't be dominated by the instance represented by model. (it can't be worst in any objective). """ EqualMetrics = list() for i in range(len(self.metrics_variables)): EqualMetrics.append( SMTLib.SMT_EQ( self.metrics_variables[i], Common.evalForNum(model, self.metrics_variables[i]))) return SMTLib.SMT_And(EqualMetrics)
def quant_one(exprs, ifConstraints): ''' There's probably a better way to do this. ''' condList = getQuantifierConditionList(exprs) if ifConstraints: condList = [mAnd(i, j) for i, j in zip(ifConstraints, condList)] exprList = [] for i in range(len(condList)): exprList.append( SMTLib.SMT_If(condList[i], SMTLib.SMT_IntConst(1), SMTLib.SMT_IntConst(0))) return SMTLib.SMT_EQ(SMTLib.createSum(*exprList), SMTLib.SMT_IntConst(1))
def createInstancesConstraintsAndFunctions(self): for i in range(self.numInstances): (lower, upper, extraAbsenceConstraint) = self.instanceRanges[i] #lower == upper case (simpler) if lower == upper: constraint = SMTLib.SMT_EQ(self.instances[i], SMTLib.SMT_IntConst(upper)) if extraAbsenceConstraint: self.constraints.addInstanceConstraint( SMTLib.SMT_Or(self.isOff(i), constraint)) else: #TODO self.instances[i] = SMTLib.SMT_IntConst(lower) self.constraints.addInstanceConstraint(constraint) else: #parent pointer is >= lower self.constraints.addInstanceConstraint( SMTLib.SMT_GE(self.instances[i], SMTLib.SMT_IntConst(lower))) constraint = SMTLib.SMT_LE(self.instances[i], SMTLib.SMT_IntConst(upper)) if extraAbsenceConstraint: #parent pointer is <= upper , or equal to parentInstances self.constraints.addInstanceConstraint( SMTLib.SMT_Or(self.isOff(i), constraint)) else: #parent pointer is <= upper self.constraints.addInstanceConstraint(constraint) #sorted parent pointers (only consider things that are not part of an abstract) if not self.beneathAnAbstract: if i != self.numInstances - 1: self.constraints.addInstanceConstraint( SMTLib.SMT_LE(self.instances[i], self.instances[i + 1])) if not self.parent: return #if the parent is not live, then no child can point to it for i in range(self.parent.numInstances): for j in range(self.numInstances): self.constraints.addInstanceConstraint( SMTLib.SMT_Implies( self.parent.isOff(i), SMTLib.SMT_NE(self.instances[j], SMTLib.SMT_IntConst(i))), self.parent.known_polarity(i, local=True) != Common.DEFINITELY_ON)
def createCardinalityConstraints(self): if not self.cfr.isUsed(self.element): return self.summs = [[] for i in range(self.parentInstances + 1)] for i in range(self.numInstances): (lower, upper, _) = self.getInstanceRange(i) for j in range(lower, upper + 1): self.summs[j].append( SMTLib.SMT_If( SMTLib.SMT_EQ(self.instances[i], SMTLib.SMT_IntConst(j)), SMTLib.SMT_IntConst(1), SMTLib.SMT_IntConst(0))) for i in range(len(self.summs)): if self.summs[i]: self.summs[i] = SMTLib.createSum(*[self.summs[i]]) else: self.summs[i] = SMTLib.SMT_IntConst(0) for i in range(self.parentInstances): if self.parent: self.constraints.addCardConstraint( SMTLib.SMT_Implies( self.parent.isOn(i), SMTLib.SMT_GE( self.summs[i], SMTLib.SMT_IntConst(self.lowerCardConstraint)))) if self.upperCardConstraint != -1: self.constraints.addCardConstraint( SMTLib.SMT_Implies( self.parent.isOn(i), SMTLib.SMT_LE( self.summs[i], SMTLib.SMT_IntConst( self.upperCardConstraint)))) else: self.constraints.addCardConstraint( SMTLib.SMT_GE( self.summs[i], SMTLib.SMT_IntConst(self.lowerCardConstraint))) if self.upperCardConstraint != -1: self.constraints.addCardConstraint( SMTLib.SMT_LE( self.summs[i], SMTLib.SMT_IntConst(self.upperCardConstraint)))
def joinWithClaferRef(arg): instances = arg.getInstances(nonsupered=True) newArg = ExprArg(nonsupered=True) for (sort, index) in instances.keys(): (expr, pol) = instances[(sort, index)] while not sort.refSort: (sort, index) = joinWithSuper(sort, index) if isinstance(sort.refSort, PrimitiveType): joinWithPrimitive(newArg, sort, index, expr, pol) else: for i in range(sort.refSort.numInstances): (prev_expr, _) = newArg.getInstances(nonsupered=True).get( (sort.refSort, i), (SMTLib.SMT_BoolConst(False), Common.DEFINITELY_OFF)) newArg.getInstances(nonsupered=True)[(sort.refSort, i)] = (mOr( prev_expr, mAnd( expr, SMTLib.SMT_EQ( sort.refs[index], SMTLib.SMT_IntConst(i)))), Common.UNKNOWN) return newArg
def addRefConstraints(self): if not self.refSort: return elif isinstance(self.refSort, PrimitiveType) and self.refSort.type == "real": self.refs = SMTLib.SMT_RealVector(self.element.uid + "_ref", self.numInstances) elif isinstance(self.refSort, PrimitiveType): self.refs = SMTLib.SMT_IntVector(self.element.uid + "_ref", self.numInstances) else: self.refs = SMTLib.SMT_IntVector( self.element.uid + "_ref", self.numInstances, bits=self.getBits(self.refSort.parentInstances + 1)) if not isinstance(self.refSort, PrimitiveType): for i in range(self.numInstances): #refs pointer is >= 0 self.constraints.addRefConstraint( SMTLib.SMT_GE(self.refs[i], SMTLib.SMT_IntConst(0))) #ref pointer is <= upper card of ref parent self.constraints.addRefConstraint( SMTLib.SMT_LE( self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances))) #if integer refs, zero out refs that do not have live parents, #if clafer refs, set equal to ref.parentInstances if not live #reference symmetry breaking if not self.element.isAbstract: for i in range(self.numInstances - 1): for j in range(i + 1, self.numInstances): if isinstance(self.refSort, PrimitiveType): self.constraints.addRefConstraint( SMTLib.SMT_Implies( SMTLib.SMT_EQ(self.instances[i], self.instances[j]), SMTLib.SMT_LE(self.refs[i], self.refs[j]))) else: self.constraints.addRefConstraint( SMTLib.SMT_Implies( mAnd( SMTLib.SMT_NE( self.refs[i], SMTLib.SMT_IntConst( self.refSort.numInstances)), SMTLib.SMT_EQ(self.instances[i], self.instances[j])), SMTLib.SMT_LE(self.refs[i], self.refs[j]))) for i in range(self.numInstances): if isinstance(self.refSort, PrimitiveType): if self.refSort == "integer": self.constraints.addRefConstraint( SMTLib.SMT_Implies( self.isOff(i), SMTLib.SMT_EQ(self.refs[i], SMTLib.SMT_IntConst(0))), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) elif self.refSort == "string": if Options.STRING_CONSTRAINTS: self.constraints.addRefConstraint( SMTLib.SMT_Implies( self.isOff(i), SMTLib.SMT_EQ(self.refs[i], self.cfr.EMPTYSTRING)), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) else: self.constraints.addRefConstraint( SMTLib.SMT_Implies( self.isOff(i), SMTLib.SMT_EQ(self.refs[i], SMTLib.SMT_IntConst(0))), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) else: self.constraints.addRefConstraint( SMTLib.SMT_Implies( self.isOff(i), SMTLib.SMT_EQ(self.refs[i], SMTLib.SMT_IntConst(0))), self.known_polarity(i, local=True) != Common.DEFINITELY_ON) else: if self.known_polarity(i, local=True) != Common.DEFINITELY_ON: self.constraints.addRefConstraint( SMTLib.SMT_If( self.isOff(i), SMTLib.SMT_EQ( self.refs[i], SMTLib.SMT_IntConst( self.refSort.numInstances)), SMTLib.SMT_NE( self.refs[i], SMTLib.SMT_IntConst( self.refSort.numInstances)))) else: self.constraints.addRefConstraint( SMTLib.SMT_NE( self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances))) #if refsort.full does not exist, create it if not self.refSort.full: self.refSort.full = lambda x: mOr(*[ SMTLib.SMT_And( SMTLib.SMT_EQ(x, SMTLib.SMT_IntConst(i)), self.refSort.isOn(i)) for i in range(self.refSort.numInstances) ]) #the clafer that the reference points to must be "on" self.constraints.addRefConstraint( SMTLib.SMT_Implies( SMTLib.SMT_NE( self.refs[i], SMTLib.SMT_IntConst(self.refSort.numInstances)), self.refSort.full(self.refs[i])))
def op_eq(left, right, cacheJoins=False, bc=None): ''' :param left: :type left: :class:`~ExprArg` :param right: :type right: :class:`~ExprArg` :returns: :class:`~BoolArg` Ensures that the left = right. ''' assert isinstance(left, ExprArg) assert isinstance(right, ExprArg) if cacheJoins and bc: #TODO CLEAN left_key = None right_key = None keys = [] #asil allocation speedup, if both sides are sets, we can perform expression substitution in other constraints #bc is the bracketed constraint to put the cache for i in [left, right]: if isinstance(i, JoinArg): newkeys = Common.computeCacheKeys(i.flattenJoin()) #print(tuple(key)) keys = keys + newkeys #need to return all keys during the progress of join, add flag? #get the all keys all_keys = i.checkIfJoinIsComputed(nonsupered=True, getAllKeys=True) #print(keys) #print(all_keys) keys = keys + all_keys #sys.exit() #print() #print("GGGG right" + str(right.__class__)) #print(right.clafers) if len(left.clafers) != len(right.clafers): minJoinVal = left.clafers if len(left.clafers) < len( right.clafers) else right.clafers for i in keys: #TODO make more robust (e.g. if multiple equalities exist for the same join key, aggregate expressions bc.cache[i] = ExprArg(minJoinVal) #print(i) #print(minJoinVal) #print(str(len(minJoinVal)) + " " + str(len(left.clafers)) + " " + str(len(right.clafers))) #print(str(len(left.clafers)) + " " + str(len(right.clafers))) cond = [] #int equality case lints = [(e, c) for (e, c) in left.getInts() if str(c) != "False"] rints = [(e, c) for (e, c) in right.getInts() if str(c) != "False"] if lints or rints: for (e, c) in lints: #exists r in R s.t. e == r expr = mOr(*[mAnd(rc, SMTLib.SMT_EQ(e, r)) for (r, rc) in rints]) if str(c) != "True": expr = SMTLib.SMT_Implies(c, expr) cond.append(expr) for (e, c) in rints: #exists l in L s.t. e == l expr = mOr(*[mAnd(lc, SMTLib.SMT_EQ(e, l)) for (l, lc) in lints]) if str(c) != "True": expr = SMTLib.SMT_Implies(c, expr) cond.append(expr) #clafer-set equality case matches = getSetInstancePairs(left, right) for ((lexpr, lpol), (rexpr, rpol)) in matches.values(): if lpol == Common.DEFINITELY_OFF and rpol == Common.DEFINITELY_OFF: continue elif lpol == Common.DEFINITELY_OFF: cond.append(SMTLib.createNot(rexpr)) elif rpol == Common.DEFINITELY_OFF: cond.append(SMTLib.createNot(lexpr)) else: cond.append(SMTLib.SMT_Implies(lexpr, rexpr)) cond.append(SMTLib.SMT_Implies(rexpr, lexpr)) return BoolArg(mAnd(*cond))