Exemplo n.º 1
0
 def recuse(self, level, low, up):
     if level == 0:
         # solve
         # start_clock = clock()
         self.solver.solve()
         # self.record['solve'] += 1
         # self.record[l] = clock()-start_clock
         status = self.solver.solution.get_status_string()
         if status.find("optimal") >= 0:
             # process solutions
             result_variables = self.solver.solution.get_values()
             cplex_result = CplexSolResult(result_variables, status,
                                           self.problem)
             self.addTocplexSolutionSetMap(cplex_result)
             return {cplex_result.getResultID(): result_variables}
         else:
             return dict()
     else:
         # prepare all solutions set
         all_solutions = dict()
         # get the up and low bound
         relaxed_up = math.ceil(up[level])
         relaxed_low = math.ceil(low[level])
         constraint_name = 'obj_' + str(level)
         for l in range(relaxed_up, relaxed_low - 1, -1):
             # update rhs
             self.solver.linear_constraints.set_rhs(constraint_name, l)
             solutions = self.recuse(level - 1, low, up)
             all_solutions = {**all_solutions, **solutions}
         # end for
         return all_solutions
Exemplo n.º 2
0
 def recuse(self, level, up, attribute, k):
     if level == 0:
         # solve
         # start_clock = clock()
         self.solver.solve()
         # self.record['solve'] += 1
         # self.record[l] = clock()-start_clock
         status = self.solver.solution.get_status_string()
         if status.find("optimal") >= 0:
             result_variables = self.solver.solution.get_values()
             cplex_result = CplexSolResult(result_variables, status,
                                           self.problem)
             self.addTocplexSolutionSetMap(cplex_result)
             return {cplex_result.getResultID(): result_variables}
         else:
             return dict()
     else:
         # recuse
         constraint_name = 'obj_' + str(level)
         l = math.ceil(up[level])
         all_solutions = dict()
         while True:
             self.solver.linear_constraints.set_rhs(constraint_name, l)
             solutions = self.recuse(level - 1, up, attribute, k)
             # cannot find solutions, break
             if not solutions:
                 break
             # update l
             l = self.getMaxForObjKonMe(attribute[level], solutions)
             all_solutions = {**all_solutions, **solutions}
         # end while
         return all_solutions
Exemplo n.º 3
0
 def travelAllObjConstr(self, level=1, passDict={}):
     if level == self.moipProblem.objectiveCount - 1:
         #no need to go deep
         (ub, lb) = self.boundsDict[level]
         ub_relaxed = math.ceil(ub)
         lb_relaxed = math.floor(lb)
         for value in range(ub_relaxed, lb_relaxed - 1, -1):
             passDict['o' + str(level) + '_R'] = value
             self.updateSolver(passDict)
             self.solveCounter += 1
             #continue
             self.solver.solve()
             #debugging purpose
             #print ("Solution value  = ", self.solver.solution.get_objective_value())
             #debugging purpose
             #xsol = self.solver.solution.get_values()
             #debugging purpose
             #print ('xsol = ',  xsol )
             if (self.solver.solution.get_status_string().find("optimal") ==
                     -1):
                 continue
             cplexResults = CplexSolResult(
                 self.solver.solution.get_values(),
                 self.solver.solution.get_status_string(), self.moipProblem)
             self.addTocplexSolutionSetMap(cplexResults)
     else:
         (ub, lb) = self.boundsDict[level]
         ub_relaxed = math.ceil(ub)
         lb_relaxed = math.floor(lb)
         for value in range(ub_relaxed, lb_relaxed - 1, -1):
             passDict['o' + str(level) + '_R'] = value
             self.travelAllObjConstr(level + 1, passDict)
