swarm-sim.py 4.02 KB
Newer Older
1
"""This is the main module of the Opportunistic Robotics Network Simulator"""
Ahmad Reza's avatar
Ahmad Reza committed
2
import importlib
Ahmad Reza's avatar
Ahmad Reza committed
3
4
5
6
import getopt
import logging
import os
import sys
7
import time
8
import random
Karol Actun's avatar
Karol Actun committed
9
from lib import world, config, vis3d
Ahmad Reza's avatar
Ahmad Reza committed
10
from lib.gnuplot_generator import gnuplot_generator
Ahmad Reza's avatar
Ahmad Reza committed
11
12


13
def swarm_sim(argv):
Ahmad Reza's avatar
Ahmad Reza committed
14
    """In the main function first the config is getting parsed and than
15
    the swarm_sim_world and the swarm_sim_world object is created. Afterwards the run method of the swarm_sim_world
Ahmad Reza's avatar
Ahmad Reza committed
16
    is called in which the simlator is going to start to run"""
Ahmad Reza's avatar
Ahmad Reza committed
17
18
19
    logging.basicConfig(filename='system.log', filemode='w', level=logging.INFO, format='%(message)s')
    logging.info('Started')

20
    config_data = config.ConfigData()
Karol Actun's avatar
Karol Actun committed
21

Ahmad Reza's avatar
Ahmad Reza committed
22
23
    read_cmd_args(argv, config_data)

24
    create_direction_for_data(config_data)
Ahmad Reza's avatar
Ahmad Reza committed
25

26
    random.seed(config_data.seed_value)
27
    swarm_sim_world = world.World(config_data)
Ahmad Reza's avatar
Ahmad Reza committed
28

Karol Actun's avatar
Karol Actun committed
29
    round_start_timestamp = time.perf_counter()
Karol Actun's avatar
Karol Actun committed
30
31
    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:
32

33
        if config_data.visualization:
Karol Actun's avatar
Karol Actun committed
34
            swarm_sim_world.vis.run(round_start_timestamp)
35
            round_start_timestamp = time.perf_counter()
Ahmad Reza's avatar
Ahmad Reza committed
36
37
38

        run_solution(swarm_sim_world)

Karol Actun's avatar
Karol Actun committed
39
    logging.info('Finished')
Ahmad Reza's avatar
Ahmad Reza committed
40

41
    generate_data(config_data, swarm_sim_world)
Ahmad Reza's avatar
Ahmad Reza committed
42
43


44
45
46
47
48
def draw_scenario(config_data, swarm_sim_world):
    mod = importlib.import_module('scenario.' + config_data.scenario)
    mod.scenario(swarm_sim_world)
    if config_data.particle_random_order:
        random.shuffle(swarm_sim_world.particles)
Ahmad Reza's avatar
Ahmad Reza committed
49
50
51


def read_cmd_args(argv, config_data):
Ahmad Reza's avatar
Ahmad Reza committed
52
    try:
53
        opts, args = getopt.getopt(argv, "hs:w:r:n:m:d:v:", ["solution=", "scenario="])
Ahmad Reza's avatar
Ahmad Reza committed
54
    except getopt.GetoptError:
55
        print('Error: swarm-swarm_sim_world.py -r <seed> -w <scenario> -s <solution> -n <maxRounds>')
Ahmad Reza's avatar
Ahmad Reza committed
56
57
58
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
59
            print('swarm-swarm_sim_world.py -r <seed> -w <scenario> -s <solution> -n <maxRounds>')
Ahmad Reza's avatar
Ahmad Reza committed
60
61
            sys.exit()
        elif opt in ("-s", "--solution"):
Ahmad Reza's avatar
Ahmad Reza committed
62
            config_data.solution = arg
63
        elif opt in ("-w", "--scenario"):
Ahmad Reza's avatar
Ahmad Reza committed
64
            config_data.scenario = arg
Ahmad Reza's avatar
Ahmad Reza committed
65
        elif opt in ("-r", "--seed"):
Ahmad Reza's avatar
Ahmad Reza committed
66
            config_data.seed_value = int(arg)
Ahmad Reza's avatar
Ahmad Reza committed
67
        elif opt in ("-n", "--maxrounds"):
Ahmad Reza's avatar
Ahmad Reza committed
68
69
70
71
            config_data.max_round = int(arg)
        elif opt in "-m":
            config_data.multiple_sim = int(arg)
        elif opt in "-v":
Ahmad Reza's avatar
Ahmad Reza committed
72
            config_data.visualization = int(arg)
Ahmad Reza's avatar
Ahmad Reza committed
73
74
        elif opt in "-d":
            config_data.local_time = str(arg)
Ahmad Reza's avatar
Ahmad Reza committed
75
76


77
def create_direction_for_data(config_data):
78
    if config_data.multiple_sim == 1:
79
        config_data.direction_name = config_data.local_time + "_" + config_data.scenario.rsplit('.', 1)[0] + \
Karol Actun's avatar
Karol Actun committed
80
81
                                     "_" + config_data.solution.rsplit('.', 1)[0] + "/" + \
                                     str(config_data.seed_value)
82

83
        config_data.direction_name = "./outputs/mulitple/" + config_data.direction_name
84
85

    else:
86
        config_data.direction_name = config_data.local_time + "_" + config_data.scenario.rsplit('.', 1)[0] + \
Karol Actun's avatar
Karol Actun committed
87
88
                                     "_" + config_data.solution.rsplit('.', 1)[0] + "_" + \
                                     str(config_data.seed_value)
89
90
91
        config_data.direction_name = "./outputs/" + config_data.direction_name
    if not os.path.exists(config_data.direction_name):
        os.makedirs(config_data.direction_name)
92
93
94


def run_solution(swarm_sim_world):
95
96
    if swarm_sim_world.config_data.particle_random_order_always:
        random.shuffle(swarm_sim_world.particles)
97
98
99
    mod = importlib.import_module('solution.' + swarm_sim_world.config_data.solution)
    mod.solution(swarm_sim_world)
    swarm_sim_world.csv_round.next_line(swarm_sim_world.get_actual_round())
100
    swarm_sim_world.inc_round_counter_by(number=1)
Ahmad Reza's avatar
Ahmad Reza committed
101

102
103
104

def generate_data(config_data, swarm_sim_world):
    swarm_sim_world.csv_aggregator()
105
    gnuplot_generator(config_data.direction_name)
106
107
108


if __name__ == "__main__":
Karol Actun's avatar
Karol Actun committed
109
    swarm_sim(sys.argv[1:])