My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
StupidTutorialModel16v14  
Tutorial: the stupid model - Model 16 version GAMA 1.4
Updated Dec 29, 2011 by alexis.d...@gmail.com



Purpose

How to create multiple classes of agents that interact.

Formulation

  • 200 predator objects are intialized and randomly distributed as the bugs are. A cell can contain a predator as well as a bug. Predators are created after bugs are.
  • Predators have one method: hunt. First, a predator looks through a shuffled list of its immediately neighboring cells (including its own cell). As soon as the predator finds a bug in one of these cells it “kills” the bug and moves into the cell. (However, if the cell already contains a predator, the hunting predator simply quits and remains at its current location.) If these cells contain no bugs, the predator moves randomly to one of them.
  • Predator hunting is scheduled after all the bug actions.

Models

Instanciating predators

We did it already in the first model (here) thus will leave to you as an exercise.

Defining predators

We also did it in the first model (here). Although the definition of the hunting action is a bit tricky thus we will define it:

species predator{
    var color type:rgb init:'blue';
    var myPlace type: stupid_grid value:location as stupid_grid;
		
    reflex hunt {
	let the_neighbours type: list value: myPlace neighbours_at 1;
        let the_neighbours_bug type:list value: the_neighbours accumulate (each.agents of_species bug);

     	let chosenPrey type: bug value: one_of(the_neighbours_bug);
	if condition: chosenPrey != nil {
 	    let new_loc type: stupid_grid value: chosenPrey.location as stupid_grid;	
	    if condition: empty(new_loc.agents of_species predator){
	   	set location value: new_loc;
		ask target: chosenPrey {
		   do action: die;
		}
	    }
        }
	else {
	    set location value: one_of(the_neighbours as list);
	}
    }
		
    aspect basic{
        draw shape: circle color: color size: 2;
    }
}

Explanations

  • We select cells around the predator (direct neighbourhood)
  • We select the bugs contained inside these cells
  • We select one of them randomly.
  • If we have a chosen prey.
    • We check it's empty of other predators.
      • If so we go there and kill the bug.
      • If there is another predator already, nothing happens.
  • If we have no place with a bug.
    • We move randomly in a neighbour cell.

Scheduling Predators

You just have to add to the scheduling_targets list (in the global section) the predator agent. Check model number 10 if you do not remember how to do.

var scheduling_targets type: list value: (stupid_grid as list) + (bugs sort_by (each as bug).size) + (predator as list);

Visualization

We just have to add the predator species to the stupid display:

display stupid_display {
    grid stupid_grid;
    species bug aspect: basic;
    species predator aspect: basic;
}

Complete model

We obtain the following model:

model StupidModel16


global {
    var numberBugs type: int init: 100 parameter: 'numberBugs';
    var globalMaxConsumption type: float init: 1 parameter: 'globalMaxConsumption';
    var globalMaxFoodProdRate type: float init: 0.01 parameter: 'globalMaxFoodProdRate';
    var survivalProbability type: float init: 0.95 parameter: 'survivalProbability';
    var initialBugSizeMean type: float init: 0.1 parameter: 'initialBugSizeMean';
    var initialBugSizeSD type: float init: 0.03 parameter: 'initialBugSizeSD';
    var init_data type: matrix init: '../data/Stupid_Cell.Data' as file const: true;
    var width type: int init: ((init_data column_at 0) copy_between {3, length(rows_list(init_data)) - 1}) max_of each const: true;
    var height type: int init: ((init_data column_at 1) copy_between {3,length(rows_list(init_data)) - 1}) max_of each const: true;
    var bugs type: list of: bug value: bug as list;
    var scheduling_targets type: list value: (stupid_grid as list) + (bugs sort_by (each as bug).size) + (predator as list);
    init {
        create species: bug number: numberBugs;
        set bugs value: bug as list;
        create species: predator number: 200;
        
        let i type: int value: 0;
    	loop from: 3 to: length(rows_list(init_data)) - 1 var: i {
   			let ind_i type: int value: init_data at {0,i};
   			let ind_j type: int value: init_data at {1,i};
			ask target: (stupid_grid as matrix) at {ind_i,ind_j} {
   				set foodProd value: init_data at {2,i};
			}
		}
    }
    reflex shouldHalt when: (time > 1000) or (empty (bug as list)) {
        do action: halt;
    }
}

