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

minor refactoring

parent 86e8f010
Branches
Tags
No related merge requests found
SUCEPTIBLE,EXPOSED,RECOVERED,INFECTED SUCEPTIBLE,EXPOSED,RECOVERED,INFECTED
9994,5,0,1 9995,4,0,1
9982,14,0,4 9983,13,0,4
9954,34,0,12 9953,36,0,11
9878,93,0,29 9882,88,1,29
9764,166,0,70 9789,140,2,69
9603,226,6,165 9654,208,6,132
9393,326,17,264 9470,302,20,208
9147,404,32,417 9251,358,30,361
8871,465,61,603 9002,436,51,511
8536,562,101,801 8658,561,87,694
8186,604,144,1066 8284,641,128,947
7837,636,201,1326 7899,706,193,1202
7398,755,280,1567 7478,788,265,1469
6979,799,365,1857 6985,873,355,1787
6505,870,477,2148 6527,883,462,2128
6038,891,613,2458 6054,937,596,2413
5594,877,771,2758 5579,957,742,2722
5175,854,957,3014 5179,873,901,3047
4751,818,1146,3285 4767,824,1080,3329
4319,875,1348,3458 4326,887,1280,3507
3890,874,1574,3662 3860,882,1524,3734
3450,866,1829,3855 3421,873,1768,3938
3032,861,2053,4054 3023,815,2010,4152
2680,791,2299,4230 2624,819,2264,4293
2286,787,2582,4345 2293,735,2548,4424
1899,781,2874,4446 1897,744,2826,4533
1571,718,3112,4599 1582,688,3089,4641
1262,680,3396,4662 1259,666,3346,4729
987,609,3675,4729 1004,587,3638,4771
780,529,3984,4707 780,523,3935,4762
559,488,4217,4736 582,461,4248,4709
412,399,4513,4676 402,403,4525,4670
298,316,4799,4587 269,346,4803,4582
216,253,5070,4461 177,265,5067,4491
144,192,5315,4349 104,219,5364,4313
84,141,5574,4201 35,175,5628,4162
38,105,5846,4011 14,110,5886,3990
15,73,6097,3815 4,63,6151,3782
2,49,6332,3617 1,33,6360,3606
1,24,6570,3405 0,21,6570,3409
0,14,6776,3210 0,7,6778,3215
0,6,6967,3027 0,3,6979,3018
0,1,7155,2844 0,1,7126,2873
0,0,7325,2675 0,1,7290,2709
0,0,7505,2495 0,1,7451,2548
0,0,7644,2356 0,0,7603,2397
0,0,7782,2218 0,0,7755,2245
0,0,7901,2099 0,0,7870,2130
0,0,8035,1965
0,0,8161,1839
0,0,8266,1734
0,0,8366,1634
0,0,8460,1540
0,0,8559,1441
0,0,8648,1352
0,0,8727,1273
0,0,8800,1200
0,0,8869,1131
0,0,8950,1050
0,0,8994,1006
0,0,9058,942
0,0,9117,883
0,0,9171,829
0,0,9214,786
0,0,9279,721
0,0,9310,690
0,0,9350,650
0,0,9396,604
0,0,9428,572
...@@ -5,8 +5,8 @@ import sma.agents.states.State; ...@@ -5,8 +5,8 @@ import sma.agents.states.State;
public interface Agent { public interface Agent {
void changeState(State state); void changeState(State state);
boolean contact(); boolean isExposed();
boolean incubate(); boolean isInfected();
boolean recover(); boolean isRecovered();
void move(); void move();
} }
...@@ -15,11 +15,7 @@ public class RandomWalkingAgent implements Agent { ...@@ -15,11 +15,7 @@ public class RandomWalkingAgent implements Agent {
private Point position; private Point position;
private Random r; private Random r;
private GraphicEnvironment environment; private GraphicEnvironment environment;
private State state; private State state;
private Boolean exposedThisCycle;
private Boolean infectedThisCycle;
public RandomWalkingAgent(Point position, int seed, GraphicEnvironment environment) { public RandomWalkingAgent(Point position, int seed, GraphicEnvironment environment) {
this.position = position; this.position = position;
...@@ -29,8 +25,9 @@ public class RandomWalkingAgent implements Agent { ...@@ -29,8 +25,9 @@ public class RandomWalkingAgent implements Agent {
} }
public void move() { public void move() {
state.onMovement();
int move = r.nextInt(4); int move = r.nextInt(4);
Point newPosition = switch (move) { Point newPosition = switch (move) {
case Environment.LEFT -> new Point(position.x-environment.RADIUS,position.y); case Environment.LEFT -> new Point(position.x-environment.RADIUS,position.y);
case Environment.RIGHT -> new Point(position.x+environment.RADIUS,position.y); case Environment.RIGHT -> new Point(position.x+environment.RADIUS,position.y);
...@@ -45,12 +42,10 @@ public class RandomWalkingAgent implements Agent { ...@@ -45,12 +42,10 @@ public class RandomWalkingAgent implements Agent {
} }
@Override @Override
public void changeState(State state) { public void changeState(State state) { this.state = state; }
this.state = state;
}
@Override @Override
public boolean contact() { public boolean isExposed() {
boolean isExposed = false; boolean isExposed = false;
for (RandomWalkingAgent neighbor: environment.getNeighbors(position)) { for (RandomWalkingAgent neighbor: environment.getNeighbors(position)) {
if (neighbor.getState() instanceof InfectedState) { if (neighbor.getState() instanceof InfectedState) {
...@@ -64,7 +59,7 @@ public class RandomWalkingAgent implements Agent { ...@@ -64,7 +59,7 @@ public class RandomWalkingAgent implements Agent {
} }
@Override @Override
public boolean incubate() { public boolean isInfected() {
boolean isSick = false; boolean isSick = false;
int roll = r.nextInt(100); int roll = r.nextInt(100);
if (roll <= YamlReader.getParams().getIncubationRate()*100) { if (roll <= YamlReader.getParams().getIncubationRate()*100) {
...@@ -74,7 +69,7 @@ public class RandomWalkingAgent implements Agent { ...@@ -74,7 +69,7 @@ public class RandomWalkingAgent implements Agent {
} }
@Override @Override
public boolean recover() { public boolean isRecovered() {
boolean isHealed = false; boolean isHealed = false;
int roll = r.nextInt(100); int roll = r.nextInt(100);
if (roll <= YamlReader.getParams().getRecoveryRate()*100) { if (roll <= YamlReader.getParams().getRecoveryRate()*100) {
...@@ -83,18 +78,8 @@ public class RandomWalkingAgent implements Agent { ...@@ -83,18 +78,8 @@ public class RandomWalkingAgent implements Agent {
return isHealed; return isHealed;
} }
public State getState() { public State getState() { return this.state; }
return this.state;
}
public void wakeUp() { public Point getPosition() { return position; }
exposedThisCycle = false;
infectedThisCycle = false;
state.onMovement();
}
public Point getPosition() {
return position;
}
} }
...@@ -10,8 +10,7 @@ public class ExposedState extends State{ ...@@ -10,8 +10,7 @@ public class ExposedState extends State{
@Override @Override
public void onMovement() { public void onMovement() {
agent.move(); if (agent.isInfected()) {
if (agent.incubate()) {
agent.changeState(new InfectedState(agent)); agent.changeState(new InfectedState(agent));
} }
} }
......
...@@ -10,8 +10,7 @@ public class InfectedState extends State{ ...@@ -10,8 +10,7 @@ public class InfectedState extends State{
@Override @Override
public void onMovement() { public void onMovement() {
agent.move(); if (agent.isRecovered()) {
if (agent.recover()) {
agent.changeState(new RecoveredState(agent)); agent.changeState(new RecoveredState(agent));
} }
} }
......
...@@ -10,7 +10,7 @@ public class RecoveredState extends State{ ...@@ -10,7 +10,7 @@ public class RecoveredState extends State{
@Override @Override
public void onMovement() { public void onMovement() {
agent.move();
} }
@Override @Override
......
...@@ -10,8 +10,7 @@ public class SuceptibleState extends State{ ...@@ -10,8 +10,7 @@ public class SuceptibleState extends State{
@Override @Override
public void onMovement() { public void onMovement() {
agent.move(); if (agent.isExposed()) {
if (agent.contact()) {
agent.changeState(new ExposedState(agent)); agent.changeState(new ExposedState(agent));
} }
} }
......
...@@ -20,7 +20,7 @@ public class FairAsynchronousScheduler implements Scheduler{ ...@@ -20,7 +20,7 @@ public class FairAsynchronousScheduler implements Scheduler{
public void nextCycle() { public void nextCycle() {
List<Future<RandomWalkingAgent>> results = queue.parallelStream().map(agent -> executor.submit(() -> {agent.wakeUp(); return agent;})).collect(Collectors.toList()); List<Future<RandomWalkingAgent>> results = queue.parallelStream().map(agent -> executor.submit(() -> {agent.move(); return agent;})).collect(Collectors.toList());
Function<Future<RandomWalkingAgent>, RandomWalkingAgent> futureTreatment = futureAgent -> { Function<Future<RandomWalkingAgent>, RandomWalkingAgent> futureTreatment = futureAgent -> {
try { try {
return futureAgent.get(); return futureAgent.get();
......
...@@ -25,7 +25,7 @@ public class FairSynchronousScheduler implements Scheduler { ...@@ -25,7 +25,7 @@ public class FairSynchronousScheduler implements Scheduler {
private void wakeAgents() { private void wakeAgents() {
while (!executionOrder.isEmpty()) { while (!executionOrder.isEmpty()) {
agents[(executionOrder.pop())].wakeUp(); agents[(executionOrder.pop())].move();
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment