Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f129c5c

Browse files
reachtarunherenorvig
authored andcommitted
Additional Changes for Python3 Porting (aimacode#222)
* Replaced mean with standard lib function introduced in 3.4 * #TODO Replaced every with all * Proper names for variables introduced in 2to3 conversion.
1 parent 5882de3 commit f129c5c

File tree

8 files changed

+18
-29
lines changed

8 files changed

+18
-29
lines changed

agents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
#
3636
# Speed control in GUI does not have any effect -- fix it.
3737

38-
from utils import mean
3938
from grid import distance2
39+
from statistics import mean
4040

4141
import random
4242
import copy

csp.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""CSP (Constraint Satisfaction Problems) problems and solvers. (Chapter 6)."""
22

3-
from utils import count, first, every, argmin_random_tie
3+
from utils import count, first, argmin_random_tie
44
import search
55

66
from collections import defaultdict
@@ -98,16 +98,16 @@ def actions(self, state):
9898
return [(var, val) for val in self.domains[var]
9999
if self.nconflicts(var, val, assignment) == 0]
100100

101-
def result(self, state, xxx_todo_changeme):
101+
def result(self, state, action):
102102
"Perform an action and return the new state."
103-
(var, val) = xxx_todo_changeme
103+
(var, val) = action
104104
return state + ((var, val),)
105105

106106
def goal_test(self, state):
107107
"The goal is to assign all variables, with all constraints satisfied."
108108
assignment = dict(state)
109109
return (len(assignment) == len(self.variables) and
110-
every(lambda variables: self.nconflicts(variables, assignment[variables], assignment) == 0, self.variables))
110+
all(self.nconflicts(variables, assignment[variables], assignment) == 0 for variables in self.variables))
111111

112112
# These are for constraint propagation
113113

@@ -177,8 +177,7 @@ def revise(csp, Xi, Xj, removals):
177177
revised = False
178178
for x in csp.curr_domains[Xi][:]:
179179
# If Xi=x conflicts with Xj=y for every possible y, eliminate Xi=x
180-
if every(lambda y: not csp.constraints(Xi, x, Xj, y),
181-
csp.curr_domains[Xj]):
180+
if all(not csp.constraints(Xi, x, Xj, y) for y in csp.curr_domains[Xj]):
182181
csp.prune(Xi, x, removals)
183182
revised = True
184183
return revised

games.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ def compute_utility(self, board, move, player):
289289
else:
290290
return 0
291291

292-
def k_in_row(self, board, move, player, xxx_todo_changeme):
292+
def k_in_row(self, board, move, player, delta_x_y):
293293
"Return true if there is a line through move on board for player."
294-
(delta_x, delta_y) = xxx_todo_changeme
294+
(delta_x, delta_y) = delta_x_y
295295
x, y = move
296296
n = 0 # n is number of moves in row
297297
while board.get((x, y)) == player:

learning.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Learn to estimate functions from examples. (Chapters 18-20)"""
22

33
from utils import (
4-
removeall, unique, product, argmax, argmax_random_tie, mean, isclose,
4+
removeall, unique, product, argmax, argmax_random_tie, isclose,
55
dotproduct, vector_add, scalar_vector_product, weighted_sample_with_replacement,
66
weighted_sampler, num_or_str, normalize, clip, sigmoid, print_table, DataFile
77
)
@@ -10,6 +10,8 @@
1010
import heapq
1111
import math
1212
import random
13+
14+
from statistics import mean
1315
from collections import defaultdict
1416

1517
# ______________________________________________________________________________

logic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"""
3333

3434
from utils import (
35-
removeall, unique, first, every, argmax, probability, num_or_str,
35+
removeall, unique, first, argmax, probability, num_or_str,
3636
isnumber, issequence, Symbol, Expr, expr, subexpressions
3737
)
3838
import agents
@@ -168,7 +168,7 @@ def is_definite_clause(s):
168168
elif s.op == '==>':
169169
antecedent, consequent = s.args
170170
return (is_symbol(consequent.op) and
171-
every(lambda arg: is_symbol(arg.op), conjuncts(antecedent)))
171+
all(is_symbol(arg.op) for arg in conjuncts(antecedent)))
172172
else:
173173
return False
174174

probability.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33

44
from utils import (
5-
product, every, argmax, element_wise_product, matrix_multiplication,
5+
product, argmax, element_wise_product, matrix_multiplication,
66
vector_to_diagonal, vector_add, scalar_vector_product, inverse_matrix,
77
weighted_sample_with_replacement, rounder, isclose, probability, normalize
88
)
@@ -176,7 +176,7 @@ def add(self, node_spec):
176176
net, and its variable must not."""
177177
node = BayesNode(*node_spec)
178178
assert node.variable not in self.variables
179-
assert every(lambda parent: parent in self.variables, node.parents)
179+
assert all((parent in self.variables) for parent in node.parents)
180180
self.nodes.append(node)
181181
self.variables.append(node.variable)
182182
for parent in node.parents:
@@ -242,7 +242,7 @@ def __init__(self, X, parents, cpt):
242242
assert isinstance(cpt, dict)
243243
for vs, p in list(cpt.items()):
244244
assert isinstance(vs, tuple) and len(vs) == len(parents)
245-
assert every(lambda v: isinstance(v, bool), vs)
245+
assert all(isinstance(v, bool) for v in vs)
246246
assert 0 <= p <= 1
247247

248248
self.variable = X

tests/test_logic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ def check_SAT(clauses, single_solution = {}):
179179
# Sometimes WalkSat may run out of flips before finding a solution
180180
soln = WalkSAT(clauses)
181181
if soln:
182-
assert every(lambda x: pl_true(x, soln), clauses)
182+
assert all(pl_true(x, soln) for x in clauses)
183183
if single_solution: #Cross check the solution if only one exists
184-
assert every(lambda x: pl_true(x, single_solution), clauses)
184+
assert all(pl_true(x, single_solution) for x in clauses)
185185
assert soln == single_solution
186186
# Test WalkSat for problems with solution
187187
check_SAT([A & B, A & C])

utils.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@ def first(iterable, default=None):
5050
except TypeError:
5151
return next(iterable, default)
5252

53-
54-
def every(predicate, seq): # TODO: replace with all
55-
"""True if every element of seq satisfies predicate."""
56-
57-
return all(predicate(x) for x in seq)
58-
59-
6053
def is_in(elt, seq):
6154
"""Similar to (elt in seq), but compares with 'is', not '=='."""
6255
return any(x is elt for x in seq)
@@ -106,11 +99,6 @@ def histogram(values, mode=0, bin_function=None):
10699
else:
107100
return sorted(bins.items())
108101

109-
def mean(numbers):
110-
"The mean or average of numbers."
111-
numbers = sequence(numbers)
112-
return sum(numbers) / len(numbers)
113-
114102

115103
def dotproduct(X, Y):
116104
"""Return the sum of the element-wise product of vectors X and Y."""

0 commit comments

Comments
 (0)