Using else statement if target not met

51 views
Skip to first unread message

J Newman

unread,
Feb 23, 2020, 11:11:12 PM2/23/20
to GAMA
Hi Everyone,

I would like the agents to move to the highest quality plot that is above a threshold. At the moment the code does not work, I would like if the good_plots are !nil then find one of the best ones, else do wander. It does not seem to be doing the else statement.
There are parts of the map there will be plots that have a low quality and so the list of good_plots will be nil.

Here is a summary of my code and I have attached the map.

model ifelsemovement

global {
int nb_fly <- 3;
float speed_value <- 1.0;
float sensing_radius <- 15#m;
float plot_tolerance <- 0.3; 
file map_init <- image_file("C:/.../mod_raster2_50px.png");

init {
create fly number: nb_fly{
ask plot {
color <- rgb (map_init at {grid_x,grid_y}) ;
plot_quality <- 1 - (((color as list) at 0) / 255) ;
}
}
}
}

species fly skills: [moving] control: fsm {
plot myPlot <- plot grid_at {0, 0};
float maturation_rate <- 0.15;
float maturation <- maturation_rate update: maturation_rate + maturation;
list<plot> plots_nearby <- plot at_distance sensing_radius update: plot at_distance sensing_radius;
list<plot> good_plots <- plots_nearby where(each.plot_quality >= plot_tolerance) update: plots_nearby where(each.plot_quality >= plot_tolerance);
init {
location <- myPlot.location;
}
action basic_wander {
do wander speed: speed_value *2;
}
action directed_move {
                    //  if good_plots = !nil { }   this code does not work
if state = "adult" {  // this code works but I would like if the plots = !nil then do the following, else just do wander.
    plot maxplot <- (good_plots - myPlot) with_max_of (each.plot_quality);
    float maxquality <- maxplot.plot_quality;
myPlot <- one_of(good_plots where(each.plot_quality = maxquality));
location <- myPlot.location;
write "directed move";
} else {
do wander speed: speed_value * 2;
write "wander";
}
}

state young initial: true {
do basic_wander;
transition to: adult when: maturation >= 1.0;
}
state adult {
do basic_wander;
do directed_move;
}
aspect default {
draw circle(sensing_radius) color: #thistle empty: true;
draw circle(0.5) color: #brown rotate: 90 + my heading;
}
}

grid plot width: 50 height: 50 neighbors: 8 {
float plot_quality <- plot_quality ;

aspect default {
draw circle(0.4) color: color border: #black;
}
}

experiment my_experiment {
float minimum_cycle_duration <- 0.15;
parameter "initial nb flies" var: nb_fly;
output {
display myDisplay {
grid plot lines: #white;
species fly aspect: default;
}
}
}
mod_raster2_50px.png

Alexis Drogoul (IRD)

unread,
Feb 23, 2020, 11:15:25 PM2/23/20
to GAMA
Hi,

Just make sure to correctly write your condition; should be " if good_plots != nil { ... } "

Cheers
Alexis


Jaye Newman

unread,
Feb 24, 2020, 12:57:11 AM2/24/20
to gama-p...@googlegroups.com
Thanks Alexis,

This made the code work, but unfortunately the agents still do not do the else statement of wander even though the target is nil. The error "Cannot evaluate plot_quality as the target agent is nil"
How can I tell the agents to do else wander, if the conditions are not met?

Cheers,
Jaye

Alexis Drogoul (IRD)

unread,
Feb 24, 2020, 1:04:38 AM2/24/20
to GAMA
Hi,

good_plots is a list, right ? So you might better check if it is not empty rather than not nil ... 

Alexis



Jaye Newman

unread,
Feb 24, 2020, 3:20:57 AM2/24/20
to gama-p...@googlegroups.com
Yes, good_plots is a list, is != empty a thing? Or would I have to define what empty is?
I want the agent to assess if there are no suitable plots in its sensing_radius (the good_plots list) then do wander

Can I do this using the if else statements?

Cheers,
Jaye

Alexis Drogoul (IRD)

unread,
Feb 24, 2020, 3:22:37 AM2/24/20
to GAMA
Hi,

The correct expression would be:  if !empty(good_plots) { ... } else { ... } 

Alexis


Reply all
Reply to author
Forward
0 new messages