Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Towards a MVC/MVVM architecture for better portrayals? #1441

Open
wang-boyu opened this issue Oct 8, 2022 · 2 comments
Open

Towards a MVC/MVVM architecture for better portrayals? #1441

wang-boyu opened this issue Oct 8, 2022 · 2 comments

Comments

@wang-boyu
Copy link
Member

What's the problem this feature will solve?

The current way of defining custom portrayals for agents is through a function, for example:

def SsAgent_portrayal(agent):
if agent is None:
return
portrayal = {}
if type(agent) is SsAgent:
portrayal["Shape"] = "sugarscape_cg/resources/ant.png"
portrayal["scale"] = 0.9
portrayal["Layer"] = 1
elif type(agent) is Sugar:
if agent.amount != 0:
portrayal["Color"] = color_dic[agent.amount]
else:
portrayal["Color"] = "#D6F5D6"
portrayal["Shape"] = "rect"
portrayal["Filled"] = "true"
portrayal["Layer"] = 0
portrayal["w"] = 1
portrayal["h"] = 1
return portrayal

There are nested if-else statements checking for types and so on. To make things worse, in Mesa-Geo there're various types of layers (e.g., raster layer, vector layer), other than agents, that would also need custom portrayals.

Describe the solution you'd like

Perhaps something like what was done in MASON (a JAVA ABM framework)? In this paper the authors talked about their MVC architecture where there are corresponding portrayals for agents, fields, etc.

Additional context

  • In a MVC setting it might be easier to implement additional GUI. For example we can have a set of WebView classes for the current Tornado server, and another set of JupyterView for Jupyter interface for the GUI #1263, or anything else (Streamlit perhaps?), so long as a well-defined interface is implemented. This also helps ensure that the ABM simulation part is separated from its visualizations.
  • The current design is kind of MVC already, with CanvasGridVisualization as a View for Grid, NetworkVisualization for NetworkGrid, and so on. What's missing (but available in MASON) are the View classes for agents/objects. In Mesa, these are essentially replaced by those portrayal functions.
@rht
Copy link
Contributor

rht commented Oct 8, 2022

Also, consider that in Matplotlib, you don't have to specify your plot's "portrayal" by default. It's a line with a default linewidth, and a color that is chosen automatically.

@rht
Copy link
Contributor

rht commented Oct 10, 2022

I looked at the portrayals in Mesa-Geo, the most complex ones are comparable to the Sugarscape CG portrayals.

It's actually not that bad if you make the portrayal function less verbose / more declarative

def SsAgent_portrayal(agent):
    if agent is None:
        return

    portrayal = None

    if type(agent) is SsAgent:
        portrayal = {
            "Shape": "sugarscape_cg/resources/ant.png",
            "scale": 0.9,
            "Layer": 1
        }

    elif type(agent) is Sugar:
        if agent.amount != 0:
            color = color_dic[agent.amount]
        else:
            color = "#D6F5D6"
        portrayal = {
            "Color": color,
            "Shape": "rect",
            "Filled": "true",
            "Layer": 0,
            "w": 1,
            "h": 1,
        }

    return portrayal

The if agent.amount != 0 is not necessary. But for illustration purpose, people are bound to need to do this sort of if anyway. Having to specify each view class for each agent types would be more verbose than this nested ifs.

Alternatively, we can specify the portrayal as a dict

ssAgentPortrayal = {
    SsAgent: lambda agent: {
        "Shape": "sugarscape_cg/resources/ant.png",
        "scale": 0.9,
        "Layer": 1
    },
    Sugar: lambda agent: {
        "Color": color_dic[agent.amount] if agent.amount != 0 else "#D6F5D6",
        "Shape": "rect",
        "Filled": "true",
        "Layer": 0,
        "w": 1,
        "h": 1,
    }
}

@wang-boyu wang-boyu changed the title Towards a MVC architecture for better portrayals? Towards a MVC/MVVM architecture for better portrayals? Apr 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants