Skip to content

Commit b0a6a4e

Browse files
authored
Add files via upload
1 parent b176a39 commit b0a6a4e

File tree

5 files changed

+428
-0
lines changed

5 files changed

+428
-0
lines changed

codes/Chapter 6/PYOMO_example_1.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Filename: PYOMO_example_1.py
2+
# Description: An example of solving a linear
3+
# programming problem with Pyomo
4+
# Authors: Papathanasiou, J. & Ploskas, N.
5+
6+
from pyomo.environ import *
7+
from pyomo.opt import SolverFactory
8+
import matplotlib.pyplot as plt
9+
import numpy as np
10+
11+
# Create an object to perform optimization
12+
opt = SolverFactory('cplex')
13+
14+
# Create an object of a concrete model
15+
model = ConcreteModel()
16+
17+
# Define the decision variables
18+
model.x1 = Var(within=NonNegativeReals)
19+
model.x2 = Var(within=NonNegativeReals)
20+
21+
# Define the objective function
22+
model.obj = Objective(expr = 60 * model.x1 +
23+
40 * model.x2)
24+
25+
# Define the constraints
26+
model.con1 = Constraint(expr = 4 * model.x1 +
27+
4 * model.x2 >= 10)
28+
model.con2 = Constraint(expr = 2 * model.x1 +
29+
model.x2 >= 4)
30+
model.con3 = Constraint(expr = 6 * model.x1 +
31+
2 * model.x2 <= 12)
32+
33+
# Solve the linear programming problem
34+
results = opt.solve(model)
35+
36+
# Print the results
37+
print ("Status: ",
38+
results.solver.termination_condition)
39+
40+
print("x1 = ", model.x1.value)
41+
print("x2 = ", model.x2.value)
42+
43+
print ("The optimal value of the objective function "
44+
"is = ", model.obj())
45+
46+
# Plot the results
47+
x = np.arange(0, 5)
48+
49+
plt.plot(x, 2.5 - x, label = '4x1 + 4x2 >= 10')
50+
plt.plot(x, 4 - 2 * x, label= ' 2x1 + x2 >= 4')
51+
plt.plot(x, 6 - 3 * x, label = '6x1 + 2x2 <= 12')
52+
plt.plot(x, 2 - 2/3*x, '--')
53+
plt.annotate('Objective\n function', xy = (0.6, 1.6),
54+
xytext = (0.3, 0.8), arrowprops =
55+
dict(facecolor = 'black', width = 1.5, headwidth = 7))
56+
plt.annotate('Optimal\n solution\n(1.5, 1)',
57+
xy = (1.48, 0.98), xytext = (1, 0.5), arrowprops =
58+
dict(facecolor = 'black', width = 1.5, headwidth = 7))
59+
plt.plot(1.5, 1, 'wo')
60+
plt.text(0.1, 4, 'Feasible area', size = '12')
61+
62+
# Define the boundaries of the feasible area in the plot
63+
a = [0, 1.5, 1.75, 0, 0]
64+
b = [4, 1, 0.75, 6, 4]
65+
plt.fill(a, b, 'grey')
66+
67+
plt.xlabel("x1")
68+
plt.ylabel("x2")
69+
plt.title('LP Example 1')
70+
plt.axis([0, 3, 0, 6])
71+
plt.grid(True)
72+
plt.legend()
73+
plt.show()

codes/Chapter 6/PYOMO_example_2.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Filename: PYOMO_example_2.py
2+
# Description: An example of solving a binary
3+
# linear programming problem with Pyomo
4+
# Authors: Papathanasiou, J. & Ploskas, N.
5+
6+
from pyomo.environ import *
7+
from pyomo.opt import SolverFactory
8+
import matplotlib.pyplot as plt
9+
import numpy as np
10+
11+
# Create an object to perform optimization
12+
opt = SolverFactory('cplex')
13+
14+
# Create an object of a concrete model
15+
model = ConcreteModel()
16+
17+
# Define the decision variables
18+
model.x1 = Var(within=Binary)
19+
model.x2 = Var(within=Binary)
20+
model.x3 = Var(within=Binary)
21+
model.x4 = Var(within=Binary)
22+
model.x5 = Var(within=Binary)
23+
model.x6 = Var(within=Binary)
24+
25+
# Define the objective function
26+
model.obj = Objective(expr = model.x1 +
27+
model.x2 + model.x3 + model.x4 + model.x5 +
28+
model.x6)
29+
30+
# Define the constraints
31+
model.con1 = Constraint(expr = model.x1 +
32+
model.x3 >= 2)
33+
model.con2 = Constraint(expr = model.x1 +
34+
model.x2 + model.x5 >= 2)
35+
model.con3 = Constraint(expr = model.x3 +
36+
model.x4 >= 1)
37+
model.con4 = Constraint(expr = model.x1 +
38+
model.x4 + model.x5 >= 1)
39+
model.con5 = Constraint(expr = model.x4 +
40+
model.x5 + model.x6 >= 1)
41+
42+
# Solve the binary linear programming problem
43+
results = opt.solve(model)
44+
45+
# Print the results
46+
print ("Status: ",
47+
results.solver.termination_condition)
48+
49+
print("x1 = ", model.x1.value)
50+
print("x2 = ", model.x2.value)
51+
print("x3 = ", model.x3.value)
52+
print("x4 = ", model.x4.value)
53+
print("x5 = ", model.x5.value)
54+
print("x6 = ", model.x6.value)
55+
56+
print ("The optimal value of the objective function "
57+
"is = ", model.obj())

