Skip to content
Snippets Groups Projects
Commit 744ff50a authored by AxelCarayon's avatar AxelCarayon
Browse files

added a new asynchronous scheduler

parent 038516f7
No related branches found
No related tags found
No related merge requests found
Showing
with 143 additions and 141 deletions
SUCEPTIBLE,EXPOSED,RECOVERED,INFECTED
9995,4,0,1
9989,8,0,3
9968,23,0,9
9901,79,0,20
9826,124,0,50
9726,161,7,106
9576,230,13,181
9368,320,23,289
9140,379,46,435
8866,454,72,608
8541,543,110,806
8215,599,165,1021
7859,647,217,1277
7449,732,287,1532
7019,771,390,1820
6517,879,514,2090
6029,892,654,2425
5545,906,812,2737
5076,911,990,3023
4613,925,1164,3298
4097,977,1381,3545
3618,941,1616,3825
3126,956,1847,4071
2695,867,2086,4352
2286,849,2351,4514
1886,836,2637,4641
1513,761,2916,4810
1199,709,3209,4883
886,670,3509,4935
632,585,3815,4968
454,451,4097,4998
318,360,4398,4924
212,280,4695,4813
142,196,4985,4677
78,161,5270,4491
34,120,5554,4292
7,84,5784,4125
0,49,6046,3905
0,27,6305,3668
0,14,6522,3464
0,6,6726,3268
0,3,6944,3053
0,2,7118,2880
0,1,7318,2681
0,0,7477,2523
0,0,7621,2379
0,0,7754,2246
0,0,7879,2121
0,0,8013,1987
0,0,8122,1878
0,0,8253,1747
0,0,8360,1640
0,0,8468,1532
0,0,8577,1423
0,0,8660,1340
0,0,8737,1263
0,0,8815,1185
0,0,8894,1106
0,0,8961,1039
0,0,9027,973
0,0,9083,917
0,0,9144,856
0,0,9198,802
9988,9,0,3
9957,32,0,11
9881,90,0,29
9795,132,3,70
9666,197,11,126
9520,245,20,215
9307,336,35,322
9093,369,53,485
8817,445,80,658
8495,537,122,846
8172,597,171,1060
7811,663,237,1289
7405,725,319,1551
6958,821,419,1802
6496,863,536,2105
6028,879,668,2425
5539,940,808,2713
5093,899,983,3025
4676,842,1177,3305
4255,822,1389,3534
3793,875,1604,3728
3383,832,1822,3963
2968,827,2050,4155
2577,805,2301,4317
2186,816,2561,4437
1792,818,2811,4579
1373,804,3091,4732
1036,764,3395,4805
763,653,3670,4914
554,519,3953,4974
393,403,4256,4948
301,282,4565,4852
212,218,4873,4697
135,192,5176,4497
86,145,5454,4315
......@@ -2,14 +2,15 @@ package models;
public class Parameters {
int seed;
int population;
int size;
int nbOfPatientZero;
float infectionChance;
float incubationRate;
float recoveryRate;
int nbOfCycles;
private int seed;
private int population;
private int size;
private int nbOfPatientZero;
private float infectionChance;
private float incubationRate;
private float recoveryRate;
private int nbOfCycles;
private boolean synchronousMode;
public Parameters() {
}
......@@ -18,57 +19,35 @@ public class Parameters {
public void setNbOfPatientZero(int nbOfPatientZero) {this.nbOfPatientZero = nbOfPatientZero;}
public int getSeed() {
return seed;
}
public int getSeed() {return seed;}
public void setSeed(int seed) {
this.seed = seed;
}
public void setSeed(int seed) {this.seed = seed;}
public int getPopulation() {
return population;
}
public int getPopulation() {return population;}
public void setPopulation(int population) {
this.population = population;
}
public void setPopulation(int population) {this.population = population;}
public int getSize() {
return size;
}
public int getSize() {return size;}
public void setSize(int size) {
this.size = size;
}
public void setSize(int size) {this.size = size;}
public float getInfectionChance() { return infectionChance; }
public void setInfectionChance(float infectionChance) {
this.infectionChance = infectionChance;
}
public void setInfectionChance(float infectionChance) {this.infectionChance = infectionChance;}
public float getIncubationRate() {
return incubationRate;
}
public float getIncubationRate() {return incubationRate;}
public void setIncubationRate(float incubationRate) {
this.incubationRate = incubationRate;
}
public void setIncubationRate(float incubationRate) {this.incubationRate = incubationRate;}
public float getRecoveryRate() {
return recoveryRate;
}
public float getRecoveryRate() {return recoveryRate;}
public void setRecoveryRate(float recoveryRate) {
this.recoveryRate = recoveryRate;
}
public void setRecoveryRate(float recoveryRate) {this.recoveryRate = recoveryRate;}
public int getNbOfCycles() {
return nbOfCycles;
}
public int getNbOfCycles() {return nbOfCycles;}
public void setNbOfCycles(int nbOfCycles) {
this.nbOfCycles = nbOfCycles;
}
public void setNbOfCycles(int nbOfCycles) {this.nbOfCycles = nbOfCycles;}
public boolean isSynchronousMode() {return synchronousMode;}
public void setSynchronousMode(boolean synchronousMode) {this.synchronousMode = synchronousMode;}
}
package sma;
import models.Parameters;
import sma.agents.Agent;
import sma.scheduler.FairAsynchronousScheduler;
import sma.scheduler.FairSynchronousScheduler;
import sma.scheduler.Scheduler;
import utils.DataAdapter;
import utils.Pair;
import utils.StatsRecorder;
......@@ -51,6 +55,14 @@ public class SMA {
}
}
private void initScheduler() {
if (parameters.isSynchronousMode()) {
scheduler = new FairSynchronousScheduler(agents, parameters.getSeed());
} else {
scheduler = new FairAsynchronousScheduler(agents);
}
}
public void init() {
environment = new GraphicEnvironment(parameters.getSize(),parameters.getSize(),agents);
populateEnvironment();
......@@ -61,26 +73,37 @@ public class SMA {
frameBuilder.addComponent(statisticsCanvas,FrameBuilder.RIGHT);
frameBuilder.buildWindow();
scheduler = new Scheduler(agents, parameters.getSeed());
initScheduler();
statisticsCanvas.updateValues(environment.getAgentStatus());
statisticsCanvas.repaint();
}
private void updateGraphics(){
environment.repaint();
statisticsCanvas.updateValues(stats);
statisticsCanvas.repaint();
environment.repaint();
}
private void doNextCycle() throws IOException, InterruptedException {
scheduler.nextCycle();
stats = environment.getAgentStatus();
StatsRecorder.writeToCSV(DataAdapter.adaptData(stats),"output.csv");
updateGraphics();
Thread.sleep(100);
}
public void run() throws IOException, InterruptedException {
int cpt = 0;
while (cpt < parameters.getNbOfCycles()) {
cpt++;
scheduler.nextCycle();
stats = environment.getAgentStatus();
updateGraphics();
StatsRecorder.writeToCSV(DataAdapter.adaptData(stats),"output.csv");
Thread.sleep(100);
if (parameters.getNbOfCycles() <0) {
while (true) {
doNextCycle();
}
} else {
int cpt = 0;
while (cpt < parameters.getNbOfCycles()) {
doNextCycle();
cpt++;
}
}
}
......
package sma;
package sma.agents;
import sma.environment.Environment;
import utils.YamlReader;
import view.GraphicEnvironment;
......@@ -23,6 +24,7 @@ public class Agent {
private Boolean exposedThisCycle;
private Boolean infectedThisCycle;
public Agent(Point position, int seed, GraphicEnvironment environment) {
this.position = position;
this.state = State.SUSCEPTIBLE;
......@@ -34,10 +36,10 @@ public class Agent {
int move = r.nextInt(4);
Point newPosition = switch (move) {
case IEnvironment.LEFT -> new Point(position.x-environment.RADIUS,position.y);
case IEnvironment.RIGHT -> new Point(position.x+environment.RADIUS,position.y);
case IEnvironment.UP -> new Point(position.x,position.y-environment.RADIUS);
case IEnvironment.DOWN -> new Point(position.x,position.y+environment.RADIUS);
case Environment.LEFT -> new Point(position.x-environment.RADIUS,position.y);
case Environment.RIGHT -> new Point(position.x+environment.RADIUS,position.y);
case Environment.UP -> new Point(position.x,position.y-environment.RADIUS);
case Environment.DOWN -> new Point(position.x,position.y+environment.RADIUS);
default -> throw new IllegalStateException("Unexpected value: " + move);
};
if (newPosition.x <= environment.getWidth()-1 && newPosition.x >= 0 && newPosition.y <= environment.getHeight()-1 && newPosition.y >=0 ) {
......
package sma;
package sma.environment;
import sma.agents.Agent;
import java.awt.Point;
import java.util.List;
public interface IEnvironment {
public interface Environment {
int LEFT = 0;
int RIGHT = 1;
int UP = 2;
......
package sma.scheduler;
import sma.agents.Agent;
import java.util.Arrays;
import java.util.List;
public class FairAsynchronousScheduler implements Scheduler{
private List<Agent> agents;
public FairAsynchronousScheduler(Agent[] agents) {
this.agents = Arrays.asList(agents);
}
public void nextCycle() {
agents.stream().parallel().forEach(a -> a.wakeUp());
}
}
package sma;
package sma.scheduler;
import sma.agents.Agent;
import java.util.*;
public class Scheduler{
public class FairSynchronousScheduler implements Scheduler {
private Agent[] agents;
private Stack<Integer> executionOrder;
private Random r;
public Scheduler(Agent[] agents,int seed) {
public FairSynchronousScheduler(Agent[] agents, int seed) {
this.agents = agents;
r = new Random(seed);
executionOrder = new Stack<>();
......
package sma.scheduler;
public interface Scheduler {
void nextCycle();
}
package utils;
import sma.Agent;
import sma.agents.Agent;
import java.awt.*;
import java.util.HashMap;
......
......@@ -14,9 +14,6 @@ public class FrameBuilder {
public final static int TOP = 2;
public final static int BOTTOM = 3;
private int frameWidth;
private int frameHeight;
private List<Pair<Component,Integer>> components;
public FrameBuilder() {
......@@ -25,8 +22,6 @@ public class FrameBuilder {
public void resetWindow() {
components = new ArrayList<>();
frameWidth = 0;
frameHeight = 0;
}
......
package view;
import sma.Agent;
import sma.IEnvironment;
import sma.agents.Agent;
import sma.environment.Environment;
import utils.Pair;
import java.awt.*;
import java.util.*;
import java.util.List;
public class GraphicEnvironment extends Canvas implements IEnvironment {
public class GraphicEnvironment extends Canvas implements Environment {
public final static int RADIUS = 10;
......
package view;
import sma.Agent;
import sma.agents.Agent;
import utils.Pair;
import utils.YamlReader;
......
......@@ -5,4 +5,5 @@ nbOfPatientZero : 1
infectionChance : 0.1 #percentage of chance that an infected agent will spread to a susceptible agent
incubationRate : 0.5 #chance that an exposed agent become infected each cycle
recoveryRate : 0.05 #chance that an infected agent become recovered each cycle
nbOfCycles : 100
\ No newline at end of file
nbOfCycles : -1 #if the number is negative, will run endlessly
synchronousMode : false #if true, will wake synchronously the agents in a pseudo-random order based on the given seed otherwise will wake up the agents asynchronously
\ No newline at end of file
......@@ -5,4 +5,5 @@ nbOfPatientZero : 1
infectionChance : 0.1 #percentage of chance that an infected agent will spread to a susceptible agent
incubationRate : 0.5 #chance that an exposed agent become infected each cycle
recoveryRate : 0.05 #chance that an infected agent become recovered each cycle
nbOfCycles : 100
\ No newline at end of file
nbOfCycles : -1 #if the number is negative, will run endlessly
synchronousMode : false #if true, will wake synchronously the agents in a pseudo-random order based on the given seed otherwise will wake up the agents asynchronously
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment