Exemplo n.º 1
0
 def enumerate(
     self
 ):  # Branch and Bound on variables. Depth-first search. With exception.
     x = self.nextVar()
     if x:
         v = x.inf
         store.push()
         try:
             assert (Logprint.logPrint(2,
                                       "==> Try {} = {}".format(x.name,
                                                                v)) == None)
             x.isEQ(v)
             self.enumerate()
         except filter.FAIL:
             store.back()
             self.NBbk += 1
             x.isGE(v + filter.UN)
             self.enforceBB()
             self.enumerate()
         else:
             store.back()
     else:
         self.NBsol += 1
         assert (Logprint.logPrint(
             2, "<=====****===== Found solution n°{} in {} backtracks{}".
             format(
                 self.NBsol, self.NBbk, " at {}={}".format(
                     self.OBJECTIVE.name, self.OBJECTIVE.inf
                     if self.MINI else self.OBJECTIVE.sup)
                 if self.OBJECTIVE else "")) == None)
         self.saveSolution()
         self.Bound = self.newBound()
         self.NBbkTot += self.NBbk
         self.NBbk = 0
         if self.ALLSOL: raise filter.FAIL
Exemplo n.º 2
0
def validate(vars, sol):  # vars is a list, sol is a dicho
    store.push()
    try:
        for v in vars:
            v.isEQ(sol[v.name])
    except filter.FAIL as e:
        Logprint.logPrint(2, e)
        Logprint.logPrint(1, "The solution is not valid:")
        showVar(sol)
        store.back()
        return False
    Logprint.logPrint(1, "The solution is valid")
    store.back()
    return True
Exemplo n.º 3
0
 def solve(self):
     store.push()
     try:
         while True:
             assert (Logprint.logPrint(
                 2, "======****====> Looking for a solution {}".format(
                     "at {}={}".format(self.OBJECTIVE.name, self.Bound)
                     if self.OBJECTIVE else "")) == None)
             self.enforceBound()
             if self.SEARCH == 0: self.search()
             elif self.SEARCH == 1: pass
             elif self.SEARCH == 2: self.enumerate()
             elif self.SEARCH == 3: self.dicho()
             else: pass
             if not self.OBJECTIVE: raise filter.FAIL
     except filter.FAIL:
         pass
     store.back()
     if self.OBJECTIVE:
         Logprint.logPrint(
             2, "<=====****===== Optimum proved in {} backtracks".format(
                 self.NBbk))
     return True
Exemplo n.º 4
0
 def dicho(
     self
 ):  # Branch and Bound on variables. Dichotomic search. With exception.
     x = self.nextVar()
     if x:
         mid = (x.inf + x.sup) // filter.DEUX
         store.push()
         try:
             assert (Logprint.logPrint(
                 2, "==> Try {} <= {}".format(x.name, mid)) == None)
             x.isLE(mid)
             self.dicho()
         except filter.FAIL:
             store.back()
             self.NBbk += 1
             self.enforceBB()
             store.push()
             try:
                 assert (Logprint.logPrint(
                     2, "==> Try {} > {}".format(x.name, mid)) == None)
                 x.isGE(mid + filter.UN)
                 self.dicho()
             except filter.FAIL:
                 store.back()
                 self.NBbk += 1
                 raise filter.FAIL
             else:
                 store.back()
         else:
             store.back()
     else:
         self.NBsol += 1
         assert (Logprint.logPrint(
             2, "<=====****===== Found solution n°{} in {} backtracks{}".
             format(
                 self.NBsol, self.NBbk, " at {}={}".format(
                     self.OBJECTIVE.name, self.OBJECTIVE.inf
                     if self.MINI else self.OBJECTIVE.sup)
                 if self.OBJECTIVE else "")) == None)
         self.saveSolution()
         self.Bound = self.newBound()
         self.NBbkTot += self.NBbk
         self.NBbk = 0
         if self.ALLSOL: raise filter.FAIL
Exemplo n.º 5
0
 def search(self):  # Branch and Bound on disjunctions with exception.
     d = self.nextDisjunction()
     if d:
         left = self.leftFirst(d)
         store.push()
         try:
             d.settled(left)
             self.search()
         except filter.FAIL:
             store.back()
             self.NBbk += 1
             self.enforceBB()
             store.push()
             try:
                 d.settled(not (left))
                 self.search()
             except filter.FAIL:
                 store.back()
                 self.NBbk += 1
                 raise filter.FAIL
             else:
                 store.back()
         else:
             store.back()
     else:
         self.NBsol += 1
         assert (Logprint.logPrint(
             2, "<=====****===== Found solution n°{} in {} backtracks{}".
             format(
                 self.NBsol, self.NBbk, " at {}={}".format(
                     self.OBJECTIVE.name, self.OBJECTIVE.inf
                     if self.MINI else self.OBJECTIVE.sup)
                 if self.OBJECTIVE else "")) == None)
         self.saveSolution()
         self.Bound = self.newBound()
         self.NBbkTot += self.NBbk
         self.NBbk = 0
         if self.ALLSOL: raise filter.FAIL