Skip to main content
Version: 🚧 Alpha 🚧

5. Dynamic weights

This 5th step illustrates how to obtain the shortest path from a point to another one and to update the weights of an existing graph.

Formulation​

  • Road agents start with a destruction_coeff randomly initialized between 1 and 2, as in the previous step.
  • Add a new parameter: the destroy parameter that represents the value of destruction when a people agent takes a road. By default, it is equal to 0.02.
  • As a person moves along the path returned by goto, the model increases the destruction_coeff of the road agents on that path. It then updates the graph weights from the new coefficients.

Model Definition​

global section​

The destroy variable controls how quickly the coefficient increases as people move along roads. The experiment exposes it as a parameter.

global {
float destroy <- 0.02;
graph the_graph;

reflex update_graph {
map<road, float> weights_map <- road as_map (each:: (each.destruction_coeff * each.shape.perimeter));
the_graph <- the_graph with_weights weights_map;
}
}
experiment road_traffic mode: gui {
parameter "Value of destruction when a people agent takes a road" var: destroy category: "Road" min: 0.0 step: 0.01;
}

people agents​

At each time step, the goto action returns the path followed. The model reads its segments, finds the corresponding road agents with agent_from_geometry, and increases their coefficients in proportion to the segment length.

species people skills: [moving] {
point the_target <- nil;

reflex move when: the_target != nil {
path path_followed <- goto(target: the_target, on: the_graph, return_path: true);
list<geometry> segments <- path_followed.segments;
loop line over: segments {
float dist <- line.perimeter;
ask road(path_followed agent_from_geometry line) {
destruction_coeff <- destruction_coeff + (destroy * dist / shape.perimeter);
}
}
if the_target = location {
the_target <- nil;
}
}
}

Complete Model​

gama.library/models/Tutorials/Road%20Traffic/models/Model%2005.gaml
loading...