I want to use CPLEX on Python, but there is only an English manual, and it is troublesome to check each one, so I have to write a Japanese manual myself. Please note that it is not exhaustive because it is for my own use. Also, it is assumed that the program will be written in jupyter notebook.
It is based on the following site. [Official CPLEX Python API Reference Manual] https://www.ibm.com/support/knowledgecenter/en/SSSA5P_12.8.0/ilog.odms.cplex.help/refpythoncplex/html/cplex-module.html
Reference article [Using IBM's optimized solver CPLEX in Python] https://qiita.com/leopardus/items/93cac0f97cb22151983a
This time, I will mainly describe what is necessary when solving LP (jupyter notebook format).
Let's program using the following problem as an example.
import cplex
First, create an instance of the optimization problem.
lp = cplex.Cplex()
Now you have an empty problem with no data. Then set this problem to be an LP (Linear Programming Problem).
lp.set_problem_type(lp.problem_type.LP)
If you want to make an optimization problem other than LP, change the argument of the above function to lp.problem_type.MILP
lp.problem_type.It can be supported by using QP etc. This time it's LP
#### **`lp.problem_type.I am substituting LP.`**
Then specify whether to maximize or minimize the objective function. By default, it is "minimize", that is, it is a minimization problem, but this time it is set to "maximize" maximization.
lp.objective.set_sense(lp.objective.sense.maximize)
You don't have to, but give the problem a name.
lp.set_problem_name("test_lp")
Up to this point, the general settings for LP have been completed.
Next, set the variables.
lp.variables.add(names=["x1","x2"],lb=[0,0])
To add a new variable, use the above function called add, but the argument of add is obj
lb
ub
types
` There are 6 types: `` names
columns```. The explanation of each is as follows.
obj
lb
ub
types
`lp.variables.type.integer``` or
'I'``` --Binary variables: ``
lp.variables.type.binary or `` `'B'
--Continuous variables: lp.variables.type.continuous
or `'C'``` --Half-integer: ``` lp.variables.type.semi_integer``` or
'N'` --Semi-continuity: ``` lp.variables.type.semi_continuous``` or
'S'`` `names
columns
Set the objective function.
lp.objective.set_linear("x1",2)
lp.objective.set_linear("x2",3)
Or
lp.objective.set_linear([(0,2),(1,3)])
As mentioned above, the arguments can be given as ``` (variable name, coefficient)`` or ``
(variable number, coefficient)
, and can be set individually or collectively for each variable. is.
Next, we will describe the constraint expression.
lp.linear_constraints.add(names=["C1","C2","C3"],
lin_expr=[cplex.SparsePair(ind=["x1","x2"],val=[3,2]),
cplex.SparsePair(ind=["x1","x2"],val=[1,2]),
cplex.SparsePair(ind=["x1","x2"],val=[1,0])],
senses=["L","L","L"],
rhs=[24,16,6])
Or
lp.linear_constraints.add(names=["C1","C2","C3"],
lin_expr=[[["x1","x2"],[3,2]],
[["x1","x2"],[1,2]],
[["x1","x2"],[1,0]]],
senses=["L","L","L"],
rhs=[24,16,6])
If you want to add linear constraints, you need to use a function called linear_constraints.add, which also has multiple arguments like variables.
names
lin_expr
rhs
senses
'L'
'G'
'E'
--Range constraint: ```'R'`` `range_values
Now that we have defined the problem, we will finally solve it, but before that, let's output the problem that we have defined so far.
lp.write("test.lp")
Looking at the output file, the problem is correctly defined. Find the solution when you can define it correctly.
lp.solve()
Shows whether the optimum solution was obtained as a result of optimization.
print(lp.solution.get_status_string())
Output: `ʻoptimal`` And the optimum solution is obtained. The optimum solution and the optimum value are displayed.
print(lp.solution.get_values())
print(lp.solution.get_objective_value())
output:
[4.0, 6.0] 26.0
The correct solution was obtained.
I don't know if there will be next time, but I would like to handle QP.
import cplex
lp = cplex.Cplex()
lp.set_problem_type(lp.problem_type.LP)
lp.objective.set_sense(lp.objective.sense.maximize)
lp.set_problem_name("test_lp")
lp.variables.add(names=["x1","x2"],lb=[0.0,0.0])
lp.objective.set_linear([("x1",2),("x2",3)])
lp.linear_constraints.add(names=["C1","C2","C3"],
lin_expr=[[["x1","x2"],[3,2]],
[["x1","x2"],[1,2]],
[["x1","x2"],[1,0]]],
senses=["L","L","L"],
rhs=[24,16,6])
lp.write("test.lp")
lp.solve()
print(lp.solution.get_status_string())
print(lp.solution.get_values())
print(lp.solution.get_objective_value())
Recommended Posts