def separate(self, xSol): sub_insts = self.instance for instance in sub_insts: for i in instance.sI: instance.x[i] = max(xSol[i - 1], 0) solve_all_instances(self.solver_manager, 'cplex', sub_insts) subObj = 0 cutRhs = 0 cutLhs = 0 q_x = 0 s_x_e1 = int(sum(xSol[i] for i in range(len(xSol)))) #abs(s), integer L-shaped for s, inst in enumerate(sub_insts, 0): subObj += round(inst.oSub(), 4) q_x += 1.0 / self.numScen * inst.oSub() #get right-hand-side tmp = -(q_x - self.L) * (s_x_e1 - 1) + self.L cutRhs = tmp #get index and coefficient of x thecoefs = [] for i in range(len(xSol)): tmp = q_x - self.L if xSol[i] == 1.0: thecoefs.append(-tmp) #negative is because of LHS else: thecoefs.append(tmp) theind = self.mstX[:] #get index and coefficient of sita theind.append(self.mstSita) thecoefs.append(1.0) cutLhs = cplex.SparsePair(ind=theind, val=thecoefs) self.cutLhs = cutLhs self.cutRhs = cutRhs self.subObj = subObj
filename="sub2.dat")) # initialize the solver manager. solver_manager = SolverManagerFactory("serial") # miscellaneous initialization. mstr_inst.Min_Stage2_cost = float("-Inf") gap = float("Inf") max_iterations = 50 # main benders loop. for i in range(1, max_iterations + 1): print("\nIteration=%d" % (i)) #solve the subproblem solve_all_instances(solver_manager, 'cplex', sub_insts) for instance in sub_insts: print("cost for scenario="+instance.name+" is"+\ str(round(instance.oSub(),4))) print("") mstr_inst.CUTS.add(i) for s, inst in enumerate(sub_insts, 1): for t in mstr_inst.MAT: mstr_inst.dMatPro[t,s,i] = \ inst.dual[inst.cMatPro[t]] for p in mstr_inst.sPRO: mstr_inst.dMaxPro[p,s,i] = \ inst.dual[inst.cMaxPro[p]] #add master cut cut = sum((mstr_inst.prob[s] *\
def separate(self, xSol): sub_insts = self.instance for instance in sub_insts: for i in instance.sI: instance.x[i] = max(xSol[i - 1], 0) solve_all_instances(self.solver_manager, 'cplex', sub_insts) subObj = 0 cutRhs = 0 cutLhs = 0 thecoefs = [0] * len(xSol) for s, inst in enumerate(sub_insts, 1): subObj += round(inst.oSub(), 4) #print("obj. value for scenario "+str(s)+ " = "+inst.name+" is "+\ # str(subObj[s-1])+"("+str(1.0/self.numScen*subObj[s-1])+")") cut = 0 #constraint g for i in inst.scg: cut += inst.dual[inst.cSg[i]] #constraint p for i in inst.sI: for t in inst.sT: cut += inst.dual[inst.cSp[i, t]] * (-1) #constraint q,r,s,t for i in inst.sI: for t in inst.sT: for r in inst.sR: cut += inst.dual[inst.cSq[i, t, r]] cut += inst.dual[inst.cSr[i, t, r]] cut += inst.dual[inst.cSs[i, t, r]] cut += inst.dual[inst.cSt[i, t, r]] #constraint s1 for i in inst.sI: for r in inst.sR_0: cut += 2 * inst.dual[inst.cSs1[i, r]] #constraint u for t in inst.sT_0: cut += inst.dual[inst.cSu[t]] #constraint v for i in inst.sI: for t in inst.sT_ex: for r in inst.sR: cut += inst.dual[inst.cSv[i, t, r]] cut = 1.0 / self.numScen * cut #print ("added cut") #print (cut) cutRhs += cut #constraint i for i in inst.sI: #print ("+%f*x[%d]" %(1.0/self.numScen*inst.dual[inst.cSi[i]],i)), thecoefs[i - 1] = 1.0 / self.numScen * inst.dual[inst.cSi[i]] theind = self.mstX[:] theind.append(self.mstSita) thecoefs.append(1.0) cutLhs = cplex.SparsePair(ind=theind, val=thecoefs) self.cutLhs = cutLhs self.cutRhs = cutRhs self.subObj = subObj
#input: it is used for a seed control purpose, or run instances with different sets of lifetimes. for iter in range(max_iteration): print("================") print("iter=" + str(iter)) W = 1 #input: number of scenarios. create_files(iter, W) # initialize the instances. ef_mdl = import_file("ef.py").model ef_insts = [] ef_insts.append( ef_mdl.create_instance(name="ef_instance", filename="ef.dat")) solver_manager = SolverManagerFactory("serial") solve_all_instances(solver_manager, 'cplex', ef_insts) #print ("solving time = ",str(end_time1-start_time1)) """ for i in ef_insts[0].sI: for t in ef_insts[0].sT: print ("") for r in ef_insts[0].sR: print ("(%d,%d,%d)=(%d,%d)" %(i,t,r,ef_insts[0].u[i,t,r,1](),ef_insts[0].v[i,t,r,1]())), """ #for i in ef_insts[0].sI: # for w in ef_insts[0].Scen: # tmp = []; # for r in ef_insts[0].sR: # tmp.append(ef_insts[0].pLT[i,r,w]); # print (tmp)