pyomoで書いた最適化モデルをCPLEXで解くことができます。
- テスト環境
- CPLEX 22.1.2.0
- pyomo 62.9.5
- python 3.12.10
- サンプルソース
パスが通っている必要があります。Windows版のCPLEXは、デフォルトの導入ではPATHに設定しています。

サンプル問題を解いてみます。
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 mipgap →mip_tolerances_mipgap
https://www.ibm.com/docs/ja/icos/22.1.2?topic=parameters-relative-mip-gap-tolerance

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.logやtmpu3yacmmv.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ファイルは明示的に出力することもできます。
model.write('model.lp', format=ProblemFormat.cpxlp)
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_directはcplexと異なり、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