I have the following problem. I have this code. The whole code can be found here.
from gurobipy import *import gurobipy as guimport pandas as pd# Create DF out of SetsI_list = [1, 2, 3]T_list = [1, 2, 3, 4, 5, 6, 7]K_list = [1, 2, 3]I_list1 = pd.DataFrame(I_list, columns=['I'])T_list1 = pd.DataFrame(T_list, columns=['T'])K_list1 = pd.DataFrame(K_list, columns=['K'])DataDF = pd.concat([I_list1, T_list1, K_list1], axis=1)Demand_Dict = {(1, 1): 2, (1, 2): 1, (1, 3): 0, (2, 1): 1, (2, 2): 2, (2, 3): 0, (3, 1): 1, (3, 2): 1, (3, 3): 1, (4, 1): 1, (4, 2): 2, (4, 3): 0, (5, 1): 2, (5, 2): 0, (5, 3): 1, (6, 1): 1, (6, 2): 1, (6, 3): 1, (7, 1): 0, (7, 2): 3, (7, 3): 0}class MasterProblem: def __init__(self, dfData, DemandDF, iteration, current_iteration): self.iteration = iteration self.current_iteration = current_iteration self.nurses = dfData['I'].dropna().astype(int).unique().tolist() self.days = dfData['T'].dropna().astype(int).unique().tolist() self.shifts = dfData['K'].dropna().astype(int).unique().tolist() self.roster = list(range(1, self.current_iteration + 2)) self.demand = DemandDF self.model = gu.Model("MasterProblem") self.cons_demand = {} self.newvar = {} self.cons_lmbda = {} def buildModel(self): self.generateVariables() self.generateConstraints() self.model.update() self.generateObjective() self.model.update() print(f"Roster-Index: {self.roster}")... def addColumn(self, newSchedule): self.newvar = {} colName = f"Schedule[{self.nurses},{self.roster}]" newScheduleList = [] for i, t, s, r in newSchedule: newScheduleList.append(newSchedule[i, t, s, r]) Column = gu.Column([], []) self.newvar = self.model.addVar(vtype=gu.GRB.CONTINUOUS, lb=0, column=Column, name=colName) self.current_iteration = itr print(f"Roster-Index: {self.current_iteration}") self.model.update() def solveModel(self, timeLimit, EPS): self.model.setParam('TimeLimit', timeLimit) self.model.setParam('MIPGap', EPS) self.model.Params.QCPDual = 1 self.model.Params.OutputFlag = 0 self.model.optimize() def modifyConstraint(self, index, itr): self.nurseIndex = index self.rosterIndex = itr for t in self.days: for s in self.shifts: self.newcoef = 1.0 current_cons = self.cons_demand[t, s] qexpr = self.model.getQCRow(current_cons) new_var = self.newvar new_coef = self.newcoef qexpr.add(new_var * self.lmbda[self.nurseIndex, self.rosterIndex + 1], new_coef) rhs = current_cons.getAttr('QCRHS') sense = current_cons.getAttr('QCSense') name = current_cons.getAttr('QCName') newcon = self.model.addQConstr(qexpr, sense, rhs, name) self.model.remove(current_cons) self.cons_demand[t, s] = newcon return newconclass Subproblem: def __init__(self, duals_i, duals_ts, dfData, i, M, iteration):..... def getNewSchedule(self): return self.model.getAttr("X", self.motivation)....#### Column Generation# CG PrerequisitesmodelImprovable = Truet0 = time.time()max_itr = 2itr = 0# Build & Solve MPmaster = MasterProblem(DataDF, Demand_Dict, max_itr, itr)master.buildModel()print('* *****Column Generation Iteration***** \n*')while (modelImprovable) and itr < max_itr: # Start itr += 1 # Solve RMP master.solveRelaxModel() # Solve SPs modelImprovable = False for index in I_list: subproblem = Subproblem(duals_i, duals_ts, DataDF, index, 1e6, itr) subproblem.buildModel() subproblem.solveModel(3600, 1e-6) status = subproblem.getStatus() if status != 2: raise Exception("Pricing-Problem can not reach optimality!") reducedCost = subproblem.getObjVal() if reducedCost < -1e-6: ScheduleCuts = subproblem.getNewSchedule() master.addColumn(ScheduleCuts) master.modifyConstraint(index, itr) master.updateModel() modelImprovable = True master.updateModel()
Now to my problem. I initialize my MasterProblem where the index self.roster is formed based on the iterations. Since itr=0
during initialization, self.roster
is initially [1]
. Now I want this index to increase by one for each additional iteration, so in the case of itr=1
, self.roster = [1,2]
and so on. Unfortunately, I don't know how I can achieve this without "building" the model anew each time using the buildModel()
function. Thanks for your help. Since this is a Python problem, I'll post it here.