Pro Yearly is on sale from $80 to $50! »

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

8fa31051503b09846584c49cd53d2f80?s=47 Asei Sugiyama
October 15, 2019

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

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

8fa31051503b09846584c49cd53d2f80?s=128

Asei Sugiyama

October 15, 2019
Tweet

Transcript

  1. Google OR Tools Ͱ ਺ཧ࠷దԽʹೖ໳͢Δ ਺ཧ࠷దԽ: Optimization Night #1

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

    • TensorFlow Docs ຋༁ & ϨϏϡʔ • ػցֶशਤؑ ڞஶ
  3. Motivation • "ػցֶशͰίϯςϯπΛݟͨͱ͖ͷϢʔβʔͷߦಈΛ༧ଌ͠· ͢ɺͦͷ݁Ռ͔Β࠷దͳίϯςϯπΛಧ͚·͢" • ҟͳΔ 2 ͭͷλεΫ 1.ίϯςϯπΛݟͨͱ͖ͷϢʔβʔͷߦಈ༧ଌ 2.༧ଌ݁Ռ͔Β࠷దͳίϯςϯπͷ୳ࡧ

    • ࠷దͱ͸Կ͔ɺͦͷṖΛ͖͋Β͔ʹ͢΂͘զʑ͸ΞϚκϯԞ஍ ΁޲͔ͬͨ
  4. Goal • ʮͱΓ͋͑ͣಈ͔͢ʯΞϓϩʔνͰ਺ཧ࠷దԽʹ͍ͭͯ෮श ૝ఆର৅ऀ • ͔ͭͯͦ͏͍͑͹ͦΜͳ͜ͱΛֶੜͷࠒʹฉ͍ͨ͜ͱ͕͋ΔΑ ͏ͳؾ͕͢Δਓ (not Ψν੎)

  5. Agenda 1.Google OR Tools <- 2.Hello, world 3.ઢܗܭը໰୊ 4.ࠞ߹੔਺ܭը໰୊ 5.Recap

  6. ͜ͷ෼໺͸ૉਓͳͷͰ͕͢ • ιϧόʔͳΔ΋ͷ͕ඞཁͦ͏ͳ͜ͱ͸஌͍ͬͯΔ • ΞϧΰϦζϜΛθϩ͔Β࣮૷͢Δͷ͸ਏ͍ • ਺ཧ࠷దԽʹ༻͍ΒΕΔΞϧΰϦζϜͷ࣮૷ํ๏Λ஌Γ͍ͨ ͷͰ͸ͳ͍ • Ͱ͖Ε͹ָ͍ͨ͠

    • pip install [optimization-tool] ͘Β͍Ͱ࢖͍͍ͨ
  7. Google OR Tools • Google ͕ OSS ͰϦϦʔε͍ͯ͠Δ πʔϧ •

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

    • ໨తؔ਺΋੍໿৚݅΋ 1 ࣍ࣜͰ͋Δ࠷దԽ໰୊ • άϥϑΞϧΰϦζϜ΍φοϓαοΫ໰୊΋ѻ͑Δ
  9. ͭΒΈ

  10. Agenda 1.Google OR Tools 2.Hello, world <- 3.ઢܗܭը໰୊ 4.ࠞ߹੔਺ܭը໰୊ 5.Recap

  11. Hello, world ࣍ͷؔ਺ʹ͍ͭͯɺ༩͑ΒΕͨ৚݅ͷ΋ͱͰ࠷େԽ͍ͨ͠ def objective(x, y, z): return x +

    y + z ͨͩ͠ɺ͢΂ͯͷҾ਺͸0·ͨ͸1ͷ஋ΛऔΔ΋ͷͱ͢Δ
  12. Ͳ͕͜೉͍͠ͷʁ • ࠷େͰ͋Δ͜ͱͷอূ͕೉͍͠ • 3 Ҿ਺Ͱ͋Ε͹͢΂ͯͷ૊Έ߹Θͤ(8 ௨Γ)Λࢼ͢͜ͱ͸Մೳ • Ҿ਺͕ଟ͘ͳΔͱ͢΂ͯͷ૊Έ߹ΘͤΛࢼ͢͜ͱ͸ݱ࣮తͰ͸ ͳ͍

  13. OR Tools Ͱղ͘ (1/5) solver = pywraplp.Solver('Hello, world', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) •

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

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

    y + z • ໨తؔ਺Λ༩͑Δ • ࠓճ͸࠷େԽͳͷͰ Maximize(࠷খԽͷͱ͖͸ Minimize) • ࢦఆͰ͖Δ໨తؔ਺͸ 1 ͚ͭͩ (ෳ਺ճࢦఆ͢Δͱ࠷ޙʹࢦ ఆͨ͠΋ͷ͕༗ޮʹͳΔ)
  16. OR Tools Ͱղ͘ (4/5) solver.Solve() • ༩͑ΒΕͨ৚݅ͷ΋ͱͰ໰୊Λղ͘

  17. 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()
  18. DEMO

  19. ੍໿৚݅ͷ༩͑ํ # x + 7 * y <= 17.5. solver.Add(x

    + 7 * y <= 17.5) • ౳ࣜɾෆ౳ࣜΛ༻੍͍ͯ໿৚݅Λ௥Ճ
  20. Agenda 1.Google OR Tools 2.Hello, world 3.ઢܗܭը໰୊ɾࠞ߹੔਺ܭը໰୊ <- 4.Recap

  21. ઢܗܭը໰୊ : ໰୊ • The Glop Linear Solver1 ʹै ͍ɺ࣍ͷ໰୊Λղ͘

    • ໨తؔ਺ • ੍໿৚݅ • ࣮ߦՄೳྖҬ͸ӈਤͷࡾ֯ܗͷ಺ଆ (ڥքΛؚΉ) 1 https://developers.google.com/optimization/lp/glop
  22. ઢܗܭը໰୊ : ࣮૷ # 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()
  23. DEMO

  24. ઢܗܭը໰୊ : ࣮૷ͷิ଍ infinity = solver.infinity() # Create variables x

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

    , ͸࣍Λຬͨ͢੔਺ • ࣮ߦՄೳྖҬ͸ӈਤͷ୆ܗͷ಺ଆͷ఺ 2 https://developers.google.com/optimization/mip/integer_opt
  26. ࠞ߹੔਺ܭը໰୊ : ࣮૷ # 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()
  27. DEMO

  28. ࠞ߹੔਺ܭը໰୊ : ࣮૷ͷิ଍ assert solver.VerifySolution(1e-7, True) • ιϧόʔ͕ܭࢉͨ݁͠Ռ͕ଥ౰͔Ͳ͏͔ͷݕূ • Colab্Ͱ͸ଥ౰Ͱͳ͍ͱ͖ʹ͸ϥϯλΠϜ͕མͪΔ

  29. Agenda 1.Google OR Tools 2.Hello, world 3.ઢܗܭը໰୊ɾࠞ߹੔਺ܭը໰୊ 4.Recap <-

  30. Recap • Google OR Tools Λ༻͍Δͱ࠷దԽ໰୊Λղ͚Δ • ઢܗܭը໰୊ͱࠞ߹੔਺ܭը໰୊ͷΑ͏ʹɺҟͳΔ໰୊ʹର͠ ͯ͸ҟͳΔιϧόʔΛ༻͍Δ •

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