Exemplo n.º 4
0
    def calculate(self, p_k, y_up, y_ub, y_lb):
        fCWMOIP = float('nan')
        No = self.objNo
        Nv = self.varNv

        extra_A1 = []
        objMatrix = np.array(self.moipProblem.attributeMatrix)
        SolRep.appendToSparseMapList(extra_A1, objMatrix[0:No - 1])
        extra_B1 = p_k[0:No - 1]

        ub = [1] * Nv
        lb = [0] * Nv

        ff = []
        for i in range(No - 1, -1, -1):
            if i == No - 1:
                ff = objMatrix[i]
            else:
                w = 1.0 / (y_ub[i] - y_lb[i] + 1)
                ff = ff + w * objMatrix[i]

        (rstObj, rstXvar,
         rstStatusString) = NcgopSol.intlinprog(self.solver, self.xvar, ff,
                                                extra_A1, extra_B1, [], [], lb,
                                                ub)
        if (rstStatusString.find("optimal") >= 0):
            newff = np.reshape(ff, (1, len(ff)))
            new_A1 = []
            SolRep.appendToSparseMapList(new_A1, newff)
            new_b1 = np.dot(rstXvar, ff)
        if (rstStatusString.find("optimal") >= 0):
            #newff = np.reshape(ff, (1, len(ff)))
            cplexResults = CplexSolResult(rstXvar, rstStatusString,
                                          self.moipProblem)
            self.addTocplexSolutionSetMap(cplexResults)
            fCWMOIP = np.dot(rstXvar, ff)
        return fCWMOIP
Exemplo n.º 5
0
    def calculate(self):
        utopiaSols = {}
        for i in range(0, len(self.attributeMatrix_in)):
            target = np.array(self.attributeMatrix_in[i])
            solutions = UtopiaPlane.intlinprog(self.cplex, self.xVar, target,
                                               self.extraInequationsMapList,
                                               self.extraEquationsMapList,
                                               None, None)
            solution = solutions[0]
            optSolution = None
            cplexResult = CplexSolResult(solution[1], "optimal",
                                         self.moipProblem)
            if len(solutions) == 1:
                optSolution = cplexResult
                utopiaSols[optSolution.getResultID()] = optSolution
            elif cplexResult.getResultID(
            ) not in utopiaSols and len(solutions) > 1:
                optSolution = cplexResult
                for j in range(0, len(solutions)):
                    solution = solutions[j]
                    cplexResult2 = CplexSolResult(solution[1], "optimal",
                                                  self.moipProblem)
                    if j == 0:
                        optSolution = cplexResult2
                utopiaSols[optSolution.getResultID()] = optSolution
            elif cplexResult.getResultID(
            ) in utopiaSols and len(solutions) > 1:
                best = float("+inf")
                for j in range(1, len(solutions)):
                    """
                    debug purpose
                    """
                    if i == 2 and j == 1:
                        solution = solutions[j]
                        cplexResult2 = CplexSolResult(solution[1], "optimal",
                                                      self.moipProblem)
                        optSolution = cplexResult2
                        break
                    """
                    end of debugging purpose
                    """
                    solution = solutions[j]
                    cplexResult2 = CplexSolResult(solution[1], "optimal",
                                                  self.moipProblem)
                    if cplexResult2.getResultID(
                    ) not in utopiaSols and cplexResult2.getThisObj() < best:
                        best = cplexResult2.getThisObj()
                        optSolution = cplexResult2
                    #end of if
                #end of for
                utopiaSols[optSolution.getResultID()] = optSolution
            #end of if-elif
            X = np.array(optSolution.xvar)
            self.x_up[i] = X
            self.y_up[i] = UtopiaPlane.calculateObjs(optSolution.xvar,
                                                     self.attributeMatrix_in)
            y1 = optSolution.getKthObj(i)
            self.y_lb[i] = y1

            negTarget = target * (-1.0)
            negSolutions = UtopiaPlane.intlinprog(self.cplex, self.xVar,
                                                  negTarget,
                                                  self.extraInequationsMapList,
                                                  self.extraEquationsMapList,
                                                  None, None)
            negSolution = negSolutions[0]
            X2 = negSolution[1]
            y2 = negSolution[0] * (-1.0)
            self.y_ub[i] = y2
        #end of for
        return
