Asei Sugiyama
October 15, 2019
10k

デモで用いた Jupyter Notebook は https://gist.github.com/AseiSugiyama/1fa1e4a2c3e90e1393f4ff398c473143

October 15, 2019

## Transcript

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

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

• ࠷దͱ͸Կ͔ɺͦͷṖΛ͖͋Β͔ʹ͢΂͘զʑ͸ΞϚκϯԞ஍ ΁޲͔ͬͨ

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

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

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

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

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

y + z ͨͩ͠ɺ͢΂ͯͷҾ਺͸0·ͨ͸1ͷ஋ΛऔΔ΋ͷͱ͢Δ

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 ͚ͭͩ (ෳ਺ճࢦఆ͢Δͱ࠷ޙʹࢦ ఆͨ͠΋ͷ͕༗ޮʹͳΔ)

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

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

+ 7 * y <= 17.5) • ౳ࣜɾෆ౳ࣜΛ༻੍͍ͯ໿৚݅Λ௥Ճ

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

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

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

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