gyorilab / mira

MIRA modeling framework
BSD 2-Clause "Simplified" License
9 stars 7 forks source link

Unable to export regnet as JSON after stratification #275

Closed brandomr closed 7 months ago

brandomr commented 7 months ago

After stratifying a regnet the export to JSON fails with a basic example for some reason. I also noticed that regnet's don't have the normal export (e.g. from mira.modeling.amr.petrinet import template_model_to_petrinet_json).

Is this user error on my end or a bug, etc?

from mira.sources import amr
from mira.modeling import Model
from mira.metamodel.ops import stratify

from mira.modeling.amr.petrinet import template_model_to_petrinet_json
from mira.modeling.amr.regnet import AMRRegNetModel

model = amr.regnet.model_from_url('https://raw.githubusercontent.com/DARPA-ASKEM/Model-Representations/main/regnet/examples/lotka_volterra.json')

model_2_city = stratify(
    model,
    key="city",
    strata=[
        "Toronto",
        "Montreal",
    ],
)

# export to Regnet JSON works on original model
AMRRegNetModel(Model(model)).to_json()

# but export of stratified model doesn't
###### THIS FAILS ######
AMRRegNetModel(Model(model_2_city)).to_json()
bgyori commented 7 months ago

There is a little bit of a problem with this model. When you stratify a model like this, in addition to duplicating the regulatory structure to two cities, by default, movement is introduced between the two cities. While MIRA supports this internally, this is not something that the regnet framework can represent. So the root cause of the failure here is not a bug but a limitation of the regnet framework.

To avoid this, you can modify the stratification as

model_2_city = stratify(
    model,
    key="city",
    strata=[
        "Toronto",
        "Montreal",
    ],
    structure=[],
)

where structure=[] ensures that there is no movement between the two locations which makes the model fit into the regnet framework.

brandomr commented 7 months ago

Got it, thanks for the clarification; I'll add that handler in the case the user wants to stratify over a regnet. FYI @mwdchang @Tom-Szendrey