College

Dear beloved readers, welcome to our website! We hope your visit here brings you valuable insights and meaningful inspiration. Thank you for taking the time to stop by and explore the content we've prepared for you.
------------------------------------------------ N-Queens Problem Solver using Differential Evolution and the Backtracking Algorithm.

Requirements:
- Need project in Python language.

Answer :

The usage of the N-Queens problem solver using the Differential Evolution algorithm combined with the Backtracking algorithm in Python:

python

import random

def is_safe(board, row, col, N):

# Check the row on the left side

for i in range(col):

if board[row][i] == 1:

return False

# Check the upper diagonal on the left side

i, j = row, col

while i >= 0 and j >= 0:

if board[i][j] == 1:

return False

i -= 1

j -= 1

# Check the lower diagonal on the left side

i, j = row, col

while i < N and j >= 0:

if board[i][j] == 1:

return False

i += 1

j -= 1

return True

def print_solution(board):

N = len(board)

for i in range(N):

for j in range(N):

print(board[i][j], end=" ")

print()

def differential_evolution(N, population_size, crossover_rate, mutation_rate, max_generations):

population = [[random.randint(0, N - 1) for _ in range(N)] for _ in range(population_size)]

generation = 0

while generation < max_generations:

for i in range(population_size):

candidate = population[i]

random_indexes = random.sample(range(population_size), 3)

a, b, c = population[random_indexes[0]], population[random_indexes[1]], population[random_indexes[2]]

mutated_candidate = [a[q] + mutation_rate * (b[q] - c[q]) for q in range(N)]

crossover = random.random()

crossed_candidate = [

candidate[q] if random.random() < crossover_rate else mutated_candidate[q] for q in range(N)

]

if sum(crossed_candidate) != sum(candidate): # Ensure each candidate has N queens

continue

if is_safe([[0] * N for _ in range(N)], 0, crossed_candidate[0], N):

population[i] = crossed_candidate

generation += 1

return population

def backtracking(N, population):

for i in range(len(population)):

board = [[0] * N for _ in range(N)]

for j in range(N):

board[j][population[i][j]] = 1

if is_solution(board, N):

return board

return None

def is_solution(board, N):

for i in range(N):

if sum(board[i]) != 1:

return False

return True

def solve_n_queens(N, population_size, crossover_rate, mutation_rate, max_generations):

population = differential_evolution(N, population_size, crossover_rate, mutation_rate, max_generations)

solution = backtracking(N, population)

if solution is None:

print("No solution found.")

else:

print("Solution:")

print_solution(solution)

# Example usage

N = 8

population_size = 100

crossover_rate = 0.8

mutation_rate = 0.2

max_generations = 100

solve_n_queens(N, population_size, crossover_rate, mutation_rate, max_generations)

What is the Python code

The above code uses an algorithm called Differential Evolution to create a group of possible solutions. After that, it uses the Backtracking algorithm to examine each possible solution and determine if it meets the conditions of the N-Queens problem.

So, one can change the values of N (the size of the chessboard), population_size, crossover_rate, mutation_rate, and max_generations as per one's requirements.

Learn more about Backtracking Algorithm on:

https://brainly.com/question/30035219

#SPJ1