1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-13 23:12:28 +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)
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)

View File

@ -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

View File

@ -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):