-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add wrapper to pause and run the simulation and rename package
- Loading branch information
1 parent
c333e02
commit 0889fa0
Showing
10 changed files
with
105 additions
and
27 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from simulationsandbox.two_d_simulation import SimpleSimulation | ||
from simulationsandbox.three_d_simulation import ThreeDSimulation | ||
|
||
SIMULATIONS = {"two_d": SimpleSimulation, | ||
"three_d": ThreeDSimulation} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import threading | ||
import time | ||
|
||
from jax import random | ||
|
||
from simulationsandbox.two_d_simulation import SimpleSimulation | ||
|
||
# Independant of any simulation environment | ||
class SimulationWrapper: | ||
""" | ||
Simulation wrapper to start, pause, resume and stop a simulation. | ||
Independant of any simulation type. | ||
""" | ||
def __init__(self, simulation, state, key): | ||
self.running = False | ||
self.paused = False | ||
self.stop_requested = False | ||
self.update_thread = None | ||
# simulation_dependent | ||
self.simulation = simulation | ||
self.state = state | ||
self.key = key | ||
|
||
def start(self): | ||
if not self.running: | ||
self.running = True | ||
self.stop_requested = False | ||
self.update_thread = threading.Thread(target=self.simulation_loop) | ||
self.update_thread.start() | ||
|
||
def pause(self): | ||
self.paused = True | ||
|
||
def resume(self): | ||
self.paused = False | ||
|
||
def stop(self): | ||
self.stop_requested = True | ||
|
||
def simulation_loop(self): | ||
while not self.stop_requested: | ||
if self.paused: | ||
time.sleep(0.1) | ||
continue | ||
|
||
self.state = self._update_simulation() | ||
print(f"{self.state = }") | ||
|
||
time.sleep(0.1) | ||
|
||
def _update_simulation(self): | ||
self.key, a_key, step_key = random.split(self.key, 3) | ||
actions = self.simulation.choose_action(self.state.obs, a_key) | ||
return self.simulation.step(self.state, actions, step_key) | ||
|
||
NUM_AGENTS = 5 | ||
MAX_AGENTS = 10 | ||
NUM_OBS = 3 | ||
GRID_SIZE = 20 | ||
SLEEP_TIME = 5 | ||
SEED = 0 | ||
|
||
key = random.PRNGKey(SEED) | ||
env = SimpleSimulation(MAX_AGENTS, GRID_SIZE) | ||
state = env.init_state(NUM_AGENTS, NUM_OBS, key) | ||
|
||
# Example usage: | ||
sim = SimulationWrapper(env, state, key) | ||
|
||
print('Started') | ||
sim.start_simulation() | ||
time.sleep(SLEEP_TIME) | ||
|
||
sim.pause_simulation() | ||
print('Paused') | ||
time.sleep(SLEEP_TIME) | ||
|
||
print('Resumed') | ||
sim.resume_simulation() | ||
time.sleep(SLEEP_TIME) | ||
|
||
sim.stop_simulation() | ||
print('stopped') |