-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1. Linear Programming Part 1.py
87 lines (62 loc) · 2.02 KB
/
1. Linear Programming Part 1.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
from pulp import *
prob = LpProblem("test1", LpMaximize)
# Variables
# 0 <= x
x = LpVariable("tables", 0, None)
# 0 <= y
y = LpVariable("chairs", 0, None)
# 0 <= z
# z = LpVariable("z", 0)
# Use None for +/- Infinity, i.e. z <= 0 -> LpVariable("z", None, 0)
# Objective
prob += x*7 + y*5, "obj"
# (the name at the end is facultative)
# Constraints
# (the names at the end are facultative)
prob += x*3 + y*4 <= 2400, "c1"
prob += x*2 + y*1 <= 1000, "c2"
prob += y <= 450, "c3"
prob += x >= 100, "c4"
# Write the problem as an LP file
# prob.writeLP("test1.lp")
# Solve the problem using the default solver
prob.solve()
# Use prob.solve(GLPK()) instead to choose GLPK as the solver
# Use GLPK(msg = 0) to suppress GLPK messages
# If GLPK is not in your path and you lack the pulpGLPK module,
# replace GLPK() with GLPK("/path/")
# Where /path/ is the path to glpsol (excluding glpsol itself).
# If you want to use CPLEX, use CPLEX() instead of GLPK().
# If you want to use XPRESS, use XPRESS() instead of GLPK().
# If you want to use COIN, use COIN() instead of GLPK(). In this last case,
# two paths may be provided (one to clp, one to cbc).
# Print the status of the solved LP
print("Status:", LpStatus[prob.status])
# Print the value of the variables at the optimum
for v in prob.variables():
print(v.name, "=", v.varValue)
# Print the value of the objective
print("objective=", value(prob.objective))
from pulp import *
prob = LpProblem("Bonus", LpMaximize)
# Variables
# 0 <= x
x = LpVariable("tables", 0, None)
# 0 <= y
y = LpVariable("chairs", 0, None)
# Objective
prob += x*7 + y*5, "obj"
# Constraints
prob += x*3 + y*4 <= 2400, "c1"
prob += x*2 + y*1 <= 1000, "c2"
prob += y <= 450, "c3"
prob += x >= 100, "c4"
# Solve the problem using the default solver
prob.solve()
# Print the status of the solved LP
print("Status:", LpStatus[prob.status])
# Print the value of the variables at the optimum
for v in prob.variables():
print(v.name, "=", v.varValue)
# Print the value of the objective
print("Total Profit: ", value(prob.objective))