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 54 of file benders_2stage_mt.py.

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

◆ get_master_text()

def benders_2stage_mt.get_master_text ( )

Definition at line 94 of file benders_2stage_mt.py.

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

◆ get_sub_text()

def benders_2stage_mt.get_sub_text ( )

Definition at line 151 of file benders_2stage_mt.py.

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

◆ scen_solve()

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

Definition at line 29 of file benders_2stage_mt.py.

References benders_2stage.iter, and scen_solve.

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

◆ args

benders_2stage_mt.args

Definition at line 293 of file benders_2stage_mt.py.

◆ coef

dictionary benders_2stage_mt.coef = {}

Definition at line 279 of file benders_2stage_mt.py.

◆ cons

list benders_2stage_mt.cons = []

Definition at line 281 of file benders_2stage_mt.py.

◆ cp_master

benders_2stage_mt.cp_master = ws.add_checkpoint()

Definition at line 216 of file benders_2stage_mt.py.

◆ cp_sub

benders_2stage_mt.cp_sub = ws.add_checkpoint()

Definition at line 217 of file benders_2stage_mt.py.

◆ cutcoeff

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

Definition at line 224 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 223 of file benders_2stage_mt.py.

◆ data

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

Definition at line 200 of file benders_2stage_mt.py.

◆ databases

benders_2stage_mt.databases

Definition at line 220 of file benders_2stage_mt.py.

◆ dem_dict

dictionary benders_2stage_mt.dem_dict = {}

Definition at line 262 of file benders_2stage_mt.py.

◆ dem_queue

benders_2stage_mt.dem_queue = queue.Queue()

Definition at line 232 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 235 of file benders_2stage_mt.py.

◆ io_lock

benders_2stage_mt.io_lock = threading.Lock()

Definition at line 278 of file benders_2stage_mt.py.

◆ it

int benders_2stage_mt.it = 1

Definition at line 245 of file benders_2stage_mt.py.

◆ lowerbound

benders_2stage_mt.lowerbound = float('-inf')

Definition at line 242 of file benders_2stage_mt.py.

◆ master

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

Definition at line 219 of file benders_2stage_mt.py.

◆ masteri

benders_2stage_mt.masteri = cp_master.add_modelinstance()

Definition at line 222 of file benders_2stage_mt.py.

◆ maxiter

int benders_2stage_mt.maxiter = 40

Definition at line 212 of file benders_2stage_mt.py.

◆ num_threads

int benders_2stage_mt.num_threads = 2

Definition at line 230 of file benders_2stage_mt.py.

◆ objmaster

benders_2stage_mt.objmaster = float('inf')

Definition at line 244 of file benders_2stage_mt.py.

◆ objsub

list benders_2stage_mt.objsub = []

Definition at line 280 of file benders_2stage_mt.py.

◆ objsubsum

float benders_2stage_mt.objsubsum = 0.0

Definition at line 275 of file benders_2stage_mt.py.

◆ opt

benders_2stage_mt.opt = ws.add_options()

Definition at line 210 of file benders_2stage_mt.py.

◆ opt_data

benders_2stage_mt.opt_data = ws.add_options()

Definition at line 202 of file benders_2stage_mt.py.

◆ queue_lock

benders_2stage_mt.queue_lock = threading.Lock()

Definition at line 277 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 234 of file benders_2stage_mt.py.

◆ scen_solve

benders_2stage_mt.scen_solve

Definition at line 293 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 208 of file benders_2stage_mt.py.

◆ subi

list benders_2stage_mt.subi = []

Definition at line 231 of file benders_2stage_mt.py.

◆ target

benders_2stage_mt.target

Definition at line 293 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 225 of file benders_2stage_mt.py.

◆ theta_fix

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

Definition at line 226 of file benders_2stage_mt.py.

◆ threads

dictionary benders_2stage_mt.threads = {}

Definition at line 291 of file benders_2stage_mt.py.

◆ upperbound

benders_2stage_mt.upperbound = float('inf')

Definition at line 243 of file benders_2stage_mt.py.

◆ value

benders_2stage_mt.value

Definition at line 252 of file benders_2stage_mt.py.

◆ ws

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

Definition at line 196 of file benders_2stage_mt.py.