Beispiel #1
0
def mAnd(*args):
    '''
    Short for MaybeAnd.
    Helper Function to simplify formulas passed to Z3, but mostly to make debugging output more comprehensible.
    Only applies the And function if there are actually multiple arguments.
    '''
    args = list(args)
    newArgs = []
    while (args):
        i = args.pop()
        if isinstance(i, SMTLib.SMT_And):
            for j in i.children():
                args.append(j)
            continue
        if i:
            if str(i) == "True":
                continue
            if str(i) == "False":
                return SMTLib.SMT_BoolConst(False)
            newArgs.append(i)
    if len(newArgs) == 0:
        return SMTLib.SMT_BoolConst(True)
    elif len(newArgs) == 1:
        return newArgs[0]
    else:
        return SMTLib.SMT_And(*newArgs)
Beispiel #2
0
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))
Beispiel #3
0
 def addSubSortConstraints(self, sub):
     #the super cannot exist without the sub, and vice-versa
     for i in range(sub.numInstances):
         self.constraints.addInheritanceConstraint(
             SMTLib.SMT_And(
                 SMTLib.SMT_Implies(self.isOn(i + sub.indexInSuper),
                                    sub.isOn(i)),
                 SMTLib.SMT_Implies(sub.isOn(i),
                                    self.isOn(i + sub.indexInSuper))))
Beispiel #4
0
 def ConstraintMustDominatesX(self, model):
     """
     Returns a constraint that a new instance has to be better than the instance represented by model in at least one dimension, 
     and better or equal in all the other ones.
     """
     dominationDisjunction = []
     i = 0
     for dominatedByMetric in self.metrics_variables:
         dominationConjunction = []
         j = 0
         if self.metrics_objective_direction[i] == Common.METRICS_MAXIMIZE:
             dominationConjunction.append(
                 SMTLib.SMT_GT(
                     dominatedByMetric,
                     SMTLib.SMT_IntConst(
                         Common.evalForNum(
                             model,
                             dominatedByMetric.convert(
                                 self.cfr.solver.converter)))))
         else:
             dominationConjunction.append(
                 SMTLib.SMT_LT(
                     dominatedByMetric,
                     SMTLib.SMT_IntConst(
                         Common.evalForNum(
                             model,
                             dominatedByMetric.convert(
                                 self.cfr.solver.converter)))))
         for AtLeastEqualInOtherMetric in self.metrics_variables:
             if j != i:
                 if self.metrics_objective_direction[
                         j] == Common.METRICS_MAXIMIZE:
                     dominationConjunction.append(
                         SMTLib.SMT_GE(
                             AtLeastEqualInOtherMetric,
                             SMTLib.SMT_IntConst(
                                 Common.evalForNum(
                                     model,
                                     AtLeastEqualInOtherMetric.convert(
                                         self.cfr.solver.converter)))))
                 else:
                     dominationConjunction.append(
                         SMTLib.SMT_LE(
                             AtLeastEqualInOtherMetric,
                             SMTLib.SMT_IntConst(
                                 Common.evalForNum(
                                     model,
                                     AtLeastEqualInOtherMetric.convert(
                                         self.cfr.solver.converter)))))
             j = 1 + j
         i = 1 + i
         dominationDisjunction.append(
             SMTLib.SMT_And(*dominationConjunction))
     constraintDominateX = SMTLib.SMT_Or(*dominationDisjunction)
     return constraintDominateX
Beispiel #5
0
 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)
Beispiel #6
0
    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])))