-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
89 lines (69 loc) · 2.21 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
import networkx as nx
import matplotlib.pyplot as plt
import Algo
import time
from CSP.solver import backtrack, backtrack_AC_3
from Graph import graph as g
from Graph import graph_distance as gd
from Graph.grid import Grid
from CSP import Csp
if __name__ == '__main__':
# visualize graph for testing algo
g = g.graph3
G = nx.DiGraph(g)
# draw the graph
nx.draw(G, with_labels=True)
plt.show()
# Search algorithms: default from A to D
# BFS
isFoundBFS = Algo.breadth_first_search(g, "A", "T")
print(isFoundBFS)
# DFS
isFoundDFS = Algo.depth_first_search(g, "A", "T")
print(isFoundDFS)
# IDS
isFoundIDS = Algo.iterative_deepening_search(g, "A", "T")
print(isFoundDFS)
#
gdd = gd.graph2
isFoundIDS_Not_uniform_cost = Algo.ids_not_uniformed_cost(gdd, "A", "F")
print(isFoundDFS)
# Graph with distance ----------------------------------------------------------------------------------------------
# Uniform-Cost Search
gd = gd.graph3
G = nx.DiGraph(gd)
# draw the graph
nx.draw(G, with_labels=True)
plt.show()
isFoundUniform = Algo.uniform_cost(gd)
print(isFoundUniform)
# use grid data struct
start = (0, 0)
target = (4, 4)
obstacles = {(2, 2), (3, 2), (2, 3), (3, 3)}
grid = Grid(5, 5, obstacles)
print(Algo.a_star_search(grid, start, target))
print(Algo.a_star_search(grid, start, (4, 3)))
# CSP starts ------------------------------------------------------------------------------------------------------
print("\n Simple CSP: \n")
regions = ['WA', 'NT', 'SA', 'Q', 'NSW', 'V', 'T']
colors = ['red', 'green', 'blue',]
neighbors = {
'WA': ['NT', 'SA'],
'NT': ['WA', 'SA', 'Q'],
'SA': ['WA', 'NT', 'Q', 'NSW', 'V'],
'Q': ['NT', 'SA', 'NSW'],
'NSW': ['Q', 'SA', 'V'],
'V': ['SA', 'NSW'],
'T': []
}
csp = Csp.MapColoringCSP(regions, colors, neighbors)
# Call basic backtrack
assignment = {}
result = backtrack(assignment, csp)
print(result)
# Call AC-3 backtrack
csp = Csp.MapColoringCSP(regions, colors, neighbors)
assignment = {}
result = backtrack_AC_3(assignment, csp)
print(result)