11. Writing Files
This 11th step illustrates how to save data in a text file.
Formulation​
- At each simulation step, write in a text file:
- The time step
- The number of prey and predator agents
- The min and max energy of the prey and predator agents
Model Definition​
global section​
The model writes a text line to results.txt with the save statement:
save my_data to: file_name;
With:
my_data: depends on the data to save and of the type of filefile_name: string
The save statement supports several output formats, including:
shp(shapefile - GIS data): in that case,my_datais treated as a list of agents or geometries: all their geometries are saved in the file (with some variables as attributes),txt(text): in that case,my_datais treated as a string, which is written directly in the file,csv: in that case,my_datais treated as a list of values: [val1, val2, val3], that will be written in the file, separated by the,separator.
We use this statement (in a global reflex called save_result) to write:
- Each line contains the simulation cycle, prey and predator counts, and the minimum and maximum energy value in each population. The model uses
min_ofandmax_offor the energy values.
reflex save_result when: (nb_preys > 0) and (nb_predators > 0){
save ("cycle: "+ cycle + "; nbPreys: " + nb_preys
+ "; minEnergyPreys: " + (prey min_of each.energy)
+ "; maxSizePreys: " + (prey max_of each.energy)
+ "; nbPredators: " + nb_predators
+ "; minEnergyPredators: " + (predator min_of each.energy)
+ "; maxSizePredators: " + (predator max_of each.energy))
to: "results.txt" rewrite: (cycle = 0) ? true : false;
}
In the model source, the output labels are
maxSizePreysandmaxSizePredators, although the values that follow are maximum energy values.
Complete Model​
gama.library/models/Tutorials/Predator%20Prey%20Tutorial/models/Model%2011.gaml
loading...