CBC
What is PuLP?
PuLP is an LP modeler written in Python.
PuLP generates MPS or LP files,
Linear problems can be solved by calling GLPK, COIN-OR CLP/CBC, CPLEX, GUROBI, MOSEK, XPRESS, CHOCO, MIPCL, and SCIP.
Install
$ pip install pulp
Supported Python versions
- Python 2.7
- Python >= 3.4.
Example scan
To create a new variable, use LpVariable().
0 <= x <= 3:
x = LpVariable("x", 0, 3)
0 <= y <= 1:
y = LpVariable("y", 0, 1)
Use LpProblem() to create a new problem. Create myProblem ".
prob = LpProblem("myProblem", LpMinimize)
Combine variables to create expressions and constraints and add them to your problem.
prob += x + y <= 2
If you add an expression (not a constraint), it becomes an objective function.
prob += -4*x + y
Untie.
status = prob.solve()
View the status of the solution.
LpStatus[status]
To get the value of a variable, use value().
value(x)
Examples due to compounding problems
import pulp
# Set to minimize functions
problem = pulp. LpProblem("Formulation Problem", pulp. LpMinimize)
# Set variables (variable name, minimum, maximum, type)
# Proportion of 9 types of alloys
x1 = pulp. LpVariable('X1', 0, 1, 'Continuous')
x2 = pulp. LpVariable('X2', 0, 1, 'Continuous')
x3 = pulp. LpVariable('X3', 0, 1, 'Continuous')
x4 = pulp. LpVariable('X4', 0, 1, 'Continuous')
x5 = pulp. LpVariable('X5', 0, 1, 'Continuous')
x6 = pulp. LpVariable('X6', 0, 1, 'Continuous')
x7 = pulp. LpVariable('X7', 0, 1, 'Continuous')
x8 = pulp. LpVariable('X8', 0, 1, 'Continuous')
x9 = pulp. LpVariable('X9', 0, 1, 'Continuous')
# Objective function (defines the cost you want to minimize)
problem += 7.3*x1 + 6.9*x2 + 7.3*x3 + 7.5*x4 + 7.6*x5 + 6.0*x6 + 5.8*x7 + 4.3*x8 + 4.1*x9
# Setting Constraints
# Total 1 (100%)
problem += x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 == 1
# Lead mixing ratio
problem += 20*x1 + 50*x2 + 30*x3 +30*x4 + 30*x5 + 60*x6 + 40*x7 + 10*x8 + 10*x9 == 30
# Zinc mixing ratio
problem += 30*x1 + 40*x2 + 20*x3 +40*x4 + 30*x5 + 30*x6 + 50*x7 + 30*x8 + 10*x9 == 30
# Tin mixing ratio
problem += 50*x1 + 10*x2 + 50*x3 +30*x4 + 40*x5 + 10*x6 + 10*x7 + 60*x8 + 80*x9 == 40
# Checking the Problem Definition
print(problem)
# Execution
result = problem.solve()
# Checking the Results
print("X1:" ,pulp.value(x1))
print("X2:" ,pulp.value(x2))
print("X3:" ,pulp.value(x3))
print("X4:" ,pulp.value(x4))
print("X5:" ,pulp.value(x5))
print("X6:" ,pulp.value(x6))
print("X7:" ,pulp.value(x7))
print("X8:" ,pulp.value(x8))
print("X9:" ,pulp.value(x9))
print("Cost:" ,pulp.value(problem.objective))
Numerical optimizer
http://www.msi.co.jp/nuopt/
use SIMPLE to use the Numerical Optimizer solver in Python.
What is SIMPLE?
PySIMPLE is the Python interface of Numerical Optimizer.
PySIMPLE provides a connection interface to Numerical Optimizer for linear programming problems,
problems where the objective function and constraint expression are all linear.
Install
$ pip install pysimple
Supported Python versions
- Python3.6
- Python3.7
- Python3.8
Examples due to compounding problems
from pysimple import *
problem = Problem(name='配合問題', type=min)
# Variables
x1 = Variable(index=1, lb=0, name='mixture ratio')
x2 = Variable(index=2, lb=0, name='Mixed ratio')
x3 = Variable(index=3, lb=0, name='mixture ratio')
x4 = Variable(index=4, lb=0, name='mixture ratio')
x5 = Variable(index=5, lb=0, name='mixture ratio')
x6 = Variable(index=6, lb=0, name='mixture ratio')
x7 = Variable(index=7, lb=0, name='Mixture ratio')
x8 = Variable(index=8, lb=0, name='mixture ratio')
x9 = Variable(index=9, lb=0, name='mixture ratio')
# Objective function
problem += 7.3*x1 + 6.9*x2 + 7.3*x3 + 7.5*x4 + 7.6*x5 + 6.0*x6 + 5.8*x7 + 4.3*x8 + 4.1*x9
# Lead mixing ratio
problem += 20*x1 + 50*x2 + 30*x3 +30*x4 + 30*x5 + 60*x6 + 40*x7 + 10*x8 + 10*x9 == 30
# Zinc mixing ratio
problem += 30*x1 + 40*x2 + 20*x3 +40*x4 + 30*x5 + 30*x6 + 50*x7 + 30*x8 + 10*x9 == 30
# Tin mixing ratio
problem += 50*x1 + 10*x2 + 50*x3 +30*x4 + 40*x5 + 10*x6 + 10*x7 + 60*x8 + 80*x9 == 40
# Execution
problem.solve()
# Output
print()
print("Cost:", problem.result.optValue())
Gurobi
https://www.engineering-eye.com/GUROBI/index.html To use the Gurobi Optimizer solver in Python, use gurobipy.
Install
$ pip install gurobipy
Examples due to compounding problems
from gurobipy import *
m = Model()
X1 = m.addVar()
X2 = m.addVar()
X3 = m.addVar()
X4 = m.addVar()
X5 = m.addVar()
X6 = m.addVar()
X7 = m.addVar()
X8 = m.addVar()
X9 = m.addVar()
# Objective function (defines the cost you want to minimize)
# Setting Constraints
m.setObjective(
7.3*X1 + 6.9*X2 + 7.3*X3 + 7.5*X4 + 7.6*X5 + 6.0*X6 + 5.8*X7 + 4.3*X8 + 4.1*X9,
GRB. MINIMIZE
)
# Total 1 (100%)
m.addConstr(X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 == 1)
# Lead mixing ratio
m.addConstr(20*X1 + 50*X2 + 30*X3 +30*X4 + 30*X5 + 60*X6 + 40*X7 + 10*X8 + 10*X9 == 30)
# Zinc mixing ratio
m.addConstr(30*X1 + 40*X2 + 20*X3 +40*X4 + 30*X5 + 30*X6 + 50*X7 + 30*X8 + 10*X9 == 30)
# Tin mixing ratio
m.addConstr(50*X1 + 10*X2 + 50*X3 +30*X4 + 40*X5 + 10*X6 + 10*X7 + 60*X8 + 80*X9 == 40)
# Checking the Problem Definition
print(m)
# Execution
m.optimize()
print("X1:" ,X1.x)
print("X2:" ,X2.x)
print("X3:" ,X3.x)
print("X4:" ,X4.x)
print("X5:" ,X5.x)
print("X6:" ,X6.x)
print("X7:" ,X7.x)
print("X8:" ,X8.x)
print("X9:" ,X9.x)
print("Cost:" ,m.objVal)
Click here for DeepTech's magazine "DeepMagazine"