Apr 4
backtracking
First, finish our discussion of backtrack search
with the N-queens problem.
- queens.py : some utility routines for this problem
- eight_queens_backtrack.py : full matrix, marking squares as we progress
- queens_backtrack.py : simpler, using Skienna's template as in last class
The backtrack method is a brute-force (look at everything)
incremental search. It works great ... as long as
the search space isn't too big.
To work with any search problem,
your first task is to find a way to represent
the problem (typically as a list or matrix of numbers),
and to write methods that manipulate that representation.
Some typical tasks include
- turn it into a string (so you can see it and debug things)
- incremental improvement (backtrack) methods :
- create an empty starting situation
- make a move
- undo a move
- find the legal moves
pruning
Discuss his sudoku example, and how/why he
gets better results in backtrack by choosing
to look next at the square with the fewest options.
What does a representation of this in python look like?
Aside:
heuristics
Very often the problem you're trying to solve is
too big for brute-force, though, for example trying
to solve N-queens for (say) a 30 x 30 board.
(30! = 265252859812191058636308480000000)
So then what?
- In many cases you can find a "heuristic" (i.e. guess) that tells you how far any representation is from a good one.
- And sometimes you don't even need the completely correct answer, but just one that's good enough.
The general idea is to try to "look around" the search space and
head in the right direction rather than just looking at everything.
There are a slew of variations on this theme. We'll just mention a couple now.
1. Random search.
- This isn't always as stupid as it sounds. (Maybe there are many good answers.) Pick a configuration at random, and see if it's close enough. Repeat.
2. local gradient search
- Use the heuristic to see if any of the "nearby" configurations are closer to the goal. Then move in that direction.
3. Simulated annealing
- Similar, but sometimes move away, not towards, the goal. This can avoid getting stuck in local minima (if you're lucky). In class I'll talk about what is often meant by "sometimes." There's a notion of "temperature" that's similar to what physicists use that can be useful: you start at a "hot" temperature, with a high probability of moving away from the goal, and gradually cool things down so that you're always going toward the goal. Skienna likes this one.
4. genetic algorithm
- Sounds sexy, but difficult to get to work in practice. The essential idea is to find a way to combine two configurations, hoping to get a better one.
more N-queens
In class: try to adapt the first few to the N-queens problem so that you see exactly what this looks like in practice.
For example: the heuristic can be the number of queens that can attack each other. (We want that to be 0, but if there are 30 queens on the board, having only 2 that can attack each other seems closer to a solution than if 28 can attack each other). Moving to a "nearby" position might mean moving one queen to a new column.
homework
Discuss the assignment, particularly what the
representations, heuristic, and "neighbor"
can mean for the traveling salesman problem.
next week
Coming next: game trees, min/max search, and alpha/beta pruning.