-
Notifications
You must be signed in to change notification settings - Fork 133
/
app.py
76 lines (63 loc) · 1.8 KB
/
app.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
"""
Configure visualization elements and instantiate a server
"""
import networkx as nx
import solara
from aco_tsp.model import AcoTspModel, TSPGraph
from matplotlib.figure import Figure
from mesa.visualization import SolaraViz
def circle_portrayal_example(agent):
return {"node_size": 20, "width": 0.1}
tsp_graph = TSPGraph.from_tsp_file("aco_tsp/data/kroA100.tsp")
model_params = {
"num_agents": tsp_graph.num_cities,
"tsp_graph": tsp_graph,
"ant_alpha": {
"type": "SliderFloat",
"value": 1.0,
"label": "Alpha: pheromone exponent",
"min": 0.0,
"max": 10.0,
"step": 0.1,
},
"ant_beta": {
"type": "SliderFloat",
"value": 5.0,
"label": "Beta: heuristic exponent",
"min": 0.0,
"max": 10.0,
"step": 0.1,
},
}
def make_graph(model):
fig = Figure()
ax = fig.subplots()
ax.set_title("Cities and pheromone trails")
graph = model.grid.G
pos = model.tsp_graph.pos
weights = [graph[u][v]["pheromone"] for u, v in graph.edges()]
# normalize the weights
weights = [w / max(weights) for w in weights]
nx.draw(
graph,
ax=ax,
pos=pos,
node_size=10,
width=weights,
edge_color="gray",
)
solara.FigureMatplotlib(fig)
def ant_level_distances(model):
# ant_distances = model.datacollector.get_agent_vars_dataframe()
# Plot so that the step index is the x-axis, there's a line for each agent,
# and the y-axis is the distance traveled
# ant_distances['tsp_distance'].unstack(level=1).plot(ax=ax)
pass
page = SolaraViz(
AcoTspModel,
model_params,
space_drawer=None,
measures=["best_distance_iter", "best_distance", make_graph],
agent_portrayal=circle_portrayal_example,
play_interval=1,
)