統合型最適化Pythonフレームワーク flopt をオープンソースで開発中です. floptは
を目標に作成しています.
PuLP
PythonにはCOIN-ORが開発しているPuLPという線形形計画(LP)モデラーかつ LPソルバーのoverwraperがありますが, それの非線形やブラックボックス関数対応版と思ってください. GitHub - coin-or/pulp: A python Linear Programming API PyPI, Read and Docs
モデリングとしての特徴で,
- 最適化変数を, そのままPython変数として定義, 使用できる.
- ユーザーが目的関数, 制約式をPythonの変数の四則演算により定義できる
- ユーザーが好きなタイミングで, 目的関数, 制約を入力できる
点があります. 最適化問題の標準形に変換する必要なく, 問題をモデリングできます.
#!/usr/bin/env python # @(#) $Jeannot: test1.py,v 1.11 2005/01/06 21:22:39 js Exp $ # Import PuLP modeler functions from pulp import * # A new LP problem prob = LpProblem("test1", LpMinimize) # Variables # 0 <= x <= 4 x = LpVariable("x", 0, 4) # -1 <= y <= 1 y = LpVariable("y", -1, 1) # 0 <= z z = LpVariable("z", 0) # Use None for +/- Infinity, i.e. z <= 0 -> LpVariable("z", None, 0) # Objective prob += x + 4*y + 9*z, "obj" # (the name at the end is facultative) # Constraints prob += x+y <= 5, "c1" prob += x+z >= 10, "c2" prob += -y+z == 7, "c3" # (the names at the end are facultative) # Write the problem as an LP file prob.writeLP("test1.lp") # Solve the problem using the default solver prob.solve() # Print the status of the solved LP print("Status:", LpStatus[prob.status]) # Print the value of the variables at the optimum for v in prob.variables(): print(v.name, "=", v.varValue) # Print the value of the objective print("objective=", value(prob.objective))
上記は公式実装例
prob に +=
で目的関数と制約を入力しているのが印象的ですね.
flopt
floptはこのPuLPインターフェイスを踏襲して作成しています.
from flopt import Variable, Problem, Solver # Variables a = Variable('a', lowBound=0, upBound=1, cat='Integer') b = Variable('b', lowBound=1, upBound=2, cat='Continuous') c = Variable('c', lowBound=1, upBound=3, cat='Continuous') # Problem prob = Problem() prob += 2*(3*a+b)*c**2+3 # set the objective function # Solver solver = Solver(algo='RandomSearch') # select the heuristic algorithm solver.setParams(n_trial=1000) # setting of the hyper parameters prob.solve(solver, msg=True) # run solver to solve the problem # display the result, incumbent solution print('obj value', prob.getObjectiveValue()) print('a', a.value()) print('b', b.value()) print('c', c.value())
上記はfloptでの実装例. PuLPライクな実装が可能で, さらにブラックボックス関数を目的関数や制約として使用できる仕組みもあります.
数回に渡って, 簡単に実装方法などをまとめます.