AI Lab Assignment#02
AI Lab Assignment#02
Lab Assignment # 02
Name: Parkash
Student ID: 11089
Class ID: 110029
Question:
Code:
import numpy as np
import random
import math
import time as tm
class NQueens:
def __init__(self, num_cols, board=None, seed=0):
self._rand_obj = np.random.RandomState()
self._rand_obj.seed(seed)
self._board_size = num_cols
self._board = board
self.seed = seed
if (board is None):
self._new_board()
def _new_board(self):
self._board = [None] * self._board_size
for i in range(self._board_size):
self._board[i] = self._rand_obj.randint(0, self._board_size - 1)
def size(self):
return (self._board_size)
def place_queen(self, row, col):
self._board[col] = row
def get_board(self):
return (self._board)
# Prints Board
def draw(self):
temp_str = ""
temp_board = self._board[:]
for i in range(self._board_size):
find_indices = []
cumul_ind = 0
try:
for j in range(0, temp_board.count(i)):
find_indices.append(temp_board.index(i))
temp_board[temp_board.index(i)] = None
find_indices.sort()
temp_str += ("x " * (find_indices[0])) + "Q" + " "
cumul_ind += find_indices[0]
for j in range(1, len(find_indices)):
cumul_ind += find_indices[j]
temp_str += ("x " * (find_indices[j] - find_indices[j - 1] - 1)) + \
"Q" + " "
temp_str += ("x " * (self._board_size - cumul_ind - 1))
except:
temp_str = temp_str + ("x " * self._board_size)
temp_str += "\n"
print(temp_str)
class _local_node:
def __init__(self, state, h_val):
self.state = state
self.h = h_val
class NQueensAgent:
def __init__(self, initial_state, seed):
self._curr_node = _local_node(initial_state, self.h(initial_state))
self._rand_obj = np.random.RandomState()
self._rand_obj.seed(seed)
self.seed = seed
def get_state(self):
return (self._curr_node.state)
def get_h(self):
return (self._curr_node.h)
return (total_h)
for i in col_list:
for j in range(1, num_cols):
temp_state = NQueens(num_cols, state._board[:], state.seed)
old_q_p = temp_state.get_board()[i]
temp_state.place_queen((old_q_p + j) % num_cols, i)
temp_node = _local_node(temp_state, self.h(temp_state))
# We always insert the node with the lowest h
# at the beginning of the list and sometimes when the current h is
# equal to the lowest h (controlled by randint)
if (lowest_h < 0 or temp_node.h <= lowest_h):
lowest_h = temp_node.h
if (temp_node.h < lowest_h or self._rand_obj.randint(0, 1) == 0):
child_list.insert(0, temp_node)
else:
child_list.append(temp_node)
else:
child_list.append(temp_node)
return (child_list)
def _local_hill_climb(self):
#Implments a form of Local Hill Climb for NQueens
prev_node = self._curr_node
curr_node = self._curr_node
children = self.expand_children(self._curr_node.state)
while curr_node.h > 0:
# save a bit of computation, by only expanding when needed
if (prev_node != curr_node):
children = self.expand_children(curr_node.state)
# Get neighbor node w/ lowest value (which we inserted w/ our expansion)
neighbor_node = children[0]
if ((neighbor_node is None) or (neighbor_node.h >= curr_node.h)):
break
curr_node = neighbor_node
curr_node.state.draw()
# Now that we've found our final state, change to that state
self._curr_node = curr_node
def find_best_answer(self):
self._local_hill_climb()
# --------Code to run NQueens Agent--------#
seed = 6
game_size = 4
test_game = NQueens(game_size, None, seed)
print("==Before Searching for Solution==\n")
test_game.draw()
print("---------------------------------\n")
Output:
Question:
Code:
import random
def fitness(chromosome):
diagonal_collisions = 0
n = len(chromosome)
for i in range(n):
left_diagonal[i + chromosome[i] - 1] += 1
right_diagonal[len(chromosome) - i + chromosome[i] - 2] += 1
diagonal_collisions = 0
for i in range(2*n-1):
counter = 0
if left_diagonal[i] > 1:
counter += left_diagonal[i]-1
if right_diagonal[i] > 1:
counter += right_diagonal[i]-1
r = random.uniform(0, total)
upto = 0
if upto + w >= r:
return c
upto += w
n = len(x)
c = random.randint(0, n - 1)
n = len(x)
c = random.randint(0, n - 1)
m = random.randint(1, n)
x[c] = m
return x
mutation_probability = 0.03
new_population = []
for i in range(len(population)):
child = reproduce(x, y) #creating two new chromosomes from the best 2 chromosomes
child = mutate(child)
print_chromosome(child)
new_population.append(child)
return new_population
def print_chromosome(chrom):
.format(str(chrom), fitness(chrom)))
if __name__ == "__main__":
generation = 1
print("")
generation += 1
chrom_out = []
if fitness(chrom) == maxFitness:
print("");
print("One of the solutions: ")
chrom_out = chrom
print_chromosome(chrom)
board = []
for x in range(nq):
board.append(["x"] * nq)
for i in range(nq):
board[nq-chrom_out[i]][i]="Q"
def print_board(board):
print()
print_board(board)
Output: