run.py 4.03 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

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 simulator and the sim object is created. Afterwards the run method of the simulator
Ahmad Reza's avatar
Ahmad Reza committed
16
17
18
19
    is called in which the simlator is going to start to run"""
    config = configparser.ConfigParser(allow_no_value=True)

    config.read("config.ini")
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    seedvalue = config.getint("Simulator", "seedvalue")
    max_round = config.getint("Simulator", "max_round")
    random_order = config.getboolean("Simulator", "random_order")
    visualization = config.getint("Simulator", "visualization")
    try:
        scenario_file = config.get ("File", "scenario")
    except (configparser.NoOptionError) as noe:
        scenario_file = "init_scenario.py"

    try:
        solution_file = config.get("File", "solution")
    except (configparser.NoOptionError) as noe:
        solution_file = "solution.py"
    size_x = config.getint("Simulator", "size_x")
    size_y = config.getint("Simulator", "size_y")
    window_size_x = config.getint("Simulator", "window_size_x")
    window_size_y = config.getint("Simulator", "window_size_y")
    border = config.getint("Simulator", "border")
    max_particles = config.getint("Simulator", "max_particles")
    mm_limitation = config.getboolean("Matter", "mm_limitation")
    mm_particle = config.getint("Matter", "particle_mm_size")
    mm_tile= config.getint("Matter", "tile_mm_size")
    mm_location=config.getint("Matter", "location_mm_size")
    # tile_color_map = config.getboolean("Matter", "tile_color_map")
Ahmad Reza's avatar
Ahmad Reza committed
44
45

    multiple_sim=0
46

Ahmad Reza's avatar
Ahmad Reza committed
47
    try:
48
        opts, args = getopt.getopt(argv, "hs:w:r:n:m:d:v:", ["solution=", "scenario="])
Ahmad Reza's avatar
Ahmad Reza committed
49
    except getopt.GetoptError:
50
        print('Error: run.py -r <randomeSeed> -w <scenario> -s <solution> -n <maxRounds>')
Ahmad Reza's avatar
Ahmad Reza committed
51
52
53
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
54
            print('run.py -r <randomeSeed> -w <scenario> -s <solution> -n <maxRounds>')
Ahmad Reza's avatar
Ahmad Reza committed
55
56
            sys.exit()
        elif opt in ("-s", "--solution"):
57
            solution_file = arg
58
        elif opt in ("-w", "--scenario"):
59
            scenario_file = arg
Ahmad Reza's avatar
Ahmad Reza committed
60
        elif opt in ("-r", "--seed"):
61
            seedvalue = int(arg)
Ahmad Reza's avatar
Ahmad Reza committed
62
        elif opt in ("-n", "--maxrounds"):
63
           max_round = int(arg)
Ahmad Reza's avatar
Ahmad Reza committed
64
        elif opt in ("-m"):
65
           multiple_sim = int(arg)
Ahmad Reza's avatar
Ahmad Reza committed
66
        elif opt in ("-v"):
67
            visualization = int(arg)
68
        elif opt in ("-d"):
69
            act_date = arg
Ahmad Reza's avatar
Ahmad Reza committed
70
71
72
73
74
75


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


76
77
78
    nTime = datetime.now().strftime('%Y-%m-%d_%H-%M-%S-%f')[:-1]
    dir_name =  nTime + "_" + solution_file.rsplit('.',1)[0]  + "_" + scenario_file.rsplit('.',1)[0] + "_" + \
                         str(seedvalue)
Ahmad Reza's avatar
Ahmad Reza committed
79
    if multiple_sim == 1:
80
81
82
83
84
85
        directory = "./outputs/mulitple/"+ act_date + "_" + scenario_file.rsplit('.',1)[0] + \
                     "_"+solution_file.rsplit('.',1)[0] + "/" + str(seedvalue)
    else:
        directory = "./outputs/" + dir_name
    if not os.path.exists(directory):
        os.makedirs(directory)
Ahmad Reza's avatar
Ahmad Reza committed
86
87
88
89



    logging.info('Started')
90
91
92
93
94
95
96
97
98
99



    simulator=sim.Sim(seed=seedvalue, max_round=max_round, solution=solution_file.rsplit('.',1)[0],
                          size_x=size_x, size_y=size_y, scenario_name=scenario_file,
                           max_particles=max_particles, mm_limitation=mm_limitation,
                           particle_mm_size=mm_particle, tile_mm_size=mm_tile, location_mm_size=mm_location,
                           dir=directory, random_order=random_order,
                          visualization=visualization,  border=border, window_size_x=window_size_x, window_size_y=window_size_y,)

100
    simulator.run()
Ahmad Reza's avatar
Ahmad Reza committed
101
102
103
104
    logging.info('Finished')


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