I'm new to python, picked it up because of Optimization and I've wanted to learn something new. I've made an lp solver program using PyQt5 and PuLP. The concept is simple: The user types in the Lp problem to a QTextEdit widget clicks the solve button then gets the result in a QTextBrowser.
The example exercise I'm using and trying to replicate:
prob = LpProblem("LP problem", LpMaximize)
x1 = LpVariable("x1", lowBound=0, cat='Integer') # Integer variable x1 >= 0
x2 = LpVariable("x2", lowBound=0, cat='Integer') # Integer variable x2 >= 0
prob += x2<=4
prob += 4*x1 + 2*x2 <= 20
prob += 1*x1 + 4*x2 <= 12
prob += 4*x1 + 4*x2
prob.solve()
It works like a charm this way. The function for the button:
def textToVar(self):
prob = LpProblem("LP problem", LpMaximize)
x1 = LpVariable("x1", lowBound=0, cat='Integer') # Integer variable x1 >= 0
x2 = LpVariable("x2", lowBound=0, cat='Integer') # Integer variable x2 >= 0
mytext = self.lpInput.toPlainText()
split = mytext.splitlines()
for ele in range(0, len(split)):
prob += split[ele]
prob.solve()
for v in prob.variables():
self.lpOutput.append(str(v.name) + ' = ' + str(v.varValue))
vmax = (value(prob.objective))
self.lpOutput.append('max = ' + str(vmax))
It does not work and I've figured it is because split = mytext.splitlines()
generates ['x2<=4', '4*x1+2*x2<=20', '1*x1+4*x2<=12', '4*x1+4*x2']
instead of [x2<=4, 4*x1+2*x2<=20, 1*x1+4*x2<=12, 4*x1+4*x2]
. How can I convert my list from the first to the second one? Maybe I could use another method to store the input in a list or variable instead of splitlines()
?
Thank you in advance!