Assignment to / from agents' shapes reveal hidden problems
Closed this issue · 1 comments
Describe the bug
When assigning the shape of an agent to another variable, the attributes of this agent are copied along (since the shape "possesses" them), which not only makes this process highly inefficient but also creates obscure bugs. Because they hold these attributes, shapes are also considered as immutable
and so, when they are assigned a new value, the object itself does not change, only its "internal" value changes.
To Reproduce
Run the following model :
model BugShape
global {
int casualties;
init {
create my_species;
}
}
species my_species {
geometry my_initial_geometry;
bool expanding <- false;
init {
shape <- square(5);
my_initial_geometry <- shape;
//write "initial is " + my_initial_geometry;
}
reflex expand when:expanding {
shape <- shape + 1;
//write "In expand, initial envelope is " + envelope(my_initial_geometry);
}
reflex shrink when: !expanding {
shape <- shape - 1;
//write "In shrink, initial envelope is " + envelope(my_initial_geometry);
}
reflex switch when: every(50#cycle) {
if (!expanding) {
shape <- my_initial_geometry;
}
expanding <- !expanding;
}
}
experiment my_experiment {
output {
display my_display {
species my_species;
}
}
}
Although my_initial_geometry
is initialised only once, every time shape
is modified, the value of initial_geometry
is too. Moreover, initial_geometry becomes null
at some point, for reasons too long to describe here, but which are due to the attributes being copied back and forth the shape
and the initial_geometry
.
Expected behavior
That (1) initial_geometry
does not become null; (2) that, when assigned an initial value and not modified after, its value is not "magically" modified.
Workaround
In such a situation, and because of #3846, the only workaround is to perform a manual copy of the geometry, for instance by writing : geometry my_initial_geometry <- geometry(shape.points);
Most of this issue has been addressed. And the things that aren't (assignment of shape
that isn't actually changing the object, but its inner geometry, for instance) will not be addressed for the 1.9.x branch, as they would require too much changes in the relationships between agents and their shapes. So I close this issue -- knowing that the architecture of the 2.x branch will likely be different in that respect.