environment {
    grid stupid_grid width: width height: height torus: false {
        var color type: rgb init: [0,[255, food * 255 *2] min_of each, 0] as rgb value: [0,[255, food * 255 * 2] min_of each, 0] as rgb;
        var maxFoodProdRate type: float value: globalMaxFoodProdRate;
        var foodProd type: float;
        var food type: float init: 0.0 value: food + foodProd;
    }
}

entities {
    species bug {
        var size type: float init: gauss({initialBugSizeMean,initialBugSizeSD});
        var color type: rgb value: (size > 0) ? rgb [255, 255/size, 255/size] : rgb [255, 255, 255];
        var maxConsumption type: float value: globalMaxConsumption;
        var myPlace type: stupid_grid value: location as stupid_grid;
        init {
        	if condition: size<0 {
        		set size value: 0;
        	}
        }
        reflex basic_move {
            let destination value: last ((shuffle ((myPlace neighbours_at 4) where empty(each.agents))) sort_by ((each as stupid_grid).food));
            if condition: destination != nil {
                set location value: destination;
            }
        }
        reflex grow {
            let transfer value: min [maxConsumption, myPlace.food];
            set size value: size + transfer;
            set myPlace.food value: myPlace.food - transfer;
        }
        reflex shallDie when: ((rnd(100)) / 100.0) > survivalProbability {
            do action: die;
        }
        reflex multiply {
            if condition: size > 10 {
                let possible_nests value: (myPlace neighbours_at 3) where empty(each.agents);
                loop times: 5 {
                    let nest value: one_of(possible_nests);
                    if condition: nest != nil {
                        set possible_nests value: possible_nests - nest;
                        create species: bug number: 1 return: child;
                        ask target: child {
                            set location value: nest.location;
                        }
                    }
                }
                do action: die;
            }
        }
        aspect basic {
            draw shape: circle color: color size: size;
        }
    }
    
    species predator{
        var color type:rgb init:'blue';
        var myPlace type: stupid_grid value:location as stupid_grid;
		
	reflex hunt {
   	    let the_neighbours type: list value: myPlace neighbours_at 1;
	    let the_neighbours_bug type:list value: the_neighbours accumulate (each.agents of_species bug);
     	    let chosenPrey type: bug value: one_of(the_neighbours_bug);
	    if condition: chosenPrey != nil {
	     	let new_loc type: stupid_grid value: chosenPrey.location as stupid_grid;	
	     	if condition: empty(new_loc.agents of_species predator){
	   	    set location value: new_loc;
		    ask target: chosenPrey {
		    	do action: die;
		    }
		}
	     }
	     else {
	     	set location value: one_of(the_neighbours as list);
	     }
	}
		
	aspect basic{
  	    draw shape: circle color: color size: 2;
	}
    }
}

output {
    display stupid_display {
        grid stupid_grid;
        species bug aspect: basic;
        species predator aspect: basic;
    }
    inspect name: 'Agents' type: agent refresh_every: 5;
    inspect name: 'Species' type: species refresh_every: 5;
    display histogram_display {
        chart name: 'Size distribution' type: histogram background: rgb('lightGray') {
            data name: "[0;1]" value: bugs count (each.size < 1);
            data name: "[1;2]" value: bugs count ((each.size > 1) and (each.size < 2));
            data name: "[2;3]" value: bugs count ((each.size > 2) and (each.size < 3));
            data name: "[3;4]" value: bugs count ((each.size > 3) and (each.size < 4));
            data name: "[4;5]" value: bugs count ((each.size > 4) and (each.size < 5));
            data name: "[5;6]" value: bugs count ((each.size > 5) and (each.size < 6));
            data name: "[6;7]" value: bugs count ((each.size > 6) and (each.size < 7));
            data name: "[7;8]" value: bugs count ((each.size > 7) and (each.size < 8));
            data name: "[8;9]" value: bugs count ((each.size > 8) and (each.size < 9));
            data name: "[9;10]" value: bugs count ((each.size > 9) and (each.size < 10));
        }
    }
    file stupid_results type: text data: 'cycle: '+ (time as string) + '; minSize: '
        + ((bugs min_of each.size) as string) + '; maxSize: '
        + ((bugs max_of each.size) as string) + '; meanSize: '
        + (((sum (bugs collect ((each as bug).size))) / (length(bugs))) as string);
    display series_display {
        chart name: 'Population history' type: series background: rgb('lightGray') {
            data name: 'Bugs' value: length(bugs) color: 'blue';     
        }
    }
}

Sign in to add a comment
Powered by Google Project Hosting