| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9054eef commit a6e3192
13 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -329,7 +329,7 @@ class Direction(): | |||
| 329 | 329 | To change directions: | |
| 330 | 330 | d = d + "right" or d = d + Direction.R #Both do the same thing | |
| 331 | 331 | Note that the argument to __add__ must be a string and not a Direction object. | |
| 332 | - Also, it (the argument) can only be right or left. ''' | ||
| 332 | + Also, it (the argument) can only be right or left.''' | ||
| 333 | 333 | ||
| 334 | 334 | R = "right" | |
| 335 | 335 | L = "left" | |
@@ -428,8 +428,8 @@ def default_location(self, thing): | |||
| 428 | 428 | return (random.choice(self.width), random.choice(self.height)) | |
| 429 | 429 | ||
| 430 | 430 | def move_to(self, thing, destination): | |
| 431 | - '''Move a thing to a new location. Returns True on success or False if there is an Obstacle | ||
| 432 | - If thing is grabbing anything, they move with him ''' | ||
| 431 | + '''Move a thing to a new location. Returns True on success or False if there is an Obstacle. | ||
| 432 | + If thing is holding anything, they move with him.''' | ||
| 433 | 433 | thing.bump = self.some_things_at(destination, Obstacle) | |
| 434 | 434 | if not thing.bump: | |
| 435 | 435 | thing.location = destination | |
@@ -451,7 +451,7 @@ def move_to(self, thing, destination): | |||
| 451 | 451 | def add_thing(self, thing, location=(1, 1), exclude_duplicate_class_items=False): | |
| 452 | 452 | '''Adds things to the world. | |
| 453 | 453 | If (exclude_duplicate_class_items) then the item won't be added if the location | |
| 454 | - has at least one item of the same class''' | ||
| 454 | + has at least one item of the same class.''' | ||
| 455 | 455 | if (self.is_inbounds(location)): | |
| 456 | 456 | if (exclude_duplicate_class_items and | |
| 457 | 457 | any(isinstance(t, thing.__class__) for t in self.list_things_at(location))): | |
@@ -526,7 +526,7 @@ class Wall(Obstacle): | |||
| 526 | 526 | # Continuous environment | |
| 527 | 527 | ||
| 528 | 528 | class ContinuousWorld(Environment): | |
| 529 | - """ Model for Continuous World. """ | ||
| 529 | + """ Model for Continuous World.""" | ||
| 530 | 530 | def __init__(self, width=10, height=10): | |
| 531 | 531 | super(ContinuousWorld, self).__init__() | |
| 532 | 532 | self.width = width | |
@@ -538,7 +538,7 @@ def add_obstacle(self, coordinates): | |||
| 538 | 538 | ||
| 539 | 539 | class PolygonObstacle(Obstacle): | |
| 540 | 540 | def __init__(self, coordinates): | |
| 541 | - """ Coordinates is a list of tuples. """ | ||
| 541 | + """ Coordinates is a list of tuples.""" | ||
| 542 | 542 | super(PolygonObstacle, self).__init__() | |
| 543 | 543 | self.coordinates = coordinates | |
| 544 | 544 | ||
@@ -715,7 +715,7 @@ def init_world(self, program): | |||
| 715 | 715 | self.add_thing(Explorer(program), (1, 1), True) | |
| 716 | 716 | ||
| 717 | 717 | def get_world(self, show_walls=True): | |
| 718 | - '''returns the items in the world''' | ||
| 718 | + '''Returns the items in the world''' | ||
| 719 | 719 | result = [] | |
| 720 | 720 | x_start, y_start = (0, 0) if show_walls else (1, 1) | |
| 721 | 721 | x_end, y_end = (self.width, self.height) if show_walls else (self.width - 1, self.height - 1) | |
@@ -765,8 +765,8 @@ def percept(self, agent): | |||
| 765 | 765 | return result | |
| 766 | 766 | ||
| 767 | 767 | def execute_action(self, agent, action): | |
| 768 | - '''Modify the state of the environment based on the agent's actions | ||
| 769 | - Performance score taken directly out of the book''' | ||
| 768 | + '''Modify the state of the environment based on the agent's actions. | ||
| 769 | + Performance score taken directly out of the book.''' | ||
| 770 | 770 | ||
| 771 | 771 | if isinstance(agent, Explorer) and self.in_danger(agent): | |
| 772 | 772 | return | |
@@ -818,7 +818,7 @@ def in_danger(self, agent): | |||
| 818 | 818 | ||
| 819 | 819 | def is_done(self): | |
| 820 | 820 | '''The game is over when the Explorer is killed | |
| 821 | - or if he climbs out of the cave only at (1,1)''' | ||
| 821 | + or if he climbs out of the cave only at (1,1).''' | ||
| 822 | 822 | explorer = [agent for agent in self.agents if isinstance(agent, Explorer) ] | |
| 823 | 823 | if len(explorer): | |
| 824 | 824 | if explorer[0].alive: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,8 +12,8 @@ | |||
| 12 | 12 | class Canvas: | |
| 13 | 13 | """Inherit from this class to manage the HTML canvas element in jupyter notebooks. | |
| 14 | 14 | To create an object of this class any_name_xyz = Canvas("any_name_xyz") | |
| 15 | - The first argument given must be the name of the object being create | ||
| 16 | - IPython must be able to refernce the variable name that is being passed | ||
| 15 | + The first argument given must be the name of the object being created. | ||
| 16 | + IPython must be able to refernce the variable name that is being passed. | ||
| 17 | 17 | """ | |
| 18 | 18 | ||
| 19 | 19 | def __init__(self, varname, id=None, width=800, height=600): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | # OK, the following are not as widely useful utilities as some of the other | |
| 2 | 2 | # functions here, but they do show up wherever we have 2D grids: Wumpus and | |
| 3 | - # Vacuum worlds, TicTacToe and Checkers, and markov decision Processes. | ||
| 3 | + # Vacuum worlds, TicTacToe and Checkers, and Markov Decision Processes. | ||
| 4 | 4 | # __________________________________________________________________________ | |
| 5 | 5 | import math | |
| 6 | 6 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -499,9 +499,9 @@ def __init__(self, weights=None, inputs=None): | |||
| 499 | 499 | ||
| 500 | 500 | def network(input_units, hidden_layer_sizes, output_units): | |
| 501 | 501 | """ | |
| 502 | - Create of Directed Acyclic Network of given number layers | ||
| 502 | + Create Directed Acyclic Network of given number layers. | ||
| 503 | 503 | hidden_layers_sizes : list number of neuron units in each hidden layer | |
| 504 | - excluding input and output layers. | ||
| 504 | + excluding input and output layers | ||
| 505 | 505 | """ | |
| 506 | 506 | # Check for PerceptronLearner | |
| 507 | 507 | if hidden_layer_sizes: | |
@@ -523,7 +523,7 @@ def network(input_units, hidden_layer_sizes, output_units): | |||
| 523 | 523 | ||
| 524 | 524 | ||
| 525 | 525 | def BackPropagationLearner(dataset, net, learning_rate, epoches): | |
| 526 | - "[Figure 18.23] The back-propagation algorithm for multilayer network" | ||
| 526 | + """[Figure 18.23] The back-propagation algorithm for multilayer network""" | ||
| 527 | 527 | # Initialise weights | |
| 528 | 528 | for layer in net: | |
| 529 | 529 | for node in layer: | |
@@ -826,7 +826,7 @@ def cross_validation_wrapper(learner, dataset, k=10, trials=1): | |||
| 826 | 826 | """ | |
| 827 | 827 | Fig 18.8 | |
| 828 | 828 | Return the optimal value of size having minimum error | |
| 829 | - on validataion set | ||
| 829 | + on validataion set. | ||
| 830 | 830 | err_train: a training error array, indexed by size | |
| 831 | 831 | err_val: a validataion error array, indexed by size | |
| 832 | 832 | """ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -670,7 +670,7 @@ def sat_count(sym): | |||
| 670 | 670 | ||
| 671 | 671 | class HybridWumpusAgent(agents.Agent): | |
| 672 | 672 | ||
| 673 | - "An agent for the wumpus world that does logical inference. [Figure 7.20]""" | ||
| 673 | + """An agent for the wumpus world that does logical inference. [Figure 7.20]""" | ||
| 674 | 674 | ||
| 675 | 675 | def __init__(self): | |
| 676 | 676 | raise NotImplementedError | |
@@ -789,7 +789,7 @@ def unify(x, y, s): | |||
| 789 | 789 | ||
| 790 | 790 | ||
| 791 | 791 | def is_variable(x): | |
| 792 | - "A variable is an Expr with no args and a lowercase symbol as the op." | ||
| 792 | + """A variable is an Expr with no args and a lowercase symbol as the op.""" | ||
| 793 | 793 | return isinstance(x, Expr) and not x.args and x.op[0].islower() | |
| 794 | 794 | ||
| 795 | 795 | ||
@@ -819,7 +819,7 @@ def occur_check(var, x, s): | |||
| 819 | 819 | ||
| 820 | 820 | ||
| 821 | 821 | def extend(s, var, val): | |
| 822 | - "Copy the substitution s and extend it by setting var to val; return copy." | ||
| 822 | + """Copy the substitution s and extend it by setting var to val; return copy.""" | ||
| 823 | 823 | s2 = s.copy() | |
| 824 | 824 | s2[var] = val | |
| 825 | 825 | return s2 | |
@@ -932,7 +932,7 @@ def fetch_rules_for_goal(self, goal): | |||
| 932 | 932 | ||
| 933 | 933 | def fol_bc_ask(KB, query): | |
| 934 | 934 | """A simple backward-chaining algorithm for first-order logic. [Figure 9.6] | |
| 935 | - KB should be an instance of FolKB, and query an atomic sentence. """ | ||
| 935 | + KB should be an instance of FolKB, and query an atomic sentence.""" | ||
| 936 | 936 | return fol_bc_or(KB, query, {}) | |
| 937 | 937 | ||
| 938 | 938 | ||
@@ -995,7 +995,7 @@ def diff(y, x): | |||
| 995 | 995 | ||
| 996 | 996 | ||
| 997 | 997 | def simp(x): | |
| 998 | - "Simplify the expression x." | ||
| 998 | + """Simplify the expression x.""" | ||
| 999 | 999 | if isnumber(x) or not x.args: | |
| 1000 | 1000 | return x | |
| 1001 | 1001 | args = list(map(simp, x.args)) | |
@@ -1058,5 +1058,5 @@ def simp(x): | |||
| 1058 | 1058 | ||
| 1059 | 1059 | ||
| 1060 | 1060 | def d(y, x): | |
| 1061 | - "Differentiate and then simplify." | ||
| 1061 | + """Differentiate and then simplify.""" | ||
| 1062 | 1062 | return simp(diff(y, x)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -80,7 +80,7 @@ def T(self, state, action): | |||
| 80 | 80 | (0.1, self.go(state, turn_left(action)))] | |
| 81 | 81 | ||
| 82 | 82 | def go(self, state, direction): | |
| 83 | - "Return the state that results from going in this direction." | ||
| 83 | + """Return the state that results from going in this direction.""" | ||
| 84 | 84 | state1 = vector_add(state, direction) | |
| 85 | 85 | return state1 if state1 in self.states else state | |
| 86 | 86 | ||
@@ -110,7 +110,7 @@ def to_arrows(self, policy): | |||
| 110 | 110 | ||
| 111 | 111 | ||
| 112 | 112 | def value_iteration(mdp, epsilon=0.001): | |
| 113 | - "Solving an MDP by value iteration. [Figure 17.4]" | ||
| 113 | + """Solving an MDP by value iteration. [Figure 17.4]""" | ||
| 114 | 114 | U1 = {s: 0 for s in mdp.states} | |
| 115 | 115 | R, T, gamma = mdp.R, mdp.T, mdp.gamma | |
| 116 | 116 | while True: | |
@@ -134,14 +134,14 @@ def best_policy(mdp, U): | |||
| 134 | 134 | ||
| 135 | 135 | ||
| 136 | 136 | def expected_utility(a, s, U, mdp): | |
| 137 | - "The expected utility of doing a in state s, according to the MDP and U." | ||
| 137 | + """The expected utility of doing a in state s, according to the MDP and U.""" | ||
| 138 | 138 | return sum([p * U[s1] for (p, s1) in mdp.T(s, a)]) | |
| 139 | 139 | ||
| 140 | 140 | # ______________________________________________________________________________ | |
| 141 | 141 | ||
| 142 | 142 | ||
| 143 | 143 | def policy_iteration(mdp): | |
| 144 | - "Solve an MDP by policy iteration [Figure 17.7]" | ||
| 144 | + """Solve an MDP by policy iteration [Figure 17.7]""" | ||
| 145 | 145 | U = {s: 0 for s in mdp.states} | |
| 146 | 146 | pi = {s: random.choice(mdp.actions(s)) for s in mdp.states} | |
| 147 | 147 | while True: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,7 +34,7 @@ def Lexicon(**rules): | |||
| 34 | 34 | class Grammar: | |
| 35 | 35 | ||
| 36 | 36 | def __init__(self, name, rules, lexicon): | |
| 37 | - "A grammar has a set of rules and a lexicon." | ||
| 37 | + """A grammar has a set of rules and a lexicon.""" | ||
| 38 | 38 | self.name = name | |
| 39 | 39 | self.rules = rules | |
| 40 | 40 | self.lexicon = lexicon | |
@@ -44,11 +44,11 @@ def __init__(self, name, rules, lexicon): | |||
| 44 | 44 | self.categories[word].append(lhs) | |
| 45 | 45 | ||
| 46 | 46 | def rewrites_for(self, cat): | |
| 47 | - "Return a sequence of possible rhs's that cat can be rewritten as." | ||
| 47 | + """Return a sequence of possible rhs's that cat can be rewritten as.""" | ||
| 48 | 48 | return self.rules.get(cat, ()) | |
| 49 | 49 | ||
| 50 | 50 | def isa(self, word, cat): | |
| 51 | - "Return True iff word is of category cat" | ||
| 51 | + """Return True iff word is of category cat""" | ||
| 52 | 52 | return cat in self.categories[word] | |
| 53 | 53 | ||
| 54 | 54 | def __repr__(self): | |
@@ -293,8 +293,8 @@ def expand_pages( pages ): | |||
| 293 | 293 | return expanded | |
| 294 | 294 | ||
| 295 | 295 | def relevant_pages(query): | |
| 296 | - """relevant pages are pages that contain the query in its entireity. | ||
| 297 | - If a page's content contains the query it is returned by the function""" | ||
| 296 | + """Relevant pages are pages that contain the query in its entireity. | ||
| 297 | + If a page's content contains the query it is returned by the function.""" | ||
| 298 | 298 | relevant = {} | |
| 299 | 299 | print("pagesContent in function: ", pagesContent) | |
| 300 | 300 | for addr, page in pagesIndex.items(): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,9 +7,9 @@ | |||
| 7 | 7 | ||
| 8 | 8 | class PDLL: | |
| 9 | 9 | """ | |
| 10 | - PDLL used to define a search problem | ||
| 11 | - It stores states in a knowledge base consisting of first order logic statements | ||
| 12 | - The conjunction of these logical statements completely define a state | ||
| 10 | + PDLL used to define a search problem. | ||
| 11 | + It stores states in a knowledge base consisting of first order logic statements. | ||
| 12 | + The conjunction of these logical statements completely defines a state. | ||
| 13 | 13 | """ | |
| 14 | 14 | ||
| 15 | 15 | def __init__(self, initial_state, actions, goal_test): | |
@@ -22,7 +22,7 @@ def goal_test(self): | |||
| 22 | 22 | ||
| 23 | 23 | def act(self, action): | |
| 24 | 24 | """ | |
| 25 | - Performs the action given as argument | ||
| 25 | + Performs the action given as argument. | ||
| 26 | 26 | Note that action is an Expr like expr('Remove(Glass, Table)') or expr('Eat(Sandwich)') | |
| 27 | 27 | """ | |
| 28 | 28 | action_name = action.op | |
@@ -36,10 +36,10 @@ def act(self, action): | |||
| 36 | 36 | ||
| 37 | 37 | class Action: | |
| 38 | 38 | """ | |
| 39 | - Defines an action schema using preconditions and effects | ||
| 40 | - Use this to describe actions in PDDL | ||
| 41 | - action is an Expr where variables are given as arguments(args) | ||
| 42 | - Precondition and effect are both lists with positive and negated literals | ||
| 39 | + Defines an action schema using preconditions and effects. | ||
| 40 | + Use this to describe actions in PDDL. | ||
| 41 | + action is an Expr where variables are given as arguments(args). | ||
| 42 | + Precondition and effect are both lists with positive and negated literals. | ||
| 43 | 43 | Example: | |
| 44 | 44 | precond_pos = [expr("Human(person)"), expr("Hungry(Person)")] | |
| 45 | 45 | precond_neg = [expr("Eaten(food)")] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -357,7 +357,7 @@ def pointwise_product(factors, bn): | |||
| 357 | 357 | ||
| 358 | 358 | ||
| 359 | 359 | def sum_out(var, factors, bn): | |
| 360 | - "Eliminate var from all factors by summing over its values." | ||
| 360 | + """Eliminate var from all factors by summing over its values.""" | ||
| 361 | 361 | result, var_factors = [], [] | |
| 362 | 362 | for f in factors: | |
| 363 | 363 | (var_factors if var in f.variables else result).append(f) | |
@@ -367,40 +367,40 @@ def sum_out(var, factors, bn): | |||
| 367 | 367 | ||
| 368 | 368 | class Factor: | |
| 369 | 369 | ||
| 370 | - "A factor in a joint distribution." | ||
| 370 | + """A factor in a joint distribution.""" | ||
| 371 | 371 | ||
| 372 | 372 | def __init__(self, variables, cpt): | |
| 373 | 373 | self.variables = variables | |
| 374 | 374 | self.cpt = cpt | |
| 375 | 375 | ||
| 376 | 376 | def pointwise_product(self, other, bn): | |
| 377 | - "Multiply two factors, combining their variables." | ||
| 377 | + """Multiply two factors, combining their variables.""" | ||
| 378 | 378 | variables = list(set(self.variables) | set(other.variables)) | |
| 379 | 379 | cpt = {event_values(e, variables): self.p(e) * other.p(e) | |
| 380 | 380 | for e in all_events(variables, bn, {})} | |
| 381 | 381 | return Factor(variables, cpt) | |
| 382 | 382 | ||
| 383 | 383 | def sum_out(self, var, bn): | |
| 384 | - "Make a factor eliminating var by summing over its values." | ||
| 384 | + """Make a factor eliminating var by summing over its values.""" | ||
| 385 | 385 | variables = [X for X in self.variables if X != var] | |
| 386 | 386 | cpt = {event_values(e, variables): sum(self.p(extend(e, var, val)) | |
| 387 | 387 | for val in bn.variable_values(var)) | |
| 388 | 388 | for e in all_events(variables, bn, {})} | |
| 389 | 389 | return Factor(variables, cpt) | |
| 390 | 390 | ||
| 391 | 391 | def normalize(self): | |
| 392 | - "Return my probabilities; must be down to one variable." | ||
| 392 | + """Return my probabilities; must be down to one variable.""" | ||
| 393 | 393 | assert len(self.variables) == 1 | |
| 394 | 394 | return ProbDist(self.variables[0], | |
| 395 | 395 | {k: v for ((k,), v) in self.cpt.items()}) | |
| 396 | 396 | ||
| 397 | 397 | def p(self, e): | |
| 398 | - "Look up my value tabulated for e." | ||
| 398 | + """Look up my value tabulated for e.""" | ||
| 399 | 399 | return self.cpt[event_values(e, self.variables)] | |
| 400 | 400 | ||
| 401 | 401 | ||
| 402 | 402 | def all_events(variables, bn, e): | |
| 403 | - "Yield every way of extending e with values for all variables." | ||
| 403 | + """Yield every way of extending e with values for all variables.""" | ||
| 404 | 404 | if not variables: | |
| 405 | 405 | yield e | |
| 406 | 406 | else: | |
@@ -453,7 +453,7 @@ def rejection_sampling(X, e, bn, N): | |||
| 453 | 453 | ||
| 454 | 454 | ||
| 455 | 455 | def consistent_with(event, evidence): | |
| 456 | - "Is event consistent with the given evidence?" | ||
| 456 | + """Is event consistent with the given evidence?""" | ||
| 457 | 457 | return all(evidence.get(k, v) == v | |
| 458 | 458 | for k, v in event.items()) | |
| 459 | 459 | ||
@@ -527,7 +527,7 @@ def markov_blanket_sample(X, e, bn): | |||
| 527 | 527 | ||
| 528 | 528 | class HiddenMarkovModel: | |
| 529 | 529 | ||
| 530 | - """ A Hidden markov model which takes Transition model and Sensor model as inputs""" | ||
| 530 | + """A Hidden markov model which takes Transition model and Sensor model as inputs""" | ||
| 531 | 531 | ||
| 532 | 532 | def __init__(self, transition_model, sensor_model, prior=[0.5, 0.5]): | |
| 533 | 533 | self.transition_model = transition_model | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,7 +24,7 @@ def __init__(self, init, actlist, terminals, gamma, states): | |||
| 24 | 24 | ||
| 25 | 25 | def T(self, s, a): | |
| 26 | 26 | """Returns a list of tuples with probabilities for states | |
| 27 | - based on the learnt model P. """ | ||
| 27 | + based on the learnt model P.""" | ||
| 28 | 28 | return [(prob, res) for (res, prob) in self.P[(s, a)].items()] | |
| 29 | 29 | ||
| 30 | 30 | def __init__(self, pi, mdp): | |
@@ -62,15 +62,15 @@ def __call__(self, percept): | |||
| 62 | 62 | ||
| 63 | 63 | def update_state(self, percept): | |
| 64 | 64 | ''' To be overridden in most cases. The default case | |
| 65 | - assumes th percept to be of type (state, reward)''' | ||
| 65 | + assumes the percept to be of type (state, reward)''' | ||
| 66 | 66 | return percept | |
| 67 | 67 | ||
| 68 | 68 | ||
| 69 | 69 | class PassiveTDAgent: | |
| 70 | 70 | """The abstract class for a Passive (non-learning) agent that uses | |
| 71 | 71 | temporal differences to learn utility estimates. Override update_state | |
| 72 | 72 | method to convert percept to state and reward. The mdp being provided | |
| 73 | - should be an instance of a subclass of the MDP Class.[Figure 21.4] | ||
| 73 | + should be an instance of a subclass of the MDP Class. [Figure 21.4] | ||
| 74 | 74 | """ | |
| 75 | 75 | ||
| 76 | 76 | def __init__(self, pi, mdp, alpha=None): | |
@@ -106,7 +106,7 @@ def __call__(self, percept): | |||
| 106 | 106 | ||
| 107 | 107 | def update_state(self, percept): | |
| 108 | 108 | ''' To be overridden in most cases. The default case | |
| 109 | - assumes th percept to be of type (state, reward)''' | ||
| 109 | + assumes the percept to be of type (state, reward)''' | ||
| 110 | 110 | return percept | |
| 111 | 111 | ||
| 112 | 112 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments