benders_2stage_mt Namespace Reference

Functions

def scen_solve (i, subi, cutconst, cutcoeff, dem_queue, objsub, queue_lock, io_lock)
 
def get_data_text ()
 
def get_master_text ()
 
def get_sub_text ()
 

Variables

 ws = GamsWorkspace(system_directory = sys.argv[1])
 
 data = ws.add_job_from_string(get_data_text())
 
 opt_data = ws.add_options()
 
 scenario_data = data.out_db.get_parameter("ScenarioData")
 
 opt = ws.add_options()
 
int maxiter = 40
 
 all_model_types
 
 cp_master = ws.add_checkpoint()
 
 cp_sub = ws.add_checkpoint()
 
 master = ws.add_job_from_string(get_master_text())
 
 databases
 
 masteri = cp_master.add_modelinstance()
 
 cutconst = masteri.sync_db.add_parameter("cutconst", 1, "Benders optimality cut constant")
 
 cutcoeff = masteri.sync_db.add_parameter("cutcoeff", 2, "Benders optimality coefficients")
 
 theta = masteri.sync_db.add_variable("theta", 0, VarType.Free, "Future profit function variable")
 
 theta_fix = masteri.sync_db.add_parameter("thetaFix", 0, "")
 
int num_threads = 2
 
list subi = []
 
 dem_queue = queue.Queue()
 
list received = subi[0].sync_db.add_parameter("received", 1, "units received from first stage solution")
 
list demand = subi[0].sync_db.add_parameter("demand", 1, "stochastic demand")
 
 lowerbound = float('-inf')
 
 upperbound = float('inf')
 
 objmaster = float('inf')
 
int it = 1
 
 value
 
dictionary dem_dict = {}
 
float objsubsum = 0.0
 
 queue_lock = threading.Lock()
 
 io_lock = threading.Lock()
 
dictionary coef = {}
 
list objsub = []
 
list cons = []
 
dictionary threads = {}
 
 target
 
 scen_solve
 
 args
 

Function Documentation

◆ get_data_text()

def benders_2stage_mt.get_data_text ( )

Definition at line 55 of file benders_2stage_mt.py.

55def get_data_text():
56 return '''
57Sets
58 i factories /f1*f3/
59 j distribution centers /d1*d5/
60
61Parameter
62 capacity(i) unit capacity at factories
63 /f1 500, f2 450, f3 650/
64 demand(j) unit demand at distribution centers
65 /d1 160, d2 120, d3 270, d4 325, d5 700 /
66 prodcost unit production cost /14/
67 price sales price /24/
68 wastecost cost of removal of overstocked products /4/
69
70Table transcost(i,j) unit transportation cost
71 d1 d2 d3 d4 d5
72 f1 2.49 5.21 3.76 4.85 2.07
73 f2 1.46 2.54 1.83 1.86 4.76
74 f3 3.26 3.08 2.60 3.76 4.45;
75
76$ifthen not set useBig
77Set
78 s scenarios /lo,mid,hi/
79
80table ScenarioData(s,*) possible outcomes for demand plus probabilities
81 d1 d2 d3 d4 d5 prob
82lo 150 100 250 300 600 0.25
83mid 160 120 270 325 700 0.50
84hi 170 135 300 350 800 0.25;
85$else
86$if not set nrScen $set nrScen 10
87Set s scenarios /s1*s%nrScen%/;
88parameter ScenarioData(s,*) possible outcomes for demand plus probabilities;
89option seed=1234;
90ScenarioData(s,'prob') = 1/card(s);
91ScenarioData(s,j) = demand(j)*uniform(0.6,1.4);
92$endif
93'''
94

◆ get_master_text()

def benders_2stage_mt.get_master_text ( )

Definition at line 95 of file benders_2stage_mt.py.

