-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.py
executable file
·89 lines (70 loc) · 1.95 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#! /usr/bin/env python3
import sys
import time
from Graph import Graph
from State import State, Direction, TERMINAL_STATE
from Constants import CONST
CON_IN = sys.stdin
CON_OUT = sys.stdout
# Generate All possible next moves for each state to reduce number of iterations on each node
def genPossibleMoves(CAP_BOAT):
moves = []
for m in range(CAP_BOAT + 1):
for c in range(CAP_BOAT + 1):
if 0 < m < c:
continue
if 1 <= m + c <= CAP_BOAT:
moves.append((m, c))
return moves
def runBFS(g, INITIAL_STATE):
sys.stdout = open("outBFS.txt", "w")
print("\n\nBFS :: \n")
start_time = time.time()
p = g.BFS(INITIAL_STATE)
end_time = time.time()
# print("Printing Solution...")
if len(p):
g.printPath(p, TERMINAL_STATE)
else:
print("No Solution")
print("\n Elapsed time in BFS: %.2fms" % ((end_time - start_time)*1000))
def runDFS(g, INITIAL_STATE):
sys.stdout = open("outDFS.txt", "w")
print("\n\nDFS :: \n")
start_time = time.time()
p = g.DFS(INITIAL_STATE)
end_time = time.time()
if len(p):
g.printPath(p, TERMINAL_STATE)
else:
print("No Solution")
print("\n Elapsed time in DFS: %.2fms" % ((end_time - start_time)*1000))
def main():
sys.stdin = open("in.txt", "r")
m = int(input("m="))
print(m, end="\n")
c = int(input("c="))
print(c, end="\n")
k = int(input("k="))
print(k, end="\n")
t = int(input("TIME_LIMIT_s="))
print(t, end="\n")
n = int(input("NODE_LIMIT="))
print(n, end="\n")
CNST = CONST(m, c, k, t, n)
moves = genPossibleMoves(CNST.CAP_BOAT)
print(str(moves.__len__())+" iterations per Node.")
INITIAL_STATE = State(CNST.MAX_M, CNST.MAX_C, Direction.OLD_TO_NEW, 0, 0, 0, CNST, moves)
# TERMINAL_STATE = State(-1, -1, Direction.NEW_TO_OLD, -1, -1, 0)
g = Graph()
sys.stdout = CON_OUT
print("\nRunning BFS>")
runBFS(g, INITIAL_STATE)
sys.stdout = CON_OUT
print("Executed BFS>")
print("\nRunning DFS>")
runDFS(g, INITIAL_STATE)
sys.stdout = CON_OUT
print("Executed DFS>")
if __name__ == '__main__':
main()