| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent df97b76 commit 64fa05b
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ | |||
| 3 | 3 | from utils import ( | |
| 4 | 4 | removeall, unique, product, argmax, argmax_random_tie, mean, isclose, | |
| 5 | 5 | dotproduct, vector_add, scalar_vector_product, weighted_sample_with_replacement, | |
| 6 | - weighted_sampler, num_or_str, normalize, clip, sigmoid, print_table, DataFile, Fig | ||
| 6 | + weighted_sampler, num_or_str, normalize, clip, sigmoid, print_table, DataFile | ||
| 7 | 7 | ) | |
| 8 | 8 | ||
| 9 | 9 | import copy | |
@@ -886,7 +886,11 @@ def T(attrname, branches): | |||
| 886 | 886 | for value, child in list(branches.items())) | |
| 887 | 887 | return DecisionFork(restaurant.attrnum(attrname), attrname, branches) | |
| 888 | 888 | ||
| 889 | - Fig[18, 2] = T('Patrons', | ||
| 889 | + """ [Figure 18.2] | ||
| 890 | + A decision tree for deciding whether to wait for a table at a hotel. | ||
| 891 | + """ | ||
| 892 | + | ||
| 893 | + waiting_decision_tree = T('Patrons', | ||
| 890 | 894 | {'None': 'No', 'Some': 'Yes', 'Full': | |
| 891 | 895 | T('WaitEstimate', | |
| 892 | 896 | {'>60': 'No', '0-10': 'Yes', | |
@@ -910,7 +914,7 @@ def SyntheticRestaurant(n=20): | |||
| 910 | 914 | "Generate a DataSet with n examples." | |
| 911 | 915 | def gen(): | |
| 912 | 916 | example = list(map(random.choice, restaurant.values)) | |
| 913 | - example[restaurant.target] = Fig[18, 2](example) | ||
| 917 | + example[restaurant.target] = waiting_decision_tree(example) | ||
| 914 | 918 | return example | |
| 915 | 919 | return RestaurantDataSet([gen() for i in range(n)]) | |
| 916 | 920 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,7 +33,7 @@ | |||
| 33 | 33 | ||
| 34 | 34 | from utils import ( | |
| 35 | 35 | removeall, unique, first, every, argmax, probability, num_or_str, | |
| 36 | - isnumber, issequence, Symbol, Expr, expr, subexpressions, implies, Fig | ||
| 36 | + isnumber, issequence, Symbol, Expr, expr, subexpressions, implies | ||
| 37 | 37 | ) | |
| 38 | 38 | import agents | |
| 39 | 39 | ||
@@ -499,7 +499,7 @@ def clauses_with_premise(self, p): | |||
| 499 | 499 | def pl_fc_entails(KB, q): | |
| 500 | 500 | """Use forward chaining to see if a PropDefiniteKB entails symbol q. | |
| 501 | 501 | [Fig. 7.15] | |
| 502 | - >>> pl_fc_entails(Fig[7,15], expr('Q')) | ||
| 502 | + >>> pl_fc_entails(horn_clauses_KB, expr('Q')) | ||
| 503 | 503 | True | |
| 504 | 504 | """ | |
| 505 | 505 | count = dict([(c, len(conjuncts(c.args[0]))) for c in KB.clauses | |
@@ -518,13 +518,18 @@ def pl_fc_entails(KB, q): | |||
| 518 | 518 | agenda.append(c.args[1]) | |
| 519 | 519 | return False | |
| 520 | 520 | ||
| 521 | - # Wumpus World example [Fig. 7.13] | ||
| 522 | - Fig[7, 13] = expr("(B11 <=> (P12 | P21)) & ~B11") | ||
| 521 | + """ [Figure 7.13] | ||
| 522 | + Simple inference in a wumpus world example | ||
| 523 | + """ | ||
| 524 | + wumpus_world_inference = expr("(B11 <=> (P12 | P21)) & ~B11") | ||
| 525 | + | ||
| 523 | 526 | ||
| 524 | - # Propositional Logic Forward Chaining example [Fig. 7.16] | ||
| 525 | - Fig[7, 15] = PropDefiniteKB() | ||
| 527 | + """ [Figure 7.16] | ||
| 528 | + Propositional Logic Forward Chaining example | ||
| 529 | + """ | ||
| 530 | + horn_clauses_KB = PropDefiniteKB() | ||
| 526 | 531 | for s in "P==>Q; (L&M)==>P; (B&L)==>M; (A&P)==>L; (A&B)==>L; A;B".split(';'): | |
| 527 | - Fig[7, 15].tell(expr(s)) | ||
| 532 | + horn_clauses_KB.tell(expr(s)) | ||
| 528 | 533 | ||
| 529 | 534 | # ______________________________________________________________________________ | |
| 530 | 535 | # DPLL-Satisfiable [Fig. 7.17] | |
@@ -690,7 +695,7 @@ def SAT_plan(init, transition, goal, t_max, SAT_solver=dpll_satisfiable): | |||
| 690 | 695 | def translate_to_SAT(init, transition, goal, time): | |
| 691 | 696 | clauses = [] | |
| 692 | 697 | states = [state for state in transition] | |
| 693 | - | ||
| 698 | + | ||
| 694 | 699 | #Symbol claiming state s at time t | |
| 695 | 700 | state_counter = itertools.count() | |
| 696 | 701 | for s in states: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,7 +6,7 @@ | |||
| 6 | 6 | dictionary of {state:number} pairs. We then define the value_iteration | |
| 7 | 7 | and policy_iteration algorithms.""" | |
| 8 | 8 | ||
| 9 | - from utils import argmax, vector_add, print_table, Fig | ||
| 9 | + from utils import argmax, vector_add, print_table | ||
| 10 | 10 | from grid import orientations, turn_right, turn_left | |
| 11 | 11 | ||
| 12 | 12 | import random | |
@@ -97,8 +97,10 @@ def to_arrows(self, policy): | |||
| 97 | 97 | dict([(s, chars[a]) for (s, a) in list(policy.items())])) | |
| 98 | 98 | ||
| 99 | 99 | # ______________________________________________________________________________ | |
| 100 | - | ||
| 101 | - Fig[17, 1] = GridMDP([[-0.04, -0.04, -0.04, +1], | ||
| 100 | + """ [Figure 17.1] | ||
| 101 | + A 4x3 grid environment that presents the agent with a sequential decision problem. | ||
| 102 | + """ | ||
| 103 | + sequential_decision_environment = GridMDP([[-0.04, -0.04, -0.04, +1], | ||
| 102 | 104 | [-0.04, None, -0.04, -1], | |
| 103 | 105 | [-0.04, -0.04, -0.04, -0.04]], | |
| 104 | 106 | terminals=[(3, 2), (3, 1)]) | |
@@ -163,17 +165,17 @@ def policy_evaluation(pi, U, mdp, k=20): | |||
| 163 | 165 | return U | |
| 164 | 166 | ||
| 165 | 167 | __doc__ += """ | |
| 166 | - >>> pi = best_policy(Fig[17,1], value_iteration(Fig[17,1], .01)) | ||
| 168 | + >>> pi = best_policy(sequential_decision_environment, value_iteration(sequential_decision_environment, .01)) | ||
| 167 | 169 | ||
| 168 | - >>> Fig[17,1].to_arrows(pi) | ||
| 170 | + >>> sequential_decision_environment.to_arrows(pi) | ||
| 169 | 171 | [['>', '>', '>', '.'], ['^', None, '^', '.'], ['^', '>', '^', '<']] | |
| 170 | 172 | ||
| 171 | - >>> print_table(Fig[17,1].to_arrows(pi)) | ||
| 173 | + >>> print_table(sequential_decision_environment.to_arrows(pi)) | ||
| 172 | 174 | > > > . | |
| 173 | 175 | ^ None ^ . | |
| 174 | 176 | ^ > ^ < | |
| 175 | 177 | ||
| 176 | - >>> print_table(Fig[17,1].to_arrows(policy_iteration(Fig[17,1]))) | ||
| 178 | + >>> print_table(sequential_decision_environment.to_arrows(policy_iteration(sequential_decision_environment))) | ||
| 177 | 179 | > > > . | |
| 178 | 180 | ^ None ^ . | |
| 179 | 181 | ^ > ^ < | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -700,7 +700,7 @@ def distance_to_node(n): | |||
| 700 | 700 | g.connect(node, neighbor, int(d)) | |
| 701 | 701 | return g | |
| 702 | 702 | ||
| 703 | - """ [Fig. 3.2] | ||
| 703 | + """ [Figure 3.2] | ||
| 704 | 704 | Simplified road map of Romania | |
| 705 | 705 | """ | |
| 706 | 706 | romania_map = UndirectedGraph(dict( | |
@@ -726,7 +726,7 @@ def distance_to_node(n): | |||
| 726 | 726 | Sibiu=(207, 457), Timisoara=(94, 410), Urziceni=(456, 350), | |
| 727 | 727 | Vaslui=(509, 444), Zerind=(108, 531)) | |
| 728 | 728 | ||
| 729 | - """ [Fig. 4.9] | ||
| 729 | + """ [Figure 4.9] | ||
| 730 | 730 | Eight possible states of the vacumm world | |
| 731 | 731 | Each state is represented as | |
| 732 | 732 | * "State of the left room" "State of the right room" "Room in which the agent is present" | |
@@ -750,9 +750,8 @@ def distance_to_node(n): | |||
| 750 | 750 | State_8 = dict(Suck = ['State_8', 'State_6'], Left = ['State_7']) | |
| 751 | 751 | )) | |
| 752 | 752 | ||
| 753 | - """ [Fig. 4.23] | ||
| 753 | + """ [Figure 4.23] | ||
| 754 | 754 | One-dimensional state space Graph | |
| 755 | - | ||
| 756 | 755 | """ | |
| 757 | 756 | one_dim_state_space = Graph(dict( | |
| 758 | 757 | State_1 = dict(Right = 'State_2'), | |
@@ -770,7 +769,9 @@ def distance_to_node(n): | |||
| 770 | 769 | State_5 = 4, | |
| 771 | 770 | State_6 = 3) | |
| 772 | 771 | ||
| 773 | - # Principal states and territories of Australia | ||
| 772 | + """ [Figure 6.1] | ||
| 773 | + Principal states and territories of Australia | ||
| 774 | + """ | ||
| 774 | 775 | australia_map = UndirectedGraph(dict( | |
| 775 | 776 | T=dict(), | |
| 776 | 777 | SA=dict(WA=1, NT=1, Q=1, NSW=1, V=1), | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | import pytest | |
| 2 | 2 | from logic import * | |
| 3 | - from utils import InfixOp, expr_handle_infix_ops, Fig, count, implies, equiv | ||
| 3 | + from utils import InfixOp, expr_handle_infix_ops, count, implies, equiv | ||
| 4 | 4 | ||
| 5 | 5 | ||
| 6 | 6 | def test_expr(): | |
@@ -109,15 +109,15 @@ def test_dpll(): | |||
| 109 | 109 | == {B: False, C: True, A: True, F: False, D: True, E: False}) | |
| 110 | 110 | assert dpll_satisfiable(A&~B) == {A: True, B: False} | |
| 111 | 111 | assert dpll_satisfiable(P&~P) == False | |
| 112 | - | ||
| 112 | + | ||
| 113 | 113 | ||
| 114 | 114 | def test_unify(): | |
| 115 | 115 | assert unify(x, x, {}) == {} | |
| 116 | 116 | assert unify(x, 3, {}) == {x: 3} | |
| 117 | 117 | ||
| 118 | 118 | def test_pl_fc_entails(): | |
| 119 | - assert pl_fc_entails(Fig[7,15], expr('Q')) | ||
| 120 | - assert not pl_fc_entails(Fig[7,15], expr('SomethingSilly')) | ||
| 119 | + assert pl_fc_entails(horn_clauses_KB, expr('Q')) | ||
| 120 | + assert not pl_fc_entails(horn_clauses_KB, expr('SomethingSilly')) | ||
| 121 | 121 | ||
| 122 | 122 | def test_tt_entails(): | |
| 123 | 123 | assert tt_entails(P & Q, Q) | |
@@ -146,7 +146,7 @@ def test_move_not_inwards(): | |||
| 146 | 146 | assert repr(move_not_inwards(~(~(A | ~B) | ~~C))) == '((A | ~B) & ~C)' | |
| 147 | 147 | ||
| 148 | 148 | def test_to_cnf(): | |
| 149 | - assert (repr(to_cnf(Fig[7, 13] & ~expr('~P12'))) == | ||
| 149 | + assert (repr(to_cnf(wumpus_world_inference & ~expr('~P12'))) == | ||
| 150 | 150 | "((~P12 | B11) & (~P21 | B11) & (P12 | P21 | ~B11) & ~B11 & P12)") | |
| 151 | 151 | assert repr(to_cnf((P&Q) | (~P & ~Q))) == '((~P | P) & (~Q | P) & (~P | Q) & (~Q | Q))' | |
| 152 | 152 | assert repr(to_cnf("B <=> (P1 | P2)")) == '((~P1 | B) & (~P2 | B) & (P1 | P2 | ~B))' | |
@@ -203,7 +203,7 @@ def test_SAT_plan(): | |||
| 203 | 203 | transition = {(0, 0):{'Right': (0, 1), 'Down': (1, 0)}, | |
| 204 | 204 | (0, 1):{'Left': (1, 0), 'Down': (1, 1)}, | |
| 205 | 205 | (1, 0):{'Right': (1, 0), 'Up': (1, 0), 'Left': (1, 0), 'Down': (1, 0)}, | |
| 206 | - (1, 1):{'Left': (1, 0), 'Up': (0, 1)}} | ||
| 206 | + (1, 1):{'Left': (1, 0), 'Up': (0, 1)}} | ||
| 207 | 207 | assert SAT_plan((0, 0), transition, (1, 1), 4) == ['Right', 'Down'] | |
| 208 | 208 | ||
| 209 | 209 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ | |||
| 2 | 2 | from mdp import * # noqa | |
| 3 | 3 | ||
| 4 | 4 | def test_value_iteration(): | |
| 5 | - assert value_iteration(Fig[17, 1], .01) == {(3, 2): 1.0, (3, 1): -1.0, | ||
| 5 | + assert value_iteration(sequential_decision_environment, .01) == {(3, 2): 1.0, (3, 1): -1.0, | ||
| 6 | 6 | (3, 0): 0.12958868267972745, (0, 1): 0.39810203830605462, | |
| 7 | 7 | (0, 2): 0.50928545646220924, (1, 0): 0.25348746162470537, | |
| 8 | 8 | (0, 0): 0.29543540628363629, (1, 2): 0.64958064617168676, | |
@@ -11,14 +11,14 @@ def test_value_iteration(): | |||
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | 13 | def test_policy_iteration(): | |
| 14 | - assert policy_iteration(Fig[17, 1]) == {(0, 0): (0, 1), (0, 1): (0, 1), (0, 2): (1, 0), | ||
| 14 | + assert policy_iteration(sequential_decision_environment) == {(0, 0): (0, 1), (0, 1): (0, 1), (0, 2): (1, 0), | ||
| 15 | 15 | (1, 0): (1, 0), (1, 2): (1, 0), | |
| 16 | 16 | (2, 0): (0, 1), (2, 1): (0, 1), (2, 2): (1, 0), | |
| 17 | 17 | (3, 0): (-1, 0), (3, 1): None, (3, 2): None} | |
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | 20 | def test_best_policy(): | |
| 21 | - pi = best_policy(Fig[17, 1], value_iteration(Fig[17, 1], .01)) | ||
| 22 | - assert Fig[17, 1].to_arrows(pi) == [['>', '>', '>', '.'], | ||
| 21 | + pi = best_policy(sequential_decision_environment, value_iteration(sequential_decision_environment, .01)) | ||
| 22 | + assert sequential_decision_environment.to_arrows(pi) == [['>', '>', '>', '.'], | ||
| 23 | 23 | ['^', None, '^', '.'], | |
| 24 | 24 | ['^', '>', '^', '<']] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -81,7 +81,7 @@ def shuffled(iterable): | |||
| 81 | 81 | "Randomly shuffle a copy of iterable." | |
| 82 | 82 | items = list(iterable) | |
| 83 | 83 | random.shuffle(items) | |
| 84 | - return items | ||
| 84 | + return items | ||
| 85 | 85 | ||
| 86 | 86 | ||
| 87 | 87 | ||
@@ -345,16 +345,16 @@ def unimplemented(): | |||
| 345 | 345 | # See https://docs.python.org/3/reference/expressions.html#operator-precedence | |
| 346 | 346 | # See https://docs.python.org/3/reference/datamodel.html#special-method-names | |
| 347 | 347 | ||
| 348 | - class Expr(object): | ||
| 348 | + class Expr(object): | ||
| 349 | 349 | """A mathematical expression with an operator and 0 or more arguments. | |
| 350 | 350 | op is a str like '+' or 'sin'; args are Expressions. | |
| 351 | 351 | Expr('x') or Symbol('x') creates a symbol (a nullary Expr). | |
| 352 | 352 | Expr('-', x) creates a unary; Expr('+', x, 1) creates a binary.""" | |
| 353 | - | ||
| 354 | - def __init__(self, op, *args): | ||
| 353 | + | ||
| 354 | + def __init__(self, op, *args): | ||
| 355 | 355 | self.op = str(op) | |
| 356 | 356 | self.args = args | |
| 357 | - | ||
| 357 | + | ||
| 358 | 358 | # Operator overloads | |
| 359 | 359 | def __neg__(self): return Expr('-', self) | |
| 360 | 360 | def __pos__(self): return Expr('+', self) | |
@@ -374,10 +374,10 @@ def __matmul__(self, rhs): return Expr('@', self, rhs) | |||
| 374 | 374 | ||
| 375 | 375 | def __or__(self, rhs): | |
| 376 | 376 | if isinstance(rhs, Expression) : | |
| 377 | - return Expr('|', self, rhs) | ||
| 377 | + return Expr('|', self, rhs) | ||
| 378 | 378 | else: | |
| 379 | 379 | return NotImplemented # So that InfixOp can handle it | |
| 380 | - | ||
| 380 | + | ||
| 381 | 381 | # Reverse operator overloads | |
| 382 | 382 | def __radd__(self, lhs): return Expr('+', lhs, self) | |
| 383 | 383 | def __rsub__(self, lhs): return Expr('-', lhs, self) | |
@@ -393,20 +393,20 @@ def __rlshift__(self, lhs): return Expr('<<', lhs, self) | |||
| 393 | 393 | def __rtruediv__(self, lhs): return Expr('/', lhs, self) | |
| 394 | 394 | def __rfloordiv__(self, lhs): return Expr('//', lhs, self) | |
| 395 | 395 | def __rmatmul__(self, lhs): return Expr('@', lhs, self) | |
| 396 | - | ||
| 397 | - def __call__(self, *args): | ||
| 396 | + | ||
| 397 | + def __call__(self, *args): | ||
| 398 | 398 | "Call: if 'f' is a Symbol, then f(0) == Expr('f', 0)." | |
| 399 | 399 | return Expr(self.op, *args) | |
| 400 | 400 | ||
| 401 | 401 | # Equality and repr | |
| 402 | - def __eq__(self, other): | ||
| 402 | + def __eq__(self, other): | ||
| 403 | 403 | "'x == y' evaluates to True or False; does not build an Expr." | |
| 404 | - return (isinstance(other, Expr) | ||
| 405 | - and self.op == other.op | ||
| 404 | + return (isinstance(other, Expr) | ||
| 405 | + and self.op == other.op | ||
| 406 | 406 | and self.args == other.args) | |
| 407 | - | ||
| 407 | + | ||
| 408 | 408 | def __hash__(self): return hash(self.op) ^ hash(self.args) | |
| 409 | - | ||
| 409 | + | ||
| 410 | 410 | def __repr__(self): | |
| 411 | 411 | op = self.op | |
| 412 | 412 | args = [str(arg) for arg in self.args] | |
@@ -450,7 +450,7 @@ def arity(expression): | |||
| 450 | 450 | ||
| 451 | 451 | class InfixOp: | |
| 452 | 452 | """Allow 'P |implies| Q, where P, Q are Exprs and implies is an InfixOp.""" | |
| 453 | - def __init__(self, op, lhs=None): self.op, self.lhs = op, lhs | ||
| 453 | + def __init__(self, op, lhs=None): self.op, self.lhs = op, lhs | ||
| 454 | 454 | def __call__(self, lhs, rhs): return Expr(self.op, lhs, rhs) | |
| 455 | 455 | def __or__(self, rhs): return Expr(self.op, self.lhs, rhs) | |
| 456 | 456 | def __ror__(self, lhs): return InfixOp(self.op, lhs) | |
@@ -489,7 +489,7 @@ class defaultkeydict(collections.defaultdict): | |||
| 489 | 489 | def __missing__(self, key): | |
| 490 | 490 | self[key] = result = self.default_factory(key) | |
| 491 | 491 | return result | |
| 492 | - | ||
| 492 | + | ||
| 493 | 493 | ||
| 494 | 494 | # ______________________________________________________________________________ | |
| 495 | 495 | # Queues: Stack, FIFOQueue, PriorityQueue | |
@@ -591,9 +591,3 @@ def __delitem__(self, key): | |||
| 591 | 591 | for i, (value, item) in enumerate(self.A): | |
| 592 | 592 | if item == key: | |
| 593 | 593 | self.A.pop(i) | |
| 594 | - | ||
| 595 | - # Fig: The idea is we can define things like Fig[3,10] = ... | ||
| 596 | - # TODO: However, this is deprecated, let's remove it, | ||
| 597 | - # and instead have a comment like # Figure 3.10 | ||
| 598 | - | ||
| 599 | - Fig = {} | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments