Lab8-Solution
Lab8-Solution
if __name__=='__main__':
variables = ('John', 'Anna', 'Tom', 'Patricia')
domains = {
'John': [1, 2, 3],
'Anna': [1, 3],
'Tom': [2, 4],
'Patricia': [2, 3, 4],
}
constraints = [
(('John', 'Anna', 'Tom'), constraint_unique),
(('Tom', 'Anna'), constraint_bigger),
(('John', 'Patricia'), constraint_odd_even),
]
Lab task 2
from simpleai.search import CspProblem, backtrack
if __name__=='__main__':
# Specify the variables
names = ('Mark', 'Julia', 'Steve', 'Amanda', 'Brian',
'Joanne', 'Derek', 'Allan', 'Michelle', 'Kelly')
Lab task 3
actions = []
if row_empty > 0:
actions.append(rows[row_empty - 1][col_empty])
if row_empty < 2:
actions.append(rows[row_empty + 1][col_empty])
if col_empty > 0:
actions.append(rows[row_empty][col_empty - 1])
if col_empty < 2:
actions.append(rows[row_empty][col_empty + 1])
return actions
# Return the resulting state after moving a piece to the empty space
def result(self, state, action):
rows = string_to_list(state)
row_empty, col_empty = get_location(rows, 'e')
row_new, col_new = get_location(rows, action)
rows[row_empty][col_empty], rows[row_new][col_new] = \
rows[row_new][col_new], rows[row_empty][col_empty]
return list_to_string(rows)
distance = 0
# Starting point
INITIAL = '''1-e-2
6-3-4
7-5-8'''
print(state)
Lab task 4
import math
from simpleai.search import SearchProblem, astar
for y in range(len(self.board)):
for x in range(len(self.board[y])):
if self.board[y][x].lower() == "o":
self.initial = (x, y)
elif self.board[y][x].lower() == "x":
self.goal = (x, y)
super(MazeSolver, self).__init__(initial_state=self.initial)
return actions
if action.count("up"):
y -= 1
if action.count("down"):
y += 1
if action.count("left"):
x -= 1
if action.count("right"):
x += 1
new_state = (x, y)
return new_state
if __name__ == "__main__":
# Define the map
MAP = """
##############################
# # # #
# #### ######## # #
# o# # # #
# ### ##### ###### #
# # ### # #
# # # # # # ###
# ##### # # # x #
# # # #
##############################
"""
print()