95def get_master_text():
96 return '''
97Sets
98 i factories
99 j distribution centers
100
101Parameter
102 capacity(i) unit capacity at factories
103 prodcost unit production cost
104 transcost(i,j) unit transportation cost
105
106$if not set datain $abort 'datain not set'
107$gdxin %datain%
108$load i j capacity prodcost transcost
109
110* Benders master problem
111$if not set maxiter $set maxiter 25
112Set
113 iter max Benders iterations /1*%maxiter%/
114
115Parameter
116 cutconst(iter) constants in optimality cuts
117 cutcoeff(iter,j) coefficients in optimality cuts
118
119Variables
120 ship(i,j) shipments
121 product(i) production
122 received(j) quantity sent to market
123 zmaster objective variable of master problem
124 theta future profit
125Positive Variables ship;
126
127Equations
128 masterobj master objective function
129 production(i) calculate production in each factory
130 receive(j) calculate quantity to be send to markets
131 optcut(iter) Benders optimality cuts;
132
133masterobj..
134 zmaster =e= theta -sum((i,j), transcost(i,j)*ship(i,j))
135 - sum(i,prodcost*product(i));
136
137receive(j).. received(j) =e= sum(i, ship(i,j));
138
139production(i).. product(i) =e= sum(j, ship(i,j));
140product.up(i) = capacity(i);
141
142optcut(iter).. theta =l= cutconst(iter) +
143 sum(j, cutcoeff(iter,j)*received(j));
144
145model masterproblem /all/;
146
147* Initialize cut to be non-binding
148cutconst(iter) = 1e15;
149cutcoeff(iter,j) = eps;
150'''
151

◆ get_sub_text()

def benders_2stage_mt.get_sub_text ( )

Definition at line 152 of file benders_2stage_mt.py.

152def get_sub_text():
153 return '''
154Sets
155 i factories
156 j distribution centers
157
158Parameter
159 demand(j) unit demand at distribution centers
160 price sales price
161 wastecost cost of removal of overstocked products
162 received(j) first stage decision units received
163
164$if not set datain $abort 'datain not set'
165$gdxin %datain%
166$load i j demand price wastecost
167
168* Benders' subproblem
169
170Variables
171 sales(j) sales (actually sold)
172 waste(j) overstocked products
173 zsub objective variable of sub problem
174Positive variables sales, waste
175
176Equations
177 subobj subproblem objective function
178 selling(j) part of received is sold
179 market(j) upperbound on sales
180;
181
182subobj..
183 zsub =e= sum(j, price*sales(j)) - sum(j, wastecost*waste(j));
184
185selling(j).. sales(j) + waste(j) =e= received(j);
186
187market(j).. sales(j) =l= demand(j);
188
189model subproblem /subobj,selling,market/;
190
191* Initialize received
192received(j) = demand(j); '''
193
194

◆ scen_solve()

def benders_2stage_mt.scen_solve (   i,
  subi,
  cutconst,
  cutcoeff,
  dem_queue,
  objsub,
  queue_lock,
  io_lock 
)

Definition at line 30 of file benders_2stage_mt.py.

References benders_2stage.iter, and scen_solve.

30def scen_solve(i, subi, cutconst, cutcoeff, dem_queue, objsub, queue_lock, io_lock):
31 while True:
32 queue_lock.acquire()
33 if dem_queue.empty():
34 queue_lock.release()
35 return
36 dem_dict = dem_queue.get()
37 queue_lock.release()
38
39 subi[i].sync_db["demand"].clear()
40
41 for k,v in iter(dem_dict[2].items()):
42 subi[i].sync_db["demand"].add_record(k).value = v
43 subi[i].solve(SymbolUpdateType.BaseCase)
44
45 io_lock.acquire()
46 print(" Sub " + str(subi[i].model_status) + " : obj=" + str(subi[i].sync_db["zsub"].first_record().level))
47 io_lock.release()
48
49 probability = dem_dict[1]
50 objsub[i] += probability * subi[i].sync_db["zsub"].first_record().level
51 for k,v in iter(dem_dict[2].items()):
52 cutconst[i] += probability * subi[i].sync_db["market"].find_record(k).marginal * v
53 cutcoeff[i][k] += probability * subi[i].sync_db["selling"].find_record(k).marginal
54
def scen_solve(i, subi, cutconst, cutcoeff, dem_queue, objsub, queue_lock, io_lock)

Variable Documentation

◆ all_model_types

benders_2stage_mt.all_model_types

Definition at line 215 of file benders_2stage_mt.py.

◆ args

benders_2stage_mt.args

Definition at line 294 of file benders_2stage_mt.py.

◆ coef

dictionary benders_2stage_mt.coef = {}

Definition at line 280 of file benders_2stage_mt.py.

◆ cons

list benders_2stage_mt.cons = []

Definition at line 282 of file benders_2stage_mt.py.

◆ cp_master

benders_2stage_mt.cp_master = ws.add_checkpoint()

Definition at line 217 of file benders_2stage_mt.py.

◆ cp_sub

benders_2stage_mt.cp_sub = ws.add_checkpoint()

Definition at line 218 of file benders_2stage_mt.py.

◆ cutcoeff

