gama-platform / gama.old

Main repository for developing the 1.x versions of GAMA
GNU General Public License v3.0
304 stars 99 forks source link

Impractical slow simulations when creating the grid from a matrix #3694

Closed leenr01 closed 1 year ago

leenr01 commented 1 year ago

Describe the bug Creating the environment by using a matrix (https://gama-platform.org/wiki/GridSpecies#grid-from-a-matrix) is useful for defined geometries but extremely slow even with dealing with around 2000 agents. The code is very slow to the point that taking readings is not possible.

To Reproduce Steps to reproduce the behavior:

  1. Run the attached code that uses 2000 agents with a 500*500 grid. You will need to change the excel file directory according to your device.
  2. See the speed of the code (becomes even more problematic with batch experiments)

Files Matrix8P.csv

Desktop:

Additional context Any recommendations for speeding up the current code are highly appreciated. The code describes the spread of a disease in a population.


model Project / author: Leen Alrawas (leenr2001@gmail.com) /

global {

// Variables to control the grid
int matrixSize <- 500; // grid size geometry shape <- square(matrixSize); // grid shape file my_csv_file <- csv_file("C:/Users/leenr/Desktop/Matrix8P.csv",","); // change the location of the excel file //file my_map <- gif_file("C:/Users/leenr/Desktop/my_map.gif"); // change the location of the gif file matrix data <- matrix(my_csv_file); int numOfPatches <- 8; int numOfTunnels <- 134; map<int, list> patchesMap; // the numbered patches map<int, list> tunnelsMap; // the numbered tunnels list tunnels; // List of tunnels list entrance; // List of patch entrances list exit; // List of patch exits list patches <- []; // list of cells in all patches map<int, int> PatchesArea; // list of areas of patches int PatchArea; // patch area

// Variables to control the population int neighbours_size <- 1; // agents at distace of "neighbours_size" cells are considered neighbours float P1 <- 0.06; float P2 <- 0.505; float P3 <- 1 - P2 - P1; float Cn <- 0.649; float C <- 0.95 Cn + 0.05; list initialSList <- [ int(C30000.75P1),int(C30000.386P2),int(C30000.632P3),int(C30000.080P3), int(C30000.287P3),int(C30000.25P1),int(C30000.495P2),int(C30000.119*P2) ]; //list initialSList <- [90,390,550,70,250,30,500,120]; // Initial Susceptible population sizes for each patch list initialIList <- [0, 5, 0, 5, 0, 5, 0, 5]; // Initial Infected population sizes for each patch list initialVList; // Initial vaccinated population sizes list sick_agents;// <- [63, 52, 83, 22, 63, 22, 63, 52]; // population with chronic or health conditions - per patch map<int, float> crowd_index; // population density list AgeMeanList <- [40.0, 50.0, 60.0, 60.0, 60.0, 40.0, 50.0, 50.0]; // Age mean for each patch list AgeSDList; // Age standard deviation for each patch map<int, float> age_index; // [agents(+60) / all agents] - per patch map<int, float> health_index <- [1::0.173, 2::0.173, 3::0.173, 4::0.173, 5::0.173, 6::0.173, 7::0.173, 8::0.173]; // [sick_agents / all agents] - per patch

// Variables to control the mobility list local_mobility_list <- [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]; // ratio of agents allowed to move locally - per patch int stay_time <- 10; // time being in a foreign patch (in cycles) float mobility_probability <- 1.0;

// Variables to control the disease int safe_exposure_time <- 5; // time being exposed before becoming infectious (in cycles) float alpha <- 0.3; // parameters in flowchart // float betaSUS <- 0.5; // float betaVAC <- 0.1; map<int, float> betaSUSList; // list of betaSUS for all patches (determined by age and health indexes) map<int, float> betaVACList; // list of betaVAC for all patches (determined by age and health indexes) float theta <- 0.05; float gamma <- 0.01; int CounterofVac <- 0; // number of vaccinated people since beginning of simulation int CounterofInf; // number of infected people since beginning of simulation list efficacy_list <- [0.9, 0.7, 0.5, 0.5, 0.5, 0.9, 0.7, 0.7]; // list of vaccination efficacies (lambda in flowchart) float vaccination_percentage <- 0.25; // the percentage of the whole population who are vaccinated
map<int, int> NHI_local; // number of susceptible who have been infected - per patch map<int, int> NAI_local; // number of active infected agents - per patch map<int, float> R0_local; // Basic Reproduction Number - defined as the max of NHI/NAI - per patch float R0_global <- 0.0; // defined as the average of R0_local values map<int, int> I_of_peak_local; // peak of infection is maximum number of infected agents int I_of_peak_global <- 0; map<int, int> time_of_peak_local;
int time_of_peak_global <- 0; map<int, int> R0_max_time; // time of each R0_local float R01; // individual R0 values of patches float R02; float R03; float R04; float R05; float R06; float R07; float R08;

float D <- ( P1*C*3000*(P1*C*3000-1) + P2*C*3000*(P2*C*3000-1) + P3*C*3000*(P3*C*3000-1) )
           / ( C*3000*(C*3000-1) );
float fitness;
float K1 <- 0.2;
float K2 <- 0.2;
float K3 <- 0.2;
float K4 <- 0.2;
float K5 <- 1-K1-K2-K3-K4;

init {

// Set colors in grid ask sir_grid { grid_value <- float(data[grid_x, grid_y]); do setColor; }

    loop i from: 1 to: numOfPatches {

// Set patch color list patchCell <- (sir_grid where (each.grid_value = i)); ask patchCell { color <- #black; }

// Initialize values add patchCell at: i to: patchesMap; //number the patches add 0.0 at: i to: NHI_local; add 0.0 at: i to: NAI_local; add 0.0 at: i to: I_of_peak_local; add 0.0 at: i to: R0_local; add 0 at: i to: R0_max_time; add 0 at: i to: time_of_peak_local; //add 50 to: AgeMeanList; // uniform mean age for all patches add 15 to: AgeSDList; // age standard deviation for all patches

        PatchArea <- sir_grid count(each.grid_value=i); //count the number of cells in each patch
        add PatchArea at: i to: PatchesArea; //set the area=number of cells for each patch
    }   

    loop i from: numOfPatches + 1 to: numOfTunnels + numOfPatches { //cells numbered from 9 to 142

        list<sir_grid> tunnelCell <- (sir_grid where (each.grid_value = i));

// Set tunnel color ask tunnelCell { color <- #slategrey; }

        add tunnelCell at: i to: tunnelsMap; //number the tunnels (from 9 to 142)

    }

    loop i from: 1 to: length(patchesMap) {
        patches <- patches + (patchesMap at i); //list of all cells inside patches
    }

    loop i from: numOfPatches + 1 to: numOfTunnels + numOfPatches { 
        tunnels <- tunnels + (tunnelsMap at i); //list of all cells inside tunnels
    }

    entrance <- sir_grid where (each.grid_value = (numOfTunnels + numOfPatches + 5)); //entrance at cells numbered 147
    exit <- sir_grid where (each.grid_value = (numOfTunnels + numOfPatches + 6)); //exit at cells numbered 148  

// Initiate population
loop i from: 1 to: numOfPatches { create Host number: (initialSList at (i - 1)) { //create initial Susceptible agents is_susceptible <- true; is_exposed <- false; is_infected <- false; is_immune <- false; is_vac <- false; color <- #green; currentPatch <- (patchesMap at i); originalPatch <- (patchesMap at i); local_mobility <- (local_mobility_list at (i - 1)); time_in_patch <- 0; efficacy <- (efficacy_list at (i - 1)); mean_age <- (AgeMeanList at (i - 1)); SD_age <- (AgeSDList at (i - 1)); }

        create Host number: (initialIList at (i - 1)) { //create initial Infected agents
            is_susceptible <- false;
            is_exposed <- false;
            is_infected <- true;
            is_immune <- false;
            is_vac <- false;                
            color <- #red;
            currentPatch <- (patchesMap at i);
            originalPatch <- (patchesMap at i);
            local_mobility <- (local_mobility_list at (i - 1));
            time_in_patch <- 0;
            efficacy <- (efficacy_list at (i - 1));
            mean_age <- (AgeMeanList at (i - 1));
            SD_age <- (AgeSDList at (i - 1));
            CounterofInf <- Host count(each.is_infected);
        }

        add (Host count(each.currentPatch = patchesMap at (i)))/(PatchesArea at (i))
         at: i to: crowd_index; // calculate the population density (crowd index)
   }

    ask Host {
        do setAge;
        do AbletoTravel;
        do setInitialLocation;
        do agerisk;
    }

    loop i from: 1 to: numOfPatches { // calculate health and age indexes AND beta values
        /*add (sick_agents at (i - 1))/(Host count (each.currentPatch = patchesMap at (i)))
         at: i to: health_index;*/
        add (Host count (each.age>=60 and each.currentPatch = patchesMap at (i)))/
            (Host count (each.currentPatch = patchesMap at (i))) 
         at: i to: age_index;
        /*add 0.440 + 0.0715*(age_index at (i)) + 0.324*(health_index at (i)) at: i to: betaSUSList;
        add 0.0841 + 0.0861*(age_index at (i)) + 0.152*(health_index at (i)) at: i to: betaVACList;*/

        add 0.40 + 0.20*(age_index at (i)) + 0.30*(health_index at (i)) at: i to: betaSUSList;
        add 0.15*(age_index at (i)) + 0.2*(health_index at (i)) at: i to: betaVACList;

        ask Host {
            if (currentPatch = patchesMap at (i)) {
                betaSUS <- betaSUSList at (i);
                betaVAC <- betaVACList at (i);
            }
        }
    }

    // save population data
    save crowd_index to: "crowd_index_SAproject.csv" type: "csv";
    save PatchesArea to: "PatchesArea_SAproject.csv" type: "csv";
    save health_index to: "health_index_SAproject.csv" type: "csv";
    save age_index to: "age_index_SAproject.csv" type: "csv";
    save betaSUSList to: "betaSUSList_SAproject.csv" type: "csv";
    save betaVACList to: "betaVACList_SAproject.csv" type: "csv";

}   

//Grid used to discretize space grid sir_grid width: matrixSize height: matrixSize neighbors: 8 {

// Set entrance color action setColor { if (grid_value = numOfTunnels + numOfPatches + 5) { color <- #orange; } // Set exit color if (grid_value = numOfTunnels + numOfPatches + 6) { color <- #brown; } } }

}

// Species host which represents the host of the disease species Host skills: [moving] {

// Varibales to control agent state and age bool is_susceptible <- true; bool is_exposed <- false; bool is_infected <- false; bool is_immune <- false; bool is_vac <- false; bool infectious <- false; // exposed can be infectious after some defined time bool active <- false; // number of infectious agents rgb color <- #green; float mean_age; float SD_age; int age; float age_risk;

// Variables to control agent location
list originalPatch; list currentPatch; sir_grid myPlace; list myEntrance; list current_tunnel; sir_grid targetEntrance; int time_in_patch; bool Leave <- false;

// Variables to control agent mobility float local_mobility;
float travel_status;

// Variables to control agent neighbours int ngb_infectious_number function: self neighbors_at (neighbours_size) count (each.is_infected or each.infectious); int ngb_susceptible_number function: self neighbors_at (neighbours_size + 1) count (each.is_susceptible); int ngb_exposed_number function: self neighbors_at (neighbours_size) count (each.is_exposed); list ngb_susceptible function: self neighbors_at (neighbours_size) where (each.is_susceptible); list ngb_infected function: self neighbors_at (neighbours_size) where (each.is_infected or each.infectious); list ngb_exposed function: self neighbors_at (neighbours_size + 1) where (each.is_exposed);

// Variables to control the disease int time_being_exposed; // the time an agent spends being exposed float efficacy; int patch_of_vac; // index to vaccinate an agent randomly
float betaSUS; float betaVAC;

action setInitialLocation {
    myPlace <- one_of(originalPatch);
    location <- myPlace.location;
}

action setAge {
    age <- int(abs(gauss(mean_age, SD_age))); // choosing age of agent randomly based on gauss distribution
    if (age < 0) {age <- 0;} // avoid negative age
}

action agerisk {
    if (age>=0 and age<20) {age_risk <- 0.30;} // to make exposure age-dependent
    else if (age>=20 and age<60) {age_risk <- 0.70;}
    else if (age>=60) {age_risk <- 1.0;}
}

action AbletoTravel { // ability to move between patches depends on age
    if (age > 60){ // check values 0.1 & 0.4
        travel_status <- 0.4;
    }
    else if (age <= 60){
        travel_status <- 0.8;
    }
}

// Shape of agents
aspect basic { draw circle(0.5) color: color; }

// ACTIONS to describe mobility

action moveInNeighborhood {
    sir_grid next <- one_of(myPlace.neighbors where ((each in patches) and (length(Host overlapping each) = 0)));
    if(age>=60){
        if(flip(0.6)){
            myPlace <- next = nil ? myPlace : next;
            location <- myPlace.location;
        }
    }
    else{
       myPlace <- next = nil ? myPlace : next;
       location <- myPlace.location;
    }
    /*if(flip(local_mobility)) { // local mobility control
       myPlace <- next = nil ? myPlace : next;
       location <- myPlace.location;
    }*/
}   

action EnterExit { // enter exit if inside patch and there is a nearby exit
    if(is_infected) {do moveInNeighborhood;} // if infected do not leave the patch
    else {
        if(flip(travel_status) and flip(mobility_probability)){ // mobility between patches depends on age
            sir_grid nextExit <- one_of(myPlace.neighbors where ((each in exit) and (length(Host overlapping each) = 0)));
            myPlace <- nextExit = nil ? myPlace : nextExit;
            location <- myPlace.location;
        }
        else {
            do moveInNeighborhood;
        }
    }
}

action EnterTunnel { // enter tunnel if in exit
    sir_grid nextTunnel <- one_of(myPlace.neighbors where ((each in tunnels or each in exit) and (length(Host overlapping each) = 0)));
    myPlace <- nextTunnel = nil ? myPlace : nextTunnel;
    if (myPlace = nextTunnel and myPlace in tunnels){ // if an agent enters a tunnel 
        current_tunnel <- ((tunnelsMap where (myPlace in each)) at 0); // the index of the current tunnel
        myEntrance <- entrance where (each.neighbors one_matches (each.grid_value = one_of(current_tunnel).grid_value));
        targetEntrance <- one_of(myEntrance); // the entrance at the end of the tunnel
    }
    location <- myPlace.location;
}

action moveInTunnel { // move in tunnel if in tunnel and there is no nearby entrance
    // motion in tunnel is not random, it is towards the entrance
    list<sir_grid> current_tunnel_cells <- myPlace.neighbors where ((each in tunnels) and (length(Host overlapping each) = 0));
    sir_grid next <- current_tunnel_cells closest_to targetEntrance; // move in tunnel closer to entrance
    myPlace <- next = nil ? myPlace : next;
    location <- myPlace.location;
}

action EnterEntrance { // enter entrance if in tunnel and there is a nearby entrance
    sir_grid nextEntrance <- one_of(myPlace.neighbors where ((each in entrance) and (length(Host overlapping each) = 0)));
    myPlace <- nextEntrance = nil ? myPlace : nextEntrance;
    location <- myPlace.location;       
}

action EnterPatch { // enter patch if in entrance
    sir_grid nextPatch <- one_of(myPlace.neighbors where ((each in entrance or each in patches) and (length(Host overlapping each) = 0)));
    myPlace <- nextPatch = nil ? myPlace : nextPatch;
    if (myPlace = nextPatch and myPlace in patches){ // if an agent enters a patch
        currentPatch <- ((patchesMap where (myPlace in each)) at 0); // update the current patch
        time_in_patch <- 0; // initialize the time in patch to zero again
        Leave <- false; // reset Leave bool to false (they do not have to leave the patch)
    }
    location <- myPlace.location;   
}

action moveToExit { // move to the closest exit
    list<sir_grid> accesible_exits <- exit where (each.neighbors one_matches (each.grid_value = one_of(currentPatch).grid_value));
    sir_grid targetExit <- accesible_exits closest_to myPlace;
    list<sir_grid> cells_in_patch <- myPlace.neighbors where ((each in patches) and (length(Host overlapping each) = 0));
    sir_grid next <- cells_in_patch closest_to targetExit;
    myPlace <- next = nil ? myPlace : next;
    location <- myPlace.location;
}

reflex time_in_foreign_patch when: every(1#cycles){
    time_in_patch <- time_in_patch + 1;
}   

reflex OverallMovement {        
    sir_grid nextExit <- one_of(myPlace.neighbors where ((each in exit) and (length(Host overlapping each) = 0)));
    sir_grid nextEntrance <- one_of(myPlace.neighbors where ((each in entrance) and (length(Host overlapping each) = 0)));

    if (myPlace in patches and nextExit=nil and !Leave) {do moveInNeighborhood;} 
    else if(myPlace in patches and nextExit!=nil) {do EnterExit;}
    else if(myPlace in exit) {do EnterTunnel;}
    else if(myPlace in tunnels and nextEntrance=nil) {do moveInTunnel;}
    else if(myPlace in tunnels and nextEntrance!=nil) {do EnterEntrance;}
    else if(myPlace in entrance) {do EnterPatch;}
}

// leave patch when the time in patch reaches the stay time reflex LeavePatch when: (time_in_patch >= stay_time) and (currentPatch!=originalPatch) and (myPlace in patches) { if(!is_infected){ // leave only if not infected Leave <- true; do moveToExit;
}
}

