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 22 of file benders_2stage.py.

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

◆ get_master_text()

def benders_2stage.get_master_text ( )

Definition at line 62 of file benders_2stage.py.

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

◆ get_sub_text()

def benders_2stage.get_sub_text ( )

Definition at line 119 of file benders_2stage.py.

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

Variable Documentation

◆ all_model_types

benders_2stage.all_model_types

Definition at line 182 of file benders_2stage.py.

◆ cp_master

benders_2stage.cp_master = ws.add_checkpoint()

Definition at line 184 of file benders_2stage.py.

◆ cp_sub

benders_2stage.cp_sub = ws.add_checkpoint()

Definition at line 185 of file benders_2stage.py.

◆ cutcoeff

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

Definition at line 192 of file benders_2stage.py.

◆ cutconst

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

Definition at line 191 of file benders_2stage.py.

◆ data

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

Definition at line 169 of file benders_2stage.py.

◆ databases

benders_2stage.databases

Definition at line 188 of file benders_2stage.py.

◆ demand

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

Definition at line 201 of file benders_2stage.py.

◆ iter

int benders_2stage.iter = 1

Definition at line 209 of file benders_2stage.py.

Referenced by benders_2stage_mt.scen_solve().

◆ lowerbound

benders_2stage.lowerbound = float('-inf')

Definition at line 206 of file benders_2stage.py.

◆ master

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

Definition at line 187 of file benders_2stage.py.

◆ masteri

benders_2stage.masteri = cp_master.add_modelinstance()

Definition at line 190 of file benders_2stage.py.

◆ maxiter

int benders_2stage.maxiter = 40

Definition at line 180 of file benders_2stage.py.

◆ objmaster

benders_2stage.objmaster = float('inf')

Definition at line 208 of file benders_2stage.py.

◆ objsub

float benders_2stage.objsub = 0.0

Definition at line 233 of file benders_2stage.py.

◆ opt

benders_2stage.opt = ws.add_options()

Definition at line 178 of file benders_2stage.py.

◆ opt_data

benders_2stage.opt_data = ws.add_options()

Definition at line 171 of file benders_2stage.py.

◆ probability

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

Definition at line 242 of file benders_2stage.py.

◆ received

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

Definition at line 200 of file benders_2stage.py.

◆ scenario_data

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

Definition at line 177 of file benders_2stage.py.

◆ sub

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

Definition at line 197 of file benders_2stage.py.

◆ subi

benders_2stage.subi = cp_sub.add_modelinstance()

Definition at line 199 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 193 of file benders_2stage.py.

◆ theta_fix

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

Definition at line 194 of file benders_2stage.py.

◆ upperbound

benders_2stage.upperbound = float('inf')

Definition at line 207 of file benders_2stage.py.

◆ value

benders_2stage.value

Definition at line 216 of file benders_2stage.py.

◆ ws

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

Definition at line 165 of file benders_2stage.py.