benders_2stage_mt.cutcoeff = masteri.sync_db.add_parameter("cutcoeff", 2, "Benders optimality coefficients")

Definition at line 225 of file benders_2stage_mt.py.

◆ cutconst

benders_2stage_mt.cutconst = masteri.sync_db.add_parameter("cutconst", 1, "Benders optimality cut constant")

Definition at line 224 of file benders_2stage_mt.py.

◆ data

benders_2stage_mt.data = ws.add_job_from_string(get_data_text())

Definition at line 201 of file benders_2stage_mt.py.

◆ databases

benders_2stage_mt.databases

Definition at line 221 of file benders_2stage_mt.py.

◆ dem_dict

dictionary benders_2stage_mt.dem_dict = {}

Definition at line 263 of file benders_2stage_mt.py.

◆ dem_queue

benders_2stage_mt.dem_queue = queue.Queue()

Definition at line 233 of file benders_2stage_mt.py.

◆ demand

list benders_2stage_mt.demand = subi[0].sync_db.add_parameter("demand", 1, "stochastic demand")

Definition at line 236 of file benders_2stage_mt.py.

◆ io_lock

benders_2stage_mt.io_lock = threading.Lock()

Definition at line 279 of file benders_2stage_mt.py.

◆ it

int benders_2stage_mt.it = 1

Definition at line 246 of file benders_2stage_mt.py.

◆ lowerbound

benders_2stage_mt.lowerbound = float('-inf')

Definition at line 243 of file benders_2stage_mt.py.

◆ master

benders_2stage_mt.master = ws.add_job_from_string(get_master_text())

Definition at line 220 of file benders_2stage_mt.py.

◆ masteri

benders_2stage_mt.masteri = cp_master.add_modelinstance()

Definition at line 223 of file benders_2stage_mt.py.

◆ maxiter

int benders_2stage_mt.maxiter = 40

Definition at line 213 of file benders_2stage_mt.py.

◆ num_threads

int benders_2stage_mt.num_threads = 2

Definition at line 231 of file benders_2stage_mt.py.

◆ objmaster

benders_2stage_mt.objmaster = float('inf')

Definition at line 245 of file benders_2stage_mt.py.

◆ objsub

list benders_2stage_mt.objsub = []

Definition at line 281 of file benders_2stage_mt.py.

◆ objsubsum

float benders_2stage_mt.objsubsum = 0.0

Definition at line 276 of file benders_2stage_mt.py.

◆ opt

benders_2stage_mt.opt = ws.add_options()

Definition at line 211 of file benders_2stage_mt.py.

◆ opt_data

benders_2stage_mt.opt_data = ws.add_options()

Definition at line 203 of file benders_2stage_mt.py.

◆ queue_lock

benders_2stage_mt.queue_lock = threading.Lock()

Definition at line 278 of file benders_2stage_mt.py.

◆ received

list benders_2stage_mt.received = subi[0].sync_db.add_parameter("received", 1, "units received from first stage solution")

Definition at line 235 of file benders_2stage_mt.py.

◆ scen_solve

benders_2stage_mt.scen_solve

Definition at line 294 of file benders_2stage_mt.py.

Referenced by scen_solve().

◆ scenario_data

benders_2stage_mt.scenario_data = data.out_db.get_parameter("ScenarioData")

Definition at line 209 of file benders_2stage_mt.py.

◆ subi

list benders_2stage_mt.subi = []

Definition at line 232 of file benders_2stage_mt.py.

◆ target

benders_2stage_mt.target

Definition at line 294 of file benders_2stage_mt.py.

◆ theta

benders_2stage_mt.theta = masteri.sync_db.add_variable("theta", 0, VarType.Free, "Future profit function variable")

Definition at line 226 of file benders_2stage_mt.py.

◆ theta_fix

benders_2stage_mt.theta_fix = masteri.sync_db.add_parameter("thetaFix", 0, "")

Definition at line 227 of file benders_2stage_mt.py.

◆ threads

dictionary benders_2stage_mt.threads = {}

Definition at line 292 of file benders_2stage_mt.py.

◆ upperbound

benders_2stage_mt.upperbound = float('inf')

Definition at line 244 of file benders_2stage_mt.py.

◆ value

benders_2stage_mt.value

Definition at line 253 of file benders_2stage_mt.py.

◆ ws

benders_2stage_mt.ws = GamsWorkspace(system_directory = sys.argv[1])

Definition at line 197 of file benders_2stage_mt.py.