-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Init state without configs #57
Changes from all commits
ec5c2a8
4534c5d
98046d4
b498f04
89ecef7
4308ce6
868074e
b86da7a
225c5f6
4ee97dc
d0e62e8
b7e98d9
fd96746
98a505b
0d42317
5bc67f3
adefcce
a54f920
efa959e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
jax==0.4.23 | ||
jaxlib==0.4.23 | ||
jax-md==0.2.8 | ||
scipy==1.12.0 | ||
|
||
# Interface | ||
panel==1.3.8 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import logging | ||
import argparse | ||
|
||
import numpy as np | ||
|
||
import vivarium.simulator.behaviors as behaviors | ||
from vivarium.controllers.config import SimulatorConfig, AgentConfig, ObjectConfig | ||
from vivarium.simulator.sim_computation import StateType | ||
from vivarium.simulator import behaviors | ||
from vivarium.simulator.states import init_simulator_state | ||
from vivarium.simulator.states import init_agent_state | ||
from vivarium.simulator.states import init_object_state | ||
from vivarium.simulator.states import init_nve_state | ||
from vivarium.simulator.states import init_state | ||
from vivarium.simulator.simulator import Simulator | ||
from vivarium.simulator.sim_computation import dynamics_rigid | ||
from vivarium.controllers.converters import set_state_from_config_dict | ||
from vivarium.simulator.grpc_server.simulator_server import serve | ||
|
||
lg = logging.getLogger(__name__) | ||
|
@@ -17,7 +17,9 @@ def parse_args(): | |
parser = argparse.ArgumentParser(description='Simulator Configuration') | ||
parser.add_argument('--box_size', type=float, default=100.0, help='Size of the simulation box') | ||
parser.add_argument('--n_agents', type=int, default=10, help='Number of agents') | ||
parser.add_argument('--n_existing_agents', type=int, default=10, help='Number of agents') | ||
parser.add_argument('--n_objects', type=int, default=2, help='Number of objects') | ||
parser.add_argument('--n_existing_objects', type=int, default=2, help='Number of agents') | ||
parser.add_argument('--num_steps-lax', type=int, default=4, help='Number of lax steps per loop') | ||
parser.add_argument('--dt', type=float, default=0.1, help='Time step size') | ||
parser.add_argument('--freq', type=float, default=40.0, help='Frequency parameter') | ||
|
@@ -37,36 +39,36 @@ def parse_args(): | |
|
||
logging.basicConfig(level=args.log_level.upper()) | ||
|
||
simulator_config = SimulatorConfig( | ||
simulator_state = init_simulator_state( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part works to show that we can use a list of coordinates, but I think it should be removed before the merge There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, this is meant to be deleted. I just added it to make the testing of the PR easier |
||
box_size=args.box_size, | ||
n_agents=args.n_agents, | ||
n_objects=args.n_objects, | ||
num_steps_lax=args.num_steps_lax, | ||
neighbor_radius=args.neighbor_radius, | ||
dt=args.dt, | ||
freq=args.freq, | ||
neighbor_radius=args.neighbor_radius, | ||
to_jit=args.to_jit, | ||
use_fori_loop=args.use_fori_loop, | ||
collision_eps=args.collision_eps, | ||
collision_alpha=args.collision_alpha | ||
) | ||
|
||
agent_configs = [AgentConfig(idx=i, | ||
x_position=np.random.rand() * simulator_config.box_size, | ||
y_position=np.random.rand() * simulator_config.box_size, | ||
orientation=np.random.rand() * 2. * np.pi) | ||
for i in range(simulator_config.n_agents)] | ||
agents_state = init_agent_state(simulator_state=simulator_state) | ||
|
||
objects_state = init_object_state(simulator_state=simulator_state) | ||
|
||
object_configs = [ObjectConfig(idx=simulator_config.n_agents + i, | ||
x_position=np.random.rand() * simulator_config.box_size, | ||
y_position=np.random.rand() * simulator_config.box_size, | ||
orientation=np.random.rand() * 2. * np.pi) | ||
for i in range(simulator_config.n_objects)] | ||
nve_state = init_nve_state( | ||
simulator_state=simulator_state, | ||
existing_agents=args.n_existing_agents, | ||
existing_objects=args.n_existing_objects, | ||
) | ||
|
||
state = set_state_from_config_dict({StateType.AGENT: agent_configs, | ||
StateType.OBJECT: object_configs, | ||
StateType.SIMULATOR: [simulator_config] | ||
}) | ||
state = init_state( | ||
simulator_state=simulator_state, | ||
agents_state=agents_state, | ||
objects_state=objects_state, | ||
nve_state=nve_state | ||
) | ||
|
||
simulator = Simulator(state, behaviors.behavior_bank, dynamics_rigid) | ||
|
||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from vivarium.simulator import behaviors | ||
from vivarium.simulator.states import init_simulator_state | ||
from vivarium.simulator.states import init_agent_state | ||
from vivarium.simulator.states import init_object_state | ||
from vivarium.simulator.states import init_nve_state | ||
from vivarium.simulator.states import init_state | ||
from vivarium.simulator.simulator import Simulator | ||
from vivarium.simulator.sim_computation import dynamics_rigid | ||
|
||
|
||
def test_init_simulator_no_args(): | ||
""" Test the initialization of state without arguments """ | ||
simulator_state = init_simulator_state() | ||
agents_state = init_agent_state(simulator_state=simulator_state) | ||
objects_state = init_object_state(simulator_state=simulator_state) | ||
nve_state = init_nve_state(simulator_state=simulator_state) | ||
|
||
state = init_state( | ||
simulator_state=simulator_state, | ||
agents_state=agents_state, | ||
objects_state=objects_state, | ||
nve_state=nve_state | ||
) | ||
|
||
simulator = Simulator(state, behaviors.behavior_bank, dynamics_rigid) | ||
|
||
assert simulator | ||
|
||
|
||
def test_init_simulator_args(): | ||
""" Test the initialization of state with arguments """ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might also want to test the case where we only provide a subset of the arguments, to see if it merges well with the default values There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another useful test to do would be to check if (some of) the values in the created state are indeed the ones that are provided in the args or defaults (e.g. just adding a few There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed I'll add that |
||
box_size = 100.0 | ||
n_agents = 10 | ||
n_objects = 2 | ||
col_eps = 0.1 | ||
col_alpha = 0.5 | ||
|
||
diameter = 5.0 | ||
friction = 0.1 | ||
behavior = 1 | ||
wheel_diameter = 2.0 | ||
speed_mul = 1.0 | ||
theta_mul = 1.0 | ||
prox_dist_max = 20.0 | ||
prox_cos_min = 0.0 | ||
color = "red" | ||
|
||
simulator_state = init_simulator_state( | ||
box_size=box_size, | ||
n_agents=n_agents, | ||
n_objects=n_objects, | ||
collision_eps=col_eps, | ||
collision_alpha=col_alpha) | ||
|
||
nve_state = init_nve_state( | ||
simulator_state, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong indentation |
||
diameter=diameter, | ||
friction=friction) | ||
|
||
agent_state = init_agent_state( | ||
simulator_state, | ||
behavior=behavior, | ||
wheel_diameter=wheel_diameter, | ||
speed_mul=speed_mul, | ||
theta_mul=theta_mul, | ||
prox_dist_max=prox_dist_max, | ||
prox_cos_min=prox_cos_min) | ||
|
||
object_state = init_object_state( | ||
simulator_state, | ||
color=color) | ||
|
||
state = init_state( | ||
simulator_state=simulator_state, | ||
agents_state=agent_state, | ||
objects_state=object_state, | ||
nve_state=nve_state) | ||
|
||
simulator = Simulator(state, behaviors.behavior_bank, dynamics_rigid) | ||
|
||
assert simulator |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from vivarium.simulator import behaviors | ||
from vivarium.simulator.states import init_simulator_state | ||
from vivarium.simulator.states import init_agent_state | ||
from vivarium.simulator.states import init_object_state | ||
from vivarium.simulator.states import init_nve_state | ||
from vivarium.simulator.states import init_state | ||
from vivarium.simulator.simulator import Simulator | ||
from vivarium.simulator.sim_computation import dynamics_rigid | ||
|
||
NUM_STEPS = 50 | ||
|
||
def test_simulator_run(): | ||
simulator_state = init_simulator_state() | ||
|
||
agents_state = init_agent_state(simulator_state=simulator_state) | ||
|
||
objects_state = init_object_state(simulator_state=simulator_state) | ||
|
||
nve_state = init_nve_state(simulator_state=simulator_state) | ||
|
||
state = init_state( | ||
simulator_state=simulator_state, | ||
agents_state=agents_state, | ||
objects_state=objects_state, | ||
nve_state=nve_state | ||
) | ||
|
||
simulator = Simulator(state, behaviors.behavior_bank, dynamics_rigid) | ||
|
||
simulator = Simulator(state, behaviors.behavior_bank, dynamics_rigid) | ||
|
||
simulator.run(threaded=False, num_steps=NUM_STEPS) | ||
|
||
assert simulator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with your proposition that " it would make more sense to rename the current n_agents to max_agents, and n_existing_agents to n_agents (same for objects)."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if you already do it, but it will be useful to add an
assert
checking that the number of existing agents/objects does not exceed the max numberThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I already check this