I have a linear system derived from a network of pairs and I would like to optimize the minimum using a solver in python. An example for this system derived from a small network is summarized by the following data
obj_func
{('1', '2'): [1, 1, 1],
('2', '3'): [1, 1, 1, 2, 1, 0],
('2', '4'): [1, 1, 1, 2, 0, 1],
('3', '4'): [0, 1, 1, 1]}
rhs
{('1', '2'): [0.3333487586477922, 0.3333487586477922, 0.3333024827044157, 1.0],
('2', '3'): [0.5, 0.5, 0.3333487586477922, 0.3333487586477922, 0.3333024827044157],
('2', '4'): [0.49996529223940045, 0.5000347077605996, 0.3333487586477922, 0.3333487586477922, 0.3333024827044157],
('3', '4'): [0.49996529223940045, 0.5000347077605996, 0.5, 0.5]}
constraints
defaultdict(<class 'list'>,
{('1', '2'): [[[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 1]]],
('2', '3'): [[[1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1], [1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 0, 1]]],
('2', '4'): [[[1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1], [1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 0, 1]]],
('3', '4'): [[[1, 1, 0, 0], [0, 0, 1, 1], [1, 0, 1, 0], [0, 1, 0, 1]]]})
Previously, I did the optimization using lpSolve in R
but for programming reasons I changed to python3
and I used Scipy.optimize.linprog(). Now I use lp_solve from lpsolve55
but the results are not the same as the one resulted from scipy.optimize.linprog()
and LpSolve
in R
!
Here is the code using scipy.optimize.linprog()
from scipy.optimize import linprog
min_for_pairs = []
for pair in list(network.edges()):
A = np.reshape(constraints[pair], (-1, len(obj_func[pair])))
res = linprog(obj_func[pair], A_eq=A, b_eq=rhs[pair], method='interior-point', options={'presolve': True})
min_for_pairs.append(res.fun)
min_for_pairs
[1.0, 0.6666975173156104, 0.666651241372254, 0.5000347083535648]
and this one using lp_solve
:
from lpsolve55 import *
from lp_maker import *
from lp_solve import *
min_for_pairs = []
for pair in list(network.edges()):
A = np.reshape(constraints[pair], (-1, len(obj_func[pair])))
sense_equality = [0] * len(A)
lp = lp_maker(obj_func[pair], A , rhs[pair], sense_equality)
solvestat = lpsolve('solve', lp)
obj = lpsolve('get_objective', lp)
min_for_pairs.append(obj)
min_for_pairs
[1.0, 1.3333487586477921, 1.3333487586477921, 1.0]
I would like to know :
1) What is wrong in my code so that I get different results? Is this normal since lp_solve
doesn't find the optimum?
2) I changed from scipy (to lpsolve55) because it was too slow when working on huge networks. For example, I used scipy
to get the minimum for an objective function derived from a small network of less than 16000 pairs and it took more than 6 hours! Generally, are lp_solve
and lp_maker
better for huge systems? or do I need to change for another solver?