M.Malah
M.Malah

Reputation: 21

PuLP optimization

I'm trying to create a program using PuLP library that when you hit the button it solves a linear problem and outputs values. But i can't make it work. It's only write my ""Enter more values"" and doesn't want to solve. Maybe I have some problems with input values but i'm not really sure.

Here is my code:

from tkinter import*
from pulp import*

def problem(a_val, b_val, c_val, d_val, e_val, f_val, g_val):
    prob=LpProblem("problem", LpMaximize)
    x1=LpVariable("x1", lowBound=0)
    x2=LpVariable("x2", lowBound=0)
    x3=LpVariable("x3", lowBound=0)
    prob+= a_val*x1 +b_val*x2 +c_val*x3,
    prob+= d_val*x1 +e_val*x2 + f_val*x3 <= g_val,
    prob.solve ()
    print("status:", LpStatus[prob.status])
    for v in prob.variables():
        print (v.name, "=", v.varValue)
        print("objective=%s$" % value(prob.objective))
root =Tk()
root.title("System")
root.geometry("1300x500+0+0")
a=Entry(Top, font=("arial", 10, "bold"), bd=8)
a.grid(row=1, column=1)

b=Entry(Top,  font=("arial", 10, "bold"), bd=8)
b.grid(row=1, column=2)

c=Entry(Top,  font=("arial", 10, "bold"), bd=8)
c.grid(row=1, column=3)

d=Entry(Top,  font=("arial", 10, "bold"), bd=8)
d.grid(row=2, column=1)

e=Entry(Top,  font=("arial", 10, "bold"), bd=8)
e.grid(row=2, column=2)

f=Entry(Top,  font=("arial", 10, "bold"), bd=8)
f.grid(row=2, column=3)

g=Entry(Top, font=("arial", 10, "bold"), bd=8)
g.grid(row=3, column=1)

def inserter (value):
    w.delete("0.0", "end")
    w.insert("0.0", value)
def handler():
    try:
        g_val = float(g.get())
        a_val = float(a.get())
        b_val = float(b.get())
        c_val = float(c.get())
        d_val = float(d.get())
        e_val = float(e.get())
        f_val = float(f.get())
        inserter(problem(a_val,b_val,c_val,d_val,e_val,f_val, g_val))
    except ValueError:
            inserter("Enter more values")

w=Text(Top, font=("arial", 10, "bold"), bd=6)
w.grid(row=4, column=1)

info6=Button(Top, font=("arial", 10,"bold"), text="Optimize", bd=8,                                 command=handler)
info6.grid(row=4, column=0)

root.mainloop()

Upvotes: 1

Views: 381

Answers (1)

Stevo Mitric
Stevo Mitric

Reputation: 1619

According to PuLP documentation you are required to put a short string at the end of the function statement.

The variable prob now begins collecting problem data with the += operator. The objective function is logically entered first, with an important comma , at the end of the statement and a short string explaining what this objective function is:

# The objective function is added to 'prob' first
prob += 0.013*x1 + 0.008*x2, "Total Cost of Ingredients per can"

So you would need to:

prob+= a_val*x1 +b_val*x2 +c_val*x3, "whats this"
prob+= d_val*x1 +e_val*x2 + f_val*x3 <= g_val, "something here too"

Furthermore your function problem doesnt return anything (None) and you're trying to put it in a Text widget. You would need to make the insertion inside your function (or add a return statement). For example:

def problem(a_val, b_val, c_val, d_val, e_val, f_val, g_val):

    ...

    w.delete("0.0", "end")                          # Clear the Text widget
    print("status:", LpStatus[prob.status])
    for v in prob.variables():
        print (v.name, "=", v.varValue)
        print("objective=%s$" % value(prob.objective))
        w.insert(END, str(v.name) + "=" + str(v.varValue) + '\n' )       # Insert data at the END (rather then at the beginning)
        w.insert(END, "objective=%s$" % value(prob.objective) + '\n' )

If you decide to do something like this (without return statement), then make sure you delete your custom insert call:

#inserter(problem(a_val,b_val,c_val,d_val,e_val,f_val, g_val))     #instead of this
problem(a_val,b_val,c_val,d_val,e_val,f_val, g_val)                #do this

Upvotes: 2

Related Questions