Exemplo n.º 6
0
 def solveBySingleObj(self, inequationsMapList, equationsMapList, objMatIn,
                      objMatOut, k, solutionMap, resultMap):
     solutionMapOut = {}
     lbs = np.zeros((1, self.moipProblem.featureCount))
     ubs = np.ones((1, self.moipProblem.featureCount))
     feasibleFlag = False
     if k == 1:
         # The single-objective problem
         (rsltObj, rsltXvar,
          rsltSolString) = self.intlinprog(objMatOut[k - 1],
                                           inequationsMapList,
                                           equationsMapList, lbs, ubs)
         #check whether it is optimal
         if (rsltSolString.find("optimal") >= 0):
             cplexResults = CplexSolResult(rsltXvar, rsltSolString,
                                           self.moipProblem)
             self.addTocplexSolutionSetMap(cplexResults)
             solutionMapOut[cplexResults.getResultID()] = rsltXvar
     else:
         fGUB = np.zeros(k)
         fGLB = np.zeros(k)
         fRange = np.zeros(k)
         w = Decimal(1.0)
         for i in range(0, k):
             feasibleFlag = True
             (rsltObj, rsltXvar,
              rsltString) = self.intlinprog(objMatOut[i],
                                            inequationsMapList,
                                            equationsMapList, lbs, ubs)
             if (rsltString.find("optimal") >= 0):
                 fGLB[i] = 1.0 * MOOUtility.round(rsltObj)
                 objOutNeg = objMatOut[i] * (-1)
                 rslt2Obj, rslt2Xvar, rslt2String = self.intlinprog(
                     objOutNeg, inequationsMapList, equationsMapList, lbs,
                     ubs)
                 fGUB[i] = -1.0 * MOOUtility.round(rslt2Obj)
                 fRange[i] = fGUB[i] - fGLB[i] + 1
                 w = w * MOOUtility.round(fRange[i])
             else:
                 feasibleFlag = False
                 break
             #end of if-esle
         #end of for
         if (feasibleFlag == True):
             w = Decimal(1) / w
             w = float(w)
             l = fGUB[k - 1]
             #shallow copy
             new_inequationsMapList = list(inequationsMapList)
             new_equationsMapList = list(equationsMapList)
             while True:
                 # Step 1: Solve the CW(k-1)OIP problem with l
                 solutions = []
                 objMat_out1 = np.zeros(
                     (k - 1, self.moipProblem.featureCount))
                 for i in range(0, k - 1):
                     objMat_out1[i] = objMatOut[i] + w * objMatOut[k - 1]
                 lastConstr = {}
                 if len(new_inequationsMapList) > 0:
                     lastConstr = new_inequationsMapList[
                         len(new_inequationsMapList) - 1]
                 if len(inequationsMapList) > 0 and MOOUtility.arrayEqual(
                         lastConstr, objMatIn[k - 1]):
                     lastConstr[self.moipProblem.featureCount] = l
                 else:
                     #add the new constraint to new_inequationsMapList
                     self.addNewConstrToInequationsMapList(
                         new_inequationsMapList, objMatIn[k - 1], l)
                 solutions = self.solveBySingleObj(new_inequationsMapList,
                                                   new_equationsMapList,
                                                   objMatIn, objMat_out1,
                                                   k - 1, solutionMap,
                                                   resultMap)
                 if len(solutions) == 0:
                     break
                 else:
                     #Step 2: put ME into E, find the new l
                     solutionMap = {**solutionMap, **solutions}
                     solutionMapOut = {**solutionMapOut, **solutions}
                     l = self.getMaxForObjKonMe(objMatIn[k - 1], solutions)
                     lastConstr = new_inequationsMapList[
                         len(new_inequationsMapList) - 1]
                     lastConstr[self.moipProblem.featureCount] = l
         #end of while
         #end of if
     return solutionMapOut