Loops and iterations?

40 views
Skip to first unread message

J Smart

unread,
Dec 11, 2019, 1:24:27 AM12/11/19
to GAMA
Hi,

Lets say that I have a species that have a random value attribute. These species are located on a grid, and there can be multiple agents from species on a single cell of the grid. 
I want each grid cell to calculate the sum of the species attribute. 

How do I do this? Is it something to do with loops or iterations?

I found how to make a list of the species inside the grid cell 
list<species> species_inside -> {species inside self}; 
but I'm not sure how to calculate the sum of the attribute of the species within the grid cell.

Thanks so much for your help,

J

Benoit Gaudou

unread,
Dec 11, 2019, 1:53:55 AM12/11/19
to gama-p...@googlegroups.com
Hi,

Let consider you have a species : 
species people {
  int my_attr;
}

If you have now the grid species plot, you can use the sum_of operator to compute the sum of an attribute value over a list / species of agents:

grid plot {
   reflex computation { 
      int  value -> (people inside self) sum_of (each.my_attr);
   }
}

Cheers

Benoit


--
You received this message because you are subscribed to the Google Groups "GAMA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gama-platfor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gama-platform/7b6e0dd1-9a38-4784-921a-213bc616a677%40googlegroups.com.

J Smart

unread,
Dec 11, 2019, 4:41:25 AM12/11/19
to GAMA


On Wednesday, 11 December 2019 16:53:55 UTC+10, Benoit Gaudou wrote:
Hi,

Let consider you have a species : 
species people {
  int my_attr;
}

If you have now the grid species plot, you can use the sum_of operator to compute the sum of an attribute value over a list / species of agents:

grid plot {
   reflex computation { 
      int  value -> (people inside self) sum_of (each.my_attr);
   }
}

Cheers

Benoit


Le mer. 11 déc. 2019 à 13:24, J Smart <JayeNe...@gmail.com> a écrit :
Hi,

Lets say that I have a species that have a random value attribute. These species are located on a grid, and there can be multiple agents from species on a single cell of the grid. 
I want each grid cell to calculate the sum of the species attribute. 

How do I do this? Is it something to do with loops or iterations?

I found how to make a list of the species inside the grid cell 
list<species> species_inside -> {species inside self}; 
but I'm not sure how to calculate the sum of the attribute of the species within the grid cell.

Thanks so much for your help,

J

--
You received this message because you are subscribed to the Google Groups "GAMA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gama-p...@googlegroups.com.

J Smart

unread,
Dec 11, 2019, 4:43:10 AM12/11/19
to GAMA
Hi Benoit,

Thank you for your response, but it is not working for me... this is the code, with an error in the grid species

global {
int nb_people <- 100;
init {
create people number: nb_people;
}
}
species people {
int my_attr <- rnd(0,5);
aspect circle {
draw circle(1) color: #orange;
}
}
grid plot width:10 height: 10 {
int value;
      reflex computation { 
      int value -> (people inside self) sum_of (each.my_attr);     // this line has the error "unknown facet function" 
   }
}

experiment Loops type: gui {
output {
display main_display {
grid plot lines: #green;
species people aspect:circle;
}
}
}

On Wednesday, 11 December 2019 16:53:55 UTC+10, Benoit Gaudou wrote:
Hi,

Let consider you have a species : 
species people {
  int my_attr;
}

If you have now the grid species plot, you can use the sum_of operator to compute the sum of an attribute value over a list / species of agents:

grid plot {
   reflex computation { 
      int  value -> (people inside self) sum_of (each.my_attr);
   }
}

Cheers

Benoit


Le mer. 11 déc. 2019 à 13:24, J Smart <JayeNe...@gmail.com> a écrit :
Hi,

Lets say that I have a species that have a random value attribute. These species are located on a grid, and there can be multiple agents from species on a single cell of the grid. 
I want each grid cell to calculate the sum of the species attribute. 

How do I do this? Is it something to do with loops or iterations?

I found how to make a list of the species inside the grid cell 
list<species> species_inside -> {species inside self}; 
but I'm not sure how to calculate the sum of the attribute of the species within the grid cell.

Thanks so much for your help,

J

--
You received this message because you are subscribed to the Google Groups "GAMA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gama-p...@googlegroups.com.

Srirama Bhamidipati

unread,
Dec 11, 2019, 4:46:46 AM12/11/19
to GAMA
hi,

focus on the assignment operator ( <- ), it should not be ->

Srirama

Jaye Newman

unread,
Dec 11, 2019, 10:07:21 PM12/11/19
to gama-p...@googlegroups.com
Changing the assignment got rid of the error, but it still does not calculate the people attribute for each cell? I have attached an image of the simulation and highlighted where I expect it to calculate the attribute for the cell.

Here is the code:

global {

int nb_people <- 5;

init {
create people number: nb_people;
}
}
species people {
int my_attr <- rnd(0,5);

aspect circle {
draw circle(1) color: #orange;
}
}
grid plot width:6 height: 6 {

int value;

    reflex computation {
    int value <- (people inside self) sum_of (each.my_attr);
   }
}

To unsubscribe from this group and stop receiving emails from it, send an email to gama-platfor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gama-platform/8a964f17-fbac-4c1d-aa1f-132cd22357ba%40googlegroups.com.
cell_value.png

Benoit Gaudou

unread,
Dec 11, 2019, 10:34:05 PM12/11/19
to gama-p...@googlegroups.com
Dear Jaye,

I run your code on my GAMA 1.8.
If you look at your code, you should have a warning on the line inside the reflex computation.

grid plot width: 6 height: 6 {

int value;


reflex computation {

int value <- (people inside self) sum_of (each.my_attr);

}


}


The warning message is the following one :

This declaration of value shadows the declaration of an attribute of plot


First of all, in GAML (as in many language with type) there is a difference between a variable declaration and a value assignment.
The declaration is: int value;
The assignment is:  value <- 2;
Notice that you can do both on the same line: int value <- 2;

The important point is that declaring a variable creates a new object/variable whereas assigning a value to a variable only modifies an existing variable.

The second point is that  a variable exists only in the scope / in the block in which it is declared.
So if you declare a variable in a reflex, with:
reflex computation {
    int value <- 1;
}

This variable value exists only inside the reflex. In another reflex it does not exist.

So in your case, you declare a new variable in the reflex computation (that is thus different from the variable in the grid). You give it a value.
But this variable only exists during the execution of the reflex.

What introduces a confusion is that this variable has the same name as the variable/attribute of the grid plot. But GAMA considers that you create a new variable that shadows the existing attribute (as it has the same name).

A solution is to just transform the declaration+assignement in the reflex computation in a simple assignement (removing the int before value):

grid plot width: 6 height: 6 {

int value;


reflex computation {

     value <- (people inside self) sum_of (each.my_attr);

}


}


Cheers

Benoit

Jaye Newman

unread,
Dec 12, 2019, 10:36:00 PM12/12/19
to gama-p...@googlegroups.com
Thanks Benoit,
This works for me. Thank you for your very clear and helpful response.

Cheers,
Jaye

Reply all
Reply to author
Forward
0 new messages