# Simulated Annealing Project

### Search Using Simulated Annealing and Gradient Descent

This assignment was first created and used for two semesters at the University of Southern California in the Artificial Intelligence Course CSCI561 and CSCI460. Some changes have been made to make this a self-guided project. The original homework can be found cool-ai.com

#### Problem 1 - Preparation

A major video game manufacture has hired you to figure out how to minimize the amount of sleep their programmers get by adjusting different items in the workspace. For instance, you could:

1. Turn the amount of caffeine added to their coffee up or down.
2. Adjust the lights to be brighter or dimmer.
3. Adjust the average pay up or down.
4. Make their seats harder or softer.
5. Turn up or down the volume on the corporate Muzak.
6. Reduce or increase trace amounts of Xylene in the air.

You can tell how much sleep each programmer is getting based on feedback from a microchip each one has been implanted with in their head. You then have a digital readout with the average amount of sleep for all the programmers.

#### Problems 1-4

1. Thinking about methods we have studied so far, what would you do to find the optimal adjustments for the workplace items in order to minimize sleep? Describe how you would implement such a method in this situation.
2. The video game manufacture asks you if you can guarantee that you will find the absolutely optimal workspace settings such that programmers get the least amount of sleep possible. What do you tell them?
3. You notice that one of your former TA’s from CS 561 now works at the video game manufacturer and you want to be nice to him. Could you use the same procedure to maximize the amount of sleep each programmer gets?
4. It turns out one of the officers at the software manufacturer is a real big fan of Charles Darwin. He asks you to figure out a way to do the sleep minimization using a genetic algorithm. How might you change the experiment to use a genetic algorithm? You may assume there are enough programmers and resources at your disposal to carry out such an optimization.
5. Allowing you to assume this last question is worth no points, is the plight of each programmer more Orwellian or Kafkaesque?

#### Problem 2 Coding and Experiments

You are a zookeeper in the reptile house. One of your rare South Pacific Tufted Wizzo lizards (Tufticus wizzocus) has just had several babies. Your job is to find a place to put each baby lizard in a nursery. However, there is a catch, the baby lizards have very long tongues. A baby lizard can shoot out its tongue and eat any other baby lizard before you have time to save it. As such, you want to make sure that no baby lizard can eat another baby lizard in the nursery (burp).

For each baby lizard, you can place them in one spot on a grid. From there, they can shoot out their tongue up, down, left, right and diagonally as well. Their tongues are very long and can reach to the edge of the nursery from any location. Figure 1 shows in what ways a baby lizard can eat another.

 Figure 1 (A) the baby lizard can attack any other lizard in a red square. Thus it can be seen that a baby lizard can eat another lizard to its top, bottom, left right or diagonal. (B) In this setup both lizards can eat each other.

In addition to baby lizards, your nursery has some trees planted in it. Your lizards cannot shoot their tongues through the trees nor can you move a lizard into the same place as a tree. As such, a tree will block any lizard from eating another lizard if it is in the path. Additionally, the tree will block you from moving the lizard to that location. Figure 2 shows some different valid arrangements of lizards

 Figure 2 Both nurseries have valid arrangements of baby lizards such that they cannot eat one another. (A) with no trees, no lizard is in a position to eat another lizard. (B) Two trees are introduced such that the lizard in the last column cannot eat the lizard in the second or fourth column.

You will write a program in c++ that will take in an input file that has an arrangement of lizards and trees and will output a new arrangement of lizards (no you can’t move the trees) such that no baby lizard can eat another one. You will be required to create a program in GNU C++ (g++) that finds the solution. For this project we will use simulated annealing. Additionally, you should be able to run several different test files and give some analysis of your running.

• For this using c++ is of course optional. This can easily be done in Java or Matlab.

Input: Your input will start with a single number, n, followed by the n x n nursery. It will have a 0 where there is nothing, a 1 where there is a lizard and a 2 where there is a tree. You will output a new file exactly like the first (except for one extra line, see below), but with the correct lizard positions. So for instance, an input file arranged like figure 2b would look like:

```8
0       0       0       0       0       1       0       0
1       0       0       0       0       0       0       0
0       0       0       0       1       0       0       0
0       1       0       0       2       0       0       1
0       0       0       0       0       0       0       0
0       0       1       0       0       2       0       0
0       0       0       0       0       0       1       0
0       0       0       1       0       0       0       0
```

The name of your input file will be specified as the first command line argument in your program.

Output: At command line an output filename will be specified as the second argument in your program. The output is the last state of your program. If you found a solution, the output file should contain that solution. Else if you did not find a solution, output the last state. You will specify that you found a solution by placing a 1 at the last line of your output. If you found no solution, place a 0. Thus, if your output looks like figure 2a, your output file will look like this:

```8
0       0       0       0       0       1       0       0
1       0       0       0       0       0       0       0
0       0       0       0       1       0       0       0
0       1       0       0       0       0       0       0
0       0       0       0       0       0       0       1
0       0       1       0       0       0       0       0
0       0       0       0       0       0       1       0
0       0       0       1       0       0       0       0
1
```

(note that the output example shown here does not correspond to the above input example; the two are not related, since, again, you cannot move (or remove) any tree that may be present).

#### Experiments

Your program will be run on several different input examples and each time should output the solution if one exists. Additionally, keep track of the amount of time your program takes to run, in number of steps. So, you will need to keep a counter that increments by one every time you move one of your baby lizards. Additionally, you will need to be able to change the temperature schedule to find the one that works best. Thus, by keeping track of the number of steps it takes, you can determine which method worked the best.

#### Note on time restrictions

If your program takes longer than 30 seconds on any of the example problems you're probably not doing this correctly. In reality, this should run quite quickly on a modern computer.

#### Questions

1. Why is simulated annealing a good algorithm for this problem? (think about complexity and other considerations)
2. What would be reasonable temperature decrease schedules to use for this problem? To answer this question you will need to do some search on the web to find which schedules other people have typically been using for various problems solved using simulated annealing. Tell us about at least three different possible schedules. Include equations and function graphs for each schedule as well as the URL from which you found it.
3. Experiment with the different temperature schedules you found and conduct a comparative analysis, running your algorithm on at least 25 different problems of various sizes each time (that is, create inputs for 25 problems of your choice; then run your program on that same set but each time using a different temperature decrease schedule. It is okay if your program has to be recompiled to select which schedule to use. When you send us your code, just have the best schedule as your default. Motivate your answer with a table showing some measure of performance on your 25 test problems (for example, steps taken) for your different schedules. Like in (2), you should test at least 3 different schedules. Include a conclusion on which schedule seems superior with supporting statistics and illustrations.
4. In addition to using simulated annealing, could you also have used genetic algorithms to solve this problem? If so, how would you have done it (just explain, you do not have to code), if not, then why?

#### Project Material

Simulated Annealing material.rar - Rar can be opened with WinRAR