run.py 3.61 KB
Newer Older
Ahmad Reza's avatar
Ahmad Reza committed
1
"""This is the main module of the Swarm-Sim"""
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


Ahmad Reza's avatar
Ahmad Reza committed
14
def swarm_sim(argv):
Ahmad Reza's avatar
Ahmad Reza committed
15
    """In the main function first the config is getting parsed and than
16
    the simulator and the sim object is created. Afterwards the run method of the simulator
Ahmad Reza's avatar
Ahmad Reza committed
17
18
19
20
21
22
23
24
25
    is called in which the simlator is going to start to run"""
    config = configparser.ConfigParser(allow_no_value=True)

    config.read("config.ini")
    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:
26
        scenario_file = config.get ("File", "scenario")
Ahmad Reza's avatar
Ahmad Reza committed
27
    except (configparser.NoOptionError) as noe:
28
        scenario_file = "init_scenario.py"
Ahmad Reza's avatar
Ahmad Reza committed
29
30
31
32
33

    try:
        solution_file = config.get("File", "solution")
    except (configparser.NoOptionError) as noe:
        solution_file = "solution.py"
34
35
36
37
38
39
40
41
42
    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")
    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")
Ahmad Reza's avatar
Ahmad Reza committed
43
44
45
46

    multiple_sim=0

    try:
47
        opts, args = getopt.getopt(argv, "hs:w:r:n:m:d:v:", ["solution=", "scenario="])
Ahmad Reza's avatar
Ahmad Reza committed
48
    except getopt.GetoptError:
49
        print('Error: run.py -r <randomeSeed> -w <scenario> -s <solution> -n <maxRounds>')
Ahmad Reza's avatar
Ahmad Reza committed
50
51
52
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
53
            print('run.py -r <randomeSeed> -w <scenario> -s <solution> -n <maxRounds>')
Ahmad Reza's avatar
Ahmad Reza committed
54
55
56
            sys.exit()
        elif opt in ("-s", "--solution"):
            solution_file = arg
57
58
        elif opt in ("-w", "--scenario"):
            scenario_file = arg
Ahmad Reza's avatar
Ahmad Reza committed
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
        elif opt in ("-r", "--seed"):
            seedvalue = int(arg)
        elif opt in ("-n", "--maxrounds"):
           max_round = int(arg)
        elif opt in ("-m"):
           multiple = int(arg)
        elif opt in ("-v"):
            visualization = arg


    #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')


    nTime = datetime.now().strftime('%Y-%m-%d_%H-%M-%S-%f')[:-1]
    dir_name =  nTime + "_" + solution_file.rsplit('.',1)[0] + "_" + \
                         str(seedvalue)
    if multiple_sim == 1:
        directory = "./outputs/mulitple/" + dir_name + "/" + str(seedvalue)
    else:
        directory = "./outputs/" + dir_name
    if not os.path.exists(directory):
        os.makedirs(directory)



    logging.info('Started')



89
90
91
92
93
94
    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)
Ahmad Reza's avatar
Ahmad Reza committed
95

96
    simulator.run()
Ahmad Reza's avatar
Ahmad Reza committed
97
98
99
100
    logging.info('Finished')


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