codes/Chapter 6/classicalGP.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Filename: classicalGP.py
2+
# Description: Classical Goal Programming method
3+
# Authors: Papathanasiou, J. & Ploskas, N.
4+
5+
from pyomo.environ import *
6+
from pyomo.opt import SolverFactory
7+
8+
# Create an object to perform optimization
9+
opt = SolverFactory('cplex')
10+
11+
# Create an object of a concrete model
12+
model = ConcreteModel()
13+
14+
# Define the decision variables
15+
model.x1 = Var(within = NonNegativeIntegers)
16+
model.x2 = Var(within = NonNegativeIntegers)
17+
18+
# Define the deviational variables
19+
model.n1 = Var(within = NonNegativeIntegers)
20+
model.p1 = Var(within = NonNegativeIntegers)
21+
model.n2 = Var(within = NonNegativeIntegers)
22+
model.p2 = Var(within = NonNegativeIntegers)
23+
model.n3 = Var(within = NonNegativeIntegers)
24+
model.p3 = Var(within = NonNegativeIntegers)
25+
model.n4 = Var(within = NonNegativeIntegers)
26+
model.p4 = Var(within = NonNegativeIntegers)
27+
28+
# Define the objective function
29+
model.obj = Objective(expr = model.p1 + model.p2 +
30+
model.n3 + model.p4)
31+
32+
# Define the constraints
33+
model.con1 = Constraint(expr = 2 * model.x1 +
34+
4 * model.x2 + model.n1 - model.p1 == 600)
35+
model.con2 = Constraint(expr = 5 * model.x1 +
36+
3 * model.x2 + model.n2 - model.p2 == 700)
37+
model.con3 = Constraint(expr = 100 * model.x1 +
38+
90 * model.x2 + model.n3 - model.p3 == 18000)
39+
model.con4 = Constraint(expr = 2 * model.x1 +
40+
2 * model.x2 + model.n4 - model.p4 == 380)
41+
model.con5 = Constraint(expr = model.x1 +
42+
model.x2 <= 200)
43+
model.con6 = Constraint(expr = model.x1 >= 60)
44+
model.con7 = Constraint(expr = model.x2 >= 60)
45+
46+
# Solve the Goal Programming problem
47+
opt.solve(model)
48+
49+
# Print the values of the decision variables
50+
print("x1 = ", model.x1.value)
51+
print("x2 = ", model.x2.value)
52+
53+
# Print the achieved values for each goal
54+
if model.n1.value > 0:
55+
print("The first goal is underachieved by ",
56+
model.n1.value)
57+
elif model.p1.value > 0:
58+
print("The first goal is overachieved by ",
59+
model.p1.value)
60+
else:
61+
print("The first goal is fully satisfied")
62+
63+
if model.n2.value > 0:
64+
print("The second goal is underachieved by ",
65+
model.n2.value)
66+
elif model.p2.value > 0:
67+
print("The second goal is overachieved by ",
68+
model.p2.value)
69+
else:
70+
print("The second goal is fully satisfied")
71+
72+
if model.n3.value > 0:
73+
print("The third goal is underachieved by ",
74+
model.n3.value)
75+
elif model.p3.value > 0:
76+
print("The third goal is overachieved by ",
77+
model.p3.value)
78+
else:
79+
print("The third goal is fully satisfied")
80+
81+
if model.n4.value > 0:
82+
print("The fourth goal is underachieved by ",
83+
model.n4.value)
84+
elif model.p4.value > 0:
85+
print("The fourth goal is overachieved by ",
86+
model.p4.value)
87+
else:
88+
print("The fourth goal is fully satisfied")

