diff --git a/output.csv b/output.csv index 63c94aa947fd04da0a383d8a8935b103d88f4a7f..59a2b5b10ebc9dff6399021b41e2590cee85f1f1 100644 --- a/output.csv +++ b/output.csv @@ -1,64 +1,37 @@ 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 diff --git a/src/main/java/models/Parameters.java b/src/main/java/models/Parameters.java index dd13a267fed158336b482e605e786f838ef1e401..648622bb0ccca35e0b733baac15410fe88482887 100644 --- a/src/main/java/models/Parameters.java +++ b/src/main/java/models/Parameters.java @@ -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;} } diff --git a/src/main/java/sma/SMA.java b/src/main/java/sma/SMA.java index 32890bd191bfd322cf2f65c9cf798d2f1839d289..8eaf1f7b0cddd2a1f052f51d189d8ce966a3849c 100644 --- a/src/main/java/sma/SMA.java +++ b/src/main/java/sma/SMA.java @@ -1,6 +1,10 @@ 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++; + } } } diff --git a/src/main/java/sma/Agent.java b/src/main/java/sma/agents/Agent.java similarity index 87% rename from src/main/java/sma/Agent.java rename to src/main/java/sma/agents/Agent.java index a1ab9d999e745b5d82306ab0c3063dd20ad6efac..f8d029a1c0eb0e7e207a7425679f0e5a0ddbad84 100644 --- a/src/main/java/sma/Agent.java +++ b/src/main/java/sma/agents/Agent.java @@ -1,5 +1,6 @@ -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 ) { diff --git a/src/main/java/sma/IEnvironment.java b/src/main/java/sma/environment/Environment.java similarity index 78% rename from src/main/java/sma/IEnvironment.java rename to src/main/java/sma/environment/Environment.java index 8457c8182fdd9643c253d6be6e8e9eeda01e55f7..5709c4e0f68bdf6eb0e0205214a855decff37665 100644 --- a/src/main/java/sma/IEnvironment.java +++ b/src/main/java/sma/environment/Environment.java @@ -1,9 +1,11 @@ -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; diff --git a/src/main/java/sma/scheduler/FairAsynchronousScheduler.java b/src/main/java/sma/scheduler/FairAsynchronousScheduler.java new file mode 100644 index 0000000000000000000000000000000000000000..0b9e228b381acba2ff83e2bf1e39496038432053 --- /dev/null +++ b/src/main/java/sma/scheduler/FairAsynchronousScheduler.java @@ -0,0 +1,18 @@ +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()); + } +} diff --git a/src/main/java/sma/Scheduler.java b/src/main/java/sma/scheduler/FairSynchronousScheduler.java similarity index 79% rename from src/main/java/sma/Scheduler.java rename to src/main/java/sma/scheduler/FairSynchronousScheduler.java index fd5d8b79aa7f4c022dc683151fcb9aaa7154ded2..6d365e9469adb5da9598c2a3873a78a7605a23b4 100644 --- a/src/main/java/sma/Scheduler.java +++ b/src/main/java/sma/scheduler/FairSynchronousScheduler.java @@ -1,14 +1,16 @@ -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<>(); diff --git a/src/main/java/sma/scheduler/Scheduler.java b/src/main/java/sma/scheduler/Scheduler.java new file mode 100644 index 0000000000000000000000000000000000000000..b891c69233d83cf6473030f5f7377760862870cd --- /dev/null +++ b/src/main/java/sma/scheduler/Scheduler.java @@ -0,0 +1,6 @@ +package sma.scheduler; + +public interface Scheduler { + + void nextCycle(); +} diff --git a/src/main/java/utils/DataAdapter.java b/src/main/java/utils/DataAdapter.java index f5ba642fc228ef2f856ea98e994e07b00b8db791..a698185105599e8b64a4efe56b323967a9d11030 100644 --- a/src/main/java/utils/DataAdapter.java +++ b/src/main/java/utils/DataAdapter.java @@ -1,6 +1,6 @@ package utils; -import sma.Agent; +import sma.agents.Agent; import java.awt.*; import java.util.HashMap; diff --git a/src/main/java/view/FrameBuilder.java b/src/main/java/view/FrameBuilder.java index b38173c3be43f626482caef4977f8a4f78f2f667..5dd2547569f26f11cc8cd996b631dff4a13910d1 100644 --- a/src/main/java/view/FrameBuilder.java +++ b/src/main/java/view/FrameBuilder.java @@ -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; } diff --git a/src/main/java/view/GraphicEnvironment.java b/src/main/java/view/GraphicEnvironment.java index 79a5d8c69bc869b8dd0e64af597614378019b76a..a21a9a3ed92cf1ba2a9bcc6aa7c074e24e3d2827 100644 --- a/src/main/java/view/GraphicEnvironment.java +++ b/src/main/java/view/GraphicEnvironment.java @@ -1,14 +1,14 @@ 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; diff --git a/src/main/java/view/StatisticsCanvas.java b/src/main/java/view/StatisticsCanvas.java index 882dd242795899f8f613862a0fad9c0ac3493509..fea95cf66297d9b94e2c743d14d14f26d2e6e856 100644 --- a/src/main/java/view/StatisticsCanvas.java +++ b/src/main/java/view/StatisticsCanvas.java @@ -1,6 +1,6 @@ package view; -import sma.Agent; +import sma.agents.Agent; import utils.Pair; import utils.YamlReader; diff --git a/src/main/resources/parameters.yaml b/src/main/resources/parameters.yaml index 430203ac5567b935c932a61b88de3c6758e697bd..d45b361e5a667148a3671253bcf9db799462b9e3 100644 --- a/src/main/resources/parameters.yaml +++ b/src/main/resources/parameters.yaml @@ -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 diff --git a/target/classes/parameters.yaml b/target/classes/parameters.yaml index 430203ac5567b935c932a61b88de3c6758e697bd..d45b361e5a667148a3671253bcf9db799462b9e3 100644 --- a/target/classes/parameters.yaml +++ b/target/classes/parameters.yaml @@ -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