reflex randomize_vaccination when: !is_immune { // reflex to randomize vaccination among patches - it chooses the patch index
    if(flip(1/8)){patch_of_vac <- 1;}
    else if(flip(1/7)){patch_of_vac <- 2;}
    else if(flip(1/6)){patch_of_vac <- 3;}
    else if(flip(1/5)){patch_of_vac <- 4;}
    else if(flip(1/4)){patch_of_vac <- 5;}
    else if(flip(1/3)){patch_of_vac <- 6;}
    else if(flip(1/2)){patch_of_vac <- 7;}
    else {patch_of_vac <- 8;}
}

// REFLEXES to control the disease state

reflex susceptible_to_exposed when: is_susceptible { 
    if (flip(1 - (1 - betaSUS) ^ (ngb_infectious_number)) and flip(age_risk)) { 
        is_susceptible <- false;
        is_exposed <- true;
        is_infected <- false;
        is_immune <- false;
        is_vac <- false;
        color <- #chocolate;
        time_being_exposed <- 0; // set time being exposed to zero once exposed
    }
}

reflex vaccinated_to_exposed when: is_vac {
    if (flip(1 - (1 - betaVAC) ^ (ngb_infectious_number)) and flip(age_risk)) {
        is_susceptible <- false;
        is_exposed <- true;
        is_infected <- false;
        is_immune <- false;
        is_vac <- false;
        color <- #chocolate;
        time_being_exposed <- 0; // set time being exposed to zero once exposed
    }
}

