コード例 #1
0
ファイル: wcspinfer.py プロジェクト: pombredanne/pracmln
 def forbid_gndatom(self, atom, truth=True):
     '''
     Adds a unary constraint that prohibits the given ground atom
     being true.
     '''
     atomidx = atom if type(atom) is int else (self.mrf.gndatom(atom).idx if type(atom) is str else atom.idx)
     varidx = self.atom2var[atomidx]
     variable = self.variables[varidx]
     evidence = list(self.mrf.evidence)
     evidence[atomidx] = {True: 1, False: 0}[truth]
     c = Constraint((varidx,))
     for _, value in variable.itervalues(evidence):
         validx = self.val2idx[varidx][value]
         c.tuple((validx,), self.wcsp.top)
     self.wcsp.constraint(c)
コード例 #2
0
ファイル: wcspinfer.py プロジェクト: pombredanne/pracmln
 def generate_constraint(self, wf):
     '''
     Generates and adds a constraint from a given weighted formula.
     '''
     varindices = tuple(map(lambda x: self.atom2var[x], wf.gndatom_indices()))
     # collect the constraint tuples
     cost2assignments = self._gather_constraint_tuples(varindices, wf)
     if cost2assignments is None:
         return
     defcost = max(cost2assignments, key=lambda x: infty if cost2assignments[x] == 'else' else len(cost2assignments[x]))
     del cost2assignments[defcost] # remove the default cost values
     
     constraint = Constraint(varindices, defcost=defcost)
     for cost, tuples in cost2assignments.iteritems():
         for t in tuples:
             constraint.tuple(t, cost)
     self.wcsp.constraint(constraint)