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

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

Asei Sugiyama
October 15, 2019

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. Google OR Tools Ͱ
    ਺ཧ࠷దԽʹೖ໳͢Δ
    ਺ཧ࠷దԽ: Optimization Night #1

    View Slide

  2. ࣗݾ঺հ
    • ਿࢁ Ѩ੟
    • Software Engineer @Repro
    • ػցֶशͱ͔౷ܭͱ͔։ൃͱ͔
    • TensorFlow Docs ຋༁ & ϨϏϡʔ
    • ػցֶशਤؑ ڞஶ

    View Slide

  3. Motivation
    • "ػցֶशͰίϯςϯπΛݟͨͱ͖ͷϢʔβʔͷߦಈΛ༧ଌ͠·
    ͢ɺͦͷ݁Ռ͔Β࠷దͳίϯςϯπΛಧ͚·͢"
    • ҟͳΔ 2 ͭͷλεΫ
    1.ίϯςϯπΛݟͨͱ͖ͷϢʔβʔͷߦಈ༧ଌ
    2.༧ଌ݁Ռ͔Β࠷దͳίϯςϯπͷ୳ࡧ
    • ࠷దͱ͸Կ͔ɺͦͷṖΛ͖͋Β͔ʹ͢΂͘զʑ͸ΞϚκϯԞ஍
    ΁޲͔ͬͨ

    View Slide

  4. Goal
    • ʮͱΓ͋͑ͣಈ͔͢ʯΞϓϩʔνͰ਺ཧ࠷దԽʹ͍ͭͯ෮श
    ૝ఆର৅ऀ
    • ͔ͭͯͦ͏͍͑͹ͦΜͳ͜ͱΛֶੜͷࠒʹฉ͍ͨ͜ͱ͕͋ΔΑ
    ͏ͳؾ͕͢Δਓ (not Ψν੎)

    View Slide

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

    View Slide

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

    View Slide

  7. Google OR Tools
    • Google ͕ OSS ͰϦϦʔε͍ͯ͠Δ
    πʔϧ
    • Python Ͱ࢖͑Δ
    • pip install ortools

    View Slide

  8. ѻ͑Δ໰୊
    • ੍໿ϓϩάϥϛϯά
    • N-queen, Job Scheduling ͳͲ
    • ઢܗܭը໰୊
    • ໨తؔ਺΋੍໿৚݅΋ 1 ࣍ࣜͰ͋Δ࠷దԽ໰୊
    • άϥϑΞϧΰϦζϜ΍φοϓαοΫ໰୊΋ѻ͑Δ

    View Slide

  9. ͭΒΈ

    View Slide

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

    View Slide

  11. Hello, world
    ࣍ͷؔ਺ʹ͍ͭͯɺ༩͑ΒΕͨ৚݅ͷ΋ͱͰ࠷େԽ͍ͨ͠
    def objective(x, y, z):
    return x + y + z
    ͨͩ͠ɺ͢΂ͯͷҾ਺͸0·ͨ͸1ͷ஋ΛऔΔ΋ͷͱ͢Δ

    View Slide

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

    View Slide

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

    View Slide

  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)
    • ΑΓෳࡶͳ੍໿৚݅ͷࢦఆͷ࢓ํ͸ޙड़

    View Slide

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

    View Slide

  16. OR Tools Ͱղ͘ (4/5)
    solver.Solve()
    • ༩͑ΒΕͨ৚݅ͷ΋ͱͰ໰୊Λղ͘

    View Slide

  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()

    View Slide

  18. DEMO

    View Slide

  19. ੍໿৚݅ͷ༩͑ํ
    # x + 7 * y <= 17.5.
    solver.Add(x + 7 * y <= 17.5)
    • ౳ࣜɾෆ౳ࣜΛ༻੍͍ͯ໿৚݅Λ௥Ճ

    View Slide

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

    View Slide

  21. ઢܗܭը໰୊ : ໰୊
    • The Glop Linear Solver1 ʹै
    ͍ɺ࣍ͷ໰୊Λղ͘
    • ໨తؔ਺
    • ੍໿৚݅
    • ࣮ߦՄೳྖҬ͸ӈਤͷࡾ֯ܗͷ಺ଆ
    (ڥքΛؚΉ)
    1 https://developers.google.com/optimization/lp/glop

    View Slide

  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()

    View Slide

  23. DEMO

    View Slide

  24. ઢܗܭը໰୊ : ࣮૷ͷิ଍
    infinity = solver.infinity()
    # Create variables
    x = solver.NumVar(-infinity, infinity, 'x')
    y = solver.NumVar(-infinity, infinity, 'y')
    • , ࣗମʹ੍໿৚͕݅ͳ͔ͬͨͨΊɺશ࣮਺͔Β୳ࡧ

    View Slide

  25. ࠞ߹੔਺ܭը໰୊
    • Mixed-Integer Programming2 ʹ
    ै͍ɺ࣍ͷ໰୊Λղ͘
    • ໨తؔ਺
    • ੍໿৚݅: , ͸࣍Λຬͨ͢੔਺
    • ࣮ߦՄೳྖҬ͸ӈਤͷ୆ܗͷ಺ଆͷ఺
    2 https://developers.google.com/optimization/mip/integer_opt

    View Slide

  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()

    View Slide

  27. DEMO

    View Slide

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

    View Slide

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

    View Slide

  30. Recap
    • Google OR Tools Λ༻͍Δͱ࠷దԽ໰୊Λղ͚Δ
    • ઢܗܭը໰୊ͱࠞ߹੔਺ܭը໰୊ͷΑ͏ʹɺҟͳΔ໰୊ʹର͠
    ͯ͸ҟͳΔιϧόʔΛ༻͍Δ
    • ໰୊ͷଊ͑ํʹΑͬͯ݁Ռ͕มΘΔͨΊ੍໿৚݅ͷهड़͸ॏཁ
    • ιϧόʔ͸ޡͬͨ౴͑Λฦ͔͢΋͠Εͳ͍ͷͰɺݕূ͕ඞཁ
    • զʑͷٻΊΔʮ࠷దʯ͸ΞϚκϯͷߋʹԞਂ͘ʹ͋Δ໛༷

    View Slide