Skip to content

GraphSpecies

benoitgaudou edited this page Aug 21, 2019 · 22 revisions

Graph Species

Using a graph in a model enables to easily show and manage interactions between agents: this can be the link between 2 points in space linked by a road or (non-spatial) interactions that exist between friends agents. It can also be a powerful tool to compute the shortest path between 2 points in space.

A graph is a concept that has several implementations in GAML: (i) it can be a datatype (graph), that can be created from a road shapefile (e.g. using as_edge_graph), (ii) it can be implemented as node and edge species,and (iii) it is a topology, i.e. an organisation of agents that influences the way distance and neighborhood is computed.

Index

Declaration

Declare a graph with handmade agents

To instantiate this graph, we rely on the two built-in species graph_node and base_edge. We need to define our own node and edge species as children species of the 2 built-in species. First, the node species must inherit from the abstract species graph_node, then the method related_to must be redefined and finally an auxiliary species that inherits from base_edge used to represent the edges of the generated graph must be declared. A graph node is an abstract species that must redefine one method called related_to.

species my_node parent: graph_node edge_species: edge_agent{
  bool related_to(my_node other){
  	return true;
  }
}

species edge_agent parent: base_edge {
}

The method related_to returns a boolean and takes the agents from the current species as argument. If the method returns true, the two agents (the current instance and the one as argument) will be linked. The method is automatically called for each agent with each other agent of the given species in argument. Note that in the following example, related_to returns always true, so each agent will be linked to each other agent: we will get a complete graph.

model NewModel

global {
    int number_of_agents <- 5;

    init {
	create my_node number: number_of_agents;
    }
}

species my_node parent: graph_node edge_species: edge_agent {
    bool related_to (my_node other) {
	return true;
    }

    aspect base {
	draw circle(1) color: #green;
    }
}

species edge_agent parent: base_edge {
    aspect base {
	draw shape color: #blue;
    }
}

experiment MyExperiment type: gui {
    output {
	display MyDisplay type: java2D {
	    species my_node aspect: base;
	    species edge_agent aspect: base;
	}
    }
}

Complete graph generated using children species of graph_node and edge_agent.

You can, for example, link 2 agents when they are closer than a certain distance. Beware: The topology used in graph species is the graph topology and not the continuous topology. You can force the use of the continuous topology with the action using as follow:

bool related_to(my_node other){
    using topology(world) {
        return (self.location distance_to other.location < 20);
    }
}

Distance graph generated using children species of graph_node and edge_agent.

The abstract mother species graph_node has an attribute my_graph, with the type graph. The graph type represents a graph composed of vertices linked with edges. This type has built-in attributes such as edges (the list of all the edges agents), or vertices (the list of all the vertices agents).

Declare a graph by using a geometry file

In most cases, you will have to construct a graph from an existing file (example: a "shp" file). In that case, you will have to first instantiate a species from the shape file (with the create statement, using the facet from). Then, you will have to construct a graph from the agent, using one of the available operators such as as_edge_graph.

model load_shape_file 
 
global {
    file roads_shapefile <- file("../includes/road.shp");
    geometry shape <- envelope(roads_shapefile);
    graph road_network;

    init {
	create road from: roads_shapefile;
	road_network <- as_edge_graph(road);
    }
}

species road {
    aspect geom {
	draw shape color: #black;
    }
}

experiment main_experiment type:gui{
    output {
	display map {
	    species road aspect:geom;		
	}
    }
}

Declare a graph with nodes and edges

Another way to create a graph is building it manually nodes by nodes, and then edges by edges, without using agent structures. Use the add_node operator and the add_edge operator to do so. Here is an example of how to do:

list<point> nodes <- [];
graph my_graph <- graph([]);

add point(0.0,0.0) to:nodes;
add point(90.0,90.0) to:nodes;
add point(20.0,20.0) to:nodes;
add point(40.0,50.0) to:nodes;
add point(100.0,0.0) to:nodes;

loop nod over: nodes {
    my_graph <- my_graph add_node(nod);
}
		
my_graph <- my_graph add_edge (nodes at 0::nodes at 2);
my_graph <- my_graph add_edge (nodes at 2::nodes at 3);
my_graph <- my_graph add_edge (nodes at 3::nodes at 1);
my_graph <- my_graph add_edge (nodes at 0::nodes at 4);
my_graph <- my_graph add_edge (nodes at 4::nodes at 1);

Using this solution, my_graph can have two types: it can be an a-spatial graph, or a spatial graph. The spatial graph will have a proper geometry, with segments that follow the position of your graph (you can access to the segments by using the built-in "segments"). The a-spatial graph will not have any shape.

global {
    graph my_spatial_graph<-spatial_graph([]);
    graph my_aspatial_graph<-graph([]);
	
    init {
	point node1 <- {0.0,0.0};
	point node2 <- {10.0,10.0};
	
        my_spatial_graph <- my_spatial_graph add_node(node1);
	my_spatial_graph <- my_spatial_graph add_node(node2);
	my_spatial_graph <- my_spatial_graph add_edge(node1::node2);
	write my_spatial_graph.edges;
		
	// the output is [polyline ([{0.0,0.0,0.0},{10.0,10.0,0.0}])]
	my_aspatial_graph <- my_aspatial_graph add_node(node1);
	my_aspatial_graph <- my_aspatial_graph add_node(node2);
	my_aspatial_graph <- my_aspatial_graph add_edge(node1::node2);
	write my_aspatial_graph.edges;
	// the output is [{0.0,0.0,0.0}::{10.0,10.0,0.0}]
    }
}

Useful operators with graph

Knowing the degree of a node

The operator degree_of returns the number of edges attached to a node. To use it, you have to specify a graph (on the left side of the operator), and a node (on the right side of the operator).

The following code (to put inside the node species) displays the number of edges attached to each node:

aspect base {
    draw string(my_graph degree_of node(5)) color:# black;
}

Get the neighbors of a node

To get the list of neighbors of a node, you should use the neighbors_of operator. On the left side of the operator, specify the graph you are using, and on the right side, specify the node. The operator returns the list of nodes located at a distance inferior or equal to 1, considering the graph topology.

species graph_agent parent: graph_node edge_species: edge_agent
{
  list<graph_agent> list_neighbors <- list<graph_agent>(my_graph neighbors_of (self));
}

Here is an example of a model using those two previous concepts (a random node is chosen each step, displayed in red, and his neighbors are displayed in yellow):

Graph displaying the neighbor nodes of a random node.

model graph_model

global {
    int number_of_agents <- 50;
	
    init {
	create my_node number: number_of_agents;
    }
	
    reflex update {
	ask one_of(my_node) {
	    status <- 2;
	    do update_neighbors;
	}
    }
}

species my_node parent: graph_node edge_species: edge_agent {
    int status <- 0;
    list<int> list_connected_index;
	
    init {
	int i <- 0;
	loop g over: my_node {
	    if (flip(0.1)) {
		add i to:list_connected_index;
	    }
	    i <- i+1;
	}
    }
	
    bool related_to(my_node other){
        if (list_connected_index contains (my_node index_of other)) {
	    return true;
	}
        return false;
    }
	
    action update_neighbors {
	list<my_node> list_neighbors <- my_graph neighbors_of (self);

	loop neighb over: list_neighbors {
	    neighb.status <- 1;
	}
    }

    aspect base {
	if (status = 0) {
	    draw circle(2) color: #green border: #black;
	} else if (status = 1) {
	    draw circle(2) color: #yellow border: #black;
	} else if (status = 2) {
	    draw circle(2) color: #red border: #black;
	}
	
        draw string(my_graph degree_of self) color: #black size: 4 at: {self.location.x-1, self.location.y-2};
	status <- 0;
    }
}

species edge_agent parent: base_edge {
    aspect base {
	draw shape color: #blue;
    }
}

experiment MyExperiment type: gui {
    output {
	display MyDisplay type: java2D {
	    species my_node aspect: base;
	    species edge_agent aspect: base;
	}
    }
}

Compute the shortest path

