Target Linear Optimization / Linear Optimization with Python
I have been installing various linear solvers and modules for Python over the past few hours with various installation and runtime errors. Here's a summary of what I've done so far:
- I have installed Pulp w / pip install pulp
Though it is supposed to include a COIN optimizer, it didn't turn out to be like that. I followed one of the examples:
import pulp
# Create problem instance
giapetto = pulp.LpProblem("Giapetto Workshop", pulp.LpMaximize)
# Define Variables with lower bounds of zero
x1 = pulp.LpVariable("Soldiers",0)
x2 = pulp.LpVariable("Trains",0)
# Add Objective
giapetto += 3*x1 + 2*x2, "Profit"
# Add Constraints
giapetto += 2*x1 + x2 <= 100,"Finishing Labor"
giapetto += x1 + x2 <= 80, "Carpentry Labor"
giapetto += x1 <= 40, "Soldier Demand"
giapetto += x1 + x2 == 20, "Minimum Production"
giapetto.solve()
#giapetto.solve(GLPK())
print pulp.LpStatus[giapetto.status]
print pulp.LpSenses[giapetto.sense], giapetto.objective.name, "=", pulp.value(giapetto.objective)
.. included in the Pulp documentation and got this error:
AttributeError: Object "NoneType" has no attribute "actualSolve"
I also tried it after importing GLPK (brew install glpk which took> 30 minutes bc of missing gcc dependency) and got the error:
NameError: name "GLPK" is undefined
However, when I simulated the command line in iPython, I was able to get GLPK up and running:
%%script glpsol -m /dev/stdin -o /dev/stdout --out output
# declare problem variables
var x;
var y;
var z;
# list all equations
eqn1 : 3*x + 2*y + z = 12;
eqn2 : 2.1*x + y = -3;
eqn3 : y - z = 4;
# solve
solve;
# display results
display x, y, z;
end;
print output
[Out:]
GLPSOL: GLPK LP/MIP Solver, v4.52
Parameter(s) specified in the command line:
-m /dev/stdin -o /dev/stdout
Reading model section from /dev/stdin...
18 lines were read
Generating eqn1...
Generating eqn2...
Generating eqn3...
Model has been successfully generated
So my question is twofold:
- How can I get GLPK (or any solver) to work with the first example code (without using the command line)?
- Is there a solver that is more Python compatible or easier to use, I can use inline from the list below?
* Solver pulp.solvers.GLPK_CMD passed.
Solver pulp.solvers.PULP_CBC_CMD unavailable
Solver pulp.solvers.CPLEX_DLL unavailable
Solver pulp.solvers.CPLEX_CMD unavailable
Solver pulp.solvers.CPLEX_PY unavailable
Solver pulp.solvers.COIN_CMD unavailable
Solver pulp.solvers.COINMP_DLL unavailable
Solver pulp.solvers.GLPK_CMD unavailable
Solver pulp.solvers.XPRESS unavailable
Solver pulp.solvers.GUROBI unavailable
Solver pulp.solvers.GUROBI_CMD unavailable
Solver pulp.solvers.PYGLPK unavailable
Solver pulp.solvers.YAPOSIB unavailable
source to share
You can download the (free) COIN-OR solution here: http://www.coin-or.org/download/binary/SYMPHONY/
Once installed, you can just use giapetto.solve () and it should automatically detect and use the COIN-OR solver.
source to share