-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pulp Tkinter.py
360 lines (313 loc) · 12.9 KB
/
Pulp Tkinter.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import tkinter as tk
import tkinter.ttk as ttk
from pulp import *
import matplotlib.pyplot as plt
import numpy as np
from tkinter import *
import time
toplevel2 = None #Main Top Level
frame1 = None #Main Frame
prob = None #LP Problem Variable
probType = None #Lp Problem Type
option1 = False #Step1 Option Check
option2 = False #Step2 Option Check
option3 = False #Step3 Option Check
option4 = False #Step4 Option Check
option5 = False #Step5 Option Check
############## Decision Variable Config ###############
entries = [] #Decision Variable Text Field Entries
descVar = [] #Decision Variables in LP Variable
descVarCoff = [] ##Decision Variables Cofficients in LP Variable
numDescVarField = None #Total No. of Decision Variables Text Field
numDecsVar = None #Total No. of Decision Variables
dx = 80
dy = 135
############## Constraints Variable Config ###############
numConstField = None #Total No. of Constraints Text Field
numConst = None #Total No. of Constraints
constVarCoff = [] #Cofficients of constraints Array
# empty arrays for your Entrys and StringVars
text_var = []
entries2 = []
matrix = []
m = 0.05
n = dy + 0.15
setConstraintMatrix = None #Find Solution Button
addConstraintBtn = None #Add Constraint Button
############################################
toplevel2 = tk.Tk() #Main Top Level
frame1 = ttk.Frame(toplevel2) #Main Frame
def setConstraintMatrixFunc():
setConstraintMatrix["state"] = "disabled"
addConstraintBtn["state"] = "disabled"
global option5
if not option5:
constraint = []
for i in range(numConst):
constName = "Const" + str(i + 1)
for j in range(numDecsVar):
constraint.append(float(text_var[i][j].get()))
rhs = float(entries2[i][j + 2].get())
symbol = entries2[i][j + 1].get()
#print(constraint)
global prob
if symbol == ">=":
cons = lpSum([constraint[g]*descVar[g] for g in range(numDecsVar)]) >= rhs, constName
#print(cons)
prob += cons
constraint = []
if symbol == "<=":
cons = lpSum([constraint[g]*descVar[g] for g in range(numDecsVar)]) <= rhs, constName
#print(cons)
prob += cons
constraint = []
if symbol == "=":
cons = lpSum([constraint[g]*descVar[g] for g in range(numDecsVar)]) == rhs, constName
#print(cons)
prob += cons
constraint = []
global n
start = time.perf_counter()
prob.solve()
total = time.perf_counter() - start
solution = ttk.Label(frame1)
solution.configure(text='Status:' + LpStatus[prob.status], font=('TkTextFont 15'))
n+=50
solution.place(anchor="nw", x = 10, y = n)
optimalValue = ttk.Label(frame1)
optimalValue.configure(text='Optimal Value:' + str(value(prob.objective)), font=('TkTextFont 15'))
optimalValue.place(anchor="nw", x = 10, y= n + 40)
myx = 10
for x in prob.variables():
optimalvar = ttk.Label(frame1)
optimalvar.configure(text=x.name + '=' + str(x.varValue), font=('TkTextFont 15'))
optimalvar.place(anchor="nw", x = myx, y = n + 80)
#print(len(str(x.varValue)))
myx += 22*len(str(x.varValue))
timetaken = str(total)
cputime = ttk.Label(frame1)
cputime.configure(text='Time Taken(S):' + timetaken[0:5], font=('TkTextFont 15'))
cputime.place(anchor="nw", x = 10, y = n+130)
option5 = True
return
def addNewConstraint():
global numConst
i = numConst
numConst += 1
text_var.append([])
entries2.append([])
global m
global n
for j in range(numDecsVar):
# append your StringVar and Entry
text_var[i].append(StringVar())
entries2[i].append(Entry(frame1, textvariable=text_var[i][j], width = 4))
descx1 = ttk.Label(frame1)
descx1.configure(text="X" + str(j + 1), font=('TkTextFont 10'))
descx1.place(anchor="nw", x = m + 40, y = n)
if j != numDecsVar - 1:
plus_sign = ttk.Label(frame1)
plus_sign.configure(text="+", font=('TkTextFont 10'))
plus_sign.place(anchor="nw", x=m + 60, y=n)
entries2[i][j].place(x = m, y = n)
m += 80
symbol = ttk.Combobox(frame1)
symbol.configure(values='<= = >=', width=3, state="readonly")
symbol.current(0)
symbol.place(anchor="nw", x = m - 10, y = n)
entries2[i].append(symbol)
en = Entry(toplevel2)
en.configure(width = 6)
en.place(anchor="nw", x = m + 50, y = n + 10)
entries2[i].append(en)
n += 30
m = 10
global addConstraintBtn
addConstraintBtn.place(anchor="nw", x = 100, y = n + 5)
global setConstraintMatrix
setConstraintMatrix.place(anchor="nw", x = 10, y = n + 5)
return
def genConstraintsFunc():
global option4
if not option4:
global numConst
global dy
dy+=45
numConst = int(numConstField.get())
if numConst > 0:
step5 = ttk.Label(frame1)
step5.configure(text='Step 5. Fill the Constraints Matrix', font="TkTextFont 10")
step5.place(anchor="nw", x = 10, y = dy)
x2 = 0
y2 = 0
global n
global m
m = 10
n = dy + 20
rows, cols = (numConst, numDecsVar)
for i in range(rows):
# append an empty list to your two arrays
# so you can append to those later
text_var.append([])
entries2.append([])
for j in range(cols):
# append your StringVar and Entry
text_var[i].append(StringVar())
entries2[i].append(Entry(frame1, textvariable=text_var[i][j], width = 4))
descx1 = ttk.Label(frame1)
descx1.configure(text="X" + str(j + 1), font=('TkTextFont 10'))
descx1.place(anchor="nw", x = m + 40, y = n)
if j != numDecsVar - 1:
plus_sign = ttk.Label(frame1)
plus_sign.configure(text="+", font=('TkTextFont 10'))
plus_sign.place(anchor="nw", x=m + 60, y=n)
entries2[i][j].place(x = m, y = n)
m += 80
symbol = ttk.Combobox(frame1)
symbol.configure(values='<= = >=', width=3, state="readonly")
symbol.current(0)
symbol.place(anchor="nw", x = m - 10, y = n)
entries2[i].append(symbol)
en = Entry(toplevel2)
en.configure(width = 6)
en.place(anchor="nw", x = m + 50, y = n + 10)
entries2[i].append(en)
n += 30
m = 10
global setConstraintMatrix
setConstraintMatrix = ttk.Button(frame1)
setConstraintMatrix.configure(text='Find Solution!')
setConstraintMatrix.place(
anchor="nw", x = 10, y = n + 5)
setConstraintMatrix.configure(command=lambda :setConstraintMatrixFunc())
global addConstraintBtn
addConstraintBtn = ttk.Button(frame1)
addConstraintBtn.configure(text='Add Constraint')
addConstraintBtn.place(
anchor="nw", x = 100, y = n + 5)
addConstraintBtn.configure(command=lambda :addNewConstraint())
option4 = True
return
def setObjectiveFunc():
global option3
if not option3:
for x in range(numDecsVar): #Make Array of LP Variables
var_ = 'x' + str(x + 1)
descVar.append(LpVariable(var_, 0))
for x in range(numDecsVar): #Make Array of Constraints
var_ = 'x' + str(x + 1)
n = float(entries[x].get())
descVarCoff.append(n)
global prob
prob += lpSum([descVarCoff[i]*descVar[i] for i in range(numDecsVar)]) #Assign to the global function
global dy
dy += 45
totConst = ttk.Label(frame1)
totConst.configure(text='Step 4. Enter Total Constraints:', font="TkTextFont 10")
totConst.place(anchor="nw", x=10, y=dy)
global numConstField
numConstField = ttk.Entry(frame1)
numConstField.place(anchor="nw", x=10, y=dy+20, width=35)
genConstraintMatrix = ttk.Button(frame1)
genConstraintMatrix.configure(text='Make Constraints Matrix')
genConstraintMatrix.place(
anchor="nw", x = 50, y = dy + 20)
genConstraintMatrix.configure(command=lambda :genConstraintsFunc())
option3 = True
return
def generateObjectiveFunc():
global option2
if not option2:
global numDecsVar
global numDescVarField
numDecsVar = int(numDescVarField.get())
if numDecsVar > 0:
step3Lbl = ttk.Label(frame1)
step3Lbl.configure(text='Step 3. Fill the Objective Function:', font="TkTextFont 10")
step3Lbl.place(anchor="nw", x=10, y=105)
probTypeLbl = ttk.Label(frame1)
if probType == "max":
probTypeLbl.configure(text='Max Z = ', font="TkTextFont 10")
else:
probTypeLbl.configure(text='Min Z = ', font="TkTextFont 10")
probTypeLbl.place(anchor="nw", x = 10, y=125)
global entries
global dx
global dy
buttony = 0.23
for i in range(numDecsVar):
en = Entry(toplevel2)
en.configure(width=5)
en.place(anchor="nw", x = dx, y = dy)
descx1 = ttk.Label(frame1)
descx1.configure(text="X" + str(i + 1), font=('TkTextFont 10'))
descx1.place(anchor="nw", x = dx + 30, y = dy - 10)
if i != numDecsVar - 1:
plus_sign = ttk.Label(frame1)
plus_sign.configure(text="+", font=('Arial 10'))
plus_sign.place(anchor="nw", x = dx + 55, y = dy - 10)
dx += 85
# if dx > 0.97:
# dx = 0.22
# dy += 0.06
entries.append(en)
setObjFuncBtn = ttk.Button(frame1)
setObjFuncBtn.configure(text='Done')
setObjFuncBtn.place(anchor="nw", x = 8, y = dy + 15)
setObjFuncBtn.configure(command=lambda :setObjectiveFunc())
option2 = True
return
def problemType(type):
global option1
if not option1:
if type == "max":
global prob
prob = LpProblem("Question", LpMaximize)
if type == "min":
prob = LpProblem("Question", LpMinimize)
#print(type)
global probType
probType = type
descLbl = ttk.Label(frame1)
descLbl.configure(text='Step 2. Enter Total Decision Variables:', font="TkTextFont 10")
descLbl.place(anchor="nw", x=10, y=50)
global numDescVarField
numDescVarField = ttk.Entry(frame1)
numDescVarField.place(anchor="nw", x=10, y=75, width=35)
genObjFunc = ttk.Button(frame1)
genObjFunc.configure(text='Make Objective Equation')
genObjFunc.place(anchor="nw", x=50, y=75)
genObjFunc.configure(command=lambda :generateObjectiveFunc())
option1 = True
return
def main():
# build ui
toplevel2.configure(height=900, width=700)
toplevel2.geometry("500x700")
toplevel2.maxsize(1920, 1080)
toplevel2.minsize(900, 720)
toplevel2.title("Pulp Solver By Usman (20L-1385)")
frame1.configure(height=1080, padding=10, width=1920)
titleLbl = ttk.Label(frame1)
titleLbl.configure(
font="TkTextFont 20",
text='Pulp Solver By Usman (20L-1385)')
titleLbl.pack(side="top")
probLbl = ttk.Label(frame1)
probLbl.configure(text='Step 1. Choose Problem Type:', font="TkTextFont 10")
probLbl.place(anchor="nw", x=10, y=0)
#Maximization Button
maxBtn = ttk.Button(frame1)
maxBtn.configure(text='Maximization')
maxBtn.place(anchor="nw", x=10, y=20)
maxBtn.configure(command=lambda m="max":problemType(m))
#Minimization Button
minBtn = ttk.Button(frame1)
minBtn.configure(text='Minimization')
minBtn.place(anchor="nw", x=100, y=20)
minBtn.configure(command=lambda m="min":problemType(m))
frame1.place(anchor="nw", relheight=1.0, relwidth=1.0, x=0, y=0)
# Main widget
toplevel2.mainloop()
if __name__ == '__main__':
main()