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

using better pseudo-random alogrithm (SHA1PRNG)

parent e71ff1ac
No related branches found
No related tags found
No related merge requests found
...@@ -2,49 +2,62 @@ package agents; ...@@ -2,49 +2,62 @@ package agents;
import agents.states.SEIRSState; import agents.states.SEIRSState;
import agents.states.SuceptibleSEIRSState; import agents.states.SuceptibleSEIRSState;
import environment.ChunkedSEIRSEnvironment;
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.Random; import java.util.Random;
public class RandomWalkingAgent implements SEIRSAgent { public class RandomWalkingAgent implements SEIRSAgent {
protected Point position; protected Point position;
protected final Random r; protected Random r;
protected final SEIRSEnvironment environment; protected final SEIRSEnvironment environment;
protected SEIRSState state; protected SEIRSState state;
private List<Point> authorizedPositions;
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;
this.r = new Random(seed); try{
r = SecureRandom.getInstance("SHA1PRNG", "SUN");
}catch (Exception e) {
System.err.println(e);
}
r.setSeed(seed);
} }
private void move() { private void move() {
state.onMovement(); state.onMovement();
int move = r.nextInt(4); environment.notifyNewPosition(nextPosition,this);
Point newPosition = switch (move) { position = nextPosition;
case SEIRSEnvironment.LEFT -> new Point(position.x- ChunkedSEIRSEnvironment.RADIUS,position.y); }
case SEIRSEnvironment.RIGHT -> new Point(position.x+ ChunkedSEIRSEnvironment.RADIUS,position.y);
case SEIRSEnvironment.UP -> new Point(position.x,position.y- ChunkedSEIRSEnvironment.RADIUS); private void perceiveAuthorizedPositions() {
case SEIRSEnvironment.DOWN -> new Point(position.x,position.y+ ChunkedSEIRSEnvironment.RADIUS); authorizedPositions = environment.perceiveAuthorizedPositions(this);
default -> throw new IllegalStateException("Unexpected value: " + move); }
};
if (newPosition.x <= environment.getSize()-1 && newPosition.x >= 0 && newPosition.y <= environment.getSize()-1 && newPosition.y >=0 ) { private void decideNextMove() {
environment.notifyNewPosition(position,newPosition ,this); int next = r.nextInt(authorizedPositions.size());
position = newPosition; nextPosition = authorizedPositions.get(next);
}
} }
@Override @Override
public void wakeUp() { public void wakeUp() {
move(); perceiveAuthorizedPositions();
if (!authorizedPositions.isEmpty()) {
decideNextMove();
move();
}
} }
@Override @Override
public void changeState(SEIRSState SEIRSState) { this.state = SEIRSState; } public void changeState(SEIRSState SEIRSState) { this.state = SEIRSState; }
......
...@@ -78,15 +78,50 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment { ...@@ -78,15 +78,50 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment {
return neighbors; return neighbors;
} }
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;
}
}
public List<Point> perceiveAuthorizedPositions(Agent2D agent) {
List<Point> authorizedPosition = new ArrayList<>();
for (int move = 0; move < MAX_MOVEMENT; move++) {
Point position = switch (move) {
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) {
authorizedPosition.add(position);
}
}
return authorizedPosition;
}
@Override @Override
public void notifyNewPosition(Point oldPosition, Point newPosition, Agent2D agent) { public void notifyNewPosition(Point newPosition, Agent2D agent) {
if (chunks == null) { if (chunks == null) {
initiateChunks(); initiateChunks();
} }
Point oldPosition = agent.getPosition();
if (oldPosition.x/CHUNK_SIZE != newPosition.x/CHUNK_SIZE || oldPosition.y/CHUNK_SIZE != newPosition.y/CHUNK_SIZE) { if (oldPosition.x/CHUNK_SIZE != newPosition.x/CHUNK_SIZE || oldPosition.y/CHUNK_SIZE != newPosition.y/CHUNK_SIZE) {
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
......
...@@ -3,20 +3,24 @@ package environment; ...@@ -3,20 +3,24 @@ package environment;
import agents.Agent2D; import agents.Agent2D;
import java.awt.*; import java.awt.*;
import java.util.List;
public interface Environment2D extends Environment { public interface Environment2D extends Environment {
int FORBIDDEN = -1;
int LEFT = 0; int LEFT = 0;
int RIGHT = 1; int RIGHT = 1;
int UP = 2; int UP = 2;
int DOWN = 3; int DOWN = 3;
int CENTER = 4; int CENTER = 4;
int MAX_MOVEMENT = CENTER;
int UP_LEFT = 5; int UP_LEFT = 5;
int UP_RIGHT = 6; int UP_RIGHT = 6;
int DOWN_LEFT = 7; int DOWN_LEFT = 7;
int DOWN_RIGHT = 8; int DOWN_RIGHT = 8;
int MAX_CHUNK = 9; int MAX_CHUNK = 9;
void notifyNewPosition(Point oldPosition, Point newPosition, Agent2D agent); List<Point> perceiveAuthorizedPositions(Agent2D agent);
void notifyNewPosition(Point newPosition, Agent2D agent);
int getSize(); int getSize();
} }
...@@ -2,16 +2,24 @@ package scheduler; ...@@ -2,16 +2,24 @@ package scheduler;
import agents.Agent; import agents.Agent;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.util.*; import java.util.*;
public class FairSynchronousScheduler implements Scheduler { public class FairSynchronousScheduler implements Scheduler {
private Agent[] agents; private Agent[] agents;
private Stack<Integer> executionOrder; private Stack<Integer> executionOrder;
private final Random r; private Random r;
public FairSynchronousScheduler(int seed) { public FairSynchronousScheduler(Integer seed) {
r = new Random(seed);
try{
r = SecureRandom.getInstance("SHA1PRNG", "SUN");
}catch (Exception e) {
System.err.println(e);
}
r.setSeed(seed);
} }
private void generateExecutionOrder() { private void generateExecutionOrder() {
......
...@@ -18,6 +18,9 @@ import view.StatisticsCanvas; ...@@ -18,6 +18,9 @@ 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.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.Date; import java.util.Date;
...@@ -33,7 +36,7 @@ public class SEIRS_SMA implements SMA{ ...@@ -33,7 +36,7 @@ public class SEIRS_SMA implements SMA{
private Scheduler scheduler; private Scheduler scheduler;
private StatisticsCanvas statisticsCanvas; private StatisticsCanvas statisticsCanvas;
private DisplaySquaredEnvironment display; private DisplaySquaredEnvironment display;
private Random r; 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;
...@@ -82,9 +85,9 @@ public class SEIRS_SMA implements SMA{ ...@@ -82,9 +85,9 @@ public class SEIRS_SMA implements SMA{
Point position = new Point(r.nextInt(parameters.getSize()),r.nextInt(parameters.getSize())); Point position = new Point(r.nextInt(parameters.getSize()),r.nextInt(parameters.getSize()));
SEIRSAgent agent; SEIRSAgent agent;
if (parameters.isInfectionStacks()) { if (parameters.isInfectionStacks()) {
agent = new RandomWalkingAgent(position,parameters.getSeed()+i,environment); agent = new RandomWalkingAgent(position,(parameters.getSeed()+i),environment);
} else { } else {
agent = new FairInfectionRWAgent(position,parameters.getSeed()+i,environment); agent = new FairInfectionRWAgent(position,(parameters.getSeed()+i),environment);
} }
agents[i] = agent; agents[i] = agent;
} }
...@@ -92,7 +95,8 @@ public class SEIRS_SMA implements SMA{ ...@@ -92,7 +95,8 @@ public class SEIRS_SMA implements SMA{
private void infectPatientZero() { private void infectPatientZero() {
for (int i=0 ; i< parameters.getNbOfPatientZero(); i++) { for (int i=0 ; i< parameters.getNbOfPatientZero(); i++) {
SEIRSAgent agent = agents[(r.nextInt(parameters.getPopulation()))]; int nextInt = (r.nextInt(parameters.getPopulation()));
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.getPopulation()))];
} }
...@@ -113,7 +117,12 @@ public class SEIRS_SMA implements SMA{ ...@@ -113,7 +117,12 @@ public class SEIRS_SMA implements SMA{
@Override @Override
public void init() { public void init() {
parameters = YamlReader.getParams(); parameters = YamlReader.getParams();
r = new Random(parameters.getSeed()); try{
r = SecureRandom.getInstance("SHA1PRNG", "SUN");
}catch (Exception e) {
System.err.println(e);
}
r.setSeed(parameters.getSeed());
agents = new RandomWalkingAgent[parameters.getPopulation()]; agents = new RandomWalkingAgent[parameters.getPopulation()];
environment = new ChunkedSEIRSEnvironment(parameters.getSize(),agents); environment = new ChunkedSEIRSEnvironment(parameters.getSize(),agents);
initPopulation(); initPopulation();
......
src/main/resources/output.png

59 KiB | W: | H:

src/main/resources/output.png

58.2 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: 0.75 infectionRate: 1
looseImmunityRate: 0.008 looseImmunityRate: 0.008
recoveryRate: 0.1429 recoveryRate: 0.1429
nbOfCycles: 2000 nbOfCycles: 2000
...@@ -9,5 +9,5 @@ population: 3000 ...@@ -9,5 +9,5 @@ population: 3000
seed: 120 seed: 120
size: 1000 size: 1000
synchronousMode: false synchronousMode: false
timeBetweenCycles: 0 timeBetweenCycles: 10
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