1
0
mirror of https://github.com/gsi-upm/soil synced 2024-12-22 08:18:13 +00:00

Minor fixes

* Add option to set scheduler and environment start times
* Add `Agent.env` method to ease migration of earlier code
This commit is contained in:
J. Fernando Sánchez 2023-05-22 20:29:25 +02:00
parent 189836408f
commit 263b4e0e33
4 changed files with 29 additions and 8 deletions

View File

@ -1 +1 @@
1.0.0rc6 1.0.0rc9

View File

@ -87,6 +87,13 @@ class BaseAgent(MesaAgent, MutableMapping, metaclass=MetaAgent):
print(msg, file=sys.stderr) print(msg, file=sys.stderr)
return self.unique_id 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): def __getitem__(self, key):
try: try:
return getattr(self, key) return getattr(self, key)

View File

@ -3,6 +3,7 @@ from __future__ import annotations
import os import os
import sys import sys
import logging import logging
from datetime import datetime
from typing import Any, Callable, Dict, Optional, Union, List, Type from typing import Any, Callable, Dict, Optional, Union, List, Type
from types import coroutine from types import coroutine
@ -33,6 +34,8 @@ class BaseEnvironment(Model):
collector_class = datacollection.SoilCollector collector_class = datacollection.SoilCollector
schedule_class = time.TimedActivation schedule_class = time.TimedActivation
start_time = 0
time_format = "%Y-%m-%d %H:%M:%S"
def __new__(cls, def __new__(cls,
*args: Any, *args: Any,
@ -75,6 +78,8 @@ class BaseEnvironment(Model):
collector_class: type = datacollection.SoilCollector, collector_class: type = datacollection.SoilCollector,
agent_reporters: Optional[Any] = None, agent_reporters: Optional[Any] = None,
model_reporters: Optional[Any] = None, model_reporters: Optional[Any] = None,
start_time=None,
time_format=None,
tables: Optional[Any] = None, tables: Optional[Any] = None,
init: bool = True, init: bool = True,
**env_params, **env_params,
@ -91,12 +96,21 @@ class BaseEnvironment(Model):
self.logger = logger self.logger = logger
else: else:
self.logger = utils.logger.getChild(self.id) 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 self.schedule = schedule
if schedule is None: if schedule is None:
if schedule_class is None: if schedule_class is None:
schedule_class = self.schedule_class 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(): for (k, v) in env_params.items():
self[k] = v self[k] = v

View File

@ -62,10 +62,10 @@ class PQueueSchedule:
If no delay is returned, a default of 1 is used. 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._queue = []
self._shuffle = shuffle self._shuffle = shuffle
self.time = 0 self.time = time
self.steps = 0 self.steps = 0
self.random = random_std.Random(seed) self.random = random_std.Random(seed)
self.next_time = self.time self.next_time = self.time
@ -134,10 +134,10 @@ class PQueueSchedule:
class Schedule: class Schedule:
def __init__(self, shuffle=True, seed=None, **kwargs): def __init__(self, shuffle=True, seed=None, time=0, **kwargs):
self._queue = deque() self._queue = deque()
self._shuffle = shuffle self._shuffle = shuffle
self.time = 0 self.time = time
self.steps = 0 self.steps = 0
self.random = random_std.Random(seed) self.random = random_std.Random(seed)
self.next_time = self.time self.next_time = self.time
@ -226,12 +226,12 @@ class Schedule:
class InnerActivation(BaseScheduler): class InnerActivation(BaseScheduler):
inner_class = Schedule inner_class = Schedule
def __init__(self, model, shuffle=True, **kwargs): def __init__(self, model, shuffle=True, time=0, **kwargs):
self.model = model self.model = model
self.logger = getattr(self.model, "logger", logger).getChild(f"time_{ self.model }") self.logger = getattr(self.model, "logger", logger).getChild(f"time_{ self.model }")
self._agents = {} self._agents = {}
self.agents_by_type = defaultdict(dict) 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 @property
def steps(self): def steps(self):