Coverage for agent_model/agents/lamp.py: 100%
52 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-05-04 13:14 +0700
« prev ^ index » next coverage.py v7.2.3, created at 2023-05-04 13:14 +0700
1import numpy as np
2from . import BaseAgent
4class LampAgent(BaseAgent):
5 default_attributes = {
6 'daily_growth_factor': 1,
7 'par_rate': 1,
8 'photoperiod': 12,
9 }
10 default_capacity = {
11 'par': 5,
12 }
13 def __init__(self, *args, attributes=None, capacity=None, **kwargs):
14 attributes = {} if attributes is None else attributes
15 attributes = {**self.default_attributes, **attributes}
16 capacity = {} if capacity is None else capacity
17 capacity = {**self.default_capacity, **capacity}
18 super().__init__(*args, attributes=attributes, capacity=capacity, **kwargs)
19 # -- NON_SERIALIZED
20 self.connected_plants = []
21 self.daily_growth = []
22 self.lamp_configuration = {}
24 def _update_lamp_attributes(self):
25 # Scale the number of lamps to the number of active plants
26 lamp_configuration = {p: self.model.agents[p].active
27 for p in self.connected_plants}
28 if lamp_configuration == self.lamp_configuration:
29 return
30 self.lamp_configuration = lamp_configuration
31 self.active = sum(lamp_configuration.values())
32 # Set the photoperiod and par_rate to the max required by any plant
33 steps_per_day = 24
34 photoperiod = 0
35 par_rate = 0
36 for plant_id in self.connected_plants:
37 plant = self.model.agents[plant_id]
38 if plant.active > 0:
39 photoperiod = max(photoperiod, plant.properties['photoperiod']['value'])
40 par_baseline = plant.properties['par_baseline']['value']
41 par_rate = max(par_rate, par_baseline * steps_per_day / photoperiod)
42 self.attributes['photoperiod'] = photoperiod
43 self.attributes['par_rate'] = par_rate
44 # Update the daily growth
45 photo_start = (steps_per_day - photoperiod) // 2
46 photo_end = photo_start + photoperiod
47 self.daily_growth = np.zeros(steps_per_day)
48 self.daily_growth[photo_start:photo_end] = par_rate
50 def register(self, record_initial_state=False):
51 self.connected_plants = []
52 for agent_id, agent in self.model.agents.items():
53 if ('par' in agent.flows['in'] and
54 self.agent_id in agent.flows['in']['par']['connections']):
55 self.connected_plants.append(agent_id)
56 if self.connected_plants:
57 self._update_lamp_attributes()
58 else:
59 self.active = 0
60 super().register(record_initial_state)
62 def step(self, dT=1):
63 if not self.registered:
64 self.register()
65 self.storage['par'] = 0
66 self._update_lamp_attributes()
67 hour_of_day = self.model.time.hour
68 self.attributes['daily_growth_factor'] = self.daily_growth[hour_of_day]
69 super().step(dT)