Skip to content

Commit 3ee5fc6

Browse files
Minor fixes and tweaks based on feedback.
1 parent 92267eb commit 3ee5fc6

15 files changed

Lines changed: 75 additions & 46 deletions

File tree

ch02-search_fundamentals/uninformed_search/maze_bfs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def is_in_visited_points(current_point, visited_points):
4949
maze_game_main = mp.MazePuzzle()
5050

5151
# Run the breadth first search algorithm with the initialized maze
52-
outcome = run_bfs(maze_game_main, mp.Point(2, 2), [])
52+
starting_point = mp.Point(2, 2)
53+
outcome = run_bfs(maze_game_main, starting_point, [])
5354

5455
# Get the path found by the breadth first search algorithm
5556
bfs_path = mp.get_path(outcome)

ch02-search_fundamentals/uninformed_search/maze_dfs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def is_in_visited_points(current_point, visited_points):
4949
maze_game_main = mp.MazePuzzle()
5050

5151
# Run the depth first search algorithm with the initialized maze
52-
outcome = run_dfs(maze_game_main, mp.Point(2, 2))
52+
starting_point = mp.Point(2, 2)
53+
outcome = run_dfs(maze_game_main, starting_point)
5354

5455
# Get the path found by the depth first search algorithm
5556
dfs_path = mp.get_path(outcome)

ch02-search_fundamentals/uninformed_search/maze_puzzle.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
import math
23

34

45
# This class is used to store the idea of a point in the maze and linking it to other points to create a path.
@@ -7,7 +8,7 @@ def __init__(self, x=0, y=0):
78
self.x = x
89
self.y = y
910
self.parent = None
10-
self.cost = 99999
11+
self.cost = math.inf
1112

1213
def set_parent(self, p):
1314
self.parent = p
@@ -50,8 +51,8 @@ def get_current_point_value(self, current_point):
5051
# Return all valid neighbors around a specific point, excluding points outside of the maze and walls.
5152
def get_neighbors(self, current_point):
5253
neighbors = []
53-
# potential_neighbors = [[0, -1], [1, 0], [0, 1], [-1, 0]]
54-
potential_neighbors = [[SOUTH.x, SOUTH.y], [WEST.x, WEST.y], [NORTH.x, NORTH.y], [EAST.x, EAST.y]]
54+
# potential_neighbors = [[0, 1], [0, -1], [1, 0], [-1, 0]]
55+
potential_neighbors = [[NORTH.x, NORTH.y], [SOUTH.x, SOUTH.y], [EAST.x, EAST.y], [WEST.x, WEST.y]]
5556
for neighbor in potential_neighbors:
5657
target_point = Point(current_point.x + neighbor[0], current_point.y + neighbor[1])
5758
if 0 <= target_point.x < self.maze_size_x and 0 <= target_point.y < self.maze_size_y:
@@ -131,12 +132,16 @@ def get_direction(origin, target):
131132
return 'S'
132133
elif target.x == origin.x + 1 and target.y == origin.y:
133134
return 'E'
134-
else:
135+
elif target.x == origin.x - 1 and target.y == origin.y:
135136
return 'W'
136137

137138

138139
# Utility to determine the cost of a move given a direction. In this case, North and South is 5, and East and West is 1.
140+
STANDARD_COST = 1
141+
GRAVITY_COST = 5
142+
143+
139144
def get_cost(direction):
140145
if direction == 'N' or direction == 'S':
141-
return 5
142-
return 1
146+
return GRAVITY_COST
147+
return STANDARD_COST

ch03-intelligent_search/adversarial_search/connect_ai.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import copy
22
import random
3+
import math
34

45
# Set the min-max IDs, and pseudo infinity constants
56
MIN = -1
67
MAX = 1
7-
INFINITY_POSITIVE = 999
8-
INFINITY_NEGATIVE = -999
8+
INFINITY_POSITIVE = math.inf
9+
INFINITY_NEGATIVE = -math.inf
910

1011

1112
# This class contains a move and the respective value earned for that move

ch03-intelligent_search/adversarial_search/connect_ai_alpha_beta_pruning.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import copy
22
import random
3+
import math
34

45
# Set the min-max IDs, and pseudo infinity constants
56
MIN = -1
67
MAX = 1
7-
INFINITY_POSITIVE = 999
8-
INFINITY_NEGATIVE = -999
8+
INFINITY_POSITIVE = math.inf
9+
INFINITY_NEGATIVE = -math.inf
910

1011

1112
# This class contains a move and the respective value earned for that move

ch03-intelligent_search/adversarial_search/connect_puzzle.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def generate_board(self, board_size_x, board_size_y):
3636
def print_board(self):
3737
result = ''
3838
for y in range(0, self.board_size_y):
39-
for x in range(self.board_size_x - 1, -1, -1):
39+
for x in range(0, self.board_size_x):
4040
result += self.board[x][y]
4141
result += '\n'
4242
print(result)
@@ -133,11 +133,13 @@ def execute_move(self, player, slot_number):
133133

134134
# Execute a move for a player if there's space in the slot and choose the player based on whose turn it is
135135
def play_move(self, slot):
136-
if not self.is_slot_full(slot):
137-
if self.player_turn == PLAYERS[PLAYER_AI]:
138-
self.execute_move(PLAYER_AI, slot)
139-
else:
140-
self.execute_move(PLAYER_HUMAN, slot)
141-
self.player_turn *= -1
142-
return True
136+
if 0 <= slot <= 4:
137+
if not self.is_slot_full(slot):
138+
if self.player_turn == PLAYERS[PLAYER_AI]:
139+
self.execute_move(PLAYER_AI, slot)
140+
else:
141+
self.execute_move(PLAYER_HUMAN, slot)
142+
self.player_turn *= -1
143+
return True
144+
return False
143145
return False

ch03-intelligent_search/informed_search/maze_puzzle.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
import math
23

34

45
# This class is used to store the idea of a point in the maze and linking it to other points to create a path.
@@ -7,7 +8,7 @@ def __init__(self, x=0, y=0):
78
self.x = x
89
self.y = y
910
self.parent = None
10-
self.cost = 99999
11+
self.cost = math.inf
1112

1213
def set_parent(self, p):
1314
self.parent = p
@@ -50,8 +51,8 @@ def get_current_point_value(self, current_point):
5051
# Return all valid neighbors around a specific point, excluding points outside of the maze and walls.
5152
def get_neighbors(self, current_point):
5253
neighbors = []
53-
# potential_neighbors = [[0, -1], [1, 0], [0, 1], [-1, 0]]
54-
potential_neighbors = [[SOUTH.x, SOUTH.y], [WEST.x, WEST.y], [NORTH.x, NORTH.y], [EAST.x, EAST.y]]
54+
# potential_neighbors = [[0, 1], [0, -1], [1, 0], [-1, 0]]
55+
potential_neighbors = [[NORTH.x, NORTH.y], [SOUTH.x, SOUTH.y], [EAST.x, EAST.y], [WEST.x, WEST.y]]
5556
for neighbor in potential_neighbors:
5657
target_point = Point(current_point.x + neighbor[0], current_point.y + neighbor[1])
5758
if 0 <= target_point.x < self.maze_size_x and 0 <= target_point.y < self.maze_size_y:
@@ -131,11 +132,15 @@ def get_direction(origin, target):
131132
return 'S'
132133
elif target.x == origin.x + 1 and target.y == origin.y:
133134
return 'E'
134-
else:
135+
elif target.x == origin.x - 1 and target.y == origin.y:
135136
return 'W'
136137

137138

138139
# Utility to determine the cost of a move given a direction. In this case, North and South is 5, and East and West is 1.
140+
STANDARD_COST = 1
141+
GRAVITY_COST = 5
142+
143+
139144
def get_cost(direction):
140145
if direction == 'N' or direction == 'S':
141146
return 5

ch04-evolutionary_algorithms/knapsack_brute_force.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def get_all_combinations(items):
5656
combinations = []
5757
for index in range(0, len(items)):
5858
combinations.append(items[index])
59-
els = [list(x) for x in itertools.combinations(items, index)]
60-
combinations.append(els)
59+
possibilities = [list(x) for x in itertools.combinations(items, index)]
60+
combinations.append(possibilities)
6161
return combinations
6262

6363

ch04-evolutionary_algorithms/knapsack_genetic_algorithm.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,17 @@ def set_probabilities(population):
121121
# Roulette wheel selection to select individuals in a population
122122
def roulette_wheel_selection(population, number_of_selections):
123123
set_probabilities(population)
124+
slices = []
125+
total = 0
126+
for r in range(0, len(population)):
127+
individual = population[r]
128+
slices.append([r, total, total + individual[INDIVIDUAL_PROBABILITY_INDEX]])
129+
total += individual[INDIVIDUAL_PROBABILITY_INDEX]
124130
chosen_ones = []
125-
for selection in range(number_of_selections):
126-
r = random.random()
127-
for individual in population:
128-
if r <= individual[INDIVIDUAL_PROBABILITY_INDEX]:
129-
chosen_ones.append(individual)
130-
break
131+
for r in range(number_of_selections):
132+
spin = random.random()
133+
result = [s[0] for s in slices if s[1] < spin <= s[2]]
134+
chosen_ones.append(population[result[0]])
131135
return chosen_ones
132136

133137

ch06-swarm_intelligence-ants/carnival_aco.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
# least resistance.
99

1010
# Set the number of attractions in the data set
11-
# Best for 5 attractions: 19
12-
# Best for 48 attractions: 33523
11+
# Best total distance for 5 attractions: 19
12+
# Best total distance for 48 attractions: 33523
1313
ATTRACTION_COUNT = 48
1414
# Initialize the 2D matrix for storing distances between attractions
1515
attraction_distances = []

0 commit comments

Comments
 (0)