To compute the shortest path to go from a point to another one, pick a source and a destination among the vertices you have for your graph. Store those values as point type.

point source;
point destination;
source <- point(one_of(my_graph.vertices));
destination <- point(one_of(my_graph.vertices));

Then, you can use the operator path_between to return the shortest path. To use this operator, you have to give the graph, then the source point, and the destination point. This operator returns a path type object.

path shortest_path;
shortest_path <- path_between (my_graph, source,destination);

Another operator exists, paths_between, that returns a list of shortest paths between two points. Please read the documentation to learn more about this operator.

Here is an example of code that shows the shortest path between two points of a graph:

Display showing the shortest path between two random nodes of a distance graph.

model graph_model

global {
    int number_of_agents <- 50;
    point source;
    point target;
    graph the_graph;
    path shortest_path;

    init {
	create my_node number: number_of_agents;
    }

    reflex pick_two_points {
	if (the_graph = nil) {
	    the_graph <- one_of(my_node).my_graph;
	}

	shortest_path <- nil;
	loop while: shortest_path = nil {
	    source <- (one_of(the_graph.vertices));
	    target <- (one_of(the_graph.vertices));
	    if (source != target) {
		shortest_path <- path_between(the_graph, source, target);
	    }
	}
    }
}

species my_node parent: graph_node edge_species: edge_agent {
    list<int> list_connected_index;

    init {
	int i <- 0;
	loop g over: my_node {
	    if (flip(0.1)) {
		add i to: list_connected_index;
	    }

	    i <- i + 1;
	}
    }

    bool related_to (my_node other) {
	using topology(world) {
	    return (self.location distance_to other.location < 20);
	}
    }

    aspect base {
	draw circle(2) color: #green border: #black;
    }
}

species edge_agent parent: base_edge {

    aspect base {
	draw shape color: #blue;
    }
}

experiment MyExperiment type: gui {
    output {
	display MyDisplay type: java2D {
	    species my_node aspect: base;
	    species edge_agent aspect: base;
	    graphics "shortest path" {
		if (shortest_path != nil) {
		    draw circle(3) at: source color: #yellow border: #black;
		    draw circle(3) at: target color: #cyan border: #black;
		    draw (shortest_path.shape + 1) color: #magenta;
		}
	    }
	}
    }
}

Control the weight in graph

You can add a map of weight for the edges that compose the graph. Use the operator with_weights to put weights in your graph. The graph has to be on the left side of the operator, and the map has to be on the right side. In the map, you have to put edges as key, and the weight for that edge as value. One common use is to put the distance as weight:

my_graph <- my_graph with_weights (my_graph.edges as_map (each::geometry(each).perimeter));

The calculation of the shortest path can change according to the weight you choose for your edges. For example, here is the result of the calculation of the shortest path when all the edges have 1 as weight value (it is the default graph topology), and when the edges have their length as weight.

Illustration of different shortest paths depending on edge weights.

Here is an example of implementation:

model shortest_path_with_weight

global {
    graph my_graph<-spatial_graph([]);
    path shortest_path;
    list<point> nodes;
		
    init {
	add point(10.0,10.0) to:nodes;
	add point(90.0,90.0) to:nodes;
	add point(40.0,20.0) to:nodes;
	add point(80.0,50.0) to:nodes;
	add point(90.0,20.0) to:nodes;

	loop nod over: nodes {
	    my_graph <- my_graph add_node(nod);
	}
		
	my_graph <- my_graph add_edge (nodes at 0::nodes at 2);
	my_graph <- my_graph add_edge (nodes at 2::nodes at 3);
	my_graph <- my_graph add_edge (nodes at 3::nodes at 1);
	my_graph <- my_graph add_edge (nodes at 0::nodes at 4);
	my_graph <- my_graph add_edge (nodes at 4::nodes at 1);
		
	// comment/decomment the following line to see the difference.
	my_graph <- my_graph with_weights (my_graph.edges as_map (each::geometry(each).perimeter));
		
	shortest_path <- path_between(my_graph,nodes at 0, nodes at 1);
    }
}

