-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt_of_other _kind.py
299 lines (169 loc) · 6.63 KB
/
opt_of_other _kind.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import random
from pulp import *
######@@@@@@@@@@--Initializations and Declarations--@@@@@@@@@@###########
#Supply limits
#s1=[random.randint(30,35), random.randint(30,35), random.randint(30,35)]
s1=random.randint(90,100)
s2=random.randint(90, 100)
s3=random.randint(90, 100)
#Demand Limits
d1=random.randint(90, 100)
d2=random.randint(90, 100)
d3=random.randint(90, 100)
#Prices of products
p1=random.randint(70,80)
p2=random.randint(35,45)
p3=random.randint(55,65)
#Cost of production and processing
c1=random.randint(50,60)
c2=random.randint(15,25)
c3=random.randint(35,45)
#quality of sources, blends and final products
a1=random.random()
a2=random.random()
a3=random.random()
b1=random.random()
b2=random.random()
b3=0
q1=random.random()
q2=random.random()
q3=random.random()
#Creating variables of flow from source to pool, with a lower limit of zero
x11=LpVariable("x11",random.randint(0,33),None,LpInteger)
x12=0
x13=LpVariable("x13",random.randint(0,33),None,LpInteger)
x21=LpVariable("x21",random.randint(0,33),None,LpInteger)
x22=0
x23=0
x31=0
x32=LpVariable("x32",random.randint(0,33),None,LpInteger)
x33=LpVariable("x33",random.randint(0,33),None,LpInteger)
#Creating variables of flow from pool to final product, with a lower limit of zero
y11=0
y12=LpVariable("y12",random.randint(0,33),None,LpInteger)
y13=0
y21=LpVariable("y21",random.randint(0,33),None,LpInteger)
y22=0
y23=LpVariable("y23",random.randint(0,33),None,LpInteger)
y31=0
y32=LpVariable("y32",random.randint(0,33),None,LpInteger)
y33=0
######@@@@@@@@@@------------------------------------@@@@@@@@@@###########
####### 1. Minimizing cost of flow from S1 ##############################
#Variable to contain problem data
prob=LpProblem("Pooling Problem", LpMinimize)
#adding objective function to prob
prob+= c1*x11+ c1*x12+ c1*x13,"flows from s1 to p1, p2 and p3"
#entering constraints to prob
prob+=x11+x12+x13 <= s1, "supply quantity from source 1 "
prob+=b1*(x11+x21+x31)>=a1*x11+a2*x21+a3*x31,"Mixing rule for pool 1"
prob+=y11+y12+y13==x11+x21+x31,"Mass balance on pool 1"
#writing the problem in a file poolingproblem.lp
prob.writeLP("poolingproblem.lp")
#calling the solver to solve the problem
prob.solve()
#printing status
print LpStatus[prob.status]
for v in prob.variables():
print v.name,"=", v.varValue
cost1=value(prob.objective)
print "cost of flow from Source 1: ", cost1, '\n'
#######################################################################
####### 2. Minimizing cost of flow from S2 ##############################
#Variable to contain problem data
prob=LpProblem("Pooling Problem", LpMinimize)
#adding objective function to prob
prob+= c2*x21+ c2*x22+ c2*x23,"flows from s2 to p1, p2 and p3"
#entering constraints to prob
prob+=x21+x22+x23 <= s2, "supply quantity from source 2"
prob+=b2*(x12+x22+x32)>=a1*x12+a2*x22+a3*x32,"Mixing rule for pool 2"
prob+=y21+y22+y23==x12+x22+x32,"Mass balance on pool 2"
#writing the problem in a file poolingproblem.lp
prob.writeLP("poolingproblem.lp")
#calling the solver to solve the problem
prob.solve()
#printing status
print LpStatus[prob.status]
for v in prob.variables():
print v.name,"=", v.varValue
cost2=value(prob.objective)
print "cost of flow from Source 2: ", cost2, '\n'
#######################################################################
####### 3. Minimizing cost of flow from S3 ##############################
#Variable to contain problem data
prob=LpProblem("Pooling Problem", LpMinimize)
#adding objective function to prob
prob+= c3*x31+ c3*x32+ c3*x33,"flows from s3 to p1, p2 and p3"
#entering constraints to prob
prob+=x31+x32+x33 <= s3, "supply quantity from source 3 "
prob+=b3*(x13+x23+x33)>=a1*x13+a2*x23+a3*x33,"Mixing rule for pool 3"
prob+=y31+y32+y33==x13+x23+x33,"Mass balance on pool 3"
#writing the problem in a file poolingproblem.lp
prob.writeLP("poolingproblem.lp")
#calling the solver to solve the problem
prob.solve()
#printing status
print LpStatus[prob.status]
for v in prob.variables():
print v.name,"=", v.varValue
cost3=value(prob.objective)
print "cost of flow from Source 3: ", cost3, '\n'
#######################################################################
####### 1. Maximizing cost of flow from Pool 1 ##############################
#Variable to contain problem data
prob=LpProblem("Pooling Problem", LpMaximize)
#adding objective function to prob
prob+= p1*y11+ p1*y21+ p1*y31,"flows from pools to product 1"
#entering constraints to prob
prob+=y11+y21+y31 <= d1, "demand for product 1 "
prob+=q1*y11+q1*y21+q1*y31>=b1*y11+b2*y21+b3*y31,"Mixing rule of Product 1"
#writing the problem in a file poolingproblem.lp
prob.writeLP("poolingproblem.lp")
#calling the solver to solve the problem
prob.solve()
#printing status
print LpStatus[prob.status]
for v in prob.variables():
print v.name,"=", v.varValue
sp1=value(prob.objective)
print "selling price of product 1: ", sp1,'\n'
#######################################################################
####### 2. Maximizing cost of flow from Pool 2 ##############################
#Variable to contain problem data
prob=LpProblem("Pooling Problem", LpMaximize)
#adding objective function to prob
prob+= p2*y12+ p2*y22+ p2*y32,"flows from pools to product 2 "
#entering constraints to prob
prob+=y12+y22+y32 <= d2, "demand for product 2 "
prob+=q2*y12+q2*y22+q2*y32>=b1*y12+b2*y22+b3*y32,"Mixing rule of product 2"
#writing the problem in a file poolingproblem.lp
prob.writeLP("poolingproblem.lp")
#calling the solver to solve the problem
prob.solve()
#printing status
print LpStatus[prob.status]
for v in prob.variables():
print v.name,"=", v.varValue
sp2=value(prob.objective)
print "selling price of product 2: ", sp2, '\n'
#######################################################################
####### 3. Maximizing cost of flow from Pool 3 ##############################
#Variable to contain problem data
prob=LpProblem("Pooling Problem", LpMaximize)
#adding objective function to prob
prob+= p3*y13+ p3*y23+ p3*y33,"flows from pools to product 3"
#entering constraints to prob
prob+=y13+y23+y33 <= d3, "demand for product 3 "
prob+=q3*(y13+y23+y33)>=b1*y13+b2*y23+b3*y33,"Mixing rule for product 3"
#writing the problem in a file poolingproblem.lp
prob.writeLP("poolingproblem.lp")
#calling the solver to solve the problem
prob.solve()
#printing status
print LpStatus[prob.status]
for v in prob.variables():
print v.name,"=", v.varValue
sp3=value(prob.objective)
print "selling price of product 3: ", sp3, '\n'
#######################################################################
print "\nFinal Maximized profit : ", (sp1-cost1)+(sp2-cost2)+(sp3-cost3)