Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Google OR Tools で数理最適化に入門する

Google OR Tools で数理最適化に入門する

数理最適化: Optimization Night #1 https://connpass.com/event/148735/ の発表資料です。
デモで用いた Jupyter Notebook は https://gist.github.com/AseiSugiyama/1fa1e4a2c3e90e1393f4ff398c473143

Asei Sugiyama

October 15, 2019
Tweet

More Decks by Asei Sugiyama

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • ਿࢁ Ѩ੟ • Software Engineer @Repro • ػցֶशͱ͔౷ܭͱ͔։ൃͱ͔

    • TensorFlow Docs ຋༁ & ϨϏϡʔ • ػցֶशਤؑ ڞஶ
  2. Google OR Tools • Google ͕ OSS ͰϦϦʔε͍ͯ͠Δ πʔϧ •

    Python Ͱ࢖͑Δ • pip install ortools
  3. ѻ͑Δ໰୊ • ੍໿ϓϩάϥϛϯά • N-queen, Job Scheduling ͳͲ • ઢܗܭը໰୊

    • ໨తؔ਺΋੍໿৚݅΋ 1 ࣍ࣜͰ͋Δ࠷దԽ໰୊ • άϥϑΞϧΰϦζϜ΍φοϓαοΫ໰୊΋ѻ͑Δ
  4. OR Tools Ͱղ͘ (1/5) solver = pywraplp.Solver('Hello, world', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) •

    ղ͖͍ͨ໰୊ʹదͨ͠ιϧόʔΛࢦఆ͢Δ • ࠓճ͸੔਺Λର৅ʹ͢ΔͷͰ CBC_MIXED_INTEGER_PROGRAMMING Λ༩͑Δ (࿈ଓ஋ͷͱ͖͸ྫ͑͹ CLP_LINEAR_PROGRAMMING)
  5. OR Tools Ͱղ͘ (2/5) x = solver.IntVar(0, 1, 'x') y

    = solver.IntVar(0, 1, 'y') z = solver.IntVar(0, 1, 'z') • ม਺Λ஍Ҭ෇͖Ͱએݴ͢Δ • ࠓճ͸੔਺ͳͷͰIntVar (࿈ଓ஋ͷͱ͖͸ NumVar) • ΑΓෳࡶͳ੍໿৚݅ͷࢦఆͷ࢓ํ͸ޙड़
  6. OR Tools Ͱղ͘ (3/5) solver.Maximize(objective(x, y, z)) # x +

    y + z • ໨తؔ਺Λ༩͑Δ • ࠓճ͸࠷େԽͳͷͰ Maximize(࠷খԽͷͱ͖͸ Minimize) • ࢦఆͰ͖Δ໨తؔ਺͸ 1 ͚ͭͩ (ෳ਺ճࢦఆ͢Δͱ࠷ޙʹࢦ ఆͨ͠΋ͷ͕༗ޮʹͳΔ)
  7. OR Tools Ͱղ͘ (5/5) print('Objective value =', solver.Objective().Value()) # 3.0

    print('x =', x.solution_value()) # 1.0 print('y =', y.solution_value()) # 1.0 print('z =', z.solution_value()) # 1.0 • ݁ՌΛදࣔ͢Δ • ໨తؔ਺ͷ஋͸ solver.Objective().Value() • ͦͷ࣌ͷม਺ͷ஋͸ x.solution_value()
  8. ੍໿৚݅ͷ༩͑ํ # x + 7 * y <= 17.5. solver.Add(x

    + 7 * y <= 17.5) • ౳ࣜɾෆ౳ࣜΛ༻੍͍ͯ໿৚݅Λ௥Ճ
  9. ઢܗܭը໰୊ : ໰୊ • The Glop Linear Solver1 ʹै ͍ɺ࣍ͷ໰୊Λղ͘

    • ໨తؔ਺ • ੍໿৚݅ • ࣮ߦՄೳྖҬ͸ӈਤͷࡾ֯ܗͷ಺ଆ (ڥքΛؚΉ) 1 https://developers.google.com/optimization/lp/glop
  10. ઢܗܭը໰୊ : ࣮૷ # Create the linear solver with the

    GLOP backend. solver = pywraplp.Solver('Hello, world, again', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) infinity = solver.infinity() # Create variables x = solver.NumVar(-infinity, infinity, 'x') y = solver.NumVar(-infinity, infinity, 'y') # Add constrains solver.Add(x + 2 * y <= 14) solver.Add(3 * x - y >= 0) solver.Add(x - y <= 2) # Set objective function solver.Maximize(objective(x, y, z)) # Invoke the solver result = solver.Solve()
  11. ઢܗܭը໰୊ : ࣮૷ͷิ଍ infinity = solver.infinity() # Create variables x

    = solver.NumVar(-infinity, infinity, 'x') y = solver.NumVar(-infinity, infinity, 'y') • , ࣗମʹ੍໿৚͕݅ͳ͔ͬͨͨΊɺશ࣮਺͔Β୳ࡧ
  12. ࠞ߹੔਺ܭը໰୊ • Mixed-Integer Programming2 ʹ ै͍ɺ࣍ͷ໰୊Λղ͘ • ໨తؔ਺ • ੍໿৚݅:

    , ͸࣍Λຬͨ͢੔਺ • ࣮ߦՄೳྖҬ͸ӈਤͷ୆ܗͷ಺ଆͷ఺ 2 https://developers.google.com/optimization/mip/integer_opt
  13. ࠞ߹੔਺ܭը໰୊ : ࣮૷ # Create the mip solver with the

    CBC backend. solver = pywraplp.Solver('simple_mip_program', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) # Create variables x = solver.IntVar(-infinity, infinity, 'x') y = solver.IntVar(-infinity, infinity, 'y') # Add constrains solver.Add(x + 7 * y <= 17.5) solver.Add(x <= 3.5) solver.Add(x >= 0) solver.Add(y >= 0) # Set objective function solver.Maximize(x + 10 * y) # Invoke the solver result = solver.Solve()
  14. Recap • Google OR Tools Λ༻͍Δͱ࠷దԽ໰୊Λղ͚Δ • ઢܗܭը໰୊ͱࠞ߹੔਺ܭը໰୊ͷΑ͏ʹɺҟͳΔ໰୊ʹର͠ ͯ͸ҟͳΔιϧόʔΛ༻͍Δ •

    ໰୊ͷଊ͑ํʹΑͬͯ݁Ռ͕มΘΔͨΊ੍໿৚݅ͷهड़͸ॏཁ • ιϧόʔ͸ޡͬͨ౴͑Λฦ͔͢΋͠Εͳ͍ͷͰɺݕূ͕ඞཁ • զʑͷٻΊΔʮ࠷దʯ͸ΞϚκϯͷߋʹԞਂ͘ʹ͋Δ໛༷