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

added an option to wrap the world

parent 975facc9
Branches
No related tags found
No related merge requests found
...@@ -10,6 +10,7 @@ import java.security.SecureRandom; ...@@ -10,6 +10,7 @@ import java.security.SecureRandom;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@SuppressWarnings("ThrowablePrintedToSystemOut")
public class RandomWalkingAgent implements SEIRSAgent { public class RandomWalkingAgent implements SEIRSAgent {
protected Point position; protected Point position;
...@@ -19,10 +20,8 @@ public class RandomWalkingAgent implements SEIRSAgent { ...@@ -19,10 +20,8 @@ public class RandomWalkingAgent implements SEIRSAgent {
private List<Point> authorizedPositions; private List<Point> authorizedPositions;
private Point nextPosition; private Point nextPosition;
private int seed;
public RandomWalkingAgent(Point position, int seed, SEIRSEnvironment environment) { public RandomWalkingAgent(Point position, int seed, SEIRSEnvironment environment) {
this.seed = seed;
this.position = position; this.position = position;
this.state = new SuceptibleSEIRSState(this); this.state = new SuceptibleSEIRSState(this);
this.environment = environment; this.environment = environment;
...@@ -30,6 +29,7 @@ public class RandomWalkingAgent implements SEIRSAgent { ...@@ -30,6 +29,7 @@ public class RandomWalkingAgent implements SEIRSAgent {
r = SecureRandom.getInstance("SHA1PRNG", "SUN"); r = SecureRandom.getInstance("SHA1PRNG", "SUN");
}catch (Exception e) { }catch (Exception e) {
System.err.println(e); System.err.println(e);
System.exit(1);
} }
r.setSeed(seed); r.setSeed(seed);
} }
...@@ -66,7 +66,7 @@ public class RandomWalkingAgent implements SEIRSAgent { ...@@ -66,7 +66,7 @@ public class RandomWalkingAgent implements SEIRSAgent {
boolean isExposed = false; boolean isExposed = false;
for (int i = 0 ; i<environment.getInfectedNeighbors(position).size() ; i++) { for (int i = 0 ; i<environment.getInfectedNeighbors(position).size() ; i++) {
int roll = r.nextInt(10000)+1; int roll = r.nextInt(10000)+1;
if (roll <= YamlReader.getParams().getInfectionRate()*10000) { if (roll <= YamlReader.getParams().infectionRate()*10000) {
isExposed = true; isExposed = true;
} }
} }
...@@ -77,7 +77,7 @@ public class RandomWalkingAgent implements SEIRSAgent { ...@@ -77,7 +77,7 @@ public class RandomWalkingAgent implements SEIRSAgent {
public boolean isInfected() { public boolean isInfected() {
boolean isSick = false; boolean isSick = false;
int roll = r.nextInt(10000)+1; int roll = r.nextInt(10000)+1;
if (roll <= YamlReader.getParams().getIncubationRate()*10000) { if (roll <= YamlReader.getParams().incubationRate()*10000) {
isSick = true; isSick = true;
} }
return isSick; return isSick;
...@@ -87,7 +87,7 @@ public class RandomWalkingAgent implements SEIRSAgent { ...@@ -87,7 +87,7 @@ public class RandomWalkingAgent implements SEIRSAgent {
public boolean isRecovered() { public boolean isRecovered() {
boolean isHealed = false; boolean isHealed = false;
int roll = r.nextInt(10000)+1; int roll = r.nextInt(10000)+1;
if (roll <= YamlReader.getParams().getRecoveryRate()*10000) { if (roll <= YamlReader.getParams().recoveryRate()*10000) {
isHealed = true; isHealed = true;
} }
return isHealed; return isHealed;
...@@ -97,7 +97,7 @@ public class RandomWalkingAgent implements SEIRSAgent { ...@@ -97,7 +97,7 @@ public class RandomWalkingAgent implements SEIRSAgent {
public boolean hasLostImmunity() { public boolean hasLostImmunity() {
boolean hasLostImmunity = false; boolean hasLostImmunity = false;
int roll = r.nextInt(10000)+1; int roll = r.nextInt(10000)+1;
if (roll <= YamlReader.getParams().getLooseImmunityRate()*10000) { if (roll <= YamlReader.getParams().looseImmunityRate()*10000) {
hasLostImmunity = true; hasLostImmunity = true;
} }
return hasLostImmunity; return hasLostImmunity;
......
...@@ -78,32 +78,12 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment { ...@@ -78,32 +78,12 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment {
return neighbors; return neighbors;
} }
private void wrapPosition(Point newPosition) { @Override
if (newPosition.x >= size) {
newPosition.x -= size-1;
}
if (newPosition.x < 0) {
newPosition.x += size-1;
}
if (newPosition.y >= size) {
newPosition.y -= size-1;
}
if (newPosition.y < 0) {
newPosition.y +=size-1;
}
}
public List<Point> perceiveAuthorizedPositions(Agent2D agent) { public List<Point> perceiveAuthorizedPositions(Agent2D agent) {
List<Point> authorizedPosition = new ArrayList<>(); List<Point> authorizedPosition = new ArrayList<>();
for (int move = 0; move < MAX_MOVEMENT; move++) { for (int move = 0; move < MAX_MOVEMENT; move++) {
Point position = switch (move) { Point position = getNextPosition(move,agent.getPosition());
case LEFT -> new Point(agent.getPosition().x-RADIUS,agent.getPosition().y);
case RIGHT -> new Point (agent.getPosition().x+RADIUS,agent.getPosition().y);
case UP -> new Point(agent.getPosition().x, agent.getPosition().y-RADIUS);
case DOWN -> new Point(agent.getPosition().x, agent.getPosition().y +RADIUS);
default -> new Point(FORBIDDEN,FORBIDDEN);
};
if (position.x < size && position.x >= 0 && position.y < size && position.y >= 0) { if (position.x < size && position.x >= 0 && position.y < size && position.y >= 0) {
authorizedPosition.add(position); authorizedPosition.add(position);
} }
...@@ -111,6 +91,16 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment { ...@@ -111,6 +91,16 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment {
return authorizedPosition; return authorizedPosition;
} }
protected Point getNextPosition(int move, Point position) {
return switch (move) {
case LEFT -> new Point(position.x-RADIUS,position.y);
case RIGHT -> new Point (position.x+RADIUS,position.y);
case UP -> new Point(position.x, position.y-RADIUS);
case DOWN -> new Point(position.x, position.y +RADIUS);
default -> new Point(FORBIDDEN,FORBIDDEN);
};
}
@Override @Override
public void notifyNewPosition(Point newPosition, Agent2D agent) { public void notifyNewPosition(Point newPosition, Agent2D agent) {
if (chunks == null) { if (chunks == null) {
...@@ -121,7 +111,6 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment { ...@@ -121,7 +111,6 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment {
chunks[oldPosition.x/CHUNK_SIZE][oldPosition.y/CHUNK_SIZE].remove((SEIRSAgent) agent); chunks[oldPosition.x/CHUNK_SIZE][oldPosition.y/CHUNK_SIZE].remove((SEIRSAgent) agent);
chunks[newPosition.x/CHUNK_SIZE][newPosition.y/CHUNK_SIZE].add((SEIRSAgent) agent); chunks[newPosition.x/CHUNK_SIZE][newPosition.y/CHUNK_SIZE].add((SEIRSAgent) agent);
} }
//wrapPosition(newPosition);
} }
@Override @Override
......
package environment;
import agents.Agent2D;
import agents.SEIRSAgent;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class WrappingChunkedSEIRSEnvironment extends ChunkedSEIRSEnvironment implements SEIRSEnvironment{
public WrappingChunkedSEIRSEnvironment(int size, SEIRSAgent[] agents) {
super(size, agents);
}
private void wrapPosition(Point newPosition) {
if (newPosition.x >= size) {
newPosition.x -= size-1;
}
if (newPosition.x < 0) {
newPosition.x += size-1;
}
if (newPosition.y >= size) {
newPosition.y -= size-1;
}
if (newPosition.y < 0) {
newPosition.y +=size-1;
}
}
@Override
public List<Point> perceiveAuthorizedPositions(Agent2D agent) {
List<Point> authorisedPositions = new ArrayList<>();
for (int move = 0; move < MAX_MOVEMENT; move++) {
Point position = getNextPosition(move,agent.getPosition());
wrapPosition(position);
authorisedPositions.add(position);
}
return authorisedPositions;
}
}
package models; package models;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class Parameters { public record Parameters(
int seed,
private int seed; int population,
private int population; int size,
private int size; int nbOfPatientZero,
private int nbOfPatientZero; float infectionRate,
private float infectionRate; float incubationRate,
private float incubationRate; float recoveryRate,
private float recoveryRate; float looseImmunityRate,
private float looseImmunityRate; int nbOfCycles,
private int nbOfCycles; int timeBetweenCycles,
private int timeBetweenCycles; boolean synchronousMode,
private boolean synchronousMode; boolean graphicalMode,
private boolean graphicalMode; boolean infectionStacks,
private boolean infectionStacks; boolean wrappingWorld) {
public Parameters() {
}
public int getNbOfPatientZero() {return nbOfPatientZero;}
public void setNbOfPatientZero(int nbOfPatientZero) {this.nbOfPatientZero = nbOfPatientZero;}
public int getSeed() {return seed;}
public void setSeed(int seed) {this.seed = seed;}
public int getPopulation() {return population;}
public void setPopulation(int population) {this.population = population;}
public int getSize() {return size;}
public void setSize(int size) {this.size = size;}
public float getInfectionRate() { return infectionRate; }
public void setInfectionRate(float infectionRate) {this.infectionRate = infectionRate;}
public float getIncubationRate() {return incubationRate;}
public void setIncubationRate(float incubationRate) {this.incubationRate = incubationRate;}
public float getRecoveryRate() {return 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 void setNbOfCycles(int nbOfCycles) {this.nbOfCycles = nbOfCycles;}
public boolean isSynchronousMode() {return synchronousMode;}
public void setSynchronousMode(boolean synchronousMode) {this.synchronousMode = synchronousMode;}
public int getTimeBetweenCycles() { return timeBetweenCycles; }
public void setTimeBetweenCycles(int timeBetweenCycles) { this.timeBetweenCycles = timeBetweenCycles; }
public boolean isGraphicalMode() { return graphicalMode; }
public void setGraphicalMode(boolean graphicalMode) { this.graphicalMode = graphicalMode; }
public boolean isInfectionStacks() { return infectionStacks; }
public void setInfectionStacks(boolean infectionStacks) { this.infectionStacks = infectionStacks; }
} }
...@@ -2,10 +2,10 @@ package scheduler; ...@@ -2,10 +2,10 @@ package scheduler;
import agents.Agent; import agents.Agent;
import java.nio.ByteBuffer;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.*; import java.util.*;
@SuppressWarnings("ThrowablePrintedToSystemOut")
public class FairSynchronousScheduler implements Scheduler { public class FairSynchronousScheduler implements Scheduler {
private Agent[] agents; private Agent[] agents;
...@@ -18,6 +18,7 @@ public class FairSynchronousScheduler implements Scheduler { ...@@ -18,6 +18,7 @@ public class FairSynchronousScheduler implements Scheduler {
r = SecureRandom.getInstance("SHA1PRNG", "SUN"); r = SecureRandom.getInstance("SHA1PRNG", "SUN");
}catch (Exception e) { }catch (Exception e) {
System.err.println(e); System.err.println(e);
System.exit(1);
} }
r.setSeed(seed); r.setSeed(seed);
} }
......
...@@ -4,6 +4,7 @@ import agents.FairInfectionRWAgent; ...@@ -4,6 +4,7 @@ import agents.FairInfectionRWAgent;
import agents.SEIRSAgent; import agents.SEIRSAgent;
import agents.states.InfectedSEIRSState; import agents.states.InfectedSEIRSState;
import environment.SEIRSEnvironment; import environment.SEIRSEnvironment;
import environment.WrappingChunkedSEIRSEnvironment;
import models.Parameters; import models.Parameters;
import agents.RandomWalkingAgent; import agents.RandomWalkingAgent;
import environment.ChunkedSEIRSEnvironment; import environment.ChunkedSEIRSEnvironment;
...@@ -18,16 +19,13 @@ import view.StatisticsCanvas; ...@@ -18,16 +19,13 @@ import view.StatisticsCanvas;
import java.awt.*; import java.awt.*;
import java.io.IOException; import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Random;
@SuppressWarnings("InfiniteLoopStatement") @SuppressWarnings({"InfiniteLoopStatement", "ThrowablePrintedToSystemOut"})
public class SEIRS_SMA implements SMA{ public class SEIRS_SMA implements SMA{
private Parameters parameters; private Parameters parameters;
...@@ -42,7 +40,7 @@ public class SEIRS_SMA implements SMA{ ...@@ -42,7 +40,7 @@ public class SEIRS_SMA implements SMA{
private HashMap<String,Integer> stats; private HashMap<String,Integer> stats;
private void initGraphics() { private void initGraphics() {
statisticsCanvas = new StatisticsCanvas(300,parameters.getSize()); statisticsCanvas = new StatisticsCanvas(300,parameters.size());
display = new DisplaySquaredEnvironment(environment,agents); display = new DisplaySquaredEnvironment(environment,agents);
fb.setSimulationCanvas(display); fb.setSimulationCanvas(display);
...@@ -68,12 +66,12 @@ public class SEIRS_SMA implements SMA{ ...@@ -68,12 +66,12 @@ public class SEIRS_SMA implements SMA{
e.printStackTrace(); e.printStackTrace();
System.exit(1); System.exit(1);
} }
if (parameters.isGraphicalMode()) { if (parameters.graphicalMode()) {
updateGraphics(); updateGraphics();
} }
if (parameters.getTimeBetweenCycles() > 0) { if (parameters.timeBetweenCycles() > 0) {
try { try {
Thread.sleep(parameters.getTimeBetweenCycles()); Thread.sleep(parameters.timeBetweenCycles());
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -81,38 +79,46 @@ public class SEIRS_SMA implements SMA{ ...@@ -81,38 +79,46 @@ public class SEIRS_SMA implements SMA{
} }
private void initPopulation() { private void initPopulation() {
for (int i = 0; i<parameters.getPopulation();i++) { for (int i = 0; i<parameters.population();i++) {
Point position = new Point(r.nextInt(parameters.getSize()),r.nextInt(parameters.getSize())); Point position = new Point(r.nextInt(parameters.size()),r.nextInt(parameters.size()));
SEIRSAgent agent; SEIRSAgent agent;
if (parameters.isInfectionStacks()) { if (parameters.infectionStacks()) {
agent = new RandomWalkingAgent(position,(parameters.getSeed()+i),environment); agent = new RandomWalkingAgent(position,(parameters.seed()+i),environment);
} else { } else {
agent = new FairInfectionRWAgent(position,(parameters.getSeed()+i),environment); agent = new FairInfectionRWAgent(position,(parameters.seed()+i),environment);
} }
agents[i] = agent; agents[i] = agent;
} }
} }
private void infectPatientZero() { private void infectPatientZero() {
for (int i=0 ; i< parameters.getNbOfPatientZero(); i++) { for (int i=0 ; i< parameters.nbOfPatientZero(); i++) {
int nextInt = (r.nextInt(parameters.getPopulation())); int nextInt = (r.nextInt(parameters.population()));
SEIRSAgent agent = agents[nextInt]; SEIRSAgent agent = agents[nextInt];
while (agent.getState() instanceof InfectedSEIRSState) { while (agent.getState() instanceof InfectedSEIRSState) {
agent = agents[(r.nextInt(parameters.getPopulation()))]; agent = agents[(r.nextInt(parameters.population()))];
} }
agent.changeState(new InfectedSEIRSState(agent)); agent.changeState(new InfectedSEIRSState(agent));
} }
} }
private void initScheduler() { private void initScheduler() {
if (parameters.isSynchronousMode()) { if (parameters.synchronousMode()) {
scheduler = new FairSynchronousScheduler(parameters.getSeed()); scheduler = new FairSynchronousScheduler(parameters.seed());
} else { } else {
scheduler = new FairAsynchronousScheduler(); scheduler = new FairAsynchronousScheduler();
} }
scheduler.init(agents); scheduler.init(agents);
} }
private void initEnvironment() {
if (parameters.wrappingWorld()) {
environment = new WrappingChunkedSEIRSEnvironment(parameters.size(),agents);
} else {
environment = new ChunkedSEIRSEnvironment(parameters.size(),agents);
}
}
@Override @Override
public void init() { public void init() {
...@@ -121,14 +127,15 @@ public class SEIRS_SMA implements SMA{ ...@@ -121,14 +127,15 @@ public class SEIRS_SMA implements SMA{
r = SecureRandom.getInstance("SHA1PRNG", "SUN"); r = SecureRandom.getInstance("SHA1PRNG", "SUN");
}catch (Exception e) { }catch (Exception e) {
System.err.println(e); System.err.println(e);
System.exit(1);
} }
r.setSeed(parameters.getSeed()); r.setSeed(parameters.seed());
agents = new RandomWalkingAgent[parameters.getPopulation()]; agents = new RandomWalkingAgent[parameters.population()];
environment = new ChunkedSEIRSEnvironment(parameters.getSize(),agents); initEnvironment();
initPopulation(); initPopulation();
infectPatientZero(); infectPatientZero();
initScheduler(); initScheduler();
if (parameters.isGraphicalMode()) { if (parameters.graphicalMode()) {
initGraphics(); initGraphics();
} }
} }
...@@ -138,13 +145,13 @@ public class SEIRS_SMA implements SMA{ ...@@ -138,13 +145,13 @@ public class SEIRS_SMA implements SMA{
public void run() { public void run() {
Instant startTime = Instant.now(); Instant startTime = Instant.now();
System.out.println("Starting simulation at : "+ Date.from(startTime)); System.out.println("Starting simulation at : "+ Date.from(startTime));
if (parameters.getNbOfCycles() <0) { if (parameters.nbOfCycles() <0) {
while (true) { while (true) {
doNextCycle(); doNextCycle();
} }
} else { } else {
int cpt = 0; int cpt = 0;
while (cpt < parameters.getNbOfCycles()) { while (cpt < parameters.nbOfCycles()) {
doNextCycle(); doNextCycle();
cpt++; cpt++;
} }
......
src/main/resources/output.png

58.2 KiB | W: | H:

src/main/resources/output.png

48.3 KiB | W: | H:

src/main/resources/output.png
src/main/resources/output.png
src/main/resources/output.png
src/main/resources/output.png
  • 2-up
  • Swipe
  • Onion skin
graphicalMode: true graphicalMode: true
incubationRate: 0.3 incubationRate: 0.3
infectionRate: 1 infectionRate: 0.8
looseImmunityRate: 0.008 looseImmunityRate: 0.008
recoveryRate: 0.1429 recoveryRate: 0.1429
nbOfCycles: 2000 nbOfCycles: 2000
...@@ -8,6 +8,7 @@ nbOfPatientZero: 1 ...@@ -8,6 +8,7 @@ nbOfPatientZero: 1
population: 3000 population: 3000
seed: 120 seed: 120
size: 1000 size: 1000
wrappingWorld : true
synchronousMode: false synchronousMode: false
timeBetweenCycles: 10 timeBetweenCycles: 0
infectionStacks : true infectionStacks : true
\ 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