-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
62 lines (48 loc) · 1.96 KB
/
test.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
from models import City
from graphs import generate_graph, check_connected
from ant_search import ant_search
import time
def display_cities(cities):
print('* CITIES')
for nr, city in enumerate(cities):
print(f'{nr}: {city}')
print()
def display_test_result(connected, result=None, duration=None):
path, cost, expanded_nodes = result
print(f'- Connected: {connected}')
print(f'- Solved: {path is not None}')
if path is not None:
print(f'- Path: {path}')
if cost is not None:
print(f'- Cost: {round(cost, 5)}')
if expanded_nodes is not None:
print(f'- Expanded nodes: {expanded_nodes}')
if duration is not None:
print(f'- Time: {round(duration, 5)}s')
print()
def test(cities, start_city, connections_drop, symmetric, seed=None):
start_time = time.time()
# Represent the created map as a weighted, directed graph
graph = generate_graph(cities, connections_drop=connections_drop, symmetric=symmetric, seed=seed)
# Make sure whether generated graph is connected
# This is necessary condition, but not sufficient
# It may be still impossible to visit every city only once
connected = check_connected(graph)
if not connected:
display_test_result(connected=False)
return
# Search graph
result = ant_search(graph, start_city)
end_time = time.time()
display_test_result(connected=True, result=result, duration=end_time-start_time)
if __name__ == '__main__':
seed = 222467
cities_count = 9
start_city = 0
# Create a set of cities
cities = City.generate(count=cities_count, x_range=(-100, 100), y_range=(-100, 100), z_range=(0, 50), seed=seed)
display_cities(cities)
for connections_drop in [0.0, 0.2]:
for symmetric in [True, False]:
print(f"{'-'*15} connections_drop: {connections_drop}; symmetric: {symmetric} {'-'*15}\n")
test(cities, start_city, connections_drop, symmetric, seed=seed)