Google OR

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による実行例

参考