| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fbdb36d commit c587f2c
28 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -354,8 +354,7 @@ def list_things_at(self, location, tclass=Thing): | |||
| 354 | 354 | return [thing for thing in self.things | |
| 355 | 355 | if thing.location == location and isinstance(thing, tclass)] | |
| 356 | 356 | return [thing for thing in self.things | |
| 357 | - if all(x==y for x,y in zip(thing.location, location)) | ||
| 358 | - and isinstance(thing, tclass)] | ||
| 357 | + if all(x == y for x, y in zip(thing.location, location)) and isinstance(thing, tclass)] | ||
| 359 | 358 | ||
| 360 | 359 | def some_things_at(self, location, tclass=Thing): | |
| 361 | 360 | """Return true if at least one of the things at location | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -354,8 +354,7 @@ def list_things_at(self, location, tclass=Thing): | |||
| 354 | 354 | return [thing for thing in self.things | |
| 355 | 355 | if thing.location == location and isinstance(thing, tclass)] | |
| 356 | 356 | return [thing for thing in self.things | |
| 357 | - if all(x==y for x,y in zip(thing.location, location)) | ||
| 358 | - and isinstance(thing, tclass)] | ||
| 357 | + if all(x == y for x, y in zip(thing.location, location)) and isinstance(thing, tclass)] | ||
| 359 | 358 | ||
| 360 | 359 | def some_things_at(self, location, tclass=Thing): | |
| 361 | 360 | """Return true if at least one of the things at location | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,9 +1,9 @@ | |||
| 1 | 1 | """Deep learning. (Chapters 20)""" | |
| 2 | 2 | ||
| 3 | - import math | ||
| 4 | 3 | import random | |
| 5 | 4 | import statistics | |
| 6 | 5 | ||
| 6 | + import numpy as np | ||
| 7 | 7 | from keras import Sequential, optimizers | |
| 8 | 8 | from keras.layers import Embedding, SimpleRNN, Dense | |
| 9 | 9 | from keras.preprocessing import sequence | |
@@ -249,7 +249,7 @@ def adam(dataset, net, loss, epochs=1000, rho=(0.9, 0.999), delta=1 / 10 ** 8, | |||
| 249 | 249 | r_hat = scalar_vector_product(1 / (1 - rho[1] ** t), r) | |
| 250 | 250 | ||
| 251 | 251 | # rescale r_hat | |
| 252 | - r_hat = map_vector(lambda x: 1 / (math.sqrt(x) + delta), r_hat) | ||
| 252 | + r_hat = map_vector(lambda x: 1 / (np.sqrt(x) + delta), r_hat) | ||
| 253 | 253 | ||
| 254 | 254 | # delta weights | |
| 255 | 255 | delta_theta = scalar_vector_product(-l_rate, element_wise_product(s_hat, r_hat)) | |
@@ -341,7 +341,7 @@ def forward(self, inputs): | |||
| 341 | 341 | res = [] | |
| 342 | 342 | # get normalized value of each input | |
| 343 | 343 | for i in range(len(self.nodes)): | |
| 344 | - val = [(inputs[i] - mu) * self.weights[0] / math.sqrt(self.epsilon + stderr ** 2) + self.weights[1]] | ||
| 344 | + val = [(inputs[i] - mu) * self.weights[0] / np.sqrt(self.epsilon + stderr ** 2) + self.weights[1]] | ||
| 345 | 345 | res.append(val) | |
| 346 | 346 | self.nodes[i].val = val | |
| 347 | 347 | return res | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,11 +1,13 @@ | |||
| 1 | - """Games or Adversarial Search. (Chapter 5)""" | ||
| 1 | + """Games or Adversarial Search (Chapter 5)""" | ||
| 2 | 2 | ||
| 3 | 3 | import copy | |
| 4 | 4 | import itertools | |
| 5 | 5 | import random | |
| 6 | 6 | from collections import namedtuple | |
| 7 | 7 | ||
| 8 | - from utils import vector_add, inf | ||
| 8 | + import numpy as np | ||
| 9 | + | ||
| 10 | + from utils import vector_add | ||
| 9 | 11 | ||
| 10 | 12 | GameState = namedtuple('GameState', 'to_move, utility, board, moves') | |
| 11 | 13 | StochasticGameState = namedtuple('StochasticGameState', 'to_move, utility, board, moves, chance') | |
@@ -24,15 +26,15 @@ def minmax_decision(state, game): | |||
| 24 | 26 | def max_value(state): | |
| 25 | 27 | if game.terminal_test(state): | |
| 26 | 28 | return game.utility(state, player) | |
| 27 | - v = -inf | ||
| 29 | + v = -np.inf | ||
| 28 | 30 | for a in game.actions(state): | |
| 29 | 31 | v = max(v, min_value(game.result(state, a))) | |
| 30 | 32 | return v | |
| 31 | 33 | ||
| 32 | 34 | def min_value(state): | |
| 33 | 35 | if game.terminal_test(state): | |
| 34 | 36 | return game.utility(state, player) | |
| 35 | - v = inf | ||
| 37 | + v = np.inf | ||
| 36 | 38 | for a in game.actions(state): | |
| 37 | 39 | v = min(v, max_value(game.result(state, a))) | |
| 38 | 40 | return v | |
@@ -53,13 +55,13 @@ def expect_minmax(state, game): | |||
| 53 | 55 | player = game.to_move(state) | |
| 54 | 56 | ||
| 55 | 57 | def max_value(state): | |
| 56 | - v = -inf | ||
| 58 | + v = -np.inf | ||
| 57 | 59 | for a in game.actions(state): | |
| 58 | 60 | v = max(v, chance_node(state, a)) | |
| 59 | 61 | return v | |
| 60 | 62 | ||
| 61 | 63 | def min_value(state): | |
| 62 | - v = inf | ||
| 64 | + v = np.inf | ||
| 63 | 65 | for a in game.actions(state): | |
| 64 | 66 | v = min(v, chance_node(state, a)) | |
| 65 | 67 | return v | |
@@ -94,7 +96,7 @@ def alpha_beta_search(state, game): | |||
| 94 | 96 | def max_value(state, alpha, beta): | |
| 95 | 97 | if game.terminal_test(state): | |
| 96 | 98 | return game.utility(state, player) | |
| 97 | - v = -inf | ||
| 99 | + v = -np.inf | ||
| 98 | 100 | for a in game.actions(state): | |
| 99 | 101 | v = max(v, min_value(game.result(state, a), alpha, beta)) | |
| 100 | 102 | if v >= beta: | |
@@ -105,7 +107,7 @@ def max_value(state, alpha, beta): | |||
| 105 | 107 | def min_value(state, alpha, beta): | |
| 106 | 108 | if game.terminal_test(state): | |
| 107 | 109 | return game.utility(state, player) | |
| 108 | - v = inf | ||
| 110 | + v = np.inf | ||
| 109 | 111 | for a in game.actions(state): | |
| 110 | 112 | v = min(v, max_value(game.result(state, a), alpha, beta)) | |
| 111 | 113 | if v <= alpha: | |
@@ -114,8 +116,8 @@ def min_value(state, alpha, beta): | |||
| 114 | 116 | return v | |
| 115 | 117 | ||
| 116 | 118 | # Body of alpha_beta_search: | |
| 117 | - best_score = -inf | ||
| 118 | - beta = inf | ||
| 119 | + best_score = -np.inf | ||
| 120 | + beta = np.inf | ||
| 119 | 121 | best_action = None | |
| 120 | 122 | for a in game.actions(state): | |
| 121 | 123 | v = min_value(game.result(state, a), best_score, beta) | |
@@ -135,7 +137,7 @@ def alpha_beta_cutoff_search(state, game, d=4, cutoff_test=None, eval_fn=None): | |||
| 135 | 137 | def max_value(state, alpha, beta, depth): | |
| 136 | 138 | if cutoff_test(state, depth): | |
| 137 | 139 | return eval_fn(state) | |
| 138 | - v = -inf | ||
| 140 | + v = -np.inf | ||
| 139 | 141 | for a in game.actions(state): | |
| 140 | 142 | v = max(v, min_value(game.result(state, a), alpha, beta, depth + 1)) | |
| 141 | 143 | if v >= beta: | |
@@ -146,7 +148,7 @@ def max_value(state, alpha, beta, depth): | |||
| 146 | 148 | def min_value(state, alpha, beta, depth): | |
| 147 | 149 | if cutoff_test(state, depth): | |
| 148 | 150 | return eval_fn(state) | |
| 149 | - v = inf | ||
| 151 | + v = np.inf | ||
| 150 | 152 | for a in game.actions(state): | |
| 151 | 153 | v = min(v, max_value(game.result(state, a), alpha, beta, depth + 1)) | |
| 152 | 154 | if v <= alpha: | |
@@ -158,8 +160,8 @@ def min_value(state, alpha, beta, depth): | |||
| 158 | 160 | # The default test cuts off at depth d or at a terminal state | |
| 159 | 161 | cutoff_test = (cutoff_test or (lambda state, depth: depth > d or game.terminal_test(state))) | |
| 160 | 162 | eval_fn = eval_fn or (lambda state: game.utility(state, player)) | |
| 161 | - best_score = -inf | ||
| 162 | - beta = inf | ||
| 163 | + best_score = -np.inf | ||
| 164 | + beta = np.inf | ||
| 163 | 165 | best_action = None | |
| 164 | 166 | for a in game.actions(state): | |
| 165 | 167 | v = min_value(game.result(state, a), best_score, beta, 1) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,11 +1,13 @@ | |||
| 1 | - """Games or Adversarial Search. (Chapter 5)""" | ||
| 1 | + """Games or Adversarial Search (Chapter 5)""" | ||
| 2 | 2 | ||
| 3 | 3 | import copy | |
| 4 | 4 | import itertools | |
| 5 | 5 | import random | |
| 6 | 6 | from collections import namedtuple | |
| 7 | 7 | ||
| 8 | - from utils4e import vector_add, MCT_Node, ucb, inf | ||
| 8 | + import numpy as np | ||
| 9 | + | ||
| 10 | + from utils4e import vector_add, MCT_Node, ucb | ||
| 9 | 11 | ||
| 10 | 12 | GameState = namedtuple('GameState', 'to_move, utility, board, moves') | |
| 11 | 13 | StochasticGameState = namedtuple('StochasticGameState', 'to_move, utility, board, moves, chance') | |
@@ -24,15 +26,15 @@ def minmax_decision(state, game): | |||
| 24 | 26 | def max_value(state): | |
| 25 | 27 | if game.terminal_test(state): | |
| 26 | 28 | return game.utility(state, player) | |
| 27 | - v = -inf | ||
| 29 | + v = -np.inf | ||
| 28 | 30 | for a in game.actions(state): | |
| 29 | 31 | v = max(v, min_value(game.result(state, a))) | |
| 30 | 32 | return v | |
| 31 | 33 | ||
| 32 | 34 | def min_value(state): | |
| 33 | 35 | if game.terminal_test(state): | |
| 34 | 36 | return game.utility(state, player) | |
| 35 | - v = inf | ||
| 37 | + v = np.inf | ||
| 36 | 38 | for a in game.actions(state): | |
| 37 | 39 | v = min(v, max_value(game.result(state, a))) | |
| 38 | 40 | return v | |
@@ -53,13 +55,13 @@ def expect_minmax(state, game): | |||
| 53 | 55 | player = game.to_move(state) | |
| 54 | 56 | ||
| 55 | 57 | def max_value(state): | |
| 56 | - v = -inf | ||
| 58 | + v = -np.inf | ||
| 57 | 59 | for a in game.actions(state): | |
| 58 | 60 | v = max(v, chance_node(state, a)) | |
| 59 | 61 | return v | |
| 60 | 62 | ||
| 61 | 63 | def min_value(state): | |
| 62 | - v = inf | ||
| 64 | + v = np.inf | ||
| 63 | 65 | for a in game.actions(state): | |
| 64 | 66 | v = min(v, chance_node(state, a)) | |
| 65 | 67 | return v | |
@@ -94,7 +96,7 @@ def alpha_beta_search(state, game): | |||
| 94 | 96 | def max_value(state, alpha, beta): | |
| 95 | 97 | if game.terminal_test(state): | |
| 96 | 98 | return game.utility(state, player) | |
| 97 | - v = -inf | ||
| 99 | + v = -np.inf | ||
| 98 | 100 | for a in game.actions(state): | |
| 99 | 101 | v = max(v, min_value(game.result(state, a), alpha, beta)) | |
| 100 | 102 | if v >= beta: | |
@@ -105,7 +107,7 @@ def max_value(state, alpha, beta): | |||
| 105 | 107 | def min_value(state, alpha, beta): | |
| 106 | 108 | if game.terminal_test(state): | |
| 107 | 109 | return game.utility(state, player) | |
| 108 | - v = inf | ||
| 110 | + v = np.inf | ||
| 109 | 111 | for a in game.actions(state): | |
| 110 | 112 | v = min(v, max_value(game.result(state, a), alpha, beta)) | |
| 111 | 113 | if v <= alpha: | |
@@ -114,8 +116,8 @@ def min_value(state, alpha, beta): | |||
| 114 | 116 | return v | |
| 115 | 117 | ||
| 116 | 118 | # Body of alpha_beta_search: | |
| 117 | - best_score = -inf | ||
| 118 | - beta = inf | ||
| 119 | + best_score = -np.inf | ||
| 120 | + beta = np.inf | ||
| 119 | 121 | best_action = None | |
| 120 | 122 | for a in game.actions(state): | |
| 121 | 123 | v = min_value(game.result(state, a), best_score, beta) | |
@@ -135,7 +137,7 @@ def alpha_beta_cutoff_search(state, game, d=4, cutoff_test=None, eval_fn=None): | |||
| 135 | 137 | def max_value(state, alpha, beta, depth): | |
| 136 | 138 | if cutoff_test(state, depth): | |
| 137 | 139 | return eval_fn(state) | |
| 138 | - v = -inf | ||
| 140 | + v = -np.inf | ||
| 139 | 141 | for a in game.actions(state): | |
| 140 | 142 | v = max(v, min_value(game.result(state, a), alpha, beta, depth + 1)) | |
| 141 | 143 | if v >= beta: | |
@@ -146,7 +148,7 @@ def max_value(state, alpha, beta, depth): | |||
| 146 | 148 | def min_value(state, alpha, beta, depth): | |
| 147 | 149 | if cutoff_test(state, depth): | |
| 148 | 150 | return eval_fn(state) | |
| 149 | - v = inf | ||
| 151 | + v = np.inf | ||
| 150 | 152 | for a in game.actions(state): | |
| 151 | 153 | v = min(v, max_value(game.result(state, a), alpha, beta, depth + 1)) | |
| 152 | 154 | if v <= alpha: | |
@@ -158,8 +160,8 @@ def min_value(state, alpha, beta, depth): | |||
| 158 | 160 | # The default test cuts off at depth d or at a terminal state | |
| 159 | 161 | cutoff_test = (cutoff_test or (lambda state, depth: depth > d or game.terminal_test(state))) | |
| 160 | 162 | eval_fn = eval_fn or (lambda state: game.utility(state, player)) | |
| 161 | - best_score = -inf | ||
| 162 | - beta = inf | ||
| 163 | + best_score = -np.inf | ||
| 164 | + beta = np.inf | ||
| 163 | 165 | best_action = None | |
| 164 | 166 | for a in game.actions(state): | |
| 165 | 167 | v = min_value(game.result(state, a), best_score, beta, 1) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,14 +1,10 @@ | |||
| 1 | + from copy import deepcopy | ||
| 1 | 2 | from tkinter import * | |
| 2 | - import sys | ||
| 3 | - import os.path | ||
| 4 | - import math | ||
| 5 | - sys.path.append(os.path.join(os.path.dirname(__file__), '..')) | ||
| 3 | + | ||
| 6 | 4 | from search import * | |
| 7 | - from search import breadth_first_tree_search as bfts, depth_first_tree_search as dfts, \ | ||
| 8 | - depth_first_graph_search as dfgs, breadth_first_graph_search as bfs, uniform_cost_search as ucs, \ | ||
| 9 | - astar_search as asts | ||
| 10 | 5 | from utils import PriorityQueue | |
| 11 | - from copy import deepcopy | ||
| 6 | + | ||
| 7 | + sys.path.append(os.path.join(os.path.dirname(__file__), '..')) | ||
| 12 | 8 | ||
| 13 | 9 | root = None | |
| 14 | 10 | city_coord = {} | |
@@ -289,7 +285,6 @@ def make_rectangle(map, x0, y0, margin, city_name): | |||
| 289 | 285 | ||
| 290 | 286 | ||
| 291 | 287 | def make_legend(map): | |
| 292 | - | ||
| 293 | 288 | rect1 = map.create_rectangle(600, 100, 610, 110, fill="white") | |
| 294 | 289 | text1 = map.create_text(615, 105, anchor=W, text="Un-explored") | |
| 295 | 290 | ||
@@ -325,13 +320,11 @@ def tree_search(problem): | |||
| 325 | 320 | display_current(node) | |
| 326 | 321 | if counter % 3 == 1 and counter >= 0: | |
| 327 | 322 | if problem.goal_test(node.state): | |
| 328 | - | ||
| 329 | 323 | return node | |
| 330 | 324 | frontier.extend(node.expand(problem)) | |
| 331 | 325 | ||
| 332 | 326 | display_frontier(frontier) | |
| 333 | 327 | if counter % 3 == 2 and counter >= 0: | |
| 334 | - | ||
| 335 | 328 | display_explored(node) | |
| 336 | 329 | return None | |
| 337 | 330 | ||
@@ -562,7 +555,7 @@ def astar_search(problem, h=None): | |||
| 562 | 555 | ||
| 563 | 556 | # TODO: | |
| 564 | 557 | # Remove redundant code. | |
| 565 | - # Make the interchangbility work between various algorithms at each step. | ||
| 558 | + # Make the interchangeability work between various algorithms at each step. | ||
| 566 | 559 | def on_click(): | |
| 567 | 560 | """ | |
| 568 | 561 | This function defines the action of the 'Next' button. | |
@@ -572,47 +565,47 @@ def on_click(): | |||
| 572 | 565 | if "Breadth-First Tree Search" == algo.get(): | |
| 573 | 566 | node = breadth_first_tree_search(romania_problem) | |
| 574 | 567 | if node is not None: | |
| 575 | - final_path = bfts(romania_problem).solution() | ||
| 568 | + final_path = breadth_first_tree_search(romania_problem).solution() | ||
| 576 | 569 | final_path.append(start.get()) | |
| 577 | 570 | display_final(final_path) | |
| 578 | 571 | next_button.config(state="disabled") | |
| 579 | 572 | counter += 1 | |
| 580 | 573 | elif "Depth-First Tree Search" == algo.get(): | |
| 581 | 574 | node = depth_first_tree_search(romania_problem) | |
| 582 | 575 | if node is not None: | |
| 583 | - final_path = dfts(romania_problem).solution() | ||
| 576 | + final_path = depth_first_tree_search(romania_problem).solution() | ||
| 584 | 577 | final_path.append(start.get()) | |
| 585 | 578 | display_final(final_path) | |
| 586 | 579 | next_button.config(state="disabled") | |
| 587 | 580 | counter += 1 | |
| 588 | 581 | elif "Breadth-First Graph Search" == algo.get(): | |
| 589 | 582 | node = breadth_first_graph_search(romania_problem) | |
| 590 | 583 | if node is not None: | |
| 591 | - final_path = bfs(romania_problem).solution() | ||
| 584 | + final_path = breadth_first_graph_search(romania_problem).solution() | ||
| 592 | 585 | final_path.append(start.get()) | |
| 593 | 586 | display_final(final_path) | |
| 594 | 587 | next_button.config(state="disabled") | |
| 595 | 588 | counter += 1 | |
| 596 | 589 | elif "Depth-First Graph Search" == algo.get(): | |
| 597 | 590 | node = depth_first_graph_search(romania_problem) | |
| 598 | 591 | if node is not None: | |
| 599 | - final_path = dfgs(romania_problem).solution() | ||
| 592 | + final_path = depth_first_graph_search(romania_problem).solution() | ||
| 600 | 593 | final_path.append(start.get()) | |
| 601 | 594 | display_final(final_path) | |
| 602 | 595 | next_button.config(state="disabled") | |
| 603 | 596 | counter += 1 | |
| 604 | 597 | elif "Uniform Cost Search" == algo.get(): | |
| 605 | 598 | node = uniform_cost_search(romania_problem) | |
| 606 | 599 | if node is not None: | |
| 607 | - final_path = ucs(romania_problem).solution() | ||
| 600 | + final_path = uniform_cost_search(romania_problem).solution() | ||
| 608 | 601 | final_path.append(start.get()) | |
| 609 | 602 | display_final(final_path) | |
| 610 | 603 | next_button.config(state="disabled") | |
| 611 | 604 | counter += 1 | |
| 612 | 605 | elif "A* - Search" == algo.get(): | |
| 613 | 606 | node = astar_search(romania_problem) | |
| 614 | 607 | if node is not None: | |
| 615 | - final_path = asts(romania_problem).solution() | ||
| 608 | + final_path = astar_search(romania_problem).solution() | ||
| 616 | 609 | final_path.append(start.get()) | |
| 617 | 610 | display_final(final_path) | |
| 618 | 611 | next_button.config(state="disabled") | |
@@ -626,6 +619,7 @@ def reset_map(): | |||
| 626 | 619 | city_map.itemconfig(city_coord[city], fill="white") | |
| 627 | 620 | next_button.config(state="normal") | |
| 628 | 621 | ||
| 622 | + | ||
| 629 | 623 | # TODO: Add more search algorithms in the OptionMenu | |
| 630 | 624 | ||
| 631 | 625 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments