swarm-sim.py 3.5 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
8
import time
from lib import world, config
Ahmad Reza's avatar
Ahmad Reza committed
9
from lib.gnuplot_generator import gnuplot_generator
Ahmad Reza's avatar
Ahmad Reza committed
10
11


12
def swarm_sim(argv):
Ahmad Reza's avatar
Ahmad Reza committed
13
    """In the main function first the config is getting parsed and than
14
    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
15
    is called in which the simlator is going to start to run"""
Ahmad Reza's avatar
Ahmad Reza committed
16
17
18
    logging.basicConfig(filename='system.log', filemode='w', level=logging.INFO, format='%(message)s')
    logging.info('Started')

19
20
    config_data = config.ConfigData()
    
Ahmad Reza's avatar
Ahmad Reza committed
21
22
    read_cmd_args(argv, config_data)

23
    create_dir_for_data(config_data)
Ahmad Reza's avatar
Ahmad Reza committed
24

25
    swarm_sim_world = world.World(config_data)
Ahmad Reza's avatar
Ahmad Reza committed
26

27
    while swarm_sim_world.get_actual_round() <= config_data.max_round and swarm_sim_world.get_end() is False:
28
29
30
31
32
33
        round_start_timestamp = time.perf_counter()
        run_solution(swarm_sim_world)
        if config_data.visualization:
            swarm_sim_world.window.draw_world(round_start_timestamp)
            if swarm_sim_world.window.window_active is False:
                break
Ahmad Reza's avatar
Ahmad Reza committed
34

35
    generate_data(config_data, swarm_sim_world)
Ahmad Reza's avatar
Ahmad Reza committed
36

37
    logging.info('Finished')
Ahmad Reza's avatar
Ahmad Reza committed
38

Ahmad Reza's avatar
Ahmad Reza committed
39
40

def read_cmd_args(argv, config_data):
Ahmad Reza's avatar
Ahmad Reza committed
41
    try:
42
        opts, args = getopt.getopt(argv, "hs:w:r:n:m:d:v:", ["solution=", "scenario="])
Ahmad Reza's avatar
Ahmad Reza committed
43
    except getopt.GetoptError:
44
        print('Error: swarm-swarm_sim_world.py -r <seed> -w <scenario> -s <solution> -n <maxRounds>')
Ahmad Reza's avatar
Ahmad Reza committed
45
46
47
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
48
            print('swarm-swarm_sim_world.py -r <seed> -w <scenario> -s <solution> -n <maxRounds>')
Ahmad Reza's avatar
Ahmad Reza committed
49
50
            sys.exit()
        elif opt in ("-s", "--solution"):
Ahmad Reza's avatar
Ahmad Reza committed
51
            config_data.solution = arg
52
        elif opt in ("-w", "--scenario"):
Ahmad Reza's avatar
Ahmad Reza committed
53
            config_data.scenario = arg
Ahmad Reza's avatar
Ahmad Reza committed
54
        elif opt in ("-r", "--seed"):
Ahmad Reza's avatar
Ahmad Reza committed
55
            config_data.seed_value = int(arg)
Ahmad Reza's avatar
Ahmad Reza committed
56
        elif opt in ("-n", "--maxrounds"):
Ahmad Reza's avatar
Ahmad Reza committed
57
58
59
60
            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
61
            config_data.visualization = int(arg)
Ahmad Reza's avatar
Ahmad Reza committed
62
63
        elif opt in "-d":
            config_data.local_time = str(arg)
Ahmad Reza's avatar
Ahmad Reza committed
64
65


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def create_dir_for_data(config_data):
    if config_data.multiple_sim == 1:
        config_data.dir_name = config_data.local_time + "_" + config_data.scenario.rsplit('.', 1)[0] + \
                               "_" + config_data.solution.rsplit('.', 1)[0] + "/" + \
                               str(config_data.seed_value)

        config_data.dir_name = "./outputs/mulitple/" + config_data.dir_name

    else:
        config_data.dir_name = config_data.local_time + "_" + config_data.scenario.rsplit('.', 1)[0] + \
                               "_" + config_data.solution.rsplit('.', 1)[0] + "_" + \
                               str(config_data.seed_value)
        config_data.dir_name = "./outputs/" + config_data.dir_name
    if not os.path.exists(config_data.dir_name):
        os.makedirs(config_data.dir_name)


def run_solution(swarm_sim_world):
    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())
    swarm_sim_world.inc_round_cnter()
Ahmad Reza's avatar
Ahmad Reza committed
88

89
90
91
92
93
94
95
96

def generate_data(config_data, swarm_sim_world):
    swarm_sim_world.csv_aggregator()
    gnuplot_generator(config_data.dir_name)


if __name__ == "__main__":
    swarm_sim(sys.argv[1:])