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
No related branches found
No related tags found
No related merge requests found
......@@ -22,10 +22,9 @@
# compilated files from intelij
target
src/main/resources/output.csv
src/main/resources/output.png
src/main/resources/pythonOutput/*.csv
src/main/resources/pythonOutput/*.png
#output files
*.csv
*.png
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
{
"java.configuration.updateBuildConfiguration": "disabled"
}
\ No newline at end of file
......@@ -3,4 +3,6 @@ package agents;
import behaviors.Wakeable;
public interface Agent extends Wakeable {
String getId();
}
......@@ -17,9 +17,11 @@ public class RandomWalkingAgent extends Randomized implements SEIRSAgent {
private List<Point> authorizedPositions;
private Point nextPosition;
private final String id;
public RandomWalkingAgent(Point position, long seed, SEIRSEnvironment environment) {
super(seed);
this.id = String.valueOf(seed);
this.position = position;
this.state = new SuceptibleSEIRSState(this);
this.environment = environment;
......@@ -101,4 +103,9 @@ public class RandomWalkingAgent extends Randomized implements SEIRSAgent {
@Override
public Point getPosition() { return position; }
@Override
public String getId() {
return this.id;
}
}
......@@ -19,6 +19,7 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment {
public final int size;
private final SEIRSAgent[] agents;
private List<SEIRSAgent>[][] chunks;
private final List<String> executionOrder = new ArrayList<>();
public ChunkedSEIRSEnvironment(int size, SEIRSAgent[] agents) {
this.agents = agents;
......@@ -111,6 +112,7 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment {
chunks[oldPosition.x/CHUNK_SIZE][oldPosition.y/CHUNK_SIZE].remove((SEIRSAgent) agent);
chunks[newPosition.x/CHUNK_SIZE][newPosition.y/CHUNK_SIZE].add((SEIRSAgent) agent);
}
executionOrder.add(((SEIRSAgent)agent).getId());
}
@Override
......@@ -148,4 +150,9 @@ public class ChunkedSEIRSEnvironment implements SEIRSEnvironment {
public int getSize() {
return size;
}
@Override
public List<String> getExecutionOrder() {
return executionOrder;
}
}
package environment;
import java.util.List;
public interface Environment {
List<String> getExecutionOrder();
}
......@@ -15,5 +15,7 @@ public record Parameters(
boolean synchronousMode,
boolean graphicalMode,
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;
import models.Parameters;
import agents.RandomWalkingAgent;
import environment.ChunkedSEIRSEnvironment;
import scheduler.DeterministScheduler;
import scheduler.FairAsynchronousScheduler;
import scheduler.FairSynchronousScheduler;
import scheduler.Scheduler;
......@@ -19,13 +20,17 @@ import view.FrameBuilder;
import view.StatisticsCanvas;
import java.awt.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@SuppressWarnings("InfiniteLoopStatement")
@SuppressWarnings({"InfiniteLoopStatement", "ResultOfMethodCallIgnored"})
public class SEIRS_SMA extends Randomized implements SMA{
private final Parameters parameters;
......@@ -116,10 +121,18 @@ public class SEIRS_SMA extends Randomized implements SMA{
}
private void initScheduler() {
if (parameters.synchronousMode()) {
scheduler = new FairSynchronousScheduler(parameters.seed());
if (parameters.playRecord()) {
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 {
scheduler = new FairAsynchronousScheduler();
if (parameters.synchronousMode()) {
scheduler = new FairSynchronousScheduler(parameters.seed());
} else {
scheduler = new FairAsynchronousScheduler();
}
}
scheduler.init(agents);
}
......@@ -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
public void run() {
Instant startTime = Instant.now();
......@@ -146,6 +179,11 @@ public class SEIRS_SMA extends Randomized implements SMA{
doNextCycle();
cpt++;
}
if (parameters.recordExperiment()) {
recordExperiment();
}
Instant endTime = Instant.now();
System.out.println("Simulation done !");
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
nbOfCycles: 2000
nbOfPatientZero: 1
population: 3000
recordExperiment: false
playRecord: true
seed: 120
size: 1000
wrappingWorld : true
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment