2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pyomoで書いた最適化モデルをCPLEXで解く

Last updated at Posted at 2025-11-27

pyomoで書いた最適化モデルをCPLEXで解くことができます。

  • テスト環境
    • CPLEX 22.1.2.0
    • pyomo 62.9.5
    • python 3.12.10
  • サンプルソース

パスが通っている必要があります。Windows版のCPLEXは、デフォルトの導入ではPATHに設定しています。
image.png

サンプル問題を解いてみます。

サンプル問題
from pyomo.environ import ConcreteModel, Var, Objective, Constraint, SolverFactory, NonNegativeReals
from pyomo.opt import ProblemFormat
# モデル作成
model = ConcreteModel()

# 変数
model.x = Var(domain=NonNegativeReals)
model.y = Var(domain=NonNegativeReals)

# 目的関数: コスト最小化
model.obj = Objective(expr=2*model.x + 3*model.y)

# 制約
model.con1 = Constraint(expr=model.x + model.y >= 10)

以下のようにソルバーを指定します。
cplexを指定すると、lpファイルを出力し、Interactive Optimizerを起動して解きます。
cplex_directを指定すると、lpファイルを出力せずに解きます。IOがない分だけ速くなります。

ソルバー設定
solver = SolverFactory('cplex')

オプションは対話式のパラメーター名の空白を_で区切って指定してください。
例:mip tolerances mipgapmip_tolerances_mipgap
https://www.ibm.com/docs/ja/icos/22.1.2?topic=parameters-relative-mip-gap-tolerance
image.png

オプション設定
solver.options['mip_tolerances_mipgap'] = 0.01
実行
results = solver.solve(model, tee=True)

# 結果表示
print(f"x = {model.x():.2f}")
print(f"y = {model.y():.2f}")
print(f"Objective = {model.obj():.2f}")

SolverFactory('cplex')を指定したのでInteractive Optimizerが起動されています。C:\Users\dsuser~1\AppData\Local\Temp\tmpzzfsta90.pyomo.lpにlpファイルが出力されてこのファイルから実行されています。tmpdb_oo2mq.cplex.logtmpu3yacmmv.cplex.solも出力されます。

結果
Welcome to IBM(R) ILOG(R) CPLEX(R) Interactive Optimizer 22.1.2.0
  with Simplex, Mixed Integer & Barrier Optimizers
5725-A06 5725-A29 5724-Y48 5724-Y49 5724-Y54 5724-Y55 5655-Y21
Copyright IBM Corp. 1988, 2024.  All Rights Reserved.

Type 'help' for a list of available commands.
Type 'help' followed by a command name for more
information on commands.

CPLEX> Logfile 'cplex.log' closed.
Logfile 'C:\Users\dsuser~1\AppData\Local\Temp\tmpdb_oo2mq.cplex.log' open.
CPLEX> New value for mixed integer optimality gap tolerance: 0.01
CPLEX> Problem 'C:\Users\dsuser~1\AppData\Local\Temp\tmpzzfsta90.pyomo.lp' read.
Read time = 0.00 sec. (0.00 ticks)
CPLEX> Problem name         : C:\Users\dsuser~1\AppData\Local\Temp\tmpzzfsta90.pyomo.lp
Objective sense      : Minimize
Variables            :       2
Objective nonzeros   :       2
Linear constraints   :       1  [Greater: 1]
  Nonzeros           :       2
  RHS nonzeros       :       1

Variables            : Min LB: 0.000000         Max UB: all infinite   
Objective nonzeros   : Min   : 2.000000         Max   : 3.000000       
Linear constraints   :
  Nonzeros           : Min   : 1.000000         Max   : 1.000000       
  RHS nonzeros       : Min   : 10.00000         Max   : 10.00000       
CPLEX> Version identifier: 22.1.2.0 | 2024-11-25 | 0edbb82fd
CPXPARAM_TimeLimit                               100
CPXPARAM_MIP_Tolerances_MIPGap                   0.01
Tried aggregator 1 time.
LP Presolve eliminated 1 rows and 2 columns.
All rows and columns eliminated.
Presolve time = 0.00 sec. (0.00 ticks)

Dual simplex - Optimal:  Objective =  2.0000000000e+01
Solution time =    0.00 sec.  Iterations = 0 (0)
Deterministic time = 0.00 ticks  (0.98 ticks/sec)

CPLEX> Solution written to file 'C:\Users\dsuser~1\AppData\Local\Temp\tmpu3yacmmv.cplex.sol'.
CPLEX> x = 10.00
y = 0.00
Objective = 20.00

lpファイルは明示的に出力することもできます。

LPファイル出力
model.write('model.lp', format=ProblemFormat.cpxlp)

cplex_directでも実行をしてみます。

ソルバー設定 cplex_direct
# ソルバー設定 cplex_direct
solver = SolverFactory('cplex_direct')

# オプション設定
solver.options['mip_tolerances_mipgap'] = 0.01

# 実行
results = solver.solve(model, tee=True)

# 結果表示
print(f"x = {model.x():.2f}")
print(f"y = {model.y():.2f}")
print(f"Objective = {model.obj():.2f}")

cplex_directcplexと異なり、Interactive Optimizerが起動せずにシンプルな出力になります。

結果
Version identifier: 22.1.2.0 | 2024-12-09 | 8bd2200c8
CPXPARAM_Read_DataCheck                          1
CPXPARAM_MIP_Tolerances_MIPGap                   0.01
Tried aggregator 1 time.
LP Presolve eliminated 1 rows and 2 columns.
All rows and columns eliminated.
Presolve time = 0.00 sec. (0.00 ticks)
x = 10.00
y = 0.00
Objective = 20.00

参考

Interactive Optimizer のオプション

python - Difference between cplex_direct and cplex in Pyomo SolverFactory - Operations Research Stack Exchange

2
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?