-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpp.py
189 lines (156 loc) · 5.22 KB
/
lpp.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
import numpy
from matplotlib import pyplot
from matplotlib.path import Path
from matplotlib.patches import PathPatch
# modules used to represent the graphs
fig, axis = pyplot.subplots(figsize=(4.5, 4))
x1 = numpy.linspace(0, 40)
# function definitions for each edge
def edge1():
# bounds represents boundary of feasible region
bounds = Path([
(0., 0.),
(0., 2.),
(3., 0.),
(0., 0.)])
return (20 - x1), ((6 - 2 * x1) / 3), bounds, 20
# returns constraint 1, constrain 2, boundary of feasible region and limit2 of axis
def edge2():
# bounds represents boundary of feasible region
bounds = Path([
(0., 0.),
(0., 2.),
(3., 0.),
(0., 0.)])
return (10 - x1), ((6 - 2 * x1) / 3), bounds, 10
# returns constraint 1, constrain 2, boundary of feasible region and limit2 of axis
def edge3():
# bounds represents boundary of feasible region
bounds = Path([
(0., 0.),
(0., 1.5),
(4., 0.),
(0., 0.)])
return (40 - x1), ((12 - 3 * x1) / 8), bounds, 40
# returns constraint 1, constrain 2, boundary of feasible region and limit2 of axis
def edge4():
# bounds represents boundary of feasible region
bounds = Path([
(0., 0.),
(0., 5.),
(2., 0.),
(0., 0.)])
return (15 - x1), ((10 - 5 * x1) / 2), bounds, 15
# returns constraint 1, constrain 2, boundary of feasible region and limit2 of axis
def edge5():
# bounds represents boundary of feasible region
bounds = Path([
(0., 0.),
(0., 1.5),
(3., 0.),
(0., 0.)])
return (5 - x1), ((15 - 5 * x1) / 10), bounds, 6
# returns constraint 1, constrain 2, boundary of feasible region and limit2 of axis
def edge6():
# bounds represents boundary of feasible region
bounds = Path([
(0., 0.),
(0., 4.),
(2., 0.),
(0., 0.)])
return (20 - x1), ((8 - 4 * x1) / 2), bounds, 20
# returns constraint 1, constrain 2, boundary of feasible region and limit2 of axis
def edge7():
# bounds represents boundary of feasible region
bounds = Path([
(0., 0.),
(0., 2.),
(3., 0.),
(0., 0.)])
return (5 - x1), ((6 - 2 * x1) / 3), bounds, 5
# returns constraint 1, constrain 2, boundary of feasible region and limit2 of axis
def edge8():
# bounds represents boundary of feasible region
bounds = Path([
(0., 0.),
(0., 3.),
(1.5, 0.),
(0., 0.)])
return (10 - x1), ((9 - 6 * x1) / 3), bounds, 10
# returns constraint 1, constrain 2, boundary of feasible region and limit2 of axis
def edge9():
# bounds represents boundary of feasible region
bounds = Path([
(0., 0.),
(0., 4.),
(2., 0.),
(0., 0.)])
return (5 - x1), ((8 - 4 * x1) / 2), bounds, 5
# returns constraint 1, constrain 2, boundary of feasible region and limit2 of axis
edge = int(input('''Enter which edge you would like to see the graph of
1. LC(1,2)
2. LC(1,3)
3. LC(2,3)
4. LC(2,4)
5. LC(3,4)
6. LC(3,5)
7. LC(4,5)
8. LC(4,6)
9. LC(5,6)
'''))
# input to represent each edge in the graph
# if an edge is selected the returned values are assigned to their respective parameters
if edge == 1:
p1_constraint, p2_constraint, bounds, limit = edge1()
elif edge == 2:
p1_constraint, p2_constraint, bounds, limit = edge2()
elif edge == 3:
p1_constraint, p2_constraint, bounds, limit = edge3()
elif edge == 4:
p1_constraint, p2_constraint, bounds, limit = edge4()
elif edge == 5:
p1_constraint, p2_constraint, bounds, limit = edge5()
elif edge == 6:
p1_constraint, p2_constraint, bounds, limit = edge6()
elif edge == 7:
p1_constraint, p2_constraint, bounds, limit = edge7()
elif edge == 8:
p1_constraint, p2_constraint, bounds, limit = edge8()
elif edge == 9:
p1_constraint, p2_constraint, bounds, limit = edge9()
# plot line for p1 and p2 with label as p1 constraint and p2 constraint
pyplot.plot(x1, p1_constraint, linewidth=3, label='P1 constraint')
pyplot.plot(x1, p2_constraint, linewidth=3, label='P2 constraint')
# plot line for x, y >= 0 constraints
pyplot.plot(numpy.zeros_like(x1), x1, linewidth=3, label='$S_1$ Sign restriction')
pyplot.plot(x1, numpy.zeros_like(x1), linewidth=3, label='$S_2$ Sign restriction')
# shades in feasible region on graph
feasible_region = PathPatch(bounds, label='Feasible region', alpha=0.5)
axis.add_patch(feasible_region)
# label axis of graph
pyplot.xlabel('$S_1$')
pyplot.ylabel('$S_2$')
# plot the graphs between -0.1 and the limit given as some graphs dont show x, y>=0 constraint if first limit is 0
pyplot.xlim(-0.1, limit)
pyplot.ylim(-0.1, limit)
# show graph
pyplot.legend()
pyplot.show()
'''
Code i Have used inside of jupyter notebook
from pulp import *
# Example to get solution to maximization and minimization values
# For Minimization define the problem as follows
prob = LpProblem("LC(1,2)", LpMinimize)
# For Maximization define the problem as follows
prob = LpProblem("LC(1,2)", LpMaximize)
s1 = LpVariable("s1", lowBound=0)
s2 = LpVariable("s2", lowBound=0)
prob += 6*s1 + 7*s2
prob += 1*s1 + 1*s2 <= 20
prob += 2*s1 + 3*s2 <= 6
prob
status = prob.solve()
LpStatus[status]
value(s1), value(s2), value(prob.objective)
'''