-
Notifications
You must be signed in to change notification settings - Fork 3
/
swarm-sim.py
141 lines (111 loc) · 5.21 KB
/
swarm-sim.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""This is the main module of the Opportunistic Robotics Network Simulator"""
import importlib
import getopt
import logging
import os
import sys
import time
import random
from core import world, config
from core.vis3d import ResetException
def swarm_sim(argv):
"""In the main function first the config is getting parsed and than
the swarm_sim_world and the swarm_sim_world item is created. Afterwards the run method of the swarm_sim_world
is called in which the simlator is going to start to run"""
config_data = config.ConfigData()
unique_descriptor = "%s_%s_%s" % (config_data.local_time,
config_data.scenario.rsplit('.', 1)[0],
config_data.solution.rsplit('.', 1)[0])
logging.basicConfig(filename="outputs/logs/system_%s.log" % unique_descriptor, filemode='w',
level=logging.INFO, format='%(message)s')
logging.info('Started')
read_cmd_args(argv, config_data)
create_directory_for_data(config_data, unique_descriptor)
random.seed(config_data.seed_value)
swarm_sim_world = world.World(config_data)
swarm_sim_world.init_scenario(get_scenario(swarm_sim_world.config_data))
reset = True
while reset:
reset = main_loop(config_data, swarm_sim_world)
logging.info('Finished')
generate_data(config_data, swarm_sim_world)
def main_loop(config_data, swarm_sim_world):
round_start_timestamp = time.perf_counter()
while (config_data.max_round == 0 or swarm_sim_world.get_actual_round() <= config_data.max_round) \
and swarm_sim_world.get_end() is False:
try:
if config_data.visualization:
swarm_sim_world.vis.run(round_start_timestamp)
round_start_timestamp = time.perf_counter()
run_solution(swarm_sim_world)
except ResetException:
do_reset(swarm_sim_world)
if config_data.visualization:
try:
swarm_sim_world.vis.run(round_start_timestamp)
while not config_data.close_at_end:
swarm_sim_world.vis.run(round_start_timestamp)
except ResetException:
do_reset(swarm_sim_world)
return True
return False
def do_reset(swarm_sim_world):
swarm_sim_world.reset()
solution = get_solution(swarm_sim_world.config_data)
scenario = get_scenario(swarm_sim_world.config_data)
importlib.reload(solution)
importlib.reload(scenario)
swarm_sim_world.init_scenario(scenario)
def read_cmd_args(argv, config_data):
try:
opts, args = getopt.getopt(argv, "hs:w:r:n:m:d:v:", ["solution=", "scenario="])
except getopt.GetoptError:
print('Error: swarm-swarm_sim_world.py -r <seed> -w <scenario> -s <solution> -n <maxRounds>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('swarm-swarm_sim_world.py -r <seed> -w <scenario> -s <solution> -n <maxRounds>')
sys.exit()
elif opt in ("-s", "--solution"):
config_data.solution = arg
elif opt in ("-w", "--scenario"):
config_data.scenario = arg
elif opt in ("-r", "--seed"):
config_data.seed_value = int(arg)
elif opt in ("-n", "--maxrounds"):
config_data.max_round = int(arg)
elif opt in "-m":
config_data.multiple_sim = int(arg)
elif opt in "-v":
config_data.visualization = int(arg)
elif opt in "-d":
config_data.local_time = str(arg)
def create_directory_for_data(config_data, unique_descriptor):
if config_data.multiple_sim == 1:
config_data.directory_name = "%s/%s" % (unique_descriptor, str(config_data.seed_value))
config_data.directory_name = "./outputs/csv/mulitple/" + config_data.directory_name
config_data.directory_plot = "./outputs/plot/mulitple/" + config_data.directory_name
else:
config_data.directory_name = "%s_%s" % (unique_descriptor, str(config_data.seed_value))
config_data.directory_csv = "./outputs/csv/" + config_data.directory_name
config_data.directory_plot = "./outputs/plot/" + config_data.directory_name
if not os.path.exists(config_data.directory_csv):
os.makedirs(config_data.directory_csv)
if not os.path.exists(config_data.directory_plot):
os.makedirs(config_data.directory_plot)
def run_solution(swarm_sim_world):
if swarm_sim_world.config_data.agent_random_order_always:
random.shuffle(swarm_sim_world.agents)
get_solution(swarm_sim_world.config_data).solution(swarm_sim_world)
swarm_sim_world.csv_round.next_line(swarm_sim_world.get_actual_round())
swarm_sim_world.inc_round_counter_by(number=1)
def get_solution(config_data):
return importlib.import_module('components.solution.' + config_data.solution)
def get_scenario(config_data):
return importlib.import_module('components.scenario.' + config_data.scenario)
def generate_data(config_data, swarm_sim_world):
swarm_sim_world.csv_aggregator()
plt_gnrtr = importlib.import_module('components.generators.plot.%s' % config_data.plot_generator)
plt_gnrtr.plot_generator(config_data.directory_csv, config_data.directory_plot )
if __name__ == "__main__":
swarm_sim(sys.argv[1:])