def mip_solver(self): try: # create a new model m = Model("mip_encode") m.params.outputflag = 0 #m.params.feasibilitytol = 1e-9 #m.params.intfeastol = 1e-9 # add variables self.add_vars(m) self.add_additional_vars(m) # integrate new variables m.update() # print("add constr") # add constraints self.add_constrs(m) self.add_spc(m) if self.objective_type == ObjectiveType.MAX_FLEX_UNCERTAINTY: m.setObjective(self.Z + 0.0, GRB.MAXIMIZE) else: m.setObjective(self.objexp, GRB.MAXIMIZE) m.update() m.write("1.lp") m.optimize() if m.status == GRB.Status.INF_OR_UNBD: m.setParam(GRB.Param.Presolve, 0) m.optimize() if m.status == GRB.Status.OPTIMAL: m.write("1.sol") m.fixed() return self.get_solution(m) if m.status != GRB.Status.INFEASIBLE: print(m.status) m.computeIIS() m.write("1.ilp") except GurobiError as e: print('Error reported') print (e.message) print (e.errno) solution = Candidate() solution.utility = -1; return solution
def get_solution(self, m): solution = Candidate() # f = open('sol.txt', 'w') for var_a in m.getVars(): vname = var_a.getAttr(GRB.Attr.VarName) if self.objective_type == ObjectiveType.MAX_FLEX_UNCERTAINTY: if vname.find("cl") == -1 and vname.find("cu") == -1: continue #f.write('%s %s\n' % (var_a.getAttr(GRB.Attr.VarName), var_a.getAttr(GRB.Attr.X))) # if vname.find("l") != -1: # print(var_a.getAttr(GRB.Attr.VarName), var_a.getAttr(GRB.Attr.X)) e = self.relax_e[vname] new_relaxation = TemporalRelaxation(e) if vname.find("cl") != -1: new_relaxation.relaxed_lb = var_a.getAttr("X") if new_relaxation.relaxed_lb != e.get_lower_bound(): solution.add_temporal_relaxation(new_relaxation) else : new_relaxation.relaxed_ub = var_a.getAttr("X") if new_relaxation.relaxed_ub != e.get_upper_bound(): solution.add_temporal_relaxation(new_relaxation) elif self.objective_type == ObjectiveType.MIN_COST: if vname.find("vlr") == -1 and vname.find("vur") == -1: continue; e = self.relax_e[vname] #print(vname,e.fro, e.to) new_relaxation = TemporalRelaxation(e) if vname.find("vlr") != -1: new_relaxation.relaxed_lb = self.l[(e.fro,e.to)].getValue() else: new_relaxation.relaxed_ub = self.u[(e.fro, e.to)].getValue() if var_a.getAttr("X") > 0: solution.add_temporal_relaxation(new_relaxation) # for e in self.network.temporal_constraints.values(): # if e.activated: # print(e.fro, e.to, self.l[(e.fro, e.to)].getValue(), self.u[(e.fro, e.to)].getValue()) solution.utility = round(m.getObjective().getValue(), 6) if solution.utility > 1000 and self.objective_type == ObjectiveType.MAX_FLEX_UNCERTAINTY: solution.utility = 1000 # for node_a in range(1, self.num_nodes+1): # for node_b in range(1, self.num_nodes+1): # if node_a != node_b: # f.write("%s->%s [%s, %s]\n"%(node_a,node_b,self.l[(node_a,node_b)].getValue(), self.u[(node_a,node_b)].getValue() )) # # if solution.utility > 100: # solution.utility = 100 # f.close() #print a dot file # f = open('sol.dot', 'w') # f.write('digraph G {\n nodesep = .45; \n size = 30;\n label="CCTP";\n') # # for e in self.network.temporal_constraints.values(): # if e.activated: # f.write('"%s"->"%s"'%(e.fro, e.to)) # tlb = self.l[(e.fro, e.to)].getValue() # tub = self.u[(e.fro, e.to)].getValue() # f.write('[ label = "[%s,%s]'%(tlb, tub)) # if not e.controllable: # f.write(' type=dashed') # f.write('"];\n') # #%d"];'%(e.fro, e.to,e.get_lower_bound(),e.get_upper_bound(),e.controllable)) # f.write("}") # f.close() return solution