introduction to computer science and programming for...

26
Reminder Artificial Intelligence Selected Search Algorithms from AI Summary Introduction to Computer Science and Programming for Astronomers Lecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12, 2020 735 Computational Astronomy

Upload: others

Post on 31-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Introduction to Computer Science andProgramming for Astronomers

Lecture 9.

István Szapudi

Institute for AstronomyUniversity of Hawaii

March 12, 2020

735 Computational Astronomy

Page 2: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Outline

1 Reminder

2 Artificial Intelligence

3 Selected Search Algorithms from AI

735 Computational Astronomy

Page 3: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Reminder

We have demonstrated with the packing problem how toobtain fast solutions with mathematical insight andtheorems.We looked at greedy algorithms.We summarized algorithmic patterns.We looked at complexity theory.Next we look at some interesting algorithms from ArtificialIntelligence (AI)

735 Computational Astronomy

Page 4: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

What is Artificial Intelligence?And how is this useful for Astronomy?

In general, AI is about (computer) systems that canthink/act “like humans”.A more humble (but still ambitious) goal is computersystems which can think/act rationally.An important aspect of AI is to perceive the world at aseries of hierarchical levels (classification of information).To what extent can we replace astronomers withcomputers?E.g. reducing large amount of data, making intelligentdecisions about observations (where, what, when, take,biases, flats, etc), finding “interesting” objects, reading andwriting papers, etc.

735 Computational Astronomy

Page 5: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Turing Test

Turing (of the Turing machine fame) have designed thistest.Can a human interrogator tell the difference between amachine and a computer?

735 Computational Astronomy

Page 6: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Can machines think?

The Turing test shifts the question: can machines behaveintelligently?Turing originated this subject in 1950.Suggested major components of AI: knowledge,reasoning, language understanding, learning.Problem: the Turing test is not reproducible, difficult toanalyse mathematically; but does express the essence ofAI.

735 Computational Astronomy

Page 7: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

AI and Computer Science

AI is considered part of computer science, however, it hasunusually strong connection with cognitive science,neuroscience, psychology, philosophy, linguisitics,logic/mathematics, etc.For the astronomer it has a lot to offer, especially in termsmachine learning, classification, perception, planning,visualization, etc.Computer science emphasizes solving specific problemsvery efficientlyAI emphasizes solving very general classes of problemsWe will get only taste by looking into some which can beconsidered variants/refinements of backtracking.

735 Computational Astronomy

Page 8: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Uninformed Searches

Reminder: in backtracking, we try all possible solutions fora problem, by expanding a “search tree”.In the algorithm we have kept an exploreList; in AI it iscalled the fringe.Depending what data structure is used for the fringe

LIFO stack→ depth first searchFIFO queue→ breadth first searchpriority queue ordered by path cost, lowest first→ uniformcost searchdepth limited search: DFS with depth limititerative deepening search (IDS): gradually increasing thedepth limit.

735 Computational Astronomy

Page 9: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Best First Search

Informed search algorithmuse a priority queue (a queue ordered by “desirability”),and always expand the most desirable element from theend of the queue.We defined desirability using what we know about theproblem.With proper choice the search will be much more efficientthen simple backtracking/uninformed search.

735 Computational Astronomy

Page 10: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Greedy SearchThe simplest variant of Best First

Let h(n) be an heuristic, an estimate of the cost to theclosest goal.Set up a priority queue as above, and use h(n) for the“desirability”.E.g. Finding the best route on a map between two cities.The cities and roads connecting them can be representedas a weighted graph. The heuristic for the greedy searchcould be the straight line distance of each city to the goal.Greedy search is not complete (can get stuck in loops),has O(bm) time and space complexity, and suboptimal. Agood heuristic gives dramatic improvement.

735 Computational Astronomy

Page 11: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

A∗SearchAn improvement over greedy search

We use a better heuristic: f (n) = g(n) + h(n) the sum ofactual cost to the node n plus the estimated cost from n tothe goal.admissible heuristic: h(n) ≤ h∗(n), where, h∗(n) is the truecost from n. We also require that h ≥ 0, thus h(G) = 0 atthe goal.For an admissible heuristic, the A∗search is optimal.h(n) = 0 is admissible (corresponds to Dijkstra’s algorithm)

735 Computational Astronomy

Page 12: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

A∗ is optimal (sketch of proof)

Suppose some suboptimal goal G2 has been generatedand is in the queue. Let n be an unexpanded node on ashortest path to an optimal goal G.

f (G2) = g(G2) since h(G2) = 0> g(G) since G2 is suboptimal

≥ f (n) since h is admissible

Since f (G2) > f (n), A∗ will never expand G2 QED.735 Computational Astronomy

Page 13: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Admissible HeuristicsE.g.: 8-puzzle

h1(n) = number of misplaced tilesh2(n) = total Manhattan distanceh1(Start) = 6h2(Start) = 4 + 0 + 3 + 3 + 1 + 0 + 2 + 1 = 14

735 Computational Astronomy

Page 14: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Typical Search Costs