codes/Chapter 6/lexicographicGP.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Filename: lexicographicGP.py
2+
# Description: Lexicographic Goal Programming
3+
# method
4+
# Authors: Papathanasiou, J. & Ploskas, N.
5+
6+
from pyomo.environ import *
7+
from pyomo.opt import SolverFactory
8+
9+
# Create an object to perform optimization
10+
opt = SolverFactory('cplex')
11+
12+
# Create an object of a concrete model
13+
model = ConcreteModel()
14+
15+
# Define the decision variables
16+
model.x1 = Var(within = NonNegativeIntegers)
17+
model.x2 = Var(within = NonNegativeIntegers)
18+
19+
# Define the deviational variables
20+
model.n1 = Var(within = NonNegativeIntegers)
21+
model.p1 = Var(within = NonNegativeIntegers)
22+
model.n2 = Var(within = NonNegativeIntegers)
23+
model.p2 = Var(within = NonNegativeIntegers)
24+
model.n3 = Var(within = NonNegativeIntegers)
25+
model.p3 = Var(within = NonNegativeIntegers)
26+
model.n4 = Var(within = NonNegativeIntegers)
27+
model.p4 = Var(within = NonNegativeIntegers)
28+
29+
# Define the objective function of the
30+
# first priority level
31+
model.obj = Objective(expr = model.p4)
32+
33+
# Define the constraints
34+
model.con1 = Constraint(expr = 2 * model.x1 +
35+
4 * model.x2 + model.n1 - model.p1 == 600)
36+
model.con2 = Constraint(expr = 5 * model.x1 +
37+
3 * model.x2 + model.n2 - model.p2 == 700)
38+
model.con3 = Constraint(expr = 100 * model.x1 +
39+
90 * model.x2 + model.n3 - model.p3 == 18000)
40+
model.con4 = Constraint(expr = 2 * model.x1 +
41+
2 * model.x2 + model.n4 - model.p4 == 380)
42+
model.con5 = Constraint(expr = model.x1 +
43+
model.x2 <= 200)
44+
model.con6 = Constraint(expr = model.x1 >= 60)
45+
model.con7 = Constraint(expr = model.x2 >= 60)
46+
47+
# Solve the Goal Programming problem of the
48+
# first priority level
49+
opt.solve(model)
50+
51+
# Retrieve the value of the first priority level
52+
p4 = model.p4.value
53+
54+
# Define the objective function of the
55+
# second priority level
56+
model.obj = Objective(expr = model.n3)
57+
58+
# Add a constraint for the value of the first
59+
# priority level
60+
model.con8 = Constraint(expr = model.p4 == p4)
61+
62+
# Solve the Goal Programming problem of the
63+
# second priority level
64+
opt.solve(model)
65+
66+
# Retrieve the value of the second priority level
67+
n3 = model.n3.value
68+
69+
# Define the objective function of the
70+
# third priority level
71+
model.obj = Objective(expr = model.p1 + model.p2)
72+
73+
# Add a constraint for the value of the second
74+
# priority level
75+
model.con9 = Constraint(expr = model.n3 == n3)
76+
77+
# Solve the Goal Programming problem of the
78+
# third priority level
79+
opt.solve(model)
80+
81+
# Print the values of the decision variables
82+
print("x1 = ", model.x1.value)
83+
print("x2 = ", model.x2.value)
84+
85+
# Print the achieved values for each goal
86+
if model.n1.value > 0:
87+
print("The first goal is underachieved by ",
88+
model.n1.value)
89+
elif model.p1.value > 0:
90+
print("The first goal is overachieved by ",
91+
model.p1.value)
92+
else:
93+
print("The first goal is fully satisfied")
94+
95+
if model.n2.value > 0:
96+
print("The second goal is underachieved by ",
97+
model.n2.value)
98+
elif model.p2.value > 0:
99+
print("The second goal is overachieved by ",
100+
model.p2.value)
101+
else:
102+
print("The second goal is fully satisfied")
103+
104+
if model.n3.value > 0:
105+
print("The third goal is underachieved by ",
106+
model.n3.value)
107+
elif model.p3.value > 0:
108+
print("The third goal is overachieved by ",
109+
model.p3.value)
110+
else:
111+
print("The third goal is fully satisfied")
112+
113+
if model.n4.value > 0:
114+
print("The fourth goal is underachieved by ",
115+
model.n4.value)
116+
elif model.p4.value > 0:
117+
print("The fourth goal is overachieved by ",
118+
model.p4.value)
119+
else:
120+
print("The fourth goal is fully satisfied")

0 commit comments

Comments
 (0)