Problem solving using AI Techniques

Name / Mutlag Faisal AL-Ajme
Id/ 426102739
Name / Osama Khalaf AL-Friedy
Id /426103322
Traveling Salesman Problem
TSP
Traveling Salesman Problem

Traveling Salesman Problem (TSP) using a Genetic Algorithm (GA). In the Traveling Salesman
Problem, the goal is to find the shortest distance between N different cities. The path that the
salesman takes is called a tour.
Testing every possibility for an N city tour would be N! math additions. A 30 city tour would have to
measure the total distance of be 2.65 X 1032 different tours. Assuming a trillion additions per second,
this would take 252,333,390,232,297 years. Adding one more city would cause the time to increase by a
factor of 31. Obviously, this is an impossible solution.





A genetic algorithm can be used to find a solution is much less time. Although it might not find the
best solution, it can find a near perfect solution for a 100 city tour in less than a minute. There are a
couple of basic steps to solving the traveling salesman problem using a GA. First, create a group of
many random tours in what is called a population. This algorithm uses a greedy initial propulation
that gives preference to linking cities that are close to each other.
Second, pick 2 of the better (shorter) tours parents in the population and combine them to make 2
new child tours. Hopefully, these children tour will be better than either parent.
A small percentage of the time, the child tours are mutated. This is done to prevent all tours in the
population from looking identical.
The new child tours are inserted into the population replacing two of the longer tours. The size of the
population remains the same.
New children tours are repeatedly created until the desired goal is reached.
As the name implies, Genetic Algorithms mimic nature and evolution using the principles of
Survival of the Fittest.
Traveling Salesman Problem

The two complex issues with using a Genetic Algorithm to solve the Traveling Salesman Problem are
the encoding of the tour and the crossover algorithm that is used to combine the two parent tours to
make the child tours.
In a standard Genetic Algorithm, the encoding is a simple sequence of numbers and Crossover is
performed by picking a random point in the parent's sequences and switching every number in the
sequence after that point. In this example, the crossover point is between the 3rd and 4th item in the
list. To create the children, every item in the parent's sequence after the crossover point is swapped.
Parent1
FAB|ECGD
Parent 2
DEA|CGBF
Childe 1
FAB|CGBF
Childe 2
DEA|ECGD
Traveling Salesman Problem

The difficulty with the Traveling Salesman Problem is that every city can only be used once in a tour. If the letters in the above
example represented cities, this child tours created by this crossover operation would be invalid. Child 1 goes to city F & B twice,
and never goes to cities D or E.
The encoding cannot simply be the list of cities in the order they are traveled. Other encoding methods have been created that
solve the crossover problem. Although these methods will not create invalid tours, they do not take into account the fact that
the tour "A B C D E F G" is the same as "G F E D C B A". To solve the problem properly the crossover algorithm will have to get
much more complicated.
My solution stores the links in both directions for each tour. In the above tour example, Parent 1 would be stored as:
City
First connection
Second connection
A
F
B
B
A
E
C
E
G
D
G
F
E
B
C
F
D
A
G
C
D
Traveling Salesman Problem
 The crossover operation is more complicated than combining 2 strings. The crossover
will take every link that exists in both parents and place those links in both children.
Then, for Child 1 it alternates between taking links that appear in Parent 2 and then
Parent 1. For Child 2, it alternates between Parent 2 and Parent 1 taking a different set of
links. For either child, there is a chance that a link could create an invalid tour where
instead of a single path in the tour there are several disconnected paths. These links must
be rejected. To fill in the remaining missing links, cities are chosen at random. Since the
crossover is not completely random, this is considered a greedy crossover.
Eventually, this GA would make every solution look identical. This is not ideal. Once
every tour in the population is identical, the GA will not be able to find a better solution.
There are two ways around this. The first is to use a very large initial population so that it
takes the GA longer to make all of the solutions the same. The second method is
mutation, where some child tours are randomly altered to produce a new unique tour.
This Genetic Algorithm also uses a greedy initial population. The city links in the initial
tours are not completely random. The GA will prefer to make links between cities that are
close to each other. This is not done 100% of the time, becuase that would cause every
tour in the initial population to be very similar.
Traveling Salesman Problem