reflex time_since_exposure when: every(1#cycles){
    time_being_exposed <- time_being_exposed + 1;
}

reflex exposed_being_infectious when: (time_being_exposed >= safe_exposure_time) and (is_exposed) {
    infectious <- true;
}

reflex exposed_to_infected when: (is_exposed and (myPlace in patches)) {
    if (flip(theta)) { 
        is_susceptible <- false;
        is_exposed <- false;
        is_infected <- true;
        is_immune <- false;
        is_vac <- false;
        color <- #red;
        infectious <- false; // not exposed anymore, infected are infectious by default 
        CounterofInf <- CounterofInf + 1; // counter of all infected agents

        // update I(peak), time(peak), R0, and time(of R0)

        int index <- int(patchesMap index_of currentPatch); // index of current patch

        int curr_NHI <- NHI_local at index; // previous NHI of current patch
        int curr_NAI <- NAI_local at index; // previous NAI of current patch
        int new_NAI <- Host count (each.active and each.currentPatch = (patchesMap at (index))); // new NAI 
        put (new_NAI) at: index in: NAI_local; // save values
        int new_NHI <- curr_NHI + 1; // new NHI
        put (new_NHI) at: index in: NHI_local; // save values

        float curr_R0 <- R0_local at index; // previous R0 of current patch
        float new_R0 <- new_NAI != 0 and (new_NHI / new_NAI) > curr_R0 ? (new_NHI / new_NAI) : curr_R0; // update R0
        put (new_R0) at: index in: R0_local; // save values

        int curr_R0_max_time <- R0_max_time at index; // previous time(of R0) of current patch
        int new_R0_max_time <- new_NAI != 0 and new_R0 = (new_NHI / new_NAI) ? cycle : curr_R0_max_time; // update time(of R0)
        put (new_R0_max_time) at: index in: R0_max_time; // save values

        int number_of_I_local <- Host count (each.is_infected and each.currentPatch = (patchesMap at (index))); 
        int curr_I_of_peak_local <- I_of_peak_local at index; // previous I(peak) of current patch
        curr_I_of_peak_local <- curr_I_of_peak_local < number_of_I_local ? number_of_I_local : curr_I_of_peak_local; // update I(peak) locally
        put (curr_I_of_peak_local) at: index in: I_of_peak_local; // save values

        int number_of_I_global <- Host count(each.is_infected);             
        I_of_peak_global <- I_of_peak_global < number_of_I_global ? number_of_I_global : I_of_peak_global; // update I(peak) globally

        int curr_time_of_peak_local <- time_of_peak_local at index; // previous time(peak) of current patch
        int new_time_of_peak_local <- curr_I_of_peak_local = number_of_I_local ? cycle : curr_time_of_peak_local; // update time(peak)
        put (new_time_of_peak_local) at: index in: time_of_peak_local; // save values

        int prev_time_of_peak_global <- time_of_peak_global; // previous time(peak) globally
        time_of_peak_global <- I_of_peak_global = number_of_I_global ? cycle : prev_time_of_peak_global; // update time(peak) globally

        // individual R0 values
        R01 <- R0_local at (1);
        R02 <- R0_local at (2);
        R03 <- R0_local at (3);
        R04 <- R0_local at (4);
        R05 <- R0_local at (5);
        R06 <- R0_local at (6);
        R07 <- R0_local at (7);
        R08 <- R0_local at (8);
        R0_global <- (R01+R02+R03+R04+R05+R06+R07+R08)/8; // update R0_global               
    }   
}

