• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

Google OR

date: 2022-04-19 excerpt: Google ORの使い方

tag: python機械学習google or最適化


Google ORの使い方

概要

  • Googleが開発した線形最適化ツール
  • 様々な最適化を行える
  • objective(目的関数)とconstrait(制約)を記述して最適化する

GLOPによる最適化の具体例

solver = pywraplp.Solver.CreateSolver('GLOP')

# Create the variables x and y.
x = solver.NumVar(0, 1, 'x')
y = solver.NumVar(0, 2, 'y')

print('Number of variables =', solver.NumVariables()) # 2

# Create a linear constraint, 0 <= x + y <= 2.
ct = solver.Constraint(0, 2, 'ct')
ct.SetCoefficient(x, 1)
ct.SetCoefficient(y, 1)

print('Number of constraints =', solver.NumConstraints()) # 1

# Create the objective function, 3 * x + y.
objective = solver.Objective()
objective.SetCoefficient(x, 3)
objective.SetCoefficient(y, 1)
objective.SetMaximization()

solver.Solve()
print('Solution:')
print('Objective value =', objective.Value()) # 4.0
print('x =', x.solution_value()) # 1.0
print('y =', y.solution_value()) # 1.0

Google Colabによる実行例

  • google-or-example

参考

  • Google OR-Tools


python機械学習google or最適化 Share Tweet