run.py 3.84 KB
Newer Older
1
"""This is the main module of the Opportunistic Robotics Network Simulator"""
Ahmad Reza's avatar
Ahmad Reza committed
2
3
4
5
6
7
8
9
10


import configparser
import getopt
import logging
import os
import sys
from datetime import datetime

11
from lib import  sim
Ahmad Reza's avatar
Ahmad Reza committed
12

Ahmad Reza's avatar
Ahmad Reza committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class ConfigData():

    def __init__(self, config):
        self.seedvalue = config.getint("Simulator", "seedvalue")
        self.max_round = config.getint("Simulator", "max_round")
        self.random_order = config.getboolean("Simulator", "random_order")
        self.visualization = config.getint("Simulator", "visualization")
        try:
            self.scenario = config.get("File", "scenario")
        except (configparser.NoOptionError) as noe:
            self.scenario = "init_scenario.py"

        try:
            self.solution = config.get("File", "solution")
        except (configparser.NoOptionError) as noe:
            self.solution = "solution.py"
29
30
        self.size_x = config.getfloat("Simulator", "size_x")
        self.size_y = config.getfloat("Simulator", "size_y")
Ahmad Reza's avatar
Ahmad Reza committed
31
32
        self.window_size_x = config.getint("Simulator", "window_size_x")
        self.window_size_y = config.getint("Simulator", "window_size_y")
33
        self.border = config.getfloat("Simulator", "border")
Ahmad Reza's avatar
Ahmad Reza committed
34
35
36
37
        self.max_particles = config.getint("Simulator", "max_particles")
        self.mm_limitation = config.getboolean("Matter", "mm_limitation")
        self.particle_mm_size = config.getint("Matter", "particle_mm_size")
        self.tile_mm_size = config.getint("Matter", "tile_mm_size")
38
        self.marker_mm_size = config.getint("Matter", "marker_mm_size")
Ahmad Reza's avatar
Ahmad Reza committed
39
40
41
        self.dir_name = None

def swarm_sim( argv ):
Ahmad Reza's avatar
Ahmad Reza committed
42
    """In the main function first the config is getting parsed and than
43
    the simulator and the sim object is created. Afterwards the run method of the simulator
Ahmad Reza's avatar
Ahmad Reza committed
44
45
46
47
    is called in which the simlator is going to start to run"""
    config = configparser.ConfigParser(allow_no_value=True)

    config.read("config.ini")
Ahmad Reza's avatar
Ahmad Reza committed
48
    config_data=ConfigData(config)
Ahmad Reza's avatar
Ahmad Reza committed
49
50

    multiple_sim=0
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
51
    local_time = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')[:-1]
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:
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
55
        print('Error: run.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':
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
59
            print('run.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.seedvalue = int(arg)
Ahmad Reza's avatar
Ahmad Reza committed
67
        elif opt in ("-n", "--maxrounds"):
Ahmad Reza's avatar
Ahmad Reza committed
68
           config_data.max_round = int(arg)
Ahmad Reza's avatar
Ahmad Reza committed
69
        elif opt in ("-m"):
70
           multiple_sim = int(arg)
Ahmad Reza's avatar
Ahmad Reza committed
71
        elif opt in ("-v"):
Ahmad Reza's avatar
Ahmad Reza committed
72
            config_data.visualization = int(arg)
73
        elif opt in ("-d"):
Ahmad Reza's avatar
Ahmad Reza committed
74
            local_time = str(arg)
Ahmad Reza's avatar
Ahmad Reza committed
75
76
77
78

    logging.basicConfig(filename='system.log', filemode='w', level=logging.INFO, format='%(message)s')

    if multiple_sim == 1:
Ahmad Reza's avatar
Ahmad Reza committed
79
80
81
        config_data.dir_name= local_time + "_" + config_data.scenario.rsplit('.', 1)[0] + \
               "_" + config_data.solution.rsplit('.', 1)[0] + "/" + \
               str(config_data.seedvalue)
Ahmad Reza's avatar
Ahmad Reza committed
82

Ahmad Reza's avatar
Ahmad Reza committed
83
        config_data.dir_name = "./outputs/mulitple/"+ config_data.dir_name
Ahmad Reza's avatar
Ahmad Reza committed
84

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

    logging.info('Started')
Ahmad Reza's avatar
Ahmad Reza committed
94
    simulator = sim.Sim( config_data )
95
    simulator.run()
Ahmad Reza's avatar
Ahmad Reza committed
96
97
98
99
    logging.info('Finished')


if __name__ == "__main__":
Ahmad Reza's avatar
Ahmad Reza committed
100
    swarm_sim(sys.argv[1:])
Ahmad Reza's avatar
Ahmad Reza committed
101