reflex has_infected when: is_infected { // reflex of infected who infect other agents
    if (flip(1 - (1 - betaSUS) ^ (ngb_susceptible_number))) { 
        active <- true;
    }
} 

/ reflex has_infected when: is_infected { // reflex of infected who infect other agents if (ngb_exposed_number > 0) { list new_exposed <- ngb_exposed where (each.time_being_exposed <= 1); if (new_exposed != nil) { active <- true; } } } / // Alternative definition for active

reflex infected_to_immune when: ((is_infected) and flip(gamma)) { 
    is_susceptible <- false;
    is_exposed <- false;
    is_infected <- false;
    is_immune <- true;
    is_vac <- false;
    color <- #blue;
    infectious <- false;
}

reflex susceptible_to_vaccinated when: is_susceptible { 
    if(flip(alpha) and currentPatch=patchesMap at (patch_of_vac)){
        is_susceptible <- false;
        is_exposed <- false;
        is_infected <- false;
        is_immune <- false;
        is_vac <- true;
        color <- #purple;   
        infectious <- false;
        CounterofVac <- CounterofVac + 1; // counter of all vaccinated agents 
    }

}

reflex vaccinated_to_immune when: ((is_vac) and flip(efficacy)) { 
    is_susceptible <- false;
    is_exposed <- false;
    is_infected <- false;
    is_immune <- true;
    is_vac <- false;
    color <- #blue;
    infectious <- false;
}

reflex stop_vaccination when: ((CounterofVac / length(Host)) >= vaccination_percentage) {
    alpha <- 0.0;  // stop vaccination when percentage of vaccinated agents is reached
}

reflex update_variables when: (Host count (each.is_infected) <= 10) {
    fitness <- K1*(1-(CounterofInf/(C*3000))) + K2*(1-D) + K3*Cn 
    + K4*mobility_probability + K5*(1-vaccination_percentage);
}       

}

experiment Simulation_SA_Project type: gui {

reflex end_simulation when: (Host count(each.is_infected) = 0) { // end simulation when disease dies out

    // save results
    save R0_local to: "R0_local_SAproject.csv" type: "csv";
    save R0_global to: "R0_global_SAproject.csv" type: "csv";
    save NHI_local to: "NHI_local_SAproject.csv" type: "csv";
    save NAI_local to: "NAI_local_SAproject.csv" type: "csv";
    save R0_max_time to: "R0_max_time_SAproject.csv" type: "csv";
    save I_of_peak_local to: "I_of_peak_local_SAproject.csv" type: "csv";
    save I_of_peak_global to: "I_of_peak_global_SAproject.csv" type: "csv";
    save time_of_peak_local to: "time_of_peak_local_SAproject.csv" type: "csv";
    save time_of_peak_global to: "time_of_peak_global_SAproject.csv" type: "csv";

    do die;
}

output {        
    display sir_display type: java2D {
      grid sir_grid border: #black;
      species Host aspect: basic; 
    }
    /*display Reference_map {
      image my_map; 
    }*/
    display GLOBALinf refresh: every(3#cycles) {
        chart "Infected Agents Globally" type: series background: #white style: exploded {
            data "Infected" value: Host count (each.is_infected) color: #red;
        }
    }
    display "Infected_local" refresh: every(3#cycles) background: #white {          
        chart "infected-local" type: series background: #white style: exploded {
            loop s from:1 to:8 {
                data "Infected " + s
                value: Host count (each.is_infected and each.currentPatch=patchesMap at s) ;
            }
        }
    }

    monitor "R0_global" value: R0_global;
    monitor "R0_patch1" value: R01;
    monitor "R0_patch2" value: R02;
    monitor "R0_patch3" value: R03;
    monitor "R0_patch4" value: R04;
    monitor "R0_patch5" value: R05;
    monitor "R0_patch6" value: R06;
    monitor "R0_patch7" value: R07;
    monitor "R0_patch8" value: R08;
    monitor "Total Vac" value: CounterofVac;
    monitor "Current number of Infected" value: Host count(each.is_infected);
    monitor "Cumulative number of infected" value: CounterofInf;
}

}

experiment Optimization type: batch repeat: 2 until: (Host count (each.is_infected) <= 5 or cycle=2) {

parameter "VC" var: vaccination_percentage min: 0.0 max: 1.0 step: 0.05;
parameter "Pm" var: mobility_probability min: 0.0 max: 1.0 step: 0.05;
parameter "p1" var: P1 min: 0.0 max: 1.0 step: 0.01;
parameter "p2" var: P2 min: 0.0 max: 1.0 step: 0.01;
parameter "cn" var: Cn min: 0.0 max: 1.0 step: 0.01;

method genetic maximize: fitness 
    pop_dim: 2 crossover_prob: 0.5 mutation_prob: 0.3 
    nb_prelim_gen: 10 max_gen: 1;

reflex save_results_explo {
    ask simulations { // simulations is of type list - it is a built-in variable
        save [int(self),
            self.vaccination_percentage,
            self.mobility_probability,
            self.P1,
            self.P2,
            self.Cn,
            self.fitness//,
            //self.CounterofInf / (self.C * 3000),
            //self.D                
        ] 
            to: "optimization_results.csv" type: "csv" rewrite: ((int(self) mod 200) = 0) ? true : false header: true;
    }
 }

}

AlexisDrogoul commented 1 year ago

Hi,

I suggest that you send this model and request for advices either in the discussions (I will convert it to discussion anyway) or -- better -- on the mailing list. And it would be more effective if you explain what you intend to do with this grid with 25k agents: if it is only for holding values and serving as an "environment" to the agents, most can be done with fields rather than grids. If problems with GAMA are to be uncovered when measuring performances, feel free to open another issue on these problems.

Thanks ! Alexis