Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Ahmad Reza Cheraghi
swarm-sim
Commits
b43d3d20
Commit
b43d3d20
authored
Apr 12, 2019
by
Ahmad Reza Cheraghi
Browse files
some code cleaning based on the results of sonarqube
parent
6566ff6a
Changes
1
Hide whitespace changes
Inline
Side-by-side
run.py
View file @
b43d3d20
...
...
@@ -10,40 +10,44 @@ from datetime import datetime
from
lib
import
sim
def
swarm_sim
(
argv
):
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"
self
.
size_x
=
config
.
getint
(
"Simulator"
,
"size_x"
)
self
.
size_y
=
config
.
getint
(
"Simulator"
,
"size_y"
)
self
.
window_size_x
=
config
.
getint
(
"Simulator"
,
"window_size_x"
)
self
.
window_size_y
=
config
.
getint
(
"Simulator"
,
"window_size_y"
)
self
.
border
=
config
.
getint
(
"Simulator"
,
"border"
)
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"
)
self
.
location_mm_size
=
config
.
getint
(
"Matter"
,
"location_mm_size"
)
self
.
dir_name
=
None
def
swarm_sim
(
argv
):
"""In the main function first the config is getting parsed and than
the simulator and the sim object is created. Afterwards the run method of the simulator
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
:
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")
config_data
=
ConfigData
(
config
)
multiple_sim
=
0
local_time
=
datetime
.
now
().
strftime
(
'%Y-%m-%d_%H:%M:%S'
)[:
-
1
]
try
:
opts
,
args
=
getopt
.
getopt
(
argv
,
"hs:w:r:n:m:d:v:"
,
[
"solution="
,
"scenario="
])
except
getopt
.
GetoptError
:
...
...
@@ -54,49 +58,42 @@ def swarm_sim(argv):
print
(
'run.py -r <randomeSeed> -w <scenario> -s <solution> -n <maxRounds>'
)
sys
.
exit
()
elif
opt
in
(
"-s"
,
"--solution"
):
solution
_file
=
arg
config_data
.
solution
=
arg
elif
opt
in
(
"-w"
,
"--scenario"
):
scenario
_file
=
arg
config_data
.
scenario
=
arg
elif
opt
in
(
"-r"
,
"--seed"
):
seedvalue
=
int
(
arg
)
config_data
.
seedvalue
=
int
(
arg
)
elif
opt
in
(
"-n"
,
"--maxrounds"
):
max_round
=
int
(
arg
)
config_data
.
max_round
=
int
(
arg
)
elif
opt
in
(
"-m"
):
multiple_sim
=
int
(
arg
)
elif
opt
in
(
"-v"
):
visualization
=
int
(
arg
)
config_data
.
visualization
=
int
(
arg
)
elif
opt
in
(
"-d"
):
act_date
=
arg
local_time
=
str
(
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
+
"_"
+
scenario_file
.
rsplit
(
'.'
,
1
)[
0
]
+
"_"
+
solution_file
.
rsplit
(
'.'
,
1
)[
0
]
+
"_"
+
\
str
(
seedvalue
)
if
multiple_sim
==
1
:
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
)
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/mulitple/"
+
config_data
.
dir_name
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
)
logging
.
info
(
'Started'
)
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
,)
simulator
=
sim
.
Sim
(
config_data
)
simulator
.
run
()
logging
.
info
(
'Finished'
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment