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

upgraded to SEIRS and improved display

parent 84d70fd6
No related branches found
No related tags found
No related merge requests found
Showing
with 209830 additions and 78 deletions
This diff is collapsed.
...@@ -6,9 +6,10 @@ public class Parameters { ...@@ -6,9 +6,10 @@ public class Parameters {
private int population; private int population;
private int size; private int size;
private int nbOfPatientZero; private int nbOfPatientZero;
private float infectionChance; private float infectionRate;
private float incubationRate; private float incubationRate;
private float recoveryRate; private float recoveryRate;
private float looseImmunityRate;
private int nbOfCycles; private int nbOfCycles;
private boolean synchronousMode; private boolean synchronousMode;
...@@ -31,9 +32,9 @@ public class Parameters { ...@@ -31,9 +32,9 @@ public class Parameters {
public void setSize(int size) {this.size = size;} public void setSize(int size) {this.size = size;}
public float getInfectionChance() { return infectionChance; } public float getInfectionRate() { return infectionRate; }
public void setInfectionChance(float infectionChance) {this.infectionChance = infectionChance;} public void setInfectionRate(float infectionRate) {this.infectionRate = infectionRate;}
public float getIncubationRate() {return incubationRate;} public float getIncubationRate() {return incubationRate;}
...@@ -43,6 +44,10 @@ public class Parameters { ...@@ -43,6 +44,10 @@ public class Parameters {
public void setRecoveryRate(float recoveryRate) {this.recoveryRate = recoveryRate;} public void setRecoveryRate(float recoveryRate) {this.recoveryRate = recoveryRate;}
public float getLooseImmunityRate() { return looseImmunityRate; }
public void setLooseImmunityRate(float looseImmunityRate) { this.looseImmunityRate = looseImmunityRate; }
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;}
......
...@@ -89,7 +89,7 @@ public class SMA { ...@@ -89,7 +89,7 @@ public class SMA {
stats = environment.getAgentStatus(); stats = environment.getAgentStatus();
StatsRecorder.writeToCSV(stats,"output.csv"); StatsRecorder.writeToCSV(stats,"output.csv");
updateGraphics(); updateGraphics();
Thread.sleep(100); //Thread.sleep(100);
} }
public void run() throws IOException, InterruptedException { public void run() throws IOException, InterruptedException {
......
...@@ -8,5 +8,6 @@ public interface Agent { ...@@ -8,5 +8,6 @@ public interface Agent {
boolean isExposed(); boolean isExposed();
boolean isInfected(); boolean isInfected();
boolean isRecovered(); boolean isRecovered();
boolean hasLostImmunity();
void move(); void move();
} }
...@@ -50,7 +50,7 @@ public class RandomWalkingAgent implements Agent { ...@@ -50,7 +50,7 @@ public class RandomWalkingAgent implements Agent {
for (RandomWalkingAgent neighbor: environment.getNeighbors(position)) { for (RandomWalkingAgent neighbor: environment.getNeighbors(position)) {
if (neighbor.getState() instanceof InfectedState) { if (neighbor.getState() instanceof InfectedState) {
int roll = r.nextInt(100); int roll = r.nextInt(100);
if (roll <= YamlReader.getParams().getInfectionChance()*100) { if (roll <= YamlReader.getParams().getInfectionRate()*100) {
isExposed = true; isExposed = true;
} }
} }
...@@ -78,6 +78,16 @@ public class RandomWalkingAgent implements Agent { ...@@ -78,6 +78,16 @@ public class RandomWalkingAgent implements Agent {
return isHealed; return isHealed;
} }
@Override
public boolean hasLostImmunity() {
boolean hasLostImmunity = false;
int roll = r.nextInt(100);
if (roll <= YamlReader.getParams().getLooseImmunityRate()*100) {
hasLostImmunity = true;
}
return hasLostImmunity;
}
public State getState() { return this.state; } public State getState() { return this.state; }
public Point getPosition() { return position; } public Point getPosition() { return position; }
......
...@@ -17,6 +17,6 @@ public class ExposedState extends State{ ...@@ -17,6 +17,6 @@ public class ExposedState extends State{
@Override @Override
public String toString() { public String toString() {
return "EXPOSED"; return EXPOSED;
} }
} }
...@@ -17,6 +17,6 @@ public class InfectedState extends State{ ...@@ -17,6 +17,6 @@ public class InfectedState extends State{
@Override @Override
public String toString() { public String toString() {
return "INFECTED"; return INFECTED;
} }
} }
...@@ -10,11 +10,13 @@ public class RecoveredState extends State{ ...@@ -10,11 +10,13 @@ public class RecoveredState extends State{
@Override @Override
public void onMovement() { public void onMovement() {
if (agent.hasLostImmunity()) {
agent.changeState(new SuceptibleState(agent));
}
} }
@Override @Override
public String toString() { public String toString() {
return "RECOVERED"; return RECOVERED;
} }
} }
...@@ -2,6 +2,8 @@ package sma.agents.states; ...@@ -2,6 +2,8 @@ package sma.agents.states;
import sma.agents.Agent; import sma.agents.Agent;
import java.awt.*;
public class SuceptibleState extends State{ public class SuceptibleState extends State{
public SuceptibleState(Agent agent) { public SuceptibleState(Agent agent) {
...@@ -17,6 +19,6 @@ public class SuceptibleState extends State{ ...@@ -17,6 +19,6 @@ public class SuceptibleState extends State{
@Override @Override
public String toString() { public String toString() {
return "SUCEPTIBLE"; return SUCEPTIBLE;
} }
} }
...@@ -14,7 +14,10 @@ public class FrameBuilder { ...@@ -14,7 +14,10 @@ public class FrameBuilder {
public final static int TOP = 2; public final static int TOP = 2;
public final static int BOTTOM = 3; public final static int BOTTOM = 3;
private List<Pair<Component,Integer>> components; private int windowWidth = 0;
private int windowHeight = 0;
private List<Pair<JComponent,Integer>> components;
public FrameBuilder() { public FrameBuilder() {
resetWindow(); resetWindow();
...@@ -25,8 +28,16 @@ public class FrameBuilder { ...@@ -25,8 +28,16 @@ public class FrameBuilder {
} }
public void addComponent(Component c,int position) { public void addComponent(JComponent c,int position) {
var pair = new Pair<>(c,position); var pair = new Pair<>(c,position);
System.out.println(c.getWidth());
System.out.println(c.getHeight());
if (windowWidth < c.getWidth()) {
windowWidth = c.getWidth();
}
if (windowHeight < c.getHeight()) {
windowHeight = c.getHeight();
}
components.add(pair); components.add(pair);
} }
...@@ -35,7 +46,9 @@ public class FrameBuilder { ...@@ -35,7 +46,9 @@ public class FrameBuilder {
frame.setLayout(new java.awt.GridLayout()); frame.setLayout(new java.awt.GridLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (Pair<Component,Integer> pair : components ) { frame.setSize(new Dimension(1000,500));
for (Pair<JComponent,Integer> pair : components ) {
switch (pair.getSecond()) { switch (pair.getSecond()) {
case TOP -> frame.add(pair.getFirst(),BorderLayout.NORTH); case TOP -> frame.add(pair.getFirst(),BorderLayout.NORTH);
case BOTTOM -> frame.add(pair.getFirst(),BorderLayout.SOUTH); case BOTTOM -> frame.add(pair.getFirst(),BorderLayout.SOUTH);
...@@ -44,7 +57,7 @@ public class FrameBuilder { ...@@ -44,7 +57,7 @@ public class FrameBuilder {
default -> throw new IllegalStateException("Wrong position value"); default -> throw new IllegalStateException("Wrong position value");
} }
} }
frame.pack(); //frame.pack();
frame.setResizable(false); frame.setResizable(false);
frame.setVisible(true); frame.setVisible(true);
return frame; return frame;
......
...@@ -3,14 +3,13 @@ package view; ...@@ -3,14 +3,13 @@ package view;
import sma.agents.RandomWalkingAgent; import sma.agents.RandomWalkingAgent;
import sma.agents.states.*; import sma.agents.states.*;
import sma.environment.Environment; import sma.environment.Environment;
import utils.Pair;
import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.security.InvalidParameterException;
import java.util.*; import java.util.*;
import java.util.List; import java.util.List;
public class GraphicEnvironment extends Canvas implements Environment { public class GraphicEnvironment extends JPanel implements Environment {
public final static int RADIUS = 10; public final static int RADIUS = 10;
...@@ -24,6 +23,7 @@ public class GraphicEnvironment extends Canvas implements Environment { ...@@ -24,6 +23,7 @@ public class GraphicEnvironment extends Canvas implements Environment {
private int windowHeight; private int windowHeight;
public GraphicEnvironment(int width, int height, RandomWalkingAgent[] agents) { public GraphicEnvironment(int width, int height, RandomWalkingAgent[] agents) {
this.setDoubleBuffered(true);
this.windowWidth = width; this.windowWidth = width;
this.windowHeight = height; this.windowHeight = height;
this.agents = agents; this.agents = agents;
......
package view; package view;
import sma.agents.RandomWalkingAgent;
import sma.agents.states.State; import sma.agents.states.State;
import utils.Pair;
import utils.YamlReader; import utils.YamlReader;
import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.HashMap; import java.util.HashMap;
public class StatisticsCanvas extends Canvas { public class StatisticsCanvas extends JPanel {
private int canvasWidth; private int canvasWidth;
private int canvasHeight; private int canvasHeight;
...@@ -16,6 +15,7 @@ public class StatisticsCanvas extends Canvas { ...@@ -16,6 +15,7 @@ public class StatisticsCanvas extends Canvas {
private int total; private int total;
public StatisticsCanvas(int width,int height) { public StatisticsCanvas(int width,int height) {
this.setDoubleBuffered(false);
canvasHeight = height; canvasHeight = height;
canvasWidth = width; canvasWidth = width;
values = new HashMap<>(); values = new HashMap<>();
...@@ -41,10 +41,7 @@ public class StatisticsCanvas extends Canvas { ...@@ -41,10 +41,7 @@ public class StatisticsCanvas extends Canvas {
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
int start = 0; int start = 0;
g.clearRect(0,0,getWidth(),getHeight());
for (String state : values.keySet()) {
}
for (int i=0 ; i <values.keySet().size();i++) { for (int i=0 ; i <values.keySet().size();i++) {
String state = (String) values.keySet().toArray()[i]; String state = (String) values.keySet().toArray()[i];
int value = values.get(state); int value = values.get(state);
......
seed : 120 seed : 120
population : 10000 #number of agents population : 3000 #number of agents
size : 500 #size of the world in pixels size : 500 #size of the world in pixels
nbOfPatientZero : 1 nbOfPatientZero : 1
infectionChance : 0.1 #percentage of chance that an infected agent will spread to a susceptible agent infectionRate : 0.05 #chance that an infected agent will spread to a susceptible agent
incubationRate : 0.5 #chance that an exposed agent become infected each cycle incubationRate : 0.5 #chance that an exposed agent become infected each cycle
recoveryRate : 0.05 #chance that an infected agent become recovered each cycle recoveryRate : 0.05 #chance that an infected agent become recovered each cycle
looseImmunityRate : 0.01 #chance that a recovered agent become suceptible again
nbOfCycles : -1 #if the number is negative, will run endlessly 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 synchronousMode : true #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 \ No newline at end of file
seed : 120 seed : 120
population : 10000 #number of agents population : 3000 #number of agents
size : 500 #size of the world in pixels size : 500 #size of the world in pixels
nbOfPatientZero : 1 nbOfPatientZero : 1
infectionChance : 0.1 #percentage of chance that an infected agent will spread to a susceptible agent infectionRate : 0.05 #chance that an infected agent will spread to a susceptible agent
incubationRate : 0.5 #chance that an exposed agent become infected each cycle incubationRate : 0.5 #chance that an exposed agent become infected each cycle
recoveryRate : 0.05 #chance that an infected agent become recovered each cycle recoveryRate : 0.05 #chance that an infected agent become recovered each cycle
looseImmunityRate : 0.01 #chance that a recovered agent become suceptible again
nbOfCycles : -1 #if the number is negative, will run endlessly 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 synchronousMode : true #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 \ 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