This code is a Python implementation of three different algorithms—Steepest-Ascent...
This code is a Python implementation of three different algorithms—Steepest-Ascent Hill Climbing, Simulated Annealing, and a Genetic Algorithm—to solve the 8-Queens problem. The 8-Queens problem is a classic constraint satisfaction problem in which you attempt to place 8 queens on a standard chessboard (8x8 grid) such that no two queens threaten each other. Queens threaten each other if they are in the same row, column, or diagonal.
Here's a breakdown of the code:
1. Utility Functions
-
generate_board()
: Generates a random initial board representing a possible solution to the 8-Queens problem. Each element in the board represents a queen's row position for each column. -
compute_conflicts(board)
: Calculates the number of conflicts for a given board configuration. A conflict indicates that two queens threaten each other. -
print_board(board)
: Prints a visual representation of the chessboard, marking queens (Q
) and open spaces (.
). -
get_neighbors(board)
: Generates all possible boards (neighbors) by moving each queen to every other row in its respective column.
2. Steepest-Ascent Hill Climbing
steepest_ascent_hill_climbing(board)
:- Implements the hill climbing search algorithm.
- Starting from a given board, it evaluates all its neighbors and selects the one with the fewest conflicts.
- Continues iteratively until no neighbor is better than the current board.
- Returns either a solution (conflicts = 0) or the best-found configuration along with the number of steps.
Hill Climbing Characteristics:
- Fast but prone to getting stuck in local minima.
3. Simulated Annealing
simulated_annealing(board, max_steps=100000)
:- Implements simulated annealing, a probabilistic search algorithm that can escape local minima.
- Starts with a high "temperature" that progressively cools down.
- At each step, a random neighbor is chosen, and acceptance is determined by the difference in conflicts and the current temperature.
- If the difference is favorable (or a random chance based on the temperature allows it), the neighbor is accepted as the new state.
- Runs for a max number of steps or until a solution is found.
Simulated Annealing Characteristics:
- More likely to find a global solution compared to Hill Climbing, but it may take more time.
4. Genetic Algorithm
-
random_individual()
: Generates a random individual/board for the population, with one queen per column. -
ga_fitness(individual)
: Evaluates the fitness of an individual based on the number of non-attacking queen pairs. The maximum fitness is 28 (as there are 28 possible unique pairs of queens). -
tournament_selection(population)
: Selects the fittest individual from a subset of the population using a tournament selection strategy. -
crossover(parent1, parent2)
: Combines two parent solutions into a child solution using genetic crossover. It partially inherits rows from one parent and fills the rest from the other. -
mutate(individual)
: Randomly swaps two queen positions in an individual based on a mutation rate. -
genetic_algorithm()
:- Implements a genetic algorithm to solve the 8-Queens problem.
- Begins with a random population and iteratively improves via selection, crossover, and mutation.
- Stops when an individual with fitness 28 is found or when the maximum generation limit is reached.
Genetic Algorithm Characteristics:
- Optimized for global exploration and can often find a solution even in highly complex problem spaces.
- Slower compared to Hill Climbing or Simulated Annealing.
5. Trial Running and Evaluation
trialRunner(trials=100)
:- Runs the three algorithms (
steepest_ascent_hill_climbing
,simulated_annealing
, andgenetic_algorithm
) for a specified number of trials. - For Hill Climbing and Simulated Annealing, trials start with randomly generated boards.
- For the Genetic Algorithm, trials begin with a randomly generated population.
- Tracks and reports metrics:
- Success rate (how many trials solved the problem).
- Average number of steps/time per successful trial.
- Total runtime for all trials.
- Runs the three algorithms (
6. Program Execution
main()
:- Runs the
trialRunner()
function to evaluate the performance of all algorithms. - Finally, prints a sample solution found using the Genetic Algorithm.
- Runs the
Summary
This code evaluates and compares three search algorithms (Hill Climbing, Simulated Annealing, and Genetic Algorithm) to solve the 8-Queens problem. The algorithms utilize different strategies to find solutions and are benchmarked in terms of success rate, speed, and efficiency.