Skip to content

Commit

Permalink
Merge pull request #8 from LennartJKlein/development
Browse files Browse the repository at this point in the history
Release v0.7 - A* algorithm
  • Loading branch information
LennartJKlein authored Nov 28, 2017
2 parents 02d46f2 + c8b55ff commit ce5688e
Show file tree
Hide file tree
Showing 10 changed files with 722 additions and 442 deletions.
534 changes: 534 additions & 0 deletions sample/classes.py

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions sample/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Terminal colours
DEFAULT = '\033[0m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
160 changes: 88 additions & 72 deletions sample/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
Pathfinder will find the most efficient path between two gates on a board.
"""

import csv
import helpers
import settings

import numpy as np
import matplotlib.pyplot as plt
import csv

# Program settings
BOARD_WIDTH = 18
BOARD_HEIGHT = 13
BOARD_DEPTH = 7
SIGN_PATH_START = 2
FILE_NETLIST = 1
FILE_GATES = 'data/gates1.csv'
PATH_NUMBER = 2 # start at two
SIGN_GATE = 1
import classes
from classes import Board
from classes import Netlist
from classes import Gate
import colors as CLR

def main():
'''
Expand All @@ -30,67 +28,85 @@ def main():
# Config NumPy
np.set_printoptions(threshold=np.nan)

# Initiate a board with a specified size
board = helpers.Board(BOARD_WIDTH, BOARD_HEIGHT, BOARD_DEPTH)

# Read a CSV file for gate tuples
with open(FILE_GATES, 'r') as csvfile:
reader = csv.reader(csvfile)
print("Using: " + FILE_GATES)

# Skip the header
next(reader, None)

# Initiate a list of gates
gates = {}

for row in reader:
# Skip row if the data is commented
if row[0][:1] != '#':

# Get the name of the gate
gateLabel = int(row[0])

# Fetch the coords X and Y
gateX = int(row[1])
gateY = int(row[2])
gateZ = int(row[3])

# Save gate object in gates list
gates[gateLabel] = helpers.Gate(gateLabel, gateX, gateY, gateZ)

# Set a gate in the grid for every row in the file
board.set_gate(gateX, gateY, gateZ)

# Create a netlist and calculate path
netlist = helpers.Netlist(FILE_NETLIST)
print("Using in Netlist #" + str(FILE_NETLIST))

# Loop through every connection in the netlist
label = SIGN_PATH_START
for connection in netlist.list:
a = connection[0]
b = connection[1]
a_list = [gates[a].z, gates[a].y, gates[a].x]
b_list = [gates[b].z, gates[b].y, gates[b].x]

# Create a new path object
new_path = helpers.Path(a_list, b_list, label, "grey")

# Add this path to the board object
board.paths.append(new_path)

# Calculate the route for this path
new_path.calculate_DIJKSTRA(board)

# Set a new label for the next path
label += 1

# Print the board data
board.print_board()

# Plot the board
board.plot()
# Show chosen settings
print("Using netlist #" + str(settings.FILE_NETLIST))
print("Using gates file #" + str(settings.FILE_GATES))
print("")

# Keep track of results on different weights
weights = []
score = []

# Experiment
for i in range(settings.AMOUNT_BOARDS):

# Initiate a board with a specified size
board = Board(settings.BOARD_WIDTH, settings.BOARD_HEIGHT, settings.BOARD_DEPTH)

# Create a netlist and calculate path
netlist = Netlist(settings.FILE_NETLIST)

# Read a CSV file for gate tuples
with open('data/gates'+ str(settings.FILE_GATES) + '.csv', 'r') as csvfile:
reader = csv.reader(csvfile)

# Skip the header
next(reader, None)

for row in reader:
# Skip row if the data is commented
if row[0][:1] != '#':

# Get the name of the gate
gateLabel = int(row[0])

# Fetch the coords X and Y
gateX = int(row[1])
gateY = int(row[2])
gateZ = int(row[3])

# Save gate object in gates list
new_gate = Gate(netlist, gateLabel, gateX, gateY, gateZ)

# Set a gate in the grid for every row in the file
board.gatesObjects[gateZ, gateY, gateX] = new_gate
board.gatesNumbers[gateZ, gateY, gateX] = gateLabel
board.board[gateZ, gateY, gateX] = settings.SIGN_GATE

# Calculate the connections in this netlist
amount_paths, amount_fail, amount_success = netlist.execute_connections(board)

weights.append(settings.ASTAR_WEIGHT)
score.append(amount_success)

# APPEND SETTINGS
settings.ASTAR_WEIGHT += 2

# Print results of this execution
print("------------ BOARD: " + str(i) + " --------------")
print("Weight: " + str(settings.ASTAR_WEIGHT))
print(CLR.YELLOW + "Paths calculated: " + str(amount_success) + " / " + str(amount_paths) + CLR.DEFAULT)
# print(CLR.YELLOW + str(round(amount_success / amount_paths * 100, 2)) + "%" + CLR.DEFAULT)
# print("")
# print(CLR.YELLOW + "Score: " + str(board.get_score()) + CLR.DEFAULT)
# print("")
# print("")

# Print the board data
# board.print_board()

# Plot the board
# board.plot()

# Config graph plot for iteration information
# fig = plt.figure()
# ax = fig.gca()
# ax.set_xlim(0, 100)
# ax.set_ylim(30, 80)
# ax.set_xlabel("Weight")
# ax.set_ylabel("Paths drawn")
# ax.plot(weights, score)
# plt.show()

if __name__ == '__main__':
main()
4 changes: 0 additions & 4 deletions sample/data/gates.csv

This file was deleted.

4 changes: 4 additions & 0 deletions sample/data/gates0.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name,x,y,z
1,0,1,0
2,1,1,0
3,2,1,0
50 changes: 25 additions & 25 deletions sample/data/gates1.csv
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
name,x,y,z
0,1,1,0
1,6,1,0
2,10,1,0
3,15,1,0
4,4,2,0
5,12,2,0
6,14,2,0
7,12,3,0
8,8,4,0
9,1,5,0
10,4,5,0
11,11,5,0
12,16,5,0
13,13,7,0
14,16,7,0
15,2,8,0
16,6,8,0
17,9,8,0
18,11,8,0
19,15,8,0
20,1,9,0
21,2,10,0
22,9,10,0
23,1,11,0
24,12,11,0
1,1,1,0
2,6,1,0
3,10,1,0
4,15,1,0
5,4,2,0
6,12,2,0
7,14,2,0
8,12,3,0
9,8,4,0
10,1,5,0
11,4,5,0
12,11,5,0
13,16,5,0
14,13,7,0
15,16,7,0
16,2,8,0
17,6,8,0
18,9,8,0
19,11,8,0
20,15,8,0
21,1,9,0
22,2,10,0
23,9,10,0
24,1,11,0
25,12,11,0
100 changes: 50 additions & 50 deletions sample/data/gates2.csv
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
name,x,y,z
0,1,1,0
1,6,1,0
2,10,1,0
3,15,1,0
4,3,2,0
5,12,2,0
6,14,2,0
7,1,3,0
8,6,3,0
9,12,3,0
10,10,3,0
11,2,4,0
12,8,4,0
13,1,5,0
14,4,5,0
15,10,5,0
16,11,5,0
17,16,5,0
18,2,6,0
19,7,6,0
20,10,6,0
21,12,6,0
22,10,6,0
23,6,7,0
24,13,7,0
25,16,7,0
26,6,8,0
27,7,8,0
28,9,8,0
29,11,8,0
30,15,8,0
31,1,9,0
32,6,9,0
33,9,10,0
34,12,11,0
35,2,12,0
36,4,12,0
37,7,12,0
38,10,12,0
39,15,12,0
40,9,13,0
41,13,13,0
42,4,14,0
43,6,14,0
44,1,15,0
45,6,15,0
46,8,15,0
47,11,15,0
48,13,15,0
49,16,15,0
1,1,1,0
2,6,1,0
3,10,1,0
4,15,1,0
5,3,2,0
6,12,2,0
7,14,2,0
8,1,3,0
9,6,3,0
10,12,3,0
11,10,3,0
12,2,4,0
13,8,4,0
14,1,5,0
15,4,5,0
16,10,5,0
17,11,5,0
18,16,5,0
19,2,6,0
20,7,6,0
21,10,6,0
22,12,6,0
23,13,6,0
24,6,7,0
25,13,7,0
26,16,7,0
27,6,8,0
28,7,8,0
29,9,8,0
30,11,8,0
31,15,8,0
32,1,9,0
33,6,9,0
34,9,10,0
35,12,11,0
36,2,12,0
37,4,12,0
38,7,12,0
39,10,12,0
40,15,12,0
41,9,13,0
42,13,13,0
43,4,14,0
44,6,14,0
45,1,15,0
46,6,15,0
47,8,15,0
48,11,15,0
49,13,15,0
50,16,15,0
2 changes: 1 addition & 1 deletion sample/data/netlist0.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[(0,1),(5,7),(15,17)]
[(0,1),(1,2),(2,0),(0,1),(1,2),(2,0)]
Loading

0 comments on commit ce5688e

Please sign in to comment.