This is a simple Jupyter Notebook example presenting how to use the LPVisu class.
First, import LPVisu
class and necessary Python packages:
In [ ]:
from lp_visu import LPVisu
from scipy.optimize import linprog
import numpy as np
Define the problem:
In [ ]:
A = [[1.0, 0.0], [1.0, 2.0], [2.0, 1.0]]
b = [8.0, 15.0, 18.0]
c = [4.0, 3.0]
Define the bounds for the two variables x1
and x2
, the GUI bounds and create the visualization object (add a "fake" pivot at (1, 2) and draw objective function for value 40):
In [ ]:
x1_bounds = (0, None)
x2_bounds = (0, None)
x1_gui_bounds = (-1, 16)
x2_gui_bounds = (-1, 10)
visu = LPVisu(A, b, c,
x1_bounds, x2_bounds,
x1_gui_bounds, x2_gui_bounds,
scale = 0.8, pivot_scale = 2.0,
xk = (1, 1), obj = 40)
Define a simple callback function to be called at each step of the linprog
simplexe algorithm. This callback function must use a OptimizeResult
object as parameter:
In [ ]:
def lp_simple_callback(optimizeResult):
"""A simple callback function to see what is happening to print each
step of the algorithm and to use the visualization.
"""
print("current iteration: " + str(optimizeResult["nit"]))
print("current slack: " + str(optimizeResult["slack"]))
print("current solution: " + str(optimizeResult["x"]))
print()
LPVisu(A, b, c,
x1_bounds, x2_bounds,
x1_gui_bounds, x2_gui_bounds,
scale = 0.8, pivot_scale = 2.0,
xk = optimizeResult["x"])
Solve the problem using the callback function and print the result:
In [ ]:
res = linprog(-1.0 * np.array(c), A_ub=A, b_ub=b,
bounds=(x1_bounds, x2_bounds),
callback=lp_simple_callback,
options={"disp": True})
print(res)