Skip to content

Fix custom plant agents support #470

Description

@ezio-melotti

It should be possible to define custom agents (including plants), by adding their definition to the game_config, at the top level. The convert_configuration() function will extract certain items from the game_config, and assume that all the remaining top-level items are agents, copying them in full_game_config['agents']:

for agent_id, agent in working_config.items():
full_game_config['agents'][agent_id] = agent

Plants however are handled differently:

if 'plants' in working_config and isinstance(working_config['plants'], list):
plants = working_config.pop('plants')
for plant in plants:
amount = plant.get('amount', 0) or 0
plant_type = plant.get('species', None)
if not (plant_type and amount):
continue
plants_in_config.append(plant_type)
working_config[plant_type] = dict(amount=amount)
if is_b2:
working_config[plant_type]['properties'] = {
'crop_management_factor': {'value': crop_mgmt_factor},
'density_factor': {'value': 0.5}
}

Lines 296-297 extract the species and amount, and line 301 copies the plant agent to the top level. With this approach:

  • If a custom plant is defined at the top level of the game_config (aka working_config), line 301 will overwrite its definition leaving only the name and amount;
  • if a custom plant is defined in game_config['plants'] all the additional information (flows, properties, capacity, etc.) will be ignored, since the code only reads species and amount

While testing, we were able to work around the issue by implementing the following changes to preserve the additional information:

@@ -294,11 +294,11 @@ def convert_configuration(game_config, agent_desc=None, save_output=False):
         plants = working_config.pop('plants')
         for plant in plants:
             amount = plant.get('amount', 0) or 0
-            plant_type = plant.get('species', None)
+            plant_type = plant.pop('species')
             if not (plant_type and amount):
                 continue
             plants_in_config.append(plant_type)
-            working_config[plant_type] = dict(amount=amount)
+            working_config[plant_type] = plant
             if is_b2:
                 working_config[plant_type]['properties'] = {
                     'crop_management_factor': {'value': crop_mgmt_factor},

(the is_b2 case also needs to be handled, since it overrides the properties)

Later in the code flows were getting overridden too, so we tried to update the lamp by hardcoding a workaround, but it doesn't seem to work (and it should be fixed properly anyway):

@@ -320,7 +320,10 @@ def convert_configuration(game_config, agent_desc=None, save_output=False):
                                            'flows': {'out': {'par': {'connections': [lamp_id]}}}}
                 # Add connection to plant 'par' flow
                 par_flow_stub = {'in': {'par': {'connections': [lamp_id]}}}
-                working_config[species]['flows'] = par_flow_stub
+                if 'flows' in working_config[species]:
+                    working_config[species]['flows']['in']['par']['connections'].append(lamp_id)
+                else:
+                    working_config[species]['flows'] = par_flow_stub

Unless we are missing something, custom plant agents don't seem to currently be supported. If this is true, a proper fix that adds custom plants support by doing something similar to the workarounds pasted above should be implemented. @granawkins, can you confirm that this is necessary and that the approach we followed (passing the custom plant in game_config['plants']) makes sense?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions