Skip to content
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

[WIP] Params managing #55

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Empty file added abm/parameters/__init__.py
Empty file.
57 changes: 57 additions & 0 deletions abm/parameters/agent_parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from pydantic import BaseSettings, BaseModel


class DecisionParameters(BaseModel):
"""Decision variables"""
# NOTE: error when trying to parse an uppercase variables from .env
# SEE: https://github.com/pydantic/pydantic/issues/3936#issuecomment-1152903692
# W
t_w: float = 0.5
eps_w: float = 3
g_w: float = 0.085
b_w: float = 0
w_max: float = 1

# U
t_u: float = 0.5
eps_u: float = 3
g_u: float = 0.085
b_u: float = 0
u_max: float = 1

# Inhibition
s_wu: float = 0.25
s_uw: float = 0.01

# Calculating Private Information
tau: int = 10
f_n: float = 2
f_r: float = 1


class MovementParameters(BaseModel):
"""Movement variables"""

# Exploration movement parameters
exp_vel_min: float = 1
exp_vel_max: float = 1
exp_theta_min: float = -0.3
exp_theta_max: float = 0.3

# Relocation movement parameters
reloc_des_vel: float = 1
reloc_theta_max: float = 0.5

# Exploitation params
# deceleration when a patch is reached
exp_stop_ratio: float = 0.08


class AgentParameters(BaseSettings):
agent_movement: MovementParameters
agent_decision: DecisionParameters

class Config:
env_file = '.env'
env_nested_delimiter = '__'

60 changes: 60 additions & 0 deletions abm/parameters/playground_parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from pydantic import BaseSettings, BaseModel


class PlaygroundEnvironmentParameters(BaseModel):
"""Environment variables"""
t: int = 1000
width: int = 500
height: int = 500
framerate: int = 30 # interactive
window_pad: int = 30
use_ram_logging: bool = False
use_zarr: bool = True
save_csv_files: bool = False
parallel: bool = False


class PlaygroundUIParameters(BaseModel):
"""Playground parameters"""
with_visualization: bool = True
show_vis_field: bool = False
show_vis_field_return: bool = True
show_vision_range: bool = True


class PlaygroundAgentParameters(BaseModel):
"""Agent variables"""
n: int = 3
pooling_time: int = 0
pooling_prob: float = 0
agent_radius: int = 10
agent_consumption: int = 1
vision_range: int = 2000
agent_fov: float = 1 # interactive
visual_exclusion: bool = True # interactive
ghost_mode: bool = True # interactive
patchwise_exclusion: bool = True
collide_agents: bool = False


class PlaygroundResourceParameters(BaseModel):
"""Resource variables"""
min_resc_perpatch: int = 200
max_resc_perpatch: int = 201
min_resc_quality: float = 0.25
max_resc_quality: float = 0.25
patch_radius: int = 15 # interactive
regenerate_patches: bool = True
teleport_exploit: bool = False
allow_border_patch_overlap: bool = True


class PlaygroundParameters(BaseSettings):
environment: PlaygroundEnvironmentParameters
ui: PlaygroundUIParameters
agent: PlaygroundAgentParameters
resource: PlaygroundResourceParameters

class Config:
env_file = '.env'
env_nested_delimiter = '__'
Empty file.
30 changes: 30 additions & 0 deletions abm/parameters/tests/data/test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Decision process parameters
AGENT_DECISION__T_W=0.2
AGENT_DECISION__EPS_W=2
AGENT_DECISION__G_W=0.085
AGENT_DECISION__B_W=0
AGENT_DECISION__W_MAX=1

AGENT_DECISION__T_U=0.5
AGENT_DECISION__EPS_U=3
AGENT_DECISION__G_U=0.085
AGENT_DECISION__B_U=0
AGENT_DECISION__U_MAX=1

AGENT_DECISION__S_WU=0.25
AGENT_DECISION__S_UW=0.01

AGENT_DECISION__TAU=10
AGENT_DECISION__F_N=2
AGENT_DECISION__F_R=1

# Movement parameters
# Exploration
AGENT_MOVEMENT__EXP_VEL_MIN=0.5
AGENT_MOVEMENT__EXP_VEL_MAX=1
AGENT_MOVEMENT__EXP_THETA_MIN=-0.3
AGENT_MOVEMENT__EXP_THETA_MAX=0.3

# Relocation
AGENT_MOVEMENT__RELOC_DES_VEL=0.5
AGENT_MOVEMENT__RELOC_THETA_MAX=0.5
20 changes: 20 additions & 0 deletions abm/parameters/tests/test_agent_parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pathlib import Path

from abm.parameters.agent_parameters import AgentParameters


