Skip to content

Commit

Permalink
🎨 Modularizing
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalpaudel committed Oct 29, 2023
1 parent c64c590 commit f8556f1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions src/searchViz/Game.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np

from .constants import (
EDGE_COLOR,
E_COLOR,
SCR_SIZE,
BG_COLOR,
RED,
Expand All @@ -16,7 +16,7 @@


from .Graph import Graph
from ._typing import NodeCount, SearchMethod
from ._typing import NodeCount

from .Search import Search

Expand Down Expand Up @@ -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
Expand Down
54 changes: 27 additions & 27 deletions src/searchViz/Graph.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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


Expand All @@ -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
"""
Expand All @@ -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()
Expand Down
33 changes: 23 additions & 10 deletions src/searchViz/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,52 @@


# 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"
E_MAX_LEN = 20
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,
}
2 changes: 1 addition & 1 deletion src/searchViz/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f8556f1

Please sign in to comment.