benders_2stage Namespace Reference

Functions

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["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, "")
 
 sub = ws.add_job_from_string(get_sub_text())
 
 subi = cp_sub.add_modelinstance()
 
 received = subi.sync_db.add_parameter("received", 1, "units received from master")
 
 demand = subi.sync_db.add_parameter("demand", 1, "stochastic demand")
 
 lowerbound = float('-inf')
 
 upperbound = float('inf')
 
 objmaster = float('inf')
 
int iter = 1
 
 value
 
float objsub = 0.0
 
 probability = scenario_data.find_record((s.key(0), "prob")).value
 

Function Documentation

◆ get_data_text()

def benders_2stage.get_data_text ( )

Definition at line 23 of file benders_2stage.py.

23def get_data_text():
24 return '''
25Sets
26 i factories /f1*f3/
27 j distribution centers /d1*d5/
28
29Parameter
30 capacity(i) unit capacity at factories
31 /f1 500, f2 450, f3 650/
32 demand(j) unit demand at distribution centers
33 /d1 160, d2 120, d3 270, d4 325, d5 700 /
34 prodcost unit production cost /14/
35 price sales price /24/
36 wastecost cost of removal of overstocked products /4/
37
38Table transcost(i,j) unit transportation cost
39 d1 d2 d3 d4 d5
40 f1 2.49 5.21 3.76 4.85 2.07
41 f2 1.46 2.54 1.83 1.86 4.76
42 f3 3.26 3.08 2.60 3.76 4.45;
43
44$ifthen not set useBig
45Set
46 s scenarios /lo,mid,hi/
47
48table ScenarioData(s,*) possible outcomes for demand plus probabilities
49 d1 d2 d3 d4 d5 prob
50lo 150 100 250 300 600 0.25
51mid 160 120 270 325 700 0.50
52hi 170 135 300 350 800 0.25;
53$else
54$if not set nrScen $set nrScen 10
55Set s scenarios /s1*s%nrScen%/;
56parameter ScenarioData(s,*) possible outcomes for demand plus probabilities;
57option seed=1234;
58ScenarioData(s,'prob') = 1/card(s);
59ScenarioData(s,j) = demand(j)*uniform(0.6,1.4);
60$endif
61'''
62

◆ get_master_text()

def benders_2stage.get_master_text ( )

Definition at line 63 of file benders_2stage.py.

63def get_master_text():
64 return '''
65Sets
66 i factories
67 j distribution centers
68
69Parameter
70 capacity(i) unit capacity at factories
71 prodcost unit production cost
72 transcost(i,j) unit transportation cost
73
74$if not set datain $abort 'datain not set'
75$gdxin %datain%
76$load i j capacity prodcost transcost
77
78* Benders master problem
79$if not set maxiter $set maxiter 25
80Set
81 iter max Benders iterations /1*%maxiter%/
82
83Parameter
84 cutconst(iter) constants in optimality cuts
85 cutcoeff(iter,j) coefficients in optimality cuts
86
87Variables
88 ship(i,j) shipments
89 product(i) production
90 received(j) quantity sent to market
91 zmaster objective variable of master problem
92 theta future profit
93Positive Variables ship;
94
95Equations
96 masterobj master objective function
97 production(i) calculate production in each factory
98 receive(j) calculate quantity to be send to markets
99 optcut(iter) Benders optimality cuts;
100
101masterobj..
102 zmaster =e= theta -sum((i,j), transcost(i,j)*ship(i,j))
103 - sum(i,prodcost*product(i));
104
105receive(j).. received(j) =e= sum(i, ship(i,j));
106
107production(i).. product(i) =e= sum(j, ship(i,j));
108product.up(i) = capacity(i);
109
110optcut(iter).. theta =l= cutconst(iter) +
111 sum(j, cutcoeff(iter,j)*received(j));
112
113model masterproblem /all/;
114
115* Initialize cut to be non-binding
116cutconst(iter) = 1e15;
117cutcoeff(iter,j) = eps;
118'''
119

◆ get_sub_text()

def benders_2stage.get_sub_text ( )

Definition at line 120 of file benders_2stage.py.

120def get_sub_text():
121 return '''
122Sets
123 i factories
124 j distribution centers
125
126Parameter
127 demand(j) unit demand at distribution centers
128 price sales price
129 wastecost cost of removal of overstocked products
130 received(j) first stage decision units received
131
132$if not set datain $abort 'datain not set'
133$gdxin %datain%
134$load i j demand price wastecost
135
136* Benders' subproblem
137
138Variables
139 sales(j) sales (actually sold)
140 waste(j) overstocked products
141 zsub objective variable of sub problem
142Positive variables sales, waste
143
144Equations
145 subobj subproblem objective function
146 selling(j) part of received is sold
147 market(j) upperbound on sales
148;
149
150subobj..
151 zsub =e= sum(j, price*sales(j)) - sum(j, wastecost*waste(j));
152
153selling(j).. sales(j) + waste(j) =e= received(j);
154
155market(j).. sales(j) =l= demand(j);
156
157model subproblem /subobj,selling,market/;
158
159* Initialize received
160received(j) = demand(j);
161'''
162
163

Variable Documentation

◆ all_model_types

benders_2stage.all_model_types

Definition at line 183 of file benders_2stage.py.

◆ cp_master

benders_2stage.cp_master = ws.add_checkpoint()

Definition at line 185 of file benders_2stage.py.

◆ cp_sub

benders_2stage.cp_sub = ws.add_checkpoint()

Definition at line 186 of file benders_2stage.py.

◆ cutcoeff

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

Definition at line 193 of file benders_2stage.py.

◆ cutconst

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

Definition at line 192 of file benders_2stage.py.

◆ data

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

Definition at line 170 of file benders_2stage.py.

◆ databases

benders_2stage.databases

Definition at line 189 of file benders_2stage.py.

◆ demand

benders_2stage.demand = subi.sync_db.add_parameter("demand", 1, "stochastic demand")

Definition at line 202 of file benders_2stage.py.

◆ iter

int benders_2stage.iter = 1

Definition at line 210 of file benders_2stage.py.

Referenced by benders_2stage_mt.scen_solve().

◆ lowerbound

benders_2stage.lowerbound = float('-inf')

Definition at line 207 of file benders_2stage.py.

◆ master

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

Definition at line 188 of file benders_2stage.py.

◆ masteri

benders_2stage.masteri = cp_master.add_modelinstance()

Definition at line 191 of file benders_2stage.py.

◆ maxiter

int benders_2stage.maxiter = 40

Definition at line 181 of file benders_2stage.py.

◆ objmaster

benders_2stage.objmaster = float('inf')

Definition at line 209 of file benders_2stage.py.

◆ objsub

float benders_2stage.objsub = 0.0

Definition at line 234 of file benders_2stage.py.

◆ opt

benders_2stage.opt = ws.add_options()

Definition at line 179 of file benders_2stage.py.

◆ opt_data

benders_2stage.opt_data = ws.add_options()

Definition at line 172 of file benders_2stage.py.

◆ probability

benders_2stage.probability = scenario_data.find_record((s.key(0), "prob")).value

Definition at line 243 of file benders_2stage.py.

◆ received

benders_2stage.received = subi.sync_db.add_parameter("received", 1, "units received from master")

Definition at line 201 of file benders_2stage.py.

◆ scenario_data

benders_2stage.scenario_data = data.out_db["ScenarioData"]

Definition at line 178 of file benders_2stage.py.

◆ sub

benders_2stage.sub = ws.add_job_from_string(get_sub_text())

Definition at line 198 of file benders_2stage.py.

◆ subi

benders_2stage.subi = cp_sub.add_modelinstance()

Definition at line 200 of file benders_2stage.py.

◆ theta

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

Definition at line 194 of file benders_2stage.py.

◆ theta_fix

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

Definition at line 195 of file benders_2stage.py.

◆ upperbound

benders_2stage.upperbound = float('inf')

Definition at line 208 of file benders_2stage.py.

◆ value

benders_2stage.value

Definition at line 217 of file benders_2stage.py.

◆ ws

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

Definition at line 166 of file benders_2stage.py.