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

Try_English_LT.pdf

canonno
February 07, 2021

 Try_English_LT.pdf

canonno

February 07, 2021
Tweet

More Decks by canonno

Other Decks in Programming

Transcript

  1. If Takashi-kun runs
    from Hokkaido to Okinawa
    at 10 km an hour
    @canonno_blog
    Soichiro Kano

    View Slide

  2. I want to learn
    mathematical optimization in python
    OK, I prepare a toy problem.
    Well, well…

    View Slide

  3. If Takashi-kun runs
    from Hokkaido to Okinawa
    at 10 km an hour

    View Slide

  4. Conditions
    • Takashi-kun runs through 47 prefectural office
    locations (ex. Kobe city in Hyogo, Otsu city in Shiga)
    • Departure: Sapporo city
    • Destination: Naha city
    • He is so strong that he run only straight.
    • He is so strong that he can run through the mountain.
    • He is so strong that he can run over the sea.
    • He is so strong that he can run without a break.

    View Slide

  5. Guess which? He has to run for…
    A. 2.5 weeks
    B. 5 weeks
    C. 7.5 weeks

    View Slide

  6. My approach
    ① Get the locations of the 47 prefectural office, and
    calculate the distance from one to another.
    ② Define the objective (what we want to minimize)
    ③ Define the constraints
    ④ Solve!
    ※Useful packages:
    networkx: to build and draw networks
    pulp: to optimize and decide the best condition

    View Slide

  7. ①Get the locations
    website
    pandas.DataFrame

    View Slide

  8. ①Calculate the distances
    pandas.DataFrame
    pandas.DataFrame

    View Slide

  9. ② Define the objective value
    x2
    x1
    No.1
    No.3
    No.4
    No.2
    x3
    x4
    x5
    x6
    x1~x6: Boolean
    d1~d6: Distance

    View Slide

  10. ② Define the objective value
    d2
    d1
    No.1
    No.3
    No.4
    No.2
    d3
    d4
    d5
    d6
    x1~x6: Boolean
    d1~d6: Distance

    View Slide

  11. ② Define the objective value
    d2
    d1
    No.1
    No.3
    No.4
    No.2
    d3
    d4
    d5
    d6 x1~x6: Boolean
    d1~d6: Distance
    x1d1 + x2d2 + x3d3+ x4d4+ x5d5+ x6d6
    = Total distance

    View Slide

  12. ② Define the objective value

    View Slide

  13. ② Define the objective value

    View Slide

  14. ② Define the objective value

    View Slide

  15. ③ Define the constraints
    Departure・destination
    will have only ONE edge
    x1+x3+x4 = 1
    x2+x3+x6 = 1
    Others will have TWO edges
    x1+x2+x5 = 2
    x4+x5+x6 = 2

    View Slide

  16. ④ Solve

    View Slide

  17. ④ Solve
    4361km
    436.1 hours (if run at 10km/hour)
    about 18 days 4 hours
    about 2.5 weeks

    View Slide

  18. ④ Solve
    4361km
    436.1 hours (if run at 10km/hour)
    about 18 days 4 hours
    about 2.5 weeks

    View Slide

  19. The answer is: he has to run for…
    A. 2.5 weeks
    B. 5 weeks
    C. 7.5 weeks

    View Slide

  20. Check it out my Qiita page!
    Thank you!
    @canonno_blog

    View Slide