Commit 8292015a authored by Karol Actun's avatar Karol Actun
Browse files

reset bug fix

parent 76f45342
......@@ -175,11 +175,11 @@ def sim_tab():
reset_button = QPushButton("reset Simulation")
def reset_sim():
world.reset()
start_stop_button.setText("start Simulation")
start_stop_button.setIcon(start_stop_button.style().standardIcon(QStyle.SP_MediaPlay))
status.setText("Simulation not running")
status.setStyleSheet("font-weight:bold;color:#bb4444;")
vis.set_reset_flag()
reset_button.clicked.connect(reset_sim)
......
......@@ -14,6 +14,11 @@ from lib.visualization.toms_svg_generator import create_svg
from lib.visualization.utils import LoadingWindow, show_msg
class ResetException(Exception):
def __init__(self):
super(ResetException, self).__init__()
def close(_):
exit(0)
......@@ -29,6 +34,7 @@ class Visualization:
self._world = world
self._last_light_rotation = 0
self._rounds_per_second = 10
self._reset_flag = False
self._running = False
self._app = None
self._viewer = None
......@@ -113,13 +119,18 @@ class Visualization:
# waiting for the simulation window to be fully active
while not self._splitter.windowHandle().isExposed():
self._app.processEvents()
self._process_events()
# first update and draw call.
self._viewer.update_scene()
def is_recording(self):
return self._recording
def _process_events(self):
self._app.processEvents()
if self._reset_flag:
raise ResetException()
def reset(self):
"""
stops the simulation.
......@@ -127,6 +138,7 @@ class Visualization:
resets the camera
:return:
"""
self._reset_flag = False
self._running = False
self._viewer.particle_offset_data = {}
self._viewer.particle_update_flag = True
......@@ -153,7 +165,7 @@ class Visualization:
self._gui.setDisabled(True)
thread.start()
while thread.is_alive():
self._app.processEvents()
self._process_events()
thread.join()
loading_window.close()
self._gui.setDisabled(False)
......@@ -188,7 +200,7 @@ class Visualization:
:return:
"""
sleep_time = 1.0 / 120.0
self._app.processEvents()
self._process_events()
if self.light_rotation:
self.rotate_light()
while not self._running:
......@@ -196,7 +208,7 @@ class Visualization:
time.sleep(sleep_time)
if self.light_rotation:
self.rotate_light()
self._app.processEvents()
self._process_events()
def animate(self, round_start_time, speed):
"""
......@@ -224,7 +236,7 @@ class Visualization:
# animate
for i in range(1, steps):
self._viewer.set_animation_percentage(float(i / steps))
self._app.processEvents()
self._process_events()
self._viewer.glDraw()
# draw the last frame. its outside of the loop, so that if steps is equal or less then one,
......@@ -328,7 +340,6 @@ class Visualization:
self._viewer.tile_offset_data[tile] = (tile.coordinates, tile.color, prev_pos,
1.0 if tile.get_tile_status() else 0.0)
def remove_location(self, location):
"""
removes a location from the visualization.
......@@ -559,7 +570,7 @@ class Visualization:
# loop
while self.recorder.is_open():
self._app.processEvents()
self._process_events()
# go back to the main window
if "set_disable_sim" in dir(self._gui_module):
......@@ -610,7 +621,7 @@ class Visualization:
# animate
for j in range(1, animation_steps+1):
# process events so the gui thread does respond to interactions..
self._app.processEvents()
self._process_events()
# update loading windows text and progress bar
processing = (i - first_frame_idx + 1)*animation_steps + j
lw.set_message("Please wait!\nExporting frame %d/%d..." % (processing, out_of))
......@@ -680,3 +691,6 @@ class Visualization:
def get_main_window(self):
return self._splitter
def set_reset_flag(self):
self._reset_flag = True
......@@ -120,10 +120,13 @@ class World:
if self.config_data.visualization:
self.vis.reset()
# reload solution module (resets global variables)
mod = importlib.import_module('solution.' + self.config_data.solution)
importlib.reload(mod)
# reload scenario module (resets global variables)
mod = importlib.import_module('scenario.' + self.config_data.scenario)
importlib.reload(mod)
if self.config_data.visualization:
# if visualization is on, run the scenario in a separate thread and show that the program runs..
......
......@@ -8,6 +8,7 @@ import time
import random
from lib import world, config
from lib.gnuplot_generator import gnuplot_generator
from lib.vis3d import ResetException
def swarm_sim(argv):
......@@ -18,37 +19,33 @@ def swarm_sim(argv):
logging.info('Started')
config_data = config.ConfigData()
read_cmd_args(argv, config_data)
create_directory_for_data(config_data)
random.seed(config_data.seed_value)
swarm_sim_world = world.World(config_data)
main_loop(config_data, swarm_sim_world)
logging.info('Finished')
generate_data(config_data, swarm_sim_world)
def main_loop(config_data, swarm_sim_world):
round_start_timestamp = time.perf_counter()
while (config_data.max_round == 0 or swarm_sim_world.get_actual_round() <= config_data.max_round) \
and swarm_sim_world.get_end() is False:
if config_data.visualization:
swarm_sim_world.vis.run(round_start_timestamp)
round_start_timestamp = time.perf_counter()
try:
if config_data.visualization:
swarm_sim_world.vis.run(round_start_timestamp)
round_start_timestamp = time.perf_counter()
run_solution(swarm_sim_world)
except ResetException:
swarm_sim_world.reset()
run_solution(swarm_sim_world)
if config_data.visualization:
swarm_sim_world.vis.run(round_start_timestamp)
logging.info('Finished')
generate_data(config_data, swarm_sim_world)
def draw_scenario(config_data, swarm_sim_world):
mod = importlib.import_module('scenario.' + config_data.scenario)
mod.scenario(swarm_sim_world)
if config_data.particle_random_order:
random.shuffle(swarm_sim_world.particles)
def read_cmd_args(argv, config_data):
try:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment