From f8556f14e756bcb988b32e7ead838df27233fdcf Mon Sep 17 00:00:00 2001 From: Vishal Paudel <95016059+vishalpaudel@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:52:58 +0530 Subject: [PATCH] :art: Modularizing --- src/main.py | 2 +- src/searchViz/Game.py | 6 ++--- src/searchViz/Graph.py | 54 +++++++++++++++++++------------------- src/searchViz/constants.py | 33 ++++++++++++++++------- src/searchViz/models.py | 2 +- 5 files changed, 55 insertions(+), 42 deletions(-) diff --git a/src/main.py b/src/main.py index 8879e64..459ff08 100644 --- a/src/main.py +++ b/src/main.py @@ -13,7 +13,7 @@ def main(): dfs = depthfirstsearch() bfs = breadthfirstsearch() - aGame = Game(search=bfs, num_nodes=NodeCount(NUM_NODES)) + aGame = Game(search=dfs, num_nodes=NodeCount(NUM_NODES)) aGame.run() diff --git a/src/searchViz/Game.py b/src/searchViz/Game.py index b7e5271..134d831 100644 --- a/src/searchViz/Game.py +++ b/src/searchViz/Game.py @@ -6,7 +6,7 @@ import numpy as np from .constants import ( - EDGE_COLOR, + E_COLOR, SCR_SIZE, BG_COLOR, RED, @@ -16,7 +16,7 @@ from .Graph import Graph -from ._typing import NodeCount, SearchMethod +from ._typing import NodeCount from .Search import Search @@ -86,7 +86,7 @@ def draw_graph(self) -> None: edge_indices = np.transpose(np.where(edges)) for i, j in edge_indices: # TODO: tuple type conversion overhead? Probably not - pg.draw.line(graph_surf, EDGE_COLOR, tuple(nodes[i]), tuple(nodes[j])) + pg.draw.line(graph_surf, E_COLOR, tuple(nodes[i]), tuple(nodes[j])) # Draw nodes # TODO: vectorization of this possible? Probably not diff --git a/src/searchViz/Graph.py b/src/searchViz/Graph.py index d574705..682464d 100644 --- a/src/searchViz/Graph.py +++ b/src/searchViz/Graph.py @@ -1,9 +1,14 @@ #!/usr/bin/env python -from .constants import BLACK, NODES_X_DISTRIBUTION, NODES_Y_DISTRIBUTION, RED, YELLOW -from .constants import EDGE_CONFIDENCE, BLUE, NODE_RADIUS, WHITE - -import numpy as np +from .constants import ( + N_X_DISTRIBUTION, + N_Y_DISTRIBUTION, + colors, + EDGE_CONFIDENCE, + NODE_RADIUS, +) + +from numpy import random, column_stack, zeros, triu_indices, arange, full, uint8 import time from ._typing import NodeType, NodeList, NodeLocs, NodeCount @@ -19,19 +24,19 @@ def __init__(self, n: NodeCount): _start_time = time.time() # Node stuff - self.N = np.arange(0, self.N_num, dtype=NodeCount) + self.N = arange(0, self.N_num, dtype=NodeCount) self.N_locs = create_nodes(self.N_num) - self.N_colors = np.full((self.N_num, 4), BLUE, dtype=np.uint8) - self.N_radii = np.full((self.N_num,), NODE_RADIUS, dtype=np.uint8) + self.N_colors = full((self.N_num, 4), colors["BLUE"], dtype=uint8) + self.N_radii = full((self.N_num,), NODE_RADIUS, dtype=uint8) # start and end nodes - start, goal = np.random.randint(0, self.N_num, size=2, dtype=NodeCount) + start, goal = random.randint(0, self.N_num, size=2, dtype=NodeCount) self.start_node = Node(start) self.goal_node = Node(goal) - self.N_colors[self.start_node.id] = YELLOW - self.N_colors[self.goal_node.id] = RED + self.N_colors[self.start_node.id] = colors["YELLOW"] + self.N_colors[self.goal_node.id] = colors["RED"] self.N_radii[self.start_node.id] = 5 * NODE_RADIUS self.N_radii[self.goal_node.id] = 5 * NODE_RADIUS @@ -57,7 +62,7 @@ def MoveGen(self, state: NodeType) -> NodeList: """ neighbors = [] - id = np.uint16(0) + id = NodeCount(0) while id < self.N_num: if ( self.edge_connections[id, state.id] @@ -66,24 +71,19 @@ def MoveGen(self, state: NodeType) -> NodeList: neighbors.append(Node(id)) id += 1 - print() return neighbors def GoalTest(self, state: NodeType) -> bool: foundGoal = state.id == self.goal_node.id - if foundGoal: - print(state.id, "equals", self.goal_node.id) - else: - print(state.id, "does not equal", self.goal_node.id) return foundGoal def update_nodes(self): for open_ids in self.open_ids: - self.N_colors[open_ids[0].id] = BLUE + self.N_colors[open_ids[0].id] = colors["BLUE"] self.N_radii[open_ids[0].id] = 4 * NODE_RADIUS for closed_ids in self.closed_ids: - self.N_colors[closed_ids[0].id] = BLACK + self.N_colors[closed_ids[0].id] = colors["RED"] self.N_radii[closed_ids[0].id] = 1.5 * NODE_RADIUS @@ -100,17 +100,17 @@ def create_nodes(n: NodeCount) -> NodeLocs: """ print(f"🕰️\tCreating {n} nodes... timer started...") - x_values = NODES_X_DISTRIBUTION(int(n)) - y_values = NODES_Y_DISTRIBUTION(int(n)) + x_values = N_X_DISTRIBUTION(int(n)) + y_values = N_Y_DISTRIBUTION(int(n)) - node_locs = np.column_stack((x_values, y_values)) + node_locs = column_stack((x_values, y_values)) print("✅\tFinished creating nodes!\n") return node_locs -def create_edges(nodes: np.ndarray): +def create_edges(nodes: NodeLocs): """ Creates edges for the give nodes locations """ @@ -122,16 +122,16 @@ def create_edges(nodes: np.ndarray): dot_thread.start() n = len(nodes) - edge_connections = np.zeros((n, n)) - i, j = np.triu_indices(n, k=1) # only one way edges + edge_connections = zeros((n, n)) + i, j = triu_indices(n, k=1) # only one way edges - _toss = np.random.rand(n, n) - _confidence = np.zeros((n, n)) + _toss = random.rand(n, n) + _confidence = zeros((n, n)) _confidence[i, j] = EDGE_CONFIDENCE(nodes[i], nodes[j]) # confidence number of times there will be an edge edge_connections = _toss <= _confidence - edge_colors = np.full((n, 4), fill_value=WHITE, dtype=np.uint8) + edge_colors = full((n, 4), fill_value=colors["WHITE"], dtype=uint8) # Stop the dot animation done.set() diff --git a/src/searchViz/constants.py b/src/searchViz/constants.py index 6b99573..c2e1aed 100644 --- a/src/searchViz/constants.py +++ b/src/searchViz/constants.py @@ -4,21 +4,24 @@ # Screen configuration -SCREEN_WIDTH = 1350 -SCREEN_HEIGHT = 850 +SCREEN_WIDTH = 2000 +SCREEN_HEIGHT = 950 SCR_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT) # Search configuration -NUM_NODES = 10000 +SEARCH_RATE = 0.05 +SEARCH_METHOD = "DFS" + +# Graph configuration +NUM_NODES = 2000 NODE_RADIUS = 2 -SEARCH_RATE = 0.0005 # MODEL CONFIGURATIONS # distribution of nodes, available options: uniform, gaussian -NODE_X_MODEL = "uniform" -NODE_Y_MODEL = "uniform" +NODE_X_MODEL = "gaussian" +NODE_Y_MODEL = "gaussian" # available options threshold, exponential E_MODEL = "threshold" @@ -26,17 +29,27 @@ E_MIN_LEN = 15 # OTHERS -NODES_X_DISTRIBUTION = N_Distbn(SCREEN_WIDTH, model=NODE_X_MODEL) -NODES_Y_DISTRIBUTION = N_Distbn(SCREEN_HEIGHT, model=NODE_Y_MODEL) +N_X_DISTRIBUTION = N_Distbn(SCREEN_WIDTH, model=NODE_X_MODEL) +N_Y_DISTRIBUTION = N_Distbn(SCREEN_HEIGHT, model=NODE_Y_MODEL) EDGE_CONFIDENCE = E_Distbn(max_len=E_MAX_LEN, min_len=E_MIN_LEN, model=E_MODEL) # Colours (R G B A), A is the opacity (255 is opaque) RED = (255, 0, 0, 255) +GREEN = (0, 255, 0, 255) BLUE = (0, 255, 255, 255) WHITE = (255, 255, 255, 255) YELLOW = (255, 255, 153, 200) BLACK = (0, 0, 0, 255) -BG_COLOR = WHITE # Background color (RGB) -EDGE_COLOR = BLACK +BG_COLOR = BLACK # Background color (RGB) +E_COLOR = WHITE + +colors: dict = { + "RED": RED, + "GREEN": GREEN, + "BLUE": BLUE, + "WHITE": WHITE, + "YELLOW": YELLOW, + "BLACK": BLACK, +} diff --git a/src/searchViz/models.py b/src/searchViz/models.py index 4d7662d..9a3cced 100644 --- a/src/searchViz/models.py +++ b/src/searchViz/models.py @@ -19,7 +19,7 @@ def nodes_uniform(n: int) -> NDArray[np.float64]: return np.random.uniform(0, length, n) def nodes_gaussian(n: int) -> NDArray[np.float64]: - return np.random.normal(length / 2, length / 5, n) + return np.random.normal(length / 2, length / 7, n) # return np.random.uniform(0, length, n) return nodes_gaussian if model == "gaussian" else nodes_uniform