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

Skip to content

Commit 829fed5

Browse files
dmeoliantmarakis
authored andcommitted
added ForwardPlan, BackwardPlan, SATPlan and tests & fixed cascade_distribution doctest (aimacode#1110)
* 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
1 parent 323ddb7 commit 829fed5

File tree

8 files changed

+1108
-742
lines changed

8 files changed

+1108
-742
lines changed

logic.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@
3030
unify Do unification of two FOL sentences
3131
diff, simp Symbolic differentiation and simplification
3232
"""
33+
import itertools
34+
import random
35+
from collections import defaultdict
36+
37+
from agents import Agent, Glitter, Bump, Stench, Breeze, Scream
3338
from csp import parse_neighbors, UniversalDict
39+
from search import astar_search, PlanRoute
3440
from utils import (
3541
removeall, unique, first, argmax, probability,
3642
isnumber, issequence, Expr, expr, subexpressions
3743
)
38-
from agents import Agent, Glitter, Bump, Stench, Breeze, Scream
39-
from search import astar_search, PlanRoute
40-
41-
import itertools
42-
import random
43-
from collections import defaultdict
4444

4545

4646
# ______________________________________________________________________________
@@ -195,6 +195,7 @@ def parse_definite_clause(s):
195195
# Useful constant Exprs used in examples and code:
196196
A, B, C, D, E, F, G, P, Q, a, x, y, z, u = map(Expr, 'ABCDEFGPQaxyzu')
197197

198+
198199
# ______________________________________________________________________________
199200

200201

@@ -504,9 +505,7 @@ def pl_resolve(ci, cj):
504505
for di in disjuncts(ci):
505506
for dj in disjuncts(cj):
506507
if di == ~dj or ~di == dj:
507-
dnew = unique(removeall(di, disjuncts(ci)) +
508-
removeall(dj, disjuncts(cj)))
509-
clauses.append(associate('|', dnew))
508+
clauses.append(associate('|', unique(removeall(di, disjuncts(ci)) + removeall(dj, disjuncts(cj)))))
510509
return clauses
511510

512511

@@ -1102,8 +1101,7 @@ def set_orientation(self, orientation):
11021101
self.orientation = orientation
11031102

11041103
def __eq__(self, other):
1105-
if other.get_location() == self.get_location() and \
1106-
other.get_orientation() == self.get_orientation():
1104+
if other.get_location() == self.get_location() and other.get_orientation() == self.get_orientation():
11071105
return True
11081106
else:
11091107
return False
@@ -1246,7 +1244,7 @@ def SAT_plan(init, transition, goal, t_max, SAT_solver=dpll_satisfiable):
12461244
"""Converts a planning problem to Satisfaction problem by translating it to a cnf sentence.
12471245
[Figure 7.22]
12481246
>>> transition = {'A': {'Left': 'A', 'Right': 'B'}, 'B': {'Left': 'A', 'Right': 'C'}, 'C': {'Left': 'B', 'Right': 'C'}}
1249-
>>> SAT_plan('A', transition, 'C', 2) is None
1247+
>>> SAT_plan('A', transition, 'C', 1) is None
12501248
True
12511249
"""
12521250

@@ -1265,7 +1263,9 @@ def translate_to_SAT(init, transition, goal, time):
12651263
clauses.append(state_sym[init, 0])
12661264

12671265
# Add goal state axiom
1268-
clauses.append(state_sym[goal, time])
1266+
clauses.append(state_sym[first(clause[0] for clause in state_sym
1267+
if set(conjuncts(clause[0])).issuperset(conjuncts(goal))), time]) \
1268+
if isinstance(goal, Expr) else clauses.append(state_sym[goal, time])
12691269

12701270
# All possible transitions
12711271
transition_counter = itertools.count()
@@ -1274,8 +1274,7 @@ def translate_to_SAT(init, transition, goal, time):
12741274
s_ = transition[s][action]
12751275
for t in range(time):
12761276
# Action 'action' taken from state 's' at time 't' to reach 's_'
1277-
action_sym[s, action, t] = Expr(
1278-
"Transition_{}".format(next(transition_counter)))
1277+
action_sym[s, action, t] = Expr("Transition_{}".format(next(transition_counter)))
12791278

12801279
# Change the state from s to s_
12811280
clauses.append(action_sym[s, action, t] | '==>' | state_sym[s, t])
@@ -1314,7 +1313,7 @@ def extract_solution(model):
13141313
return [action for s, action, time in true_transitions]
13151314

13161315
# Body of SAT_plan algorithm
1317-
for t in range(t_max):
1316+
for t in range(t_max + 1):
13181317
# dictionaries to help extract the solution from model
13191318
state_sym = {}
13201319
action_sym = {}
@@ -1416,6 +1415,7 @@ def subst(s, x):
14161415
else:
14171416
return Expr(x.op, *[subst(s, arg) for arg in x.args])
14181417

1418+
14191419
def cascade_substitution(s):
14201420
"""This method allows to return a correct unifier in normal form
14211421
and perform a cascade substitution to s.
@@ -1426,24 +1426,25 @@ def cascade_substitution(s):
14261426
This issue fix: https://github.com/aimacode/aima-python/issues/1053
14271427
unify(expr('P(A, x, F(G(y)))'), expr('P(z, F(z), F(u))'))
14281428
must return {z: A, x: F(A), u: G(y)} and not {z: A, x: F(z), u: G(y)}
1429-
1430-
>>> s = {x: y, y: G(z)}
1431-
>>> cascade_substitution(s)
1432-
>>> print(s)
1433-
{x: G(z), y: G(z)}
1434-
1429+
14351430
Parameters
14361431
----------
14371432
s : Dictionary
1438-
This contain a substution
1433+
This contain a substitution
1434+
1435+
>>> s = {x: y, y: G(z)}
1436+
>>> cascade_substitution(s)
1437+
>>> s == {x: G(z), y: G(z)}
1438+
True
14391439
"""
14401440

14411441
for x in s:
14421442
s[x] = subst(s, s.get(x))
14431443
if isinstance(s.get(x), Expr) and not is_variable(s.get(x)):
1444-
# Ensure Function Terms are correct updates by passing over them again.
1444+
# Ensure Function Terms are correct updates by passing over them again.
14451445
s[x] = subst(s, s.get(x))
14461446

1447+
14471448
def standardize_variables(sentence, dic=None):
14481449
"""Replace all the variables in sentence with new variables."""
14491450
if dic is None:

mdp4e.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,19 +530,19 @@ def double_tennis_problem():
530530
Example:
531531
>>> from planning import *
532532
>>> dtp = double_tennis_problem()
533-
>>> goal_test(dtp.goals, dtp.init)
533+
>>> goal_test(dtp.goals, dtp.initial)
534534
False
535535
>>> dtp.act(expr('Go(A, RightBaseLine, LeftBaseLine)'))
536536
>>> dtp.act(expr('Hit(A, Ball, RightBaseLine)'))
537-
>>> goal_test(dtp.goals, dtp.init)
537+
>>> goal_test(dtp.goals, dtp.initial)
538538
False
539539
>>> dtp.act(expr('Go(A, LeftNet, RightBaseLine)'))
540-
>>> goal_test(dtp.goals, dtp.init)
540+
>>> goal_test(dtp.goals, dtp.initial)
541541
True
542542
"""
543543

544544
return PlanningProblem(
545-
init='At(A, LeftBaseLine) & At(B, RightNet) & Approaching(Ball, RightBaseLine) & Partner(A, B) & Partner(B, A)',
545+
initial='At(A, LeftBaseLine) & At(B, RightNet) & Approaching(Ball, RightBaseLine) & Partner(A, B) & Partner(B, A)',
546546
goals='Returned(Ball) & At(a, LeftNet) & At(a, RightNet)',
547547
actions=[Action('Hit(actor, Ball, loc)',
548548
precond='Approaching(Ball, loc) & At(actor, loc)',

0 commit comments

Comments
 (0)