def test_agent_parameters():
"""
Testing agent parameters
SEE: https://pydantic-docs.helpmanual.io/usage/settings/#dotenv-env-support
"""
test_env_file = Path(__file__).parent / 'data' / 'test.env'

# NOTE: _env_file overrides the default Config in the class
params = AgentParameters(_env_file=test_env_file)

# testing whether the nested parameters are parsed correctly
assert params.agent_decision.t_w == 0.2
assert params.agent_decision.eps_w == 2.0
assert params.agent_movement.exp_vel_min == 0.5
assert params.agent_movement.reloc_des_vel == 0.5
Empty file.
67 changes: 67 additions & 0 deletions abm/simulation/tests/data/test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
UI__WITH_VISUALIZATION=0
UI__SHOW_VISUAL_FIELDS=0
UI__SHOW_VISUAL_FIELDS_RETURN=1
UI__SHOW_VISION_RANGE=1

# Simulation time
ENVIRONMENT__T=1000
ENVIRONMENT__WIDTH=600
ENVIRONMENT__HEIGHT=600
ENVIRONMENT__FRAMERATE=25
ENVIRONMENT__WINDOW_PAD=30
ENVIRONMENT__USE_RAM_LOGGING=1
ENVIRONMENT__USE_IFDB_LOGGING=0
ENVIRONMENT__USE_ZARR=1
ENVIRONMENT__SAVE_CSV_FILES=0

RESOURCE__PATH_RADIUS=45
RESOURCE__MIN_RES_PERPATCH=400
RESOURCE__MAX_RES_PERPATCH=450
RESOURCE__REGENERATE_PATCHES=1
RESOURCE__ALLOW_BORDER_PATCH_OVERLAP=1
RESOURCE__TELEPORT_EXPLOIT=1
RESOURCE__MIN_RESC_QUALITY=0.05
RESOURCE__MAX_RESC_QUALITY=0.3

AGENT__N=3
AGENT__POOLING_TIME=0
AGENT__POOLING_PROB=0.05
AGENT__RADIUS_AGENT=10
AGENT__AGENT_CONSUMPTION=1
AGENT__VISION_RANGE=200
AGENT__AGENT_FOV=0.45
AGENT__VISUAL_EXCLUSION=1
AGENT__GHOST_MODE=1
AGENT__PATCHWISE_EXCLUSION=1
AGENT__COLLIDE_AGENTS=0

# Decision process parameters
AGENT_DECISION__T_W=0.2
AGENT_DECISION__EPS_W=2
AGENT_DECISION__G_W=0.085
AGENT_DECISION__B_W=0
AGENT_DECISION__W_MAX=1

AGENT_DECISION__T_U=0.5
AGENT_DECISION__EPS_U=3
AGENT_DECISION__G_U=0.085
AGENT_DECISION__B_U=0
AGENT_DECISION__U_MAX=1

AGENT_DECISION__S_WU=0.25
AGENT_DECISION__S_UW=0.01

AGENT_DECISION__TAU=10
AGENT_DECISION__F_N=2
AGENT_DECISION__F_R=1

# Movement parameters
# Exploration
AGENT_MOVEMENT__EXP_VEL_MIN=0.5
AGENT_MOVEMENT__EXP_VEL_MAX=1
AGENT_MOVEMENT__EXP_THETA_MIN=-0.3
AGENT_MOVEMENT__EXP_THETA_MAX=0.3

# Relocation
AGENT_MOVEMENT__RELOC_DES_VEL=0.5
AGENT_MOVEMENT__RELOC_THETA_MAX=0.5
31 changes: 31 additions & 0 deletions abm/simulation/tests/test_simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
from pathlib import Path

from xvfbwrapper import Xvfb

from abm.parameters.playground_parameters import PlaygroundParameters
from abm.simulation.sims import Simulation


def test_init_simulation():
"""
Testing simulation
"""
test_env_file = Path(__file__).parent / 'data' / 'test.env'

playground_params = PlaygroundParameters(_env_file=test_env_file)

# merge all parameters into one dict
kwargs = {
**playground_params.environment.dict(),
**playground_params.ui.dict(),
**playground_params.agent.dict(),
**playground_params.resource.dict()
}

n = kwargs.pop('n')
t = kwargs.pop('t')
os.environ['SDL_VIDEODRIVER'] = 'dummy'
with Xvfb(width=playground_params.environment.width,
height=playground_params.environment.height) as _:
sim = Simulation(n, t, **kwargs)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package_data={'p34abm': ['*.txt']},
python_requires=">=3.7",
install_requires=[
'pydantic',
'pygame',
'pygame-widgets',
'numpy',
Expand Down