Skip to content

Commit c587f2c

Browse files
dmeoliantmarakis
authored andcommitted
removed inf and isclose definition from utils and replaced with np.inf and np.isclose (#1141)
* changed queue to set in AC3 Changed queue to set in AC3 (as in the pseudocode of the original algorithm) to reduce the number of consistency-check due to the redundancy of the same arcs in queue. For example, on the harder1 configuration of the Sudoku CSP the number consistency-check has been reduced from 40464 to 12562! * re-added test commented by mistake * added the mentioned AC4 algorithm for constraint propagation AC3 algorithm has non-optimal worst case time-complexity O(cd^3 ), while AC4 algorithm runs in O(cd^2) worst case time * added doctest in Sudoku for AC4 and and the possibility of choosing the constant propagation algorithm in mac inference * removed useless doctest for AC4 in Sudoku because AC4's tests are already present in test_csp.py * added map coloring SAT problems * fixed typo errors and removed unnecessary brackets * reformulated the map coloring problem * Revert "reformulated the map coloring problem" This reverts commit 20ab0e5. * Revert "fixed typo errors and removed unnecessary brackets" This reverts commit f743146. * Revert "added map coloring SAT problems" This reverts commit 9e0fa55. * Revert "removed useless doctest for AC4 in Sudoku because AC4's tests are already present in test_csp.py" This reverts commit b3cd24c. * Revert "added doctest in Sudoku for AC4 and and the possibility of choosing the constant propagation algorithm in mac inference" This reverts commit 6986247. * Revert "added the mentioned AC4 algorithm for constraint propagation" This reverts commit 03551fb. * added map coloring SAT problem * fixed build error * Revert "added map coloring SAT problem" This reverts commit 93af259. * Revert "fixed build error" This reverts commit 6641c2c. * added map coloring SAT problem * removed redundant parentheses * added Viterbi algorithm * added monkey & bananas planning problem * simplified condition in search.py * added tests for monkey & bananas planning problem * removed monkey & bananas planning problem * Revert "removed monkey & bananas planning problem" This reverts commit 9d37ae0. * Revert "added tests for monkey & bananas planning problem" This reverts commit 24041e9. * Revert "simplified condition in search.py" This reverts commit 6d229ce. * Revert "added monkey & bananas planning problem" This reverts commit c74933a. * defined the PlanningProblem as a specialization of a search.Problem & fixed typo errors * fixed doctest in logic.py * fixed doctest for cascade_distribution * added ForwardPlanner and tests * added __lt__ implementation for Expr * added more tests * renamed forward planner * Revert "renamed forward planner" This reverts commit c4139e5. * renamed forward planner class & added doc * added backward planner and tests * fixed mdp4e.py doctests * removed ignore_delete_lists_heuristic flag * fixed heuristic for forward and backward planners * added SATPlan and tests * fixed ignore delete lists heuristic in forward and backward planners * fixed backward planner and added tests * updated doc * added nary csp definition and examples * added CSPlan and tests * fixed CSPlan * added book's cryptarithmetic puzzle example * fixed typo errors in test_csp * fixed #1111 * added sortedcontainers to yml and doc to CSPlan * added tests for n-ary csp * fixed utils.extend * updated test_probability.py * converted static methods to functions * added AC3b and AC4 with heuristic and tests * added conflict-driven clause learning sat solver * added tests for cdcl and heuristics * fixed probability.py * fixed import * fixed kakuro * added Martelli and Montanari rule-based unification algorithm * removed duplicate standardize_variables * renamed variables known as built-in functions * fixed typos in learning.py * renamed some files and fixed typos * fixed typos * fixed typos * fixed tests * removed unify_mm * remove unnecessary brackets * fixed tests * moved utility functions to utils.py * fixed typos * moved utils function to utils.py, separated probability learning classes from learning.py, fixed typos and fixed imports in .ipynb files * added missing learners * fixed Travis build * fixed typos * fixed typos * fixed typos * fixed typos * fixed typos in agents files * fixed imports in agent files * fixed deep learning .ipynb imports * fixed typos * added SVM * added .ipynb and fixed typos * adapted code for .ipynb * fixed typos * updated .ipynb * updated .ipynb * updated logic.py * updated .ipynb * updated .ipynb * updated planning.py * updated inf definition * fixed typos * fixed typos * fixed typos * fixed typos * Revert "fixed typos" This reverts commit 658309d. * Revert "fixed typos" This reverts commit 08ad660. * fixed typos * fixed typos * fixed typos * fixed typos * fixed typos and utils imports in *4e.py files * fixed typos * fixed typos * fixed typos * fixed typos * fixed import * fixed typos * fixed typos * fixd typos * fixed typos * fixed typos * updated SVM * added svm test * fixed SVM and tests * fixed some definitions and typos * fixed svm and tests * added SVMs also in learning4e.py * fixed inf definition * fixed .travis.yml * fixed .travis.yml * fixed import * fixed inf definition * replaced cvxopt with qpsolvers * replaced cvxopt with quadprog * fixed some definitions * fixed typos and removed unnecessary tests * replaced quadprog with qpsolvers * fixed extend in utils * specified error type in try-catch block * fixed extend in utils * fixed typos * fixed learning.py * fixed doctest errors * added comments * removed unnecessary if condition * updated learning.py * fixed imports * removed unnecessary imports * fixed keras imports * fixed typos * fixed learning_curve * added comments * fixed typos * removed inf and isclose definition from utils and replaced with numpy.inf and numpy.isclose * fixed doctests
1 parent fbdb36d commit c587f2c

28 files changed

+172
-225
lines changed

agents.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,7 @@ def list_things_at(self, location, tclass=Thing):
354354
return [thing for thing in self.things
355355
if thing.location == location and isinstance(thing, tclass)]
356356
return [thing for thing in self.things
357-
if all(x==y for x,y in zip(thing.location, location))
358-
and isinstance(thing, tclass)]
357+
if all(x == y for x, y in zip(thing.location, location)) and isinstance(thing, tclass)]
359358

360359
def some_things_at(self, location, tclass=Thing):
361360
"""Return true if at least one of the things at location

agents4e.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,7 @@ def list_things_at(self, location, tclass=Thing):
354354
return [thing for thing in self.things
355355
if thing.location == location and isinstance(thing, tclass)]
356356
return [thing for thing in self.things
357-
if all(x==y for x,y in zip(thing.location, location))
358-
and isinstance(thing, tclass)]
357+
if all(x == y for x, y in zip(thing.location, location)) and isinstance(thing, tclass)]
359358

360359
def some_things_at(self, location, tclass=Thing):
361360
"""Return true if at least one of the things at location

deep_learning4e.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Deep learning. (Chapters 20)"""
22

3-
import math
43
import random
54
import statistics
65

6+
import numpy as np
77
from keras import Sequential, optimizers
88
from keras.layers import Embedding, SimpleRNN, Dense
99
from keras.preprocessing import sequence
@@ -249,7 +249,7 @@ def adam(dataset, net, loss, epochs=1000, rho=(0.9, 0.999), delta=1 / 10 ** 8,
249249
r_hat = scalar_vector_product(1 / (1 - rho[1] ** t), r)
250250

251251
# rescale r_hat
252-
r_hat = map_vector(lambda x: 1 / (math.sqrt(x) + delta), r_hat)
252+
r_hat = map_vector(lambda x: 1 / (np.sqrt(x) + delta), r_hat)
253253

254254
# delta weights
255255
delta_theta = scalar_vector_product(-l_rate, element_wise_product(s_hat, r_hat))
@@ -341,7 +341,7 @@ def forward(self, inputs):
341341
res = []
342342
# get normalized value of each input
343343
for i in range(len(self.nodes)):
344-
val = [(inputs[i] - mu) * self.weights[0] / math.sqrt(self.epsilon + stderr ** 2) + self.weights[1]]
344+
val = [(inputs[i] - mu) * self.weights[0] / np.sqrt(self.epsilon + stderr ** 2) + self.weights[1]]
345345
res.append(val)
346346
self.nodes[i].val = val
347347
return res

games.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
"""Games or Adversarial Search. (Chapter 5)"""
1+
"""Games or Adversarial Search (Chapter 5)"""
22

33
import copy
44
import itertools
55
import random
66
from collections import namedtuple
77

8-
from utils import vector_add, inf
8+
import numpy as np
9+
10+
from utils import vector_add
911

1012
GameState = namedtuple('GameState', 'to_move, utility, board, moves')
1113
StochasticGameState = namedtuple('StochasticGameState', 'to_move, utility, board, moves, chance')
@@ -24,15 +26,15 @@ def minmax_decision(state, game):
2426
def max_value(state):
2527
if game.terminal_test(state):
2628
return game.utility(state, player)
27-
v = -inf
29+
v = -np.inf
2830
for a in game.actions(state):
2931
v = max(v, min_value(game.result(state, a)))
3032
return v
3133

3234
def min_value(state):
3335
if game.terminal_test(state):
3436
return game.utility(state, player)
35-
v = inf
37+
v = np.inf
3638
for a in game.actions(state):
3739
v = min(v, max_value(game.result(state, a)))
3840
return v
@@ -53,13 +55,13 @@ def expect_minmax(state, game):
5355
player = game.to_move(state)
5456

5557
def max_value(state):
56-
v = -inf
58+
v = -np.inf
5759
for a in game.actions(state):
5860
v = max(v, chance_node(state, a))
5961
return v
6062

6163
def min_value(state):
62-
v = inf
64+
v = np.inf
6365
for a in game.actions(state):
6466
v = min(v, chance_node(state, a))
6567
return v
@@ -94,7 +96,7 @@ def alpha_beta_search(state, game):
9496
def max_value(state, alpha, beta):
9597
if game.terminal_test(state):
9698
return game.utility(state, player)
97-
v = -inf
99+
v = -np.inf
98100
for a in game.actions(state):
99101
v = max(v, min_value(game.result(state, a), alpha, beta))
100102
if v >= beta:
@@ -105,7 +107,7 @@ def max_value(state, alpha, beta):
105107
def min_value(state, alpha, beta):
106108
if game.terminal_test(state):
107109
return game.utility(state, player)
108-
v = inf
110+
v = np.inf
109111
for a in game.actions(state):
110112
v = min(v, max_value(game.result(state, a), alpha, beta))
111113
if v <= alpha:
@@ -114,8 +116,8 @@ def min_value(state, alpha, beta):
114116
return v
115117

116118
# Body of alpha_beta_search:
117-
best_score = -inf
118-
beta = inf
119+
best_score = -np.inf
120+
beta = np.inf
119121
best_action = None
120122
for a in game.actions(state):
121123
v = min_value(game.result(state, a), best_score, beta)
@@ -135,7 +137,7 @@ def alpha_beta_cutoff_search(state, game, d=4, cutoff_test=None, eval_fn=None):
135137
def max_value(state, alpha, beta, depth):
136138
if cutoff_test(state, depth):
137139
return eval_fn(state)
138-
v = -inf
140+
v = -np.inf
139141
for a in game.actions(state):
140142
v = max(v, min_value(game.result(state, a), alpha, beta, depth + 1))
141143
if v >= beta:
@@ -146,7 +148,7 @@ def max_value(state, alpha, beta, depth):
146148
def min_value(state, alpha, beta, depth):
147149
if cutoff_test(state, depth):
148150
return eval_fn(state)
149-
v = inf
151+
v = np.inf
150152
for a in game.actions(state):
151153
v = min(v, max_value(game.result(state, a), alpha, beta, depth + 1))
152154
if v <= alpha:
@@ -158,8 +160,8 @@ def min_value(state, alpha, beta, depth):
158160
# The default test cuts off at depth d or at a terminal state
159161
cutoff_test = (cutoff_test or (lambda state, depth: depth > d or game.terminal_test(state)))
160162
eval_fn = eval_fn or (lambda state: game.utility(state, player))
161-
best_score = -inf
162-
beta = inf
163+
best_score = -np.inf
164+
beta = np.inf
163165
best_action = None
164166
for a in game.actions(state):
165167
v = min_value(game.result(state, a), best_score, beta, 1)

games4e.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
"""Games or Adversarial Search. (Chapter 5)"""
1+
"""Games or Adversarial Search (Chapter 5)"""
22

33
import copy
44
import itertools
55
import random
66
from collections import namedtuple
77

8-
from utils4e import vector_add, MCT_Node, ucb, inf
8+
import numpy as np
9+
10+
from utils4e import vector_add, MCT_Node, ucb
911

1012
GameState = namedtuple('GameState', 'to_move, utility, board, moves')
1113
StochasticGameState = namedtuple('StochasticGameState', 'to_move, utility, board, moves, chance')
@@ -24,15 +26,15 @@ def minmax_decision(state, game):
2426
def max_value(state):
2527
if game.terminal_test(state):
2628
return game.utility(state, player)
27-
v = -inf
29+
v = -np.inf
2830
for a in game.actions(state):
2931
v = max(v, min_value(game.result(state, a)))
3032
return v
3133

3234
def min_value(state):
3335
if game.terminal_test(state):
3436
return game.utility(state, player)
35-
v = inf
37+
v = np.inf
3638
for a in game.actions(state):
3739
v = min(v, max_value(game.result(state, a)))
3840
return v
@@ -53,13 +55,13 @@ def expect_minmax(state, game):
5355
player = game.to_move(state)
5456

5557
def max_value(state):
56-
v = -inf
58+
v = -np.inf
5759
for a in game.actions(state):
5860
v = max(v, chance_node(state, a))
5961
return v
6062

6163
def min_value(state):
62-
v = inf
64+
v = np.inf
6365
for a in game.actions(state):
6466
v = min(v, chance_node(state, a))
6567
return v
@@ -94,7 +96,7 @@ def alpha_beta_search(state, game):
9496
def max_value(state, alpha, beta):
9597
if game.terminal_test(state):
9698
return game.utility(state, player)
97-
v = -inf
99+
v = -np.inf
98100
for a in game.actions(state):
99101
v = max(v, min_value(game.result(state, a), alpha, beta))
100102
if v >= beta:
@@ -105,7 +107,7 @@ def max_value(state, alpha, beta):
105107
def min_value(state, alpha, beta):
106108
if game.terminal_test(state):
107109
return game.utility(state, player)
108-
v = inf
110+
v = np.inf
109111
for a in game.actions(state):
110112
v = min(v, max_value(game.result(state, a), alpha, beta))
111113
if v <= alpha:
@@ -114,8 +116,8 @@ def min_value(state, alpha, beta):
114116
return v
115117

116118
# Body of alpha_beta_search:
117-
best_score = -inf
118-
beta = inf
119+
best_score = -np.inf
120+
beta = np.inf
119121
best_action = None
120122
for a in game.actions(state):
121123
v = min_value(game.result(state, a), best_score, beta)
@@ -135,7 +137,7 @@ def alpha_beta_cutoff_search(state, game, d=4, cutoff_test=None, eval_fn=None):
135137
def max_value(state, alpha, beta, depth):
136138
if cutoff_test(state, depth):
137139
return eval_fn(state)
138-
v = -inf
140+
v = -np.inf
139141
for a in game.actions(state):
140142
v = max(v, min_value(game.result(state, a), alpha, beta, depth + 1))
141143
if v >= beta:
@@ -146,7 +148,7 @@ def max_value(state, alpha, beta, depth):
146148
def min_value(state, alpha, beta, depth):
147149
if cutoff_test(state, depth):
148150
return eval_fn(state)
149-
v = inf
151+
v = np.inf
150152
for a in game.actions(state):
151153
v = min(v, max_value(game.result(state, a), alpha, beta, depth + 1))
152154
if v <= alpha:
@@ -158,8 +160,8 @@ def min_value(state, alpha, beta, depth):
158160
# The default test cuts off at depth d or at a terminal state
159161
cutoff_test = (cutoff_test or (lambda state, depth: depth > d or game.terminal_test(state)))
160162
eval_fn = eval_fn or (lambda state: game.utility(state, player))
161-
best_score = -inf
162-
beta = inf
163+
best_score = -np.inf
164+
beta = np.inf
163165
best_action = None
164166
for a in game.actions(state):
165167
v = min_value(game.result(state, a), best_score, beta, 1)

gui/romania_problem.py

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1+
from copy import deepcopy
12
from tkinter import *
2-
import sys
3-
import os.path
4-
import math
5-
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
3+
64
from search import *
7-
from search import breadth_first_tree_search as bfts, depth_first_tree_search as dfts, \
8-
depth_first_graph_search as dfgs, breadth_first_graph_search as bfs, uniform_cost_search as ucs, \
9-
astar_search as asts
105
from utils import PriorityQueue
11-
from copy import deepcopy
6+
7+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
128

139
root = None
1410
city_coord = {}
@@ -289,7 +285,6 @@ def make_rectangle(map, x0, y0, margin, city_name):
289285

290286

291287
def make_legend(map):
292-
293288
rect1 = map.create_rectangle(600, 100, 610, 110, fill="white")
294289
text1 = map.create_text(615, 105, anchor=W, text="Un-explored")
295290

@@ -325,13 +320,11 @@ def tree_search(problem):
325320
display_current(node)
326321
if counter % 3 == 1 and counter >= 0:
327322
if problem.goal_test(node.state):
328-
329323
return node
330324
frontier.extend(node.expand(problem))
331325

332326
display_frontier(frontier)
333327
if counter % 3 == 2 and counter >= 0:
334-
335328
display_explored(node)
336329
return None
337330

@@ -562,7 +555,7 @@ def astar_search(problem, h=None):
562555

563556
# TODO:
564557
# Remove redundant code.
565-
# Make the interchangbility work between various algorithms at each step.
558+
# Make the interchangeability work between various algorithms at each step.
566559
def on_click():
567560
"""
568561
This function defines the action of the 'Next' button.
@@ -572,47 +565,47 @@ def on_click():
572565
if "Breadth-First Tree Search" == algo.get():
573566
node = breadth_first_tree_search(romania_problem)
574567
if node is not None:
575-
final_path = bfts(romania_problem).solution()
568+
final_path = breadth_first_tree_search(romania_problem).solution()
576569
final_path.append(start.get())
577570
display_final(final_path)
578571
next_button.config(state="disabled")
579572
counter += 1
580573
elif "Depth-First Tree Search" == algo.get():
581574
node = depth_first_tree_search(romania_problem)
582575
if node is not None:
583-
final_path = dfts(romania_problem).solution()
576+
final_path = depth_first_tree_search(romania_problem).solution()
584577
final_path.append(start.get())
585578
display_final(final_path)
586579
next_button.config(state="disabled")
587580
counter += 1
588581
elif "Breadth-First Graph Search" == algo.get():
589582
node = breadth_first_graph_search(romania_problem)
590583
if node is not None:
591-
final_path = bfs(romania_problem).solution()
584+
final_path = breadth_first_graph_search(romania_problem).solution()
592585
final_path.append(start.get())
593586
display_final(final_path)
594587
next_button.config(state="disabled")
595588
counter += 1
596589
elif "Depth-First Graph Search" == algo.get():
597590
node = depth_first_graph_search(romania_problem)
598591
if node is not None:
599-
final_path = dfgs(romania_problem).solution()
592+
final_path = depth_first_graph_search(romania_problem).solution()
600593
final_path.append(start.get())
601594
display_final(final_path)
602595
next_button.config(state="disabled")
603596
counter += 1
604597
elif "Uniform Cost Search" == algo.get():
605598
node = uniform_cost_search(romania_problem)
606599
if node is not None:
607-
final_path = ucs(romania_problem).solution()
600+
final_path = uniform_cost_search(romania_problem).solution()
608601
final_path.append(start.get())
609602
display_final(final_path)
610603
next_button.config(state="disabled")
611604
counter += 1
612605
elif "A* - Search" == algo.get():
613606
node = astar_search(romania_problem)
614607
if node is not None:
615-
final_path = asts(romania_problem).solution()
608+
final_path = astar_search(romania_problem).solution()
616609
final_path.append(start.get())
617610
display_final(final_path)
618611
next_button.config(state="disabled")
@@ -626,6 +619,7 @@ def reset_map():
626619
city_map.itemconfig(city_coord[city], fill="white")
627620
next_button.config(state="normal")
628621

622+
629623
# TODO: Add more search algorithms in the OptionMenu
630624

631625

0 commit comments

Comments
 (0)