From 263b4e0e336197b49afcb0ca9c29424aef6b9c15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Mon, 22 May 2023 20:29:25 +0200 Subject: [PATCH] Minor fixes * Add option to set scheduler and environment start times * Add `Agent.env` method to ease migration of earlier code --- soil/VERSION | 2 +- soil/agents/__init__.py | 7 +++++++ soil/environment.py | 16 +++++++++++++++- soil/time.py | 12 ++++++------ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/soil/VERSION b/soil/VERSION index 9de6f86..00267f4 100644 --- a/soil/VERSION +++ b/soil/VERSION @@ -1 +1 @@ -1.0.0rc6 +1.0.0rc9 \ No newline at end of file diff --git a/soil/agents/__init__.py b/soil/agents/__init__.py index ce5f7da..4a4fbc1 100644 --- a/soil/agents/__init__.py +++ b/soil/agents/__init__.py @@ -87,6 +87,13 @@ class BaseAgent(MesaAgent, MutableMapping, metaclass=MetaAgent): print(msg, file=sys.stderr) return self.unique_id + @property + def env(self): + msg = "This attribute is deprecated. Use `model` instead" + warnings.warn(msg, DeprecationWarning) + print(msg, file=sys.stderr) + return self.model + def __getitem__(self, key): try: return getattr(self, key) diff --git a/soil/environment.py b/soil/environment.py index 8b9e523..e49ef4e 100644 --- a/soil/environment.py +++ b/soil/environment.py @@ -3,6 +3,7 @@ from __future__ import annotations import os import sys import logging +from datetime import datetime from typing import Any, Callable, Dict, Optional, Union, List, Type from types import coroutine @@ -33,6 +34,8 @@ class BaseEnvironment(Model): collector_class = datacollection.SoilCollector schedule_class = time.TimedActivation + start_time = 0 + time_format = "%Y-%m-%d %H:%M:%S" def __new__(cls, *args: Any, @@ -75,6 +78,8 @@ class BaseEnvironment(Model): collector_class: type = datacollection.SoilCollector, agent_reporters: Optional[Any] = None, model_reporters: Optional[Any] = None, + start_time=None, + time_format=None, tables: Optional[Any] = None, init: bool = True, **env_params, @@ -91,12 +96,21 @@ class BaseEnvironment(Model): self.logger = logger else: self.logger = utils.logger.getChild(self.id) + if start_time is not None: + self.start_time = start_time + if time_format is not None: + self.time_format = time_format + + if isinstance(self.start_time, str): + self.start_time = datetime.strptime(self.start_time, self.time_format) + if isinstance(self.start_time, datetime): + self.start_time = self.start_time.timestamp() self.schedule = schedule if schedule is None: if schedule_class is None: schedule_class = self.schedule_class - self.schedule = schedule_class(self) + self.schedule = schedule_class(self, time=self.start_time) for (k, v) in env_params.items(): self[k] = v diff --git a/soil/time.py b/soil/time.py index e7b815f..4f27e7b 100644 --- a/soil/time.py +++ b/soil/time.py @@ -62,10 +62,10 @@ class PQueueSchedule: If no delay is returned, a default of 1 is used. """ - def __init__(self, shuffle=True, seed=None, **kwargs): + def __init__(self, shuffle=True, seed=None, time=0, **kwargs): self._queue = [] self._shuffle = shuffle - self.time = 0 + self.time = time self.steps = 0 self.random = random_std.Random(seed) self.next_time = self.time @@ -134,10 +134,10 @@ class PQueueSchedule: class Schedule: - def __init__(self, shuffle=True, seed=None, **kwargs): + def __init__(self, shuffle=True, seed=None, time=0, **kwargs): self._queue = deque() self._shuffle = shuffle - self.time = 0 + self.time = time self.steps = 0 self.random = random_std.Random(seed) self.next_time = self.time @@ -226,12 +226,12 @@ class Schedule: class InnerActivation(BaseScheduler): inner_class = Schedule - def __init__(self, model, shuffle=True, **kwargs): + def __init__(self, model, shuffle=True, time=0, **kwargs): self.model = model self.logger = getattr(self.model, "logger", logger).getChild(f"time_{ self.model }") self._agents = {} self.agents_by_type = defaultdict(dict) - self.inner = self.inner_class(shuffle=shuffle, seed=self.model._seed) + self.inner = self.inner_class(shuffle=shuffle, seed=self.model._seed, time=time) @property def steps(self):