Top Banner
Using Python to Solve Computationally Hard Problems
46

Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Jun 05, 2018

Download

Documents

vokhanh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Using Python to Solve Computationally Hard

Problems

Page 2: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Using Python to Solve Computationally Hard Problems

Rachael Madsen

Optimal Design Software LLC

– BS in Mathematics

– Software Engineer & Architect

– Python programmer

[email protected]

Page 3: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

NP-Complete Problems

Page 4: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

NP-Complete Problems

An individual solution can be checked for correctness in polynomial time,

but..

A solution cannot be derived in polynomial time

Page 5: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

NP-Complete Problems

Nodes 1 5 10 64

Polynomial (𝒏𝟐) 1 25 100 4096

Exponential (𝟐𝒏) 2 32 1024 1.8 x 10 19

Factorial (n!) 1 120 3.6 x 10 6 1.3 x 10 89

Page 6: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

NP-Complete Problems

• Sequencing tasks or objects for any optimization

• Database design & normalization

• Games

Page 7: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

The

Traveling Salesman

Problem

Page 8: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Traveling Salesman Problem

Given a list of cities and their pairwise distances, find the shortest possible route that visits each city exactly once and returns to the origin city.

Page 9: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Algorithms

Page 10: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Algorithms

Changing data may change which algorithm is best

Page 11: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Algorithms

Searching available software implementations may yield the best results

Page 12: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Considerations

Algorithms

Page 13: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Ease of Implementation

Algorithms > Considerations

Page 14: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Quality of Solution

Algorithms > Considerations

Page 15: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

You are looking for a solution, not the solution

Algorithms > Considerations > Quality of Solution

Page 16: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Algorithms > Considerations > Quality of Solution

𝜋 = 1

16𝑘 4

8𝑘 + 1 −

2

8𝑘 + 4 −

1

8𝑘 + 5 −

1

8𝑘 + 6

𝑘=0

Page 17: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Will this algorithm generate valleys?

Algorithms > Considerations > Quality of Solution

Page 18: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Complexity

Algorithms > Considerations

Page 19: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

• Simple

–50 lines of code

• Complex

–100,000+ lines of code

Algorithms > Considerations > Complexity

Page 20: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Parallelization

Algorithms > Considerations

Page 21: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Processing Power

Algorithms > Considerations

Page 22: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Starting From Scratch

Page 23: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Starting From Scratch

(it’s a bad idea)

Page 24: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Python Options

Page 25: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Python Options

Where to Look

Page 26: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Python Options > Where to Look

• Google code

• Git

• Forums

• Technical books

Page 27: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Python Options

Licenses

Page 28: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Python Options

Evaluation

Page 29: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Python Options > Evaluation

Operations Research Tools Developed at Google

http://code.google.com/p/or-tools/source/ browse/trunk/python/tsp.py?r=303

Page 30: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Runtime

• Lines of code: 132

• Hardware availability

Python Options > Evaluation > or-tools

Page 31: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Python Options > Evaluation > or-tools

Quality of Solution

Page 32: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Python Options > Evaluation

Github user trevlovett Python Ant Colony TSP Solver

https://github.com/trevlovett/ Python-Ant-Colony-TSP-Solver

Page 33: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Runtime

• Lines of code: 132

• Hardware availability

Python Options > Evaluation > Ant Colony TSP Solver

Page 34: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Python Options > Evaluation > Ant Colony TSP Solver

Quality of Solution

Page 35: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Optimization

Page 36: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

• Confirm implementation of algorithm as written

• Check for most efficient coding practices

Optimization

Page 37: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

• Use scientific package such as SciPy to rewrite code for speed

• Break out select code into C routine

Optimization

Page 38: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

• Parallelize

–GPU

–CPU

Optimization

Page 39: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Beware of fencepost errors and differences in hardware

Optimization

Page 40: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Web Resources

Page 41: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

• The Stony Brook Code Repository, Concorde TSP Solver http://www.cs.sunysb.edu/~algorith/implement/concorde/implement.shtml

• TSP, github user denlunev, https://github.com/denlunev/TSP

• Operations Research Tools, Google, http://code.google.com/p/or-tools/source/browse/trunk/python/tsp.py?r=303

• Ant Colony TSP Solver, github user trevlovett https://github.com/trevlovett/Python-Ant-Colony-TSP-Solver

Web Resources

Page 42: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Bibliography

Page 43: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

• Introduction to Graph Theory, 2nd Edition, Douglas Brent West, Prentice Hall, 2000

• The Traveling Salesman Problem: A Computational Study, Applegate, Bixby, Chvátal & Cook, Princeton U. Press, 2007

• The Traveling Salesman Problem: A Guided Tour of Combinatorial Optimization, Lawler, Lenstra, Kan & Shmoys, Wiley, 1985

• The Traveling Salesman Problem and its Variations, Gutin & Punnen, Springer, 2002

Bibliography

Page 44: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

• Combinatorial Optimization: Theory and Algorithms, 2nd Edition, Korte & Vygen, Springer, 2002

• Combinatorial Optimization, Cook, Cunningham, Pulleyblank & Schrijver, Wiley-Interscience, 1997

• Local Search in Combinatorial Optimization, Aarts & Lenstra, Wiley, 1997

• Combinatorial Algorithms: Generation, Enumeration, and Search, Kreher & Stinson,CRC Press, 1998

Bibliography

Page 45: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

• Counting: The Art of Enumerative Combinatorics, Martin, Springer, 2001

• Counting and Configurations, Herman, Kucera & Simsa, Springer, 2003

• Abstract Algebra, 3rd Edition, Dummit & Foote, Wiley, 2003

Bibliography

Page 46: Using Python to Solve Computationally Hard Problemsassets.en.oreilly.com/1/event/80/Using Python to Solve... · Using Python to Solve Computationally Hard Problems. Using Python to

Thank You!