experiment MyExperiment type: gui {
    output {
	display MyDisplay type: java2D {
	    graphics "shortest path" {
		if (shortest_path != nil) {
		    draw circle(3) at: point(shortest_path.source) color: #yellow;
		    draw circle(3) at: point(shortest_path.target) color: #cyan;
		    draw (shortest_path.shape+1) color: #magenta;
		}
		loop edges over: my_graph.edges {
		    draw geometry(edges) color: #black;
		}
	    }
	}
    }
}
  1. What's new (Changelog)
  1. Installation and Launching
    1. Installation
    2. Launching GAMA
    3. Updating GAMA
    4. Installing Plugins
  2. Workspace, Projects and Models
    1. Navigating in the Workspace
    2. Changing Workspace
    3. Importing Models
  3. Editing Models
    1. GAML Editor (Generalities)
    2. GAML Editor Tools
    3. Validation of Models
  4. Running Experiments
    1. Launching Experiments
    2. Experiments User interface
    3. Controls of experiments
    4. Parameters view
    5. Inspectors and monitors
    6. Displays
    7. Batch Specific UI
    8. Errors View
  5. Running Headless
    1. Headless Batch
    2. Headless Server
    3. Headless Legacy
  6. Preferences
  7. Troubleshooting
  1. Introduction
    1. Start with GAML
    2. Organization of a Model
    3. Basic programming concepts in GAML
  2. Manipulate basic Species
  3. Global Species
    1. Regular Species
    2. Defining Actions and Behaviors
    3. Interaction between Agents
    4. Attaching Skills
    5. Inheritance
  4. Defining Advanced Species
    1. Grid Species
    2. Graph Species
    3. Mirror Species
    4. Multi-Level Architecture
  5. Defining GUI Experiment
    1. Defining Parameters
    2. Defining Displays Generalities
    3. Defining 3D Displays
    4. Defining Charts
    5. Defining Monitors and Inspectors
    6. Defining Export files
    7. Defining User Interaction
  6. Exploring Models
    1. Run Several Simulations
    2. Batch Experiments
    3. Exploration Methods
  7. Optimizing Model Section
    1. Runtime Concepts
    2. Optimizing Models
  8. Multi-Paradigm Modeling
    1. Control Architecture
    2. Defining Differential Equations
  1. Manipulate OSM Data
  2. Diffusion
  3. Using Database
  4. Using FIPA ACL
  5. Using BDI with BEN
  6. Using Driving Skill
  7. Manipulate dates
  8. Manipulate lights
  9. Using comodel
  10. Save and restore Simulations
  11. Using network
  12. Headless mode
  13. Using Headless
  14. Writing Unit Tests
  15. Ensure model's reproducibility
  16. Going further with extensions
    1. Calling R
    2. Using Graphical Editor
    3. Using Git from GAMA
  1. Built-in Species
  2. Built-in Skills
  3. Built-in Architecture
  4. Statements
  5. Data Type
  6. File Type
  7. Expressions
    1. Literals
    2. Units and Constants
    3. Pseudo Variables
    4. Variables And Attributes
    5. Operators [A-A]
    6. Operators [B-C]
    7. Operators [D-H]
    8. Operators [I-M]
    9. Operators [N-R]
    10. Operators [S-Z]
  8. Exhaustive list of GAMA Keywords
  1. Installing the GIT version
  2. Developing Extensions
    1. Developing Plugins
    2. Developing Skills
    3. Developing Statements
    4. Developing Operators
    5. Developing Types
    6. Developing Species
    7. Developing Control Architectures
    8. Index of annotations
  3. Introduction to GAMA Java API
    1. Architecture of GAMA
    2. IScope
  4. Using GAMA flags
  5. Creating a release of GAMA
  6. Documentation generation

  1. Predator Prey
  2. Road Traffic
  3. 3D Tutorial
  4. Incremental Model
  5. Luneray's flu
  6. BDI Agents

  1. Team
  2. Projects using GAMA
  3. Scientific References
  4. Training Sessions

Resources

  1. Videos
  2. Conferences
  3. Code Examples
  4. Pedagogical materials
Clone this wiki locally