There are 6 parameters to control the operation of the Genetic Algorithm: Population Size - The population size is
the initial number of random tours that are created when the algorithm starts. A large population takes longer to find a
result. A smaller population increases the chance that every tour in the population will eventually look the same. This
increases the chance that the best solution will not be found.
Neighborhood / Group Size - Each generation, this number of tours are randomly chosen from the population. The
best 2 tours are the parents. The worst 2 tours get replaced by the children. For group size, a high number will increase
the likelyhood that the really good tours will be selected as parents, but it will also cause many tours to never be used
as parents. A large group size will cause the algorithm to run faster, but it might not find the best solution.
Mutation % - The percentage that each child after crossover will undergo mutation When a tour is mutated, one of
the cities is randomly moved from one point in the tour to another.
# Nearby Cities - As part of a greedy initial population, the GA will prefer to link cities that are close to each other to
make the initial tours. When creating the initial population this is the number of cities that are considered to be close.
Nearby City Odds % - This is the percent chance that any one link in a random tour in the initial population will
prefer to use a nearby city instead of a completely random city. If the GA chooses to use a nearby city, then there is an
equally random chance that it will be any one of the cities from the previous parameter.
Maximum Generations - How many crossovers are run before the algorithm is terminated.
The other options that can be configured are (note: these are only available in the downloadable version): Random
Seed - This is the seed for the random number generator. By having a fixed instead of a random seed, you can duplicate
previous results as long as all other parameters are the same. This is very helpful when looking for errors in the
algorithm.
City List - The downloadable version allows you to import city lists from XML files. Again, when debugging problems
it is useful to be able to run the algorithm with the same exact parameters.

The starting parameter values are:
Traveling Salesman Problem
Parameter
Initial value
Population size
10000
Group size
5
Mutation
3%
# Nearby cities
5
Nearby city odds
90 %
Traveling Salesman Problem
 Note: I originally wrote this program in 1995 in straight C. The tours in the
population were stored as an array of 32 bit int's, where each bit indicated a
connection. Ex: If tour[0] = 00000000000001000000010000000000 in
binary, then city 0 connected to city 11 and 19. That implementation was
much faster than the current C# version. The greedy part of crossover
could be performed by doing a binary AND on the two tours. While that
code was very fast, it had a lot of binary operations, was limited in the
number of cities it could support, and the code wasn't readable. Hopefully,
this new version will allow for more re-use.
 Click here to execute program .
N-Queens Problem
N-Queens Problem
“The standard 4 by 4 Queens problem asks how to
place 4 queens on an ordinary chess board so that
none of them cannot hit any other in one move.”
Our Problem
More specifically the problem we were asked to
address is an N by N Queens Problem, in which we are
to create an integer program model that will give us a
solution for any integer N value.
Movements
 A queen can move in any
direction
 Horizontal
 Vertical
 Diagonal
 It can move any amount of
space, meaning it can move
from one end of the board to
the other end
Constraints
 No queen can hit another queen when moved
 Only one queen can be located in each row
 Only one queen can be located in each column
 Only one queen can be located in each diagonal path
Integer Model
 0 = no queen
 1 = queen
X 1,1 X 1,2
i
Decision Variables
 i represents rows
 j represents columns
 xij , position on board at row i
and column j
 xij is binary
j
X 3,3
X i,j
Integer Model
Parameter
 N, dimensions of board & number of queens
Integer Model
Objective
 Minimize
 We have to satisfy the constraints
 Only solutions you get will be feasible solutions
Integer Model
Constraints
 There must be 1 (and only one) queen in row i:
, for all i = 1,…,N
Integer Model
 Constraints
 There must be 1 (and only one) queen in column j:


, for all j = 1,…,N
Integer Model
 Constraints
 There can be no more than one queen in each backward
diagonal ( \ ):
N………………………1
1……………….……N
Integer Model
 Constraints
 There can be no more than one queen in each forward diagonal
( / ):
N………………………1
1……………….……N
8-puzzle problem
What is 8-puzzle?
 The 8 puzzle is a simple game which consists of eight
sliding tiles, numbered by digits from 1 to 8, placed in
a 3x3 squared board of nine cells. One of the cells is
always empty, and any adjacent (horizontally and
vertically) tile can be moved into the empty cell. The
objective of the game is to start from an initial
configuration and end up in a configuration which the
tiles are placed in ascending number order.
8-puzzle problem
 The 8-puzzle is a square board with 9 positions, filled
by 8 numbered tiles and one gap. At any point, a tile
adjacent to the gap can be moved into the gap,
creating a new gap position. In other words the gap
can be swapped with an adjacent (horizontally and
vertically) tile. The objective in the game is to begin
with an arbitrary configuration of tiles, and move
them so as to get the numbered tiles arranged in
ascending order either running around the perimeter
of the board or ordered from left to right, with 1 in the
top left-hand position.
8-puzzle problem
 An example scenario is shown below
Goal State
Initial state
Here the goal state can be reached from the initial state at 5 simple steps given below.
Blank Up
Blank Up
Blank Left
Blank Down
Blank Right
8-puzzle Algorithm
 8 puzzle is a very interesting problem for software
developers around the world. It always has been an
important subject in articles, books and become a part
of course material in many universities. It is a well
known problem especially in the field of Artificial
Intelligence. This page is designed to tell you the very
basic understanding of the algorithm to solve the 8
puzzle problem. You may find "what the problem is"
from the 8 puzzle problem page If you still don't have
any idea about it.
8-puzzle Algorithm
 As it's mentioned in the 8 puzzle problem page, the
game has an initial state and the objective is to reach
to the goal state from the given initial state. An
example initial state and corresponding goal state is
shown below
Initial state
Goal state
8-puzzle Algorithm
 before beginning to tell how to reach from the initial state to the goal
state, we have to solve a sub problem which is "choosing the goal state
to be reached". As it's mentioned in the 8 puzzle problem page, the
game has two possible arrangements. We have to choose one of the
goal states to be reached because only one of them is reachable from
the given initial state. Sounds interesting? Lets see why the 8-puzzle
states are divided into two disjoint sets, such that no state in one set
can be transformed into a state in the other set by any number of
moves.
Goal state A
Goal state B
8-puzzle Algorithm
 First we begin with the definition of the order of
counting from the upper left corner to the lower right
corner as shown in the figure below.
Order of Counting
8-puzzle Algorithm
 This definition is given because we need to determine
the number of smaller digits which are coming after a
chosen tile. Its a little bit trick to tell with words. So
lets have an example.
Counting Example
8-puzzle Algorithm
 In this example above we see the tiles which comes
right after tile #8 and smaller than 8, in yellow. So if
we count the yellow tiles, we get 6. We will apply this
counting for every tile in the board. But first lets have
another example to make things crystal clear.
Counting Example 2
8-puzzle Algorithm
 This time we count the tiles which comes right after
tile #6 and smaller than 6, in yellow. As a result we get
2. Now we made things clear on how to count. Now we
will do this counting for every tile in the board.
Counting the Board
8-puzzle Algorithm
 In the figure below you see that counting for tile #1 is
skipped. That's because the result is always 0 (1 is the
smallest tile). Counting for each tile has been done
and then the results are summed. Finally we get 11 as
the result.
 Now I believe that most of you have the question "So,
what?". The answer is simple. As you can imagine the
result is always either even or odd. And this will be the
key to solve the problem of "choosing the goal state to
be reached". Lets call the result as N.
8-puzzle Algorithm
If N is odd we can only reach to the Goal State A as shown below.
If N is even we can only reach to the Goal State B as shown below.
Goal state A
Goal state B
8-puzzle Algorithm
 Now we have determined which goal state to reach, so
we can start a search. The search will be an
uninformed search which means searching for the goal
without knowing in which direction it is. We have 3
choices for the search algorithm in this case. These are:
 Breadth-first search algorithm
 Depth-first search algorithm
 Iterative deepening search algorithm
8-puzzle Algorithm
 The three algorithms given above differs on the choice of the search path
(node to node). Below you will find summarized descriptions
 Breadth-first search algorithm: finds the solution that is closest (in the
graph) to the start node that means it always expands the shallowest node.
 Depth-first search algorithm: starts at the root (selecting some node as the
root in the graph case) and explores as far as possible along each branch
before backtracking.
 Iterative deepening search algorithm: is a modified version of the dept first
search algorithm. It can be very hard to determine an optimum limit
(maximum depth) for depth first search algorithm. That's why we use
iteration starting from the minimum expected depth and search until the
goal state or the maximum depth limit of the iteration has been reached.