IDS = 3,473,941 nodesA∗(h1) = 539 nodesA∗(h2) = 113 nodesIDS ≈ 54,000,000,000 nodesA∗(h1) = 39,135 nodesA∗(h2) = 1,641 nodesIf h2(n) ≥ h1(n) for all n (both admissible)→ h2 dominates h1 and is better for search.

735 Computational Astronomy

Page 15: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

How to Find Heuristics?Look at relaxed problems and combine

The exact cost of a relaxed version of the problem willcreate heuristic.If tiles could move anywhere/any adjacent square, wewould get h1/h2.Given any admissible heuristics ha, hb,h(n) = max(ha(n),hb(n)) is also admissible and dominatesha, hb

Thus using h will result in a more efficient search.

735 Computational Astronomy

Page 16: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Best First Searches: Summary

Heuristic functions estimate costs of shortest pathsGood heuristics can dramatically reduce search costGreedy best-first search expands lowest h – incompleteand not always optimalA∗ search expands lowest g + h

it is complete and optimalalso optimally efficient (up to tie-breaks, for forward search)

Admissible heuristics can be derived from exact solution ofrelaxed problems

735 Computational Astronomy

Page 17: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Local Search AlgorithmsIterative Improvement Algorithms

In many optimization problems, path is irrelevant;The goal state itself is the solutionThen state space = set of “complete” configurations;Find optimal configuration, e.g., Traveling SalesmanProblem or, find configuration satisfying constraints, e.g., atimetableIn such cases, can use iterative improvement algorithms;keep a single “current” state, try to improve itThey are constant space complexity

735 Computational Astronomy

Page 18: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

E.g. Traveling Sales Representative Problem

Start with any complete tourPerform pairwise exchangesThis is a famous NP-complete problem, yetVariants of this approach get within 1% of the optimal sol’nvery quickly with thousands of cities

735 Computational Astronomy

Page 19: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Hill Climbing (gradient ascent)

Simply take the highest valued neighbourRandom-restart: overcomes local maximaRandom sideways moves escape from shoulders but loopon flat maxima

735 Computational Astronomy

Page 20: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Simulated AnnealingA physically motivated improvement on hill climbing

Idea: include bad moves to escape local minima, butgradually decrease their size and frequency (=cooling)Metropolis-algorithm: accept any move, which decreasesthe energy; also accept moves which increase the energywith probability e−β∆E .If T = 1/β is decreased slow enough, it will always reachthe best state, since the probability of stepping away p → 0for small T .This is a widely used randomized algorithm for solvingotherwise difficult problems.Note that in CS often “energy maximum” is sought, theexact opposite of the physical motivation.

735 Computational Astronomy

Page 21: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Local beam search

Idea: keep k states instead of 1; choose top k of all theirsuccessorsNot the same as k searches run in parallel: Searches thatfind good states recruit other searches to join themProblem: quite often, all k states end up on same local hillImprovement: choose k successors randomly, biasedtowards good ones.Close analogy to natural selection. Next: geneticalgorithms.

735 Computational Astronomy

Page 22: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Genetic algorithmsConceptual outline

stochastic local beam searchgenerate successors from pairs of states

735 Computational Astronomy

Page 23: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Genetic algorithms: ExampleN-queens problem

GAs require states encoded as stringsCrossover helps iff substrings are meaningful componentsNB: GAs 6= evolution: real genes encode replicationmachinery.

735 Computational Astronomy

Page 24: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Note on continuous state spaces

The previous algorithms are applicable to many problemswith continous space statesTypically we discretize the configuration space, andconsider steps in each directionGradient methods compute ∇f to increase/reduce f , e.g.,by x← x + α∇f (x)Sometimes can solve for ∇f (x) = 0 exactly.The Newton–Raphson method iteratesx← x− H−1

f (x)∇f (x) where Hij = ∂2f/∂xi∂xj

735 Computational Astronomy

Page 25: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Summary

Artificial Intelligence has potential applications inastronomy.It is producing algorithms and methods which solve verygeneral problems.We looked at a series of interesting search algorithmswhich are at the boundary of AI and CS.Other possible applications include machine learning,classificationPresently hot: deep learning (python libraries, graphicscards)

735 Computational Astronomy

Page 26: Introduction to Computer Science and Programming for ...ifa.hawaii.edu/~szapudi/astro735/lecture9.pdfLecture 9. István Szapudi Institute for Astronomy University of Hawaii March 12,

ReminderArtificial Intelligence

Selected Search Algorithms from AISummary

Homework # 9

(E1) Solve the N-puzzle problem (the N × N generalizationof the 8-puzzle) using A∗search. Using (at least 3) differentheuristics, count the number of nodes searched. Theprograms should have an option to display the steps of thesearch and the steps leading to a solution.(P1) Solve the N-queens problem (place N queens on anN × N chessboard such that no two queens attack eachother), with any algorithm mentioned in class. The programshould display the solution it arrives when finished. Givenan integer N, return all distinct solutions.

735 Computational Astronomy