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

added a way to record and replay experiments

parent de63b900
Branches
Tags
No related merge requests found
...@@ -22,10 +22,9 @@ ...@@ -22,10 +22,9 @@
# compilated files from intelij # compilated files from intelij
target target
src/main/resources/output.csv #output files
src/main/resources/output.png *.csv
src/main/resources/pythonOutput/*.csv *.png
src/main/resources/pythonOutput/*.png
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid* hs_err_pid*
{
"java.configuration.updateBuildConfiguration": "disabled"
}
\ No newline at end of file
...@@ -3,4 +3,6 @@ package agents; ...@@ -3,4 +3,6 @@ package agents;
import behaviors.Wakeable; import behaviors.Wakeable;
public interface Agent extends Wakeable { public interface Agent extends Wakeable {
String getId();
} }
...@@ -17,9 +17,11 @@ public class RandomWalkingAgent extends Randomized implements SEIRSAgent { ...@@ -17,9 +17,11 @@ public class RandomWalkingAgent extends Randomized implements SEIRSAgent {
private List<Point> authorizedPositions; private List<Point> authorizedPositions;
private Point nextPosition; private Point nextPosition;
private final String id;
public RandomWalkingAgent(Point position, long seed, SEIRSEnvironment environment) { public RandomWalkingAgent(Point position, long seed, SEIRSEnvironment environment) {
super(seed); super(seed);
this.id = String.valueOf(seed);
this.position = position; this.position = position;
this.state = new SuceptibleSEIRSState(this); this.state = new SuceptibleSEIRSState(this);
this.environment = environment; this.environment = environment;
...@@ -101,4 +103,9 @@ public class RandomWalkingAgent extends Randomized implements SEIRSAgent { ...@@ -101,4 +103,9 @@ public class RandomWalkingAgent extends Randomized implements SEIRSAgent {
@Override @Override
public Point getPosition() { return position; } public Point getPosition() { return position; }
@Override
public String getId() {
return this.id;
}
} }
...@@ -19,6 +19,7 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment { ...@@ -19,6 +19,7 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment {
public final int size; public final int size;
private final SEIRSAgent[] agents; private final SEIRSAgent[] agents;
private List<SEIRSAgent>[][] chunks; private List<SEIRSAgent>[][] chunks;
private final List<String> executionOrder = new ArrayList<>();
public ChunkedSEIRSEnvironment(int size, SEIRSAgent[] agents) { public ChunkedSEIRSEnvironment(int size, SEIRSAgent[] agents) {
this.agents = agents; this.agents = agents;
...@@ -111,6 +112,7 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment { ...@@ -111,6 +112,7 @@ 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);
} }
executionOrder.add(((SEIRSAgent)agent).getId());
} }
@Override @Override
...@@ -148,4 +150,9 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment { ...@@ -148,4 +150,9 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment {
public int getSize() { public int getSize() {
return size; return size;
} }
@Override
public List<String> getExecutionOrder() {
return executionOrder;
}
} }
package environment; package environment;
import java.util.List;
public interface Environment { public interface Environment {
List<String> getExecutionOrder();
} }
...@@ -15,5 +15,7 @@ public record Parameters( ...@@ -15,5 +15,7 @@ public record Parameters(
boolean synchronousMode, boolean synchronousMode,
boolean graphicalMode, boolean graphicalMode,
boolean infectionStacks, boolean infectionStacks,
boolean wrappingWorld) { boolean wrappingWorld,
boolean playRecord,
boolean recordExperiment) {
} }
package scheduler;
import behaviors.Wakeable;
import utils.YamlReader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.EmptyStackException;
import java.util.Stack;
public class DeterministScheduler implements Scheduler {
private Wakeable[] agents;
private final Stack<String> wakeUpOrder = new Stack<>();
public DeterministScheduler(String csvFile) {
readCSV(csvFile);
System.out.println(wakeUpOrder.size());
}
private void readCSV(String file) {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line = br.readLine();
String[] values = line.split(",");
wakeUpOrder.addAll(Arrays.asList(values));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void init(Wakeable[] agents) {
this.agents = agents;
}
@Override
public void doNextCycle() {
for (int i = 0 ; i<agents.length-1; i++) {
try {
int next = Integer.parseInt(wakeUpOrder.pop());
agents[next-(int)YamlReader.getParams().seed()].wakeUp();
} catch (EmptyStackException e) {
System.err.println("Last record entry was read, simulation cannot continue further.");
System.exit(1);
}
}
}
}
...@@ -9,6 +9,7 @@ import environment.WrappingChunkedSEIRSEnvironment; ...@@ -9,6 +9,7 @@ import environment.WrappingChunkedSEIRSEnvironment;
import models.Parameters; import models.Parameters;
import agents.RandomWalkingAgent; import agents.RandomWalkingAgent;
import environment.ChunkedSEIRSEnvironment; import environment.ChunkedSEIRSEnvironment;
import scheduler.DeterministScheduler;
import scheduler.FairAsynchronousScheduler; import scheduler.FairAsynchronousScheduler;
import scheduler.FairSynchronousScheduler; import scheduler.FairSynchronousScheduler;
import scheduler.Scheduler; import scheduler.Scheduler;
...@@ -19,13 +20,17 @@ import view.FrameBuilder; ...@@ -19,13 +20,17 @@ import view.FrameBuilder;
import view.StatisticsCanvas; import view.StatisticsCanvas;
import java.awt.*; import java.awt.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
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.List;
@SuppressWarnings("InfiniteLoopStatement") @SuppressWarnings({"InfiniteLoopStatement", "ResultOfMethodCallIgnored"})
public class SEIRS_SMA extends Randomized implements SMA{ public class SEIRS_SMA extends Randomized implements SMA{
private final Parameters parameters; private final Parameters parameters;
...@@ -116,10 +121,18 @@ public class SEIRS_SMA extends Randomized implements SMA{ ...@@ -116,10 +121,18 @@ public class SEIRS_SMA extends Randomized implements SMA{
} }
private void initScheduler() { private void initScheduler() {
if (parameters.synchronousMode()) { if (parameters.playRecord()) {
scheduler = new FairSynchronousScheduler(parameters.seed()); if (parameters.recordExperiment()) {
throw new IllegalStateException("You cannot record and play an experiment at the same time. " +
"Please check the parameters.yaml file.");
}
scheduler = new DeterministScheduler("src/main/resources/executionOrder.csv");
} else { } else {
scheduler = new FairAsynchronousScheduler(); if (parameters.synchronousMode()) {
scheduler = new FairSynchronousScheduler(parameters.seed());
} else {
scheduler = new FairAsynchronousScheduler();
}
} }
scheduler.init(agents); scheduler.init(agents);
} }
...@@ -132,6 +145,26 @@ public class SEIRS_SMA extends Randomized implements SMA{ ...@@ -132,6 +145,26 @@ public class SEIRS_SMA extends Randomized implements SMA{
} }
} }
private void recordExperiment() {
try{
File file = new File("src/main/resources/executionOrder.csv");
file.createNewFile();
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
List<String> executionOrder= environment.getExecutionOrder();
for (int i = 0; i < executionOrder.size()-2; i++) {
bw.write(executionOrder.get(i)+",");
}
bw.write(executionOrder.get(executionOrder.size()-1));
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override @Override
public void run() { public void run() {
Instant startTime = Instant.now(); Instant startTime = Instant.now();
...@@ -146,6 +179,11 @@ public class SEIRS_SMA extends Randomized implements SMA{ ...@@ -146,6 +179,11 @@ public class SEIRS_SMA extends Randomized implements SMA{
doNextCycle(); doNextCycle();
cpt++; cpt++;
} }
if (parameters.recordExperiment()) {
recordExperiment();
}
Instant endTime = Instant.now(); Instant endTime = Instant.now();
System.out.println("Simulation done !"); System.out.println("Simulation done !");
Duration duration = Duration.between(startTime,endTime); Duration duration = Duration.between(startTime,endTime);
......
src/main/resources/output.png

59.4 KiB | W: | H:

src/main/resources/output.png

60.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
...@@ -6,6 +6,8 @@ recoveryRate: 0.14 ...@@ -6,6 +6,8 @@ recoveryRate: 0.14
nbOfCycles: 2000 nbOfCycles: 2000
nbOfPatientZero: 1 nbOfPatientZero: 1
population: 3000 population: 3000
recordExperiment: false
playRecord: true
seed: 120 seed: 120
size: 1000 size: 1000
wrappingWorld : true wrappingWorld : true
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment