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

added behaviors

parent e08e4804
No related branches found
No related tags found
No related merge requests found
Showing
with 91 additions and 88 deletions
package agents; package agents;
public interface Agent { import behaviors.Wakeable;
void wakeUp(); public interface Agent extends Wakeable {
} }
package agents; package agents;
import java.awt.*; import behaviors.Positionable2D;
public interface Agent2D extends Agent { public interface Agent2D extends Agent, Positionable2D {
Point getPosition();
} }
...@@ -7,7 +7,7 @@ import java.awt.*; ...@@ -7,7 +7,7 @@ import java.awt.*;
public class FairInfectionRWAgent extends RandomWalkingAgent implements SEIRSAgent { public class FairInfectionRWAgent extends RandomWalkingAgent implements SEIRSAgent {
public FairInfectionRWAgent(Point position, int seed, SEIRSEnvironment environment) { public FairInfectionRWAgent(Point position, long seed, SEIRSEnvironment environment) {
super(position, seed, environment); super(position, seed, environment);
} }
...@@ -16,7 +16,7 @@ public class FairInfectionRWAgent extends RandomWalkingAgent implements SEIRSAge ...@@ -16,7 +16,7 @@ public class FairInfectionRWAgent extends RandomWalkingAgent implements SEIRSAge
boolean isExposed = false; boolean isExposed = false;
int roll = r.nextInt(10000)+1; int roll = r.nextInt(10000)+1;
if (this.environment.getInfectedNeighbors(position).size() != 0) { if (this.environment.getInfectedNeighbors(position).size() != 0) {
if (roll <= YamlReader.getParams().getInfectionRate()*10000) { if (roll <= YamlReader.getParams().infectionRate()*10000) {
isExposed = true; isExposed = true;
} }
} }
......
...@@ -2,35 +2,27 @@ package agents; ...@@ -2,35 +2,27 @@ package agents;
import agents.states.SEIRSState; import agents.states.SEIRSState;
import agents.states.SuceptibleSEIRSState; import agents.states.SuceptibleSEIRSState;
import behaviors.Randomized;
import environment.SEIRSEnvironment; import environment.SEIRSEnvironment;
import utils.YamlReader; import utils.YamlReader;
import java.awt.Point; import java.awt.Point;
import java.security.SecureRandom;
import java.util.List; import java.util.List;
import java.util.Random;
@SuppressWarnings("ThrowablePrintedToSystemOut") public class RandomWalkingAgent extends Randomized implements SEIRSAgent {
public class RandomWalkingAgent implements SEIRSAgent {
protected Point position; protected Point position;
protected Random r;
protected final SEIRSEnvironment environment; protected final SEIRSEnvironment environment;
protected SEIRSState state; protected SEIRSState state;
private List<Point> authorizedPositions; private List<Point> authorizedPositions;
private Point nextPosition; private Point nextPosition;
public RandomWalkingAgent(Point position, int seed, SEIRSEnvironment environment) { public RandomWalkingAgent(Point position, long seed, SEIRSEnvironment environment) {
super(seed);
this.position = position; this.position = position;
this.state = new SuceptibleSEIRSState(this); this.state = new SuceptibleSEIRSState(this);
this.environment = environment; this.environment = environment;
try{
r = SecureRandom.getInstance("SHA1PRNG", "SUN");
}catch (Exception e) {
System.err.println(e);
System.exit(1);
}
r.setSeed(seed); r.setSeed(seed);
} }
......
package behaviors;
import java.awt.*;
public interface Positionable2D {
Point getPosition();
}
package behaviors;
import java.security.SecureRandom;
import java.util.Random;
public abstract class Randomized {
protected Random r;
@SuppressWarnings("ThrowablePrintedToSystemOut")
public Randomized(long seed) {
try{
r = SecureRandom.getInstance("SHA1PRNG", "SUN");
}catch (Exception e) {
System.err.println(e);
System.exit(1);
}
r.setSeed(seed);
}
}
package behaviors;
public interface Wakeable {
void wakeUp();
}
\ No newline at end of file
package environment; package environment;
import agents.Agent2D;
import agents.SEIRSAgent; import agents.SEIRSAgent;
import agents.states.InfectedSEIRSState; import agents.states.InfectedSEIRSState;
import agents.states.SEIRSState; import agents.states.SEIRSState;
import behaviors.Positionable2D;
import java.awt.*; import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -79,7 +79,7 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment { ...@@ -79,7 +79,7 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment {
} }
@Override @Override
public List<Point> perceiveAuthorizedPositions(Agent2D agent) { public List<Point> perceiveAuthorizedPositions(Positionable2D 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++) {
...@@ -102,7 +102,7 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment { ...@@ -102,7 +102,7 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment {
} }
@Override @Override
public void notifyNewPosition(Point newPosition, Agent2D agent) { public void notifyNewPosition(Point newPosition, Positionable2D agent) {
if (chunks == null) { if (chunks == null) {
initiateChunks(); initiateChunks();
} }
......
package environment; package environment;
import agents.Agent2D; import behaviors.Positionable2D;
import java.awt.*; import java.awt.*;
import java.util.List; import java.util.List;
...@@ -20,7 +20,7 @@ public interface Environment2D extends Environment { ...@@ -20,7 +20,7 @@ public interface Environment2D extends Environment {
int DOWN_RIGHT = 8; int DOWN_RIGHT = 8;
int MAX_CHUNK = 9; int MAX_CHUNK = 9;
List<Point> perceiveAuthorizedPositions(Agent2D agent); List<Point> perceiveAuthorizedPositions(Positionable2D agent);
void notifyNewPosition(Point newPosition, Agent2D agent); void notifyNewPosition(Point newPosition, Positionable2D agent);
int getSize(); int getSize();
} }
package environment; package environment;
import agents.Agent2D;
import agents.SEIRSAgent; import agents.SEIRSAgent;
import behaviors.Positionable2D;
import java.awt.*; import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -29,7 +29,7 @@ public class WrappingChunkedSEIRSEnvironment extends ChunkedSEIRSEnvironment imp ...@@ -29,7 +29,7 @@ public class WrappingChunkedSEIRSEnvironment extends ChunkedSEIRSEnvironment imp
} }
@Override @Override
public List<Point> perceiveAuthorizedPositions(Agent2D agent) { public List<Point> perceiveAuthorizedPositions(Positionable2D agent) {
List<Point> authorisedPositions = new ArrayList<>(); List<Point> authorisedPositions = new ArrayList<>();
for (int move = 0; move < MAX_MOVEMENT; move++) { for (int move = 0; move < MAX_MOVEMENT; move++) {
Point position = getNextPosition(move,agent.getPosition()); Point position = getNextPosition(move,agent.getPosition());
......
...@@ -2,7 +2,7 @@ package models; ...@@ -2,7 +2,7 @@ package models;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public record Parameters( public record Parameters(
int seed, long seed,
int population, int population,
int size, int size,
int nbOfPatientZero, int nbOfPatientZero,
......
package scheduler; package scheduler;
import agents.Agent; import behaviors.Wakeable;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
...@@ -12,19 +12,19 @@ import java.util.stream.Collectors; ...@@ -12,19 +12,19 @@ import java.util.stream.Collectors;
public class FairAsynchronousScheduler implements Scheduler { public class FairAsynchronousScheduler implements Scheduler {
private final ExecutorService executor = Executors.newSingleThreadExecutor(); private final ExecutorService executor = Executors.newSingleThreadExecutor();
private Queue<Agent> queue; private Queue<Wakeable> queue;
@Override @Override
public void init(Agent[] agents) { public void init(Wakeable[] agents) {
this.queue = new ConcurrentLinkedQueue<>(Arrays.stream(agents).toList()); this.queue = new ConcurrentLinkedQueue<>(Arrays.stream(agents).toList());
} }
@Override @Override
public void doNextCycle() { public void doNextCycle() {
List<Future<Agent>> results = queue.parallelStream().map(agent -> executor.submit(() -> {agent.wakeUp(); return agent;})).toList(); List<Future<Wakeable>> results = queue.parallelStream().map(entity -> executor.submit(() -> {entity.wakeUp(); return entity;})).toList();
Function<Future<Agent>, Agent> futureTreatment = futureAgent -> { Function<Future<Wakeable>, Wakeable> futureTreatment = futureEntity -> {
try { try {
return futureAgent.get(); return futureEntity.get();
} catch (ExecutionException | InterruptedException e) { } catch (ExecutionException | InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
package scheduler; package scheduler;
import agents.Agent; import behaviors.Randomized;
import behaviors.Wakeable;
import java.security.SecureRandom;
import java.util.*; import java.util.*;
@SuppressWarnings("ThrowablePrintedToSystemOut") public class FairSynchronousScheduler extends Randomized implements Scheduler {
public class FairSynchronousScheduler implements Scheduler {
private Agent[] agents; private Wakeable[] agents;
private Stack<Integer> executionOrder; private Stack<Integer> executionOrder;
private Random r;
public FairSynchronousScheduler(Integer seed) { public FairSynchronousScheduler(long seed) {
super(seed);
try{
r = SecureRandom.getInstance("SHA1PRNG", "SUN");
}catch (Exception e) {
System.err.println(e);
System.exit(1);
}
r.setSeed(seed);
} }
private void generateExecutionOrder() { private void generateExecutionOrder() {
...@@ -38,7 +29,7 @@ public class FairSynchronousScheduler implements Scheduler { ...@@ -38,7 +29,7 @@ public class FairSynchronousScheduler implements Scheduler {
} }
@Override @Override
public void init(Agent[] agents) { public void init(Wakeable[] agents) {
this.agents = agents; this.agents = agents;
executionOrder = new Stack<>(); executionOrder = new Stack<>();
} }
......
package scheduler; package scheduler;
import agents.Agent; import behaviors.Wakeable;
public interface Scheduler { public interface Scheduler {
void init(Agent[] agents); void init(Wakeable[] agents);
void doNextCycle(); void doNextCycle();
} }
...@@ -3,6 +3,7 @@ package sma; ...@@ -3,6 +3,7 @@ package sma;
import agents.FairInfectionRWAgent; import agents.FairInfectionRWAgent;
import agents.SEIRSAgent; import agents.SEIRSAgent;
import agents.states.InfectedSEIRSState; import agents.states.InfectedSEIRSState;
import behaviors.Randomized;
import environment.SEIRSEnvironment; import environment.SEIRSEnvironment;
import environment.WrappingChunkedSEIRSEnvironment; import environment.WrappingChunkedSEIRSEnvironment;
import models.Parameters; import models.Parameters;
...@@ -19,26 +20,38 @@ import view.StatisticsCanvas; ...@@ -19,26 +20,38 @@ import view.StatisticsCanvas;
import java.awt.*; import java.awt.*;
import java.io.IOException; import java.io.IOException;
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;
@SuppressWarnings({"InfiniteLoopStatement", "ThrowablePrintedToSystemOut"}) @SuppressWarnings("InfiniteLoopStatement")
public class SEIRS_SMA implements SMA{ public class SEIRS_SMA extends Randomized implements SMA{
private Parameters parameters; private final Parameters parameters;
private SEIRSAgent[] agents; private final SEIRSAgent[] agents;
private SEIRSEnvironment environment; private SEIRSEnvironment environment;
private Scheduler scheduler; private Scheduler scheduler;
private StatisticsCanvas statisticsCanvas; private StatisticsCanvas statisticsCanvas;
private DisplaySquaredEnvironment display; private DisplaySquaredEnvironment display;
private SecureRandom r;
private final FrameBuilder fb = new FrameBuilder(); private final FrameBuilder fb = new FrameBuilder();
private HashMap<String,Integer> stats; private HashMap<String,Integer> stats;
public SEIRS_SMA(Parameters params) {
super(params.seed());
parameters = YamlReader.getParams();
r.setSeed(parameters.seed());
agents = new RandomWalkingAgent[parameters.population()];
initEnvironment();
initPopulation();
infectPatientZero();
initScheduler();
if (parameters.graphicalMode()) {
initGraphics();
}
}
private void initGraphics() { private void initGraphics() {
statisticsCanvas = new StatisticsCanvas(300,parameters.size()); statisticsCanvas = new StatisticsCanvas(300,parameters.size());
display = new DisplaySquaredEnvironment(environment,agents); display = new DisplaySquaredEnvironment(environment,agents);
...@@ -119,28 +132,6 @@ public class SEIRS_SMA implements SMA{ ...@@ -119,28 +132,6 @@ public class SEIRS_SMA implements SMA{
} }
} }
@Override
public void init() {
parameters = YamlReader.getParams();
try{
r = SecureRandom.getInstance("SHA1PRNG", "SUN");
}catch (Exception e) {
System.err.println(e);
System.exit(1);
}
r.setSeed(parameters.seed());
agents = new RandomWalkingAgent[parameters.population()];
initEnvironment();
initPopulation();
infectPatientZero();
initScheduler();
if (parameters.graphicalMode()) {
initGraphics();
}
}
@Override @Override
public void run() { public void run() {
Instant startTime = Instant.now(); Instant startTime = Instant.now();
...@@ -164,8 +155,7 @@ public class SEIRS_SMA implements SMA{ ...@@ -164,8 +155,7 @@ public class SEIRS_SMA implements SMA{
} }
public static void main(String[] args) { public static void main(String[] args) {
SMA sma = new SEIRS_SMA(); SMA sma = new SEIRS_SMA(YamlReader.getParams());
sma.init();
sma.run(); sma.run();
} }
} }
package sma; package sma;
public interface SMA{ public interface SMA{
void init();
void run(); void run();
} }
...@@ -10,7 +10,6 @@ import java.awt.*; ...@@ -10,7 +10,6 @@ import java.awt.*;
public class DisplaySquaredEnvironment extends JPanel { public class DisplaySquaredEnvironment extends JPanel {
private final SEIRSAgent[] SEIRSAgents; private final SEIRSAgent[] SEIRSAgents;
public DisplaySquaredEnvironment(SEIRSEnvironment environment, SEIRSAgent[] SEIRSAgents) { public DisplaySquaredEnvironment(SEIRSEnvironment environment, SEIRSAgent[] SEIRSAgents) {
......
...@@ -15,7 +15,7 @@ public class StatisticsCanvas extends JPanel { ...@@ -15,7 +15,7 @@ public class StatisticsCanvas extends JPanel {
public StatisticsCanvas(int width,int height) { public StatisticsCanvas(int width,int height) {
this.setDoubleBuffered(false); this.setDoubleBuffered(false);
values = new HashMap<>(); values = new HashMap<>();
total = YamlReader.getParams().getPopulation(); total = YamlReader.getParams().population();
setSize(width, height); setSize(width, height);
setVisible(true); setVisible(true);
} }
......
src/main/resources/output.png

48.3 KiB | W: | H:

src/main/resources/output.png

59.4 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
...@@ -2,7 +2,7 @@ graphicalMode: true ...@@ -2,7 +2,7 @@ graphicalMode: true
incubationRate: 0.3 incubationRate: 0.3
infectionRate: 0.8 infectionRate: 0.8
looseImmunityRate: 0.008 looseImmunityRate: 0.008
recoveryRate: 0.1429 recoveryRate: 0.14
nbOfCycles: 2000 nbOfCycles: 2000
nbOfPatientZero: 1 nbOfPatientZero: 1
population: 3000 population: 3000
...@@ -11,4 +11,4 @@ size: 1000 ...@@ -11,4 +11,4 @@ size: 1000
wrappingWorld : true wrappingWorld : true
synchronousMode: false synchronousMode: false
timeBetweenCycles: 0 timeBetweenCycles: 0
infectionStacks : true infectionStacks : false
\ 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