CPLEX Python API self-made manual LP edition

Motivation

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

environment

LP (Linear Programming Problem)

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. $ \begin{align} \max\ \ &2x_1+3x_2 \\\ \text{s.t.}\ \ &3x_1+2x_2 \leq 24 \\\ &x_1+2x_2 \leq 16 \\\ &x_1 \leq 6 \\\ &x_1 \geq 0,x_2 \geq 0 \end{align} $

Problem setting

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.

Variable setting

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.

Description of objective function / constraint expression

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
    You can set the name of the constraint expression.
  • lin_expr
    Specify the left side of the constraint expression. The form gives a Sparse Pair or a matrix-style list.
  • rhs
    Specify the right-hand part of the constraint expression.
  • senses
    Specify an equal sign or inequality sign that represents the relationship between the left and right sides of the constraint expression. The correspondence between symbols and characters to be specified is as follows.
  • \leq'L'
  • \geq'G'
  • ='E' --Range constraint: ```'R'`` `
  • range_values
    You can specify the value of the range constraint.

Equation

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")

スクリーンショット 2020-10-27 23.02.17.png

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.

next time

I don't know if there will be next time, but I would like to handle QP.

code

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

CPLEX Python API self-made manual LP edition
A * algorithm (Python edition)
First Python 3rd Edition
Python self-made class sort
Evernote API in Python
OpenCV3 Python API list
C API in Python 3
TensorFlow API memo (Python)
Explosive API construction-chalice edition-
Specification generation and code generation in REST API development (Python edition)