How to use the mesa.visualization.UserParams.UserParam function in Mesa

To help you get started, we’ve selected a few Mesa examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github projectmesa / mesa / mesa / visualization / ModularVisualization.py View on Github external
def reset_model(self):
        """ Reinstantiate the model object, using the current parameters. """

        model_params = {}
        for param, value in self.model_params.items():
            if isinstance(value, UserParam):
                model_params[param] = value.get_value()
            else:
                model_params[param] = value
        self.model = self.model_cls(**model_params)
        self.viz_states = [self.render_model()]
github projectmesa / mesa / examples / Schelling / SchellingModular.py View on Github external
if agent is None:
        return
    portrayal = {"Shape": "circle", "r": 0.5, "Filled": "true", "Layer": 0}

    if agent.type == 0:
        portrayal["Color"] = "Red"
    else:
        portrayal["Color"] = "Blue"
    return portrayal

happy_element = HappyElement()
canvas_element = CanvasGrid(schelling_draw, 20, 20, 500, 500)
happy_chart = ChartModule([{"Label": "happy", "Color": "Black"}])
model_params = {"height": 20, "width": 20, "density": 0.8, 
                #"minority_pc": 0.2, 
                "minority_pc": UserParam("minority_pc", 0.2, 0, 1.0, 0.1), 
                "homophily": UserParam("homophily", 4, 1, 8, 1)}
server = ModularServer(SchellingModel,
                       [canvas_element, happy_element, happy_chart],
                       "Schelling", model_params)
server.launch()
github projectmesa / mesa / mesa / visualization / UserParams.py View on Github external
Return the parameter's current value for insertion into JavaScript
        '''
        return self.get_value()

    def update_value(self, new_value):
        '''
        Update the current value, only if it is valid.
        '''
        if (self.min_value <= new_value <= self.max_value):
            # Removing step validation for now because of floating point issues
            self.current_value = new_value
        else:
            raise Exception("Incorrect input value")


class UserOption(UserParam):
    '''
    User-settable parameter for discrete values.
    '''

    JS_CODE = 'add(params, "{name}", {all_values});'

    def __init__(self, start_value, all_values, label=None):
        '''
        Create a new, discrete parameter.
        '''
        self.current_value = start_value
        self.all_values = all_values
        self.label = label

    def get_code(self):
        '''
github projectmesa / mesa / examples / Schelling / SchellingModular.py View on Github external
return
    portrayal = {"Shape": "circle", "r": 0.5, "Filled": "true", "Layer": 0}

    if agent.type == 0:
        portrayal["Color"] = "Red"
    else:
        portrayal["Color"] = "Blue"
    return portrayal

happy_element = HappyElement()
canvas_element = CanvasGrid(schelling_draw, 20, 20, 500, 500)
happy_chart = ChartModule([{"Label": "happy", "Color": "Black"}])
model_params = {"height": 20, "width": 20, "density": 0.8, 
                #"minority_pc": 0.2, 
                "minority_pc": UserParam("minority_pc", 0.2, 0, 1.0, 0.1), 
                "homophily": UserParam("homophily", 4, 1, 8, 1)}
server = ModularServer(SchellingModel,
                       [canvas_element, happy_element, happy_chart],
                       "Schelling", model_params)
server.launch()