Skip to content

Commit

Permalink
removed inf and isclose definition from utils and replaced with np.in…
Browse files Browse the repository at this point in the history
…f and np.isclose (aimacode#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 aimacode#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
  • Loading branch information
dmeoli authored and antmarakis committed Dec 14, 2019
1 parent fbdb36d commit c587f2c
Show file tree
Hide file tree
Showing 28 changed files with 172 additions and 225 deletions.
3 changes: 1 addition & 2 deletions agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,7 @@ def list_things_at(self, location, tclass=Thing):
return [thing for thing in self.things
if thing.location == location and isinstance(thing, tclass)]
return [thing for thing in self.things
if all(x==y for x,y in zip(thing.location, location))
and isinstance(thing, tclass)]
if all(x == y for x, y in zip(thing.location, location)) and isinstance(thing, tclass)]

def some_things_at(self, location, tclass=Thing):
"""Return true if at least one of the things at location
Expand Down
3 changes: 1 addition & 2 deletions agents4e.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,7 @@ def list_things_at(self, location, tclass=Thing):
return [thing for thing in self.things
if thing.location == location and isinstance(thing, tclass)]
return [thing for thing in self.things
if all(x==y for x,y in zip(thing.location, location))
and isinstance(thing, tclass)]
if all(x == y for x, y in zip(thing.location, location)) and isinstance(thing, tclass)]

def some_things_at(self, location, tclass=Thing):
"""Return true if at least one of the things at location
Expand Down
6 changes: 3 additions & 3 deletions deep_learning4e.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Deep learning. (Chapters 20)"""

import math
import random
import statistics

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

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

# delta weights
delta_theta = scalar_vector_product(-l_rate, element_wise_product(s_hat, r_hat))
Expand Down Expand Up @@ -341,7 +341,7 @@ def forward(self, inputs):
res = []
# get normalized value of each input
for i in range(len(self.nodes)):
val = [(inputs[i] - mu) * self.weights[0] / math.sqrt(self.epsilon + stderr ** 2) + self.weights[1]]
val = [(inputs[i] - mu) * self.weights[0] / np.sqrt(self.epsilon + stderr ** 2) + self.weights[1]]
res.append(val)
self.nodes[i].val = val
return res
Expand Down
30 changes: 16 additions & 14 deletions games.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Games or Adversarial Search. (Chapter 5)"""
"""Games or Adversarial Search (Chapter 5)"""

import copy
import itertools
import random
from collections import namedtuple

from utils import vector_add, inf
import numpy as np

from utils import vector_add

GameState = namedtuple('GameState', 'to_move, utility, board, moves')
StochasticGameState = namedtuple('StochasticGameState', 'to_move, utility, board, moves, chance')
Expand All @@ -24,15 +26,15 @@ def minmax_decision(state, game):
def max_value(state):
if game.terminal_test(state):
return game.utility(state, player)
v = -inf
v = -np.inf
for a in game.actions(state):
v = max(v, min_value(game.result(state, a)))
return v

def min_value(state):
if game.terminal_test(state):
return game.utility(state, player)
v = inf
v = np.inf
for a in game.actions(state):
v = min(v, max_value(game.result(state, a)))
return v
Expand All @@ -53,13 +55,13 @@ def expect_minmax(state, game):
player = game.to_move(state)

def max_value(state):
v = -inf
v = -np.inf
for a in game.actions(state):
v = max(v, chance_node(state, a))
return v

def min_value(state):
v = inf
v = np.inf
for a in game.actions(state):
v = min(v, chance_node(state, a))
return v
Expand Down Expand Up @@ -94,7 +96,7 @@ def alpha_beta_search(state, game):
def max_value(state, alpha, beta):
if game.terminal_test(state):
return game.utility(state, player)
v = -inf
v = -np.inf
for a in game.actions(state):
v = max(v, min_value(game.result(state, a), alpha, beta))
if v >= beta:
Expand All @@ -105,7 +107,7 @@ def max_value(state, alpha, beta):
def min_value(state, alpha, beta):
if game.terminal_test(state):
return game.utility(state, player)
v = inf
v = np.inf
for a in game.actions(state):
v = min(v, max_value(game.result(state, a), alpha, beta))
if v <= alpha:
Expand All @@ -114,8 +116,8 @@ def min_value(state, alpha, beta):
return v

# Body of alpha_beta_search:
best_score = -inf
beta = inf
best_score = -np.inf
beta = np.inf
best_action = None
for a in game.actions(state):
v = min_value(game.result(state, a), best_score, beta)
Expand All @@ -135,7 +137,7 @@ def alpha_beta_cutoff_search(state, game, d=4, cutoff_test=None, eval_fn=None):
def max_value(state, alpha, beta, depth):
if cutoff_test(state, depth):
return eval_fn(state)
v = -inf
v = -np.inf
for a in game.actions(state):
v = max(v, min_value(game.result(state, a), alpha, beta, depth + 1))
if v >= beta:
Expand All @@ -146,7 +148,7 @@ def max_value(state, alpha, beta, depth):
def min_value(state, alpha, beta, depth):
if cutoff_test(state, depth):
return eval_fn(state)
v = inf
v = np.inf
for a in game.actions(state):
v = min(v, max_value(game.result(state, a), alpha, beta, depth + 1))
if v <= alpha:
Expand All @@ -158,8 +160,8 @@ def min_value(state, alpha, beta, depth):
# The default test cuts off at depth d or at a terminal state
cutoff_test = (cutoff_test or (lambda state, depth: depth > d or game.terminal_test(state)))
eval_fn = eval_fn or (lambda state: game.utility(state, player))
best_score = -inf
beta = inf
best_score = -np.inf
beta = np.inf
best_action = None
for a in game.actions(state):
v = min_value(game.result(state, a), best_score, beta, 1)
Expand Down
30 changes: 16 additions & 14 deletions games4e.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Games or Adversarial Search. (Chapter 5)"""
"""Games or Adversarial Search (Chapter 5)"""

import copy
import itertools
import random
from collections import namedtuple

from utils4e import vector_add, MCT_Node, ucb, inf
import numpy as np

from utils4e import vector_add, MCT_Node, ucb

GameState = namedtuple('GameState', 'to_move, utility, board, moves')
StochasticGameState = namedtuple('StochasticGameState', 'to_move, utility, board, moves, chance')
Expand All @@ -24,15 +26,15 @@ def minmax_decision(state, game):
def max_value(state):
if game.terminal_test(state):
return game.utility(state, player)
v = -inf
v = -np.inf
for a in game.actions(state):
v = max(v, min_value(game.result(state, a)))
return v

def min_value(state):
if game.terminal_test(state):
return game.utility(state, player)
v = inf
v = np.inf
for a in game.actions(state):
v = min(v, max_value(game.result(state, a)))
return v
Expand All @@ -53,13 +55,13 @@ def expect_minmax(state, game):
player = game.to_move(state)

def max_value(state):
v = -inf
v = -np.inf
for a in game.actions(state):
v = max(v, chance_node(state, a))
return v

def min_value(state):
v = inf
v = np.inf
for a in game.actions(state):
v = min(v, chance_node(state, a))
return v
Expand Down Expand Up @@ -94,7 +96,7 @@ def alpha_beta_search(state, game):
def max_value(state, alpha, beta):
if game.terminal_test(state):
return game.utility(state, player)
v = -inf
v = -np.inf
for a in game.actions(state):
v = max(v, min_value(game.result(state, a), alpha, beta))
if v >= beta:
Expand All @@ -105,7 +107,7 @@ def max_value(state, alpha, beta):
def min_value(state, alpha, beta):
if game.terminal_test(state):
return game.utility(state, player)
v = inf
v = np.inf
for a in game.actions(state):
v = min(v, max_value(game.result(state, a), alpha, beta))
if v <= alpha:
Expand All @@ -114,8 +116,8 @@ def min_value(state, alpha, beta):
return v

# Body of alpha_beta_search:
best_score = -inf
beta = inf
best_score = -np.inf
beta = np.inf
best_action = None
for a in game.actions(state):
v = min_value(game.result(state, a), best_score, beta)
Expand All @@ -135,7 +137,7 @@ def alpha_beta_cutoff_search(state, game, d=4, cutoff_test=None, eval_fn=None):
def max_value(state, alpha, beta, depth):
if cutoff_test(state, depth):
return eval_fn(state)
v = -inf
v = -np.inf
for a in game.actions(state):
v = max(v, min_value(game.result(state, a), alpha, beta, depth + 1))
if v >= beta:
Expand All @@ -146,7 +148,7 @@ def max_value(state, alpha, beta, depth):
def min_value(state, alpha, beta, depth):
if cutoff_test(state, depth):
return eval_fn(state)
v = inf
v = np.inf
for a in game.actions(state):
v = min(v, max_value(game.result(state, a), alpha, beta, depth + 1))
if v <= alpha:
Expand All @@ -158,8 +160,8 @@ def min_value(state, alpha, beta, depth):
# The default test cuts off at depth d or at a terminal state
cutoff_test = (cutoff_test or (lambda state, depth: depth > d or game.terminal_test(state)))
eval_fn = eval_fn or (lambda state: game.utility(state, player))
best_score = -inf
beta = inf
best_score = -np.inf
beta = np.inf
best_action = None
for a in game.actions(state):
v = min_value(game.result(state, a), best_score, beta, 1)
Expand Down
30 changes: 12 additions & 18 deletions gui/romania_problem.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
from copy import deepcopy
from tkinter import *
import sys
import os.path
import math
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

from search import *
from search import breadth_first_tree_search as bfts, depth_first_tree_search as dfts, \
depth_first_graph_search as dfgs, breadth_first_graph_search as bfs, uniform_cost_search as ucs, \
astar_search as asts
from utils import PriorityQueue
from copy import deepcopy

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

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


def make_legend(map):

rect1 = map.create_rectangle(600, 100, 610, 110, fill="white")
text1 = map.create_text(615, 105, anchor=W, text="Un-explored")

Expand Down Expand Up @@ -325,13 +320,11 @@ def tree_search(problem):
display_current(node)
if counter % 3 == 1 and counter >= 0:
if problem.goal_test(node.state):

return node
frontier.extend(node.expand(problem))

display_frontier(frontier)
if counter % 3 == 2 and counter >= 0:

display_explored(node)
return None

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

# TODO:
# Remove redundant code.
# Make the interchangbility work between various algorithms at each step.
# Make the interchangeability work between various algorithms at each step.
def on_click():
"""
This function defines the action of the 'Next' button.
Expand All @@ -572,47 +565,47 @@ def on_click():
if "Breadth-First Tree Search" == algo.get():
node = breadth_first_tree_search(romania_problem)
if node is not None:
final_path = bfts(romania_problem).solution()
final_path = breadth_first_tree_search(romania_problem).solution()
final_path.append(start.get())
display_final(final_path)
next_button.config(state="disabled")
counter += 1
elif "Depth-First Tree Search" == algo.get():
node = depth_first_tree_search(romania_problem)
if node is not None:
final_path = dfts(romania_problem).solution()
final_path = depth_first_tree_search(romania_problem).solution()
final_path.append(start.get())
display_final(final_path)
next_button.config(state="disabled")
counter += 1
elif "Breadth-First Graph Search" == algo.get():
node = breadth_first_graph_search(romania_problem)
if node is not None:
final_path = bfs(romania_problem).solution()
final_path = breadth_first_graph_search(romania_problem).solution()
final_path.append(start.get())
display_final(final_path)
next_button.config(state="disabled")
counter += 1
elif "Depth-First Graph Search" == algo.get():
node = depth_first_graph_search(romania_problem)
if node is not None:
final_path = dfgs(romania_problem).solution()
final_path = depth_first_graph_search(romania_problem).solution()
final_path.append(start.get())
display_final(final_path)
next_button.config(state="disabled")
counter += 1
elif "Uniform Cost Search" == algo.get():
node = uniform_cost_search(romania_problem)
if node is not None:
final_path = ucs(romania_problem).solution()
final_path = uniform_cost_search(romania_problem).solution()
final_path.append(start.get())
display_final(final_path)
next_button.config(state="disabled")
counter += 1
elif "A* - Search" == algo.get():
node = astar_search(romania_problem)
if node is not None:
final_path = asts(romania_problem).solution()
final_path = astar_search(romania_problem).solution()
final_path.append(start.get())
display_final(final_path)
next_button.config(state="disabled")
Expand All @@ -626,6 +619,7 @@ def reset_map():
city_map.itemconfig(city_coord[city], fill="white")
next_button.config(state="normal")


# TODO: Add more search algorithms in the OptionMenu


Expand Down
Loading

0 comments on commit c587f2c

Please sign in to comment.