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

implemented State design patern for agents

parent c95c6d88
No related branches found
No related tags found
No related merge requests found
Showing
with 358 additions and 969 deletions
SUCEPTIBLE,EXPOSED,RECOVERED,INFECTED SUCEPTIBLE,EXPOSED,RECOVERED,INFECTED
9994,5,0,1 9994,5,0,1
9980,16,0,4 9982,14,0,4
9953,34,0,13 9954,34,0,12
9867,106,0,27 9878,93,0,29
9753,175,0,72 9764,166,0,70
9592,247,5,156 9603,226,6,165
9401,314,13,272 9393,326,17,264
9184,386,27,403 9147,404,32,417
8928,434,54,584 8871,465,61,603
8643,501,88,768 8536,562,101,801
8305,589,144,962 8186,604,144,1066
7943,639,197,1221 7837,636,201,1326
7544,706,265,1485 7398,755,280,1567
7139,754,360,1747 6979,799,365,1857
6739,777,472,2012 6505,870,477,2148
6333,812,606,2249 6038,891,613,2458
5909,808,721,2562 5594,877,771,2758
5487,827,880,2806 5175,854,957,3014
5051,851,1057,3041 4751,818,1146,3285
4601,859,1246,3294 4319,875,1348,3458
4117,918,1432,3533 3890,874,1574,3662
3638,925,1661,3776 3450,866,1829,3855
3239,850,1923,3988 3032,861,2053,4054
2858,780,2167,4195 2680,791,2299,4230
2482,770,2423,4325 2286,787,2582,4345
2146,724,2692,4438 1899,781,2874,4446
1798,699,2954,4549 1571,718,3112,4599
1505,625,3221,4649 1262,680,3396,4662
1246,566,3508,4680 987,609,3675,4729
1009,490,3783,4718 780,529,3984,4707
775,465,4042,4718 559,488,4217,4736
577,432,4342,4649 412,399,4513,4676
418,363,4626,4593 298,316,4799,4587
298,294,4926,4482 216,253,5070,4461
222,217,5169,4392 144,192,5315,4349
168,157,5428,4247 84,141,5574,4201
113,113,5680,4094 38,105,5846,4011
76,102,5925,3897 15,73,6097,3815
32,98,6168,3702 2,49,6332,3617
12,83,6399,3506 1,24,6570,3405
3,50,6602,3345 0,14,6776,3210
1,29,6821,3149 0,6,6967,3027
0,15,7015,2970 0,1,7155,2844
0,8,7184,2808 0,0,7325,2675
0,4,7345,2651 0,0,7505,2495
0,2,7495,2503 0,0,7644,2356
0,0,7648,2352 0,0,7782,2218
0,0,7798,2202 0,0,7901,2099
0,0,7955,2045 0,0,8035,1965
0,0,8087,1913 0,0,8161,1839
0,0,8210,1790 0,0,8266,1734
0,0,8312,1688 0,0,8366,1634
0,0,8421,1579 0,0,8460,1540
0,0,8519,1481 0,0,8559,1441
0,0,8616,1384 0,0,8648,1352
0,0,8698,1302 0,0,8727,1273
0,0,8788,1212 0,0,8800,1200
0,0,8856,1144 0,0,8869,1131
0,0,8931,1069 0,0,8950,1050
0,0,8992,1008 0,0,8994,1006
0,0,9052,948 0,0,9058,942
0,0,9107,893 0,0,9117,883
0,0,9156,844 0,0,9171,829
0,0,9208,792 0,0,9214,786
0,0,9259,741 0,0,9279,721
0,0,9319,681 0,0,9310,690
0,0,9360,640 0,0,9350,650
0,0,9400,600 0,0,9396,604
0,0,9443,557 0,0,9428,572
0,0,9473,527
0,0,9510,490
0,0,9541,459
0,0,9573,427
0,0,9599,401
0,0,9620,380
0,0,9638,362
0,0,9658,342
0,0,9674,326
0,0,9694,306
0,0,9710,290
0,0,9728,272
0,0,9738,262
0,0,9750,250
0,0,9759,241
0,0,9772,228
0,0,9790,210
0,0,9805,195
0,0,9819,181
0,0,9833,167
0,0,9842,158
0,0,9856,144
0,0,9866,134
0,0,9871,129
0,0,9877,123
0,0,9886,114
0,0,9895,105
0,0,9903,97
0,0,9908,92
0,0,9919,81
0,0,9925,75
0,0,9929,71
0,0,9934,66
0,0,9937,63
0,0,9939,61
0,0,9941,59
0,0,9944,56
0,0,9950,50
0,0,9955,45
0,0,9957,43
0,0,9959,41
0,0,9962,38
0,0,9963,37
0,0,9967,33
0,0,9970,30
0,0,9973,27
0,0,9973,27
0,0,9976,24
0,0,9977,23
0,0,9978,22
0,0,9978,22
0,0,9980,20
0,0,9982,18
0,0,9983,17
0,0,9984,16
0,0,9985,15
0,0,9985,15
0,0,9985,15
0,0,9986,14
0,0,9987,13
0,0,9987,13
0,0,9988,12
0,0,9989,11
0,0,9992,8
0,0,9992,8
0,0,9992,8
0,0,9992,8
0,0,9993,7
0,0,9994,6
0,0,9995,5
0,0,9996,4
0,0,9996,4
0,0,9996,4
0,0,9996,4
0,0,9996,4
0,0,9996,4
0,0,9996,4
0,0,9996,4
0,0,9996,4
0,0,9996,4
0,0,9996,4
0,0,9996,4
0,0,9997,3
0,0,9997,3
0,0,9997,3
0,0,9997,3
0,0,9997,3
0,0,9998,2
0,0,9998,2
0,0,9998,2
0,0,9999,1
0,0,9999,1
0,0,9999,1
0,0,9999,1
0,0,9999,1
0,0,9999,1
0,0,9999,1
0,0,9999,1
0,0,9999,1
0,0,9999,1
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
0,0,10000,0
package sma; package sma;
import models.Parameters; import models.Parameters;
import sma.agents.Agent; import sma.agents.RandomWalkingAgent;
import sma.agents.states.InfectedState;
import sma.scheduler.FairAsynchronousScheduler; import sma.scheduler.FairAsynchronousScheduler;
import sma.scheduler.FairSynchronousScheduler; import sma.scheduler.FairSynchronousScheduler;
import sma.scheduler.Scheduler; import sma.scheduler.Scheduler;
import utils.DataAdapter;
import utils.Pair;
import utils.StatsRecorder; import utils.StatsRecorder;
import utils.YamlReader; import utils.YamlReader;
import view.FrameBuilder; import view.FrameBuilder;
...@@ -22,19 +21,19 @@ public class SMA { ...@@ -22,19 +21,19 @@ public class SMA {
private Parameters parameters; private Parameters parameters;
private Random r; private Random r;
private Agent[] agents; private RandomWalkingAgent[] agents;
private GraphicEnvironment environment; private GraphicEnvironment environment;
private Scheduler scheduler; private Scheduler scheduler;
private StatisticsCanvas statisticsCanvas; private StatisticsCanvas statisticsCanvas;
private HashMap<Agent.State, Pair<Integer,Color>> stats; private HashMap<String,Integer> stats;
private FrameBuilder frameBuilder; private FrameBuilder frameBuilder;
public SMA() { public SMA() {
parameters = YamlReader.getParams(); parameters = YamlReader.getParams();
r = new Random(parameters.getSeed()); r = new Random(parameters.getSeed());
agents = new Agent[parameters.getPopulation()]; agents = new RandomWalkingAgent[parameters.getPopulation()];
statisticsCanvas = new StatisticsCanvas(500,500); statisticsCanvas = new StatisticsCanvas(500,500);
frameBuilder = new FrameBuilder(); frameBuilder = new FrameBuilder();
...@@ -44,14 +43,15 @@ public class SMA { ...@@ -44,14 +43,15 @@ public class SMA {
private void populateEnvironment() { private void populateEnvironment() {
for (int i = 0; i<parameters.getPopulation();i++) { for (int i = 0; i<parameters.getPopulation();i++) {
Point position = new Point(r.nextInt(parameters.getSize()),r.nextInt(parameters.getSize())); Point position = new Point(r.nextInt(parameters.getSize()),r.nextInt(parameters.getSize()));
Agent agent = new Agent(position,parameters.getSeed()+i,environment); RandomWalkingAgent agent = new RandomWalkingAgent(position,parameters.getSeed()+i,environment);
agents[i] = agent; agents[i] = agent;
} }
} }
private void infectPatientZero() { private void infectPatientZero() {
for (int i=0 ; i< parameters.getNbOfPatientZero(); i++) { for (int i=0 ; i< parameters.getNbOfPatientZero(); i++) {
agents[(r.nextInt(parameters.getPopulation()))].setState(Agent.State.INFECTED); var agent = agents[(r.nextInt(parameters.getPopulation()))];
agent.changeState(new InfectedState(agent));
} }
} }
...@@ -87,7 +87,7 @@ public class SMA { ...@@ -87,7 +87,7 @@ public class SMA {
private void doNextCycle() throws IOException, InterruptedException { private void doNextCycle() throws IOException, InterruptedException {
scheduler.nextCycle(); scheduler.nextCycle();
stats = environment.getAgentStatus(); stats = environment.getAgentStatus();
StatsRecorder.writeToCSV(DataAdapter.adaptData(stats),"output.csv"); StatsRecorder.writeToCSV(stats,"output.csv");
updateGraphics(); updateGraphics();
Thread.sleep(100); Thread.sleep(100);
} }
......
package sma.agents; package sma.agents;
import sma.environment.Environment; import sma.agents.states.State;
import utils.YamlReader;
import view.GraphicEnvironment;
import java.awt.Point; public interface Agent {
import java.util.Random;
public class Agent { void changeState(State state);
boolean contact();
public enum State { boolean incubate();
SUSCEPTIBLE, boolean recover();
EXPOSED, void move();
INFECTED,
RECOVERED
}
private Point position;
private Random r;
private GraphicEnvironment environment;
private State state;
private Boolean exposedThisCycle;
private Boolean infectedThisCycle;
public Agent(Point position, int seed, GraphicEnvironment environment) {
this.position = position;
this.state = State.SUSCEPTIBLE;
this.environment = environment;
this.r = new Random(seed);
}
private void move() {
int move = r.nextInt(4);
Point newPosition = switch (move) {
case Environment.LEFT -> new Point(position.x-environment.RADIUS,position.y);
case Environment.RIGHT -> new Point(position.x+environment.RADIUS,position.y);
case Environment.UP -> new Point(position.x,position.y-environment.RADIUS);
case Environment.DOWN -> new Point(position.x,position.y+environment.RADIUS);
default -> throw new IllegalStateException("Unexpected value: " + move);
};
if (newPosition.x <= environment.getWidth()-1 && newPosition.x >= 0 && newPosition.y <= environment.getHeight()-1 && newPosition.y >=0 ) {
environment.notifyNewPosition(position,newPosition,this);
position = newPosition;
}
}
private void contact() {
for (Agent neighbor: environment.getNeighbors(position)) {
if (neighbor.getState() == State.INFECTED) {
int roll = r.nextInt(100);
if (roll <= YamlReader.getParams().getInfectionChance()*100) {
state = State.EXPOSED;
exposedThisCycle = true;
}
}
}
}
private void incubate() {
int roll = r.nextInt(100);
if (roll <= YamlReader.getParams().getIncubationRate()*100) {
state = State.INFECTED;
infectedThisCycle = true;
}
}
private void recover() {
int roll = r.nextInt(100);
if (roll <= YamlReader.getParams().getRecoveryRate()*100) {
state = State.RECOVERED;
}
}
public State getState() {
return this.state;
}
public void setState(State state) {
this.state = state;
}
public void wakeUp() {
exposedThisCycle = false;
infectedThisCycle = false;
move();
if (state == State.SUSCEPTIBLE) {
contact();
}
if (state == State.EXPOSED && !exposedThisCycle) {
incubate();
}
if (state == State.INFECTED && !infectedThisCycle) {
recover();
}
}
public Point getPosition() {
return position;
}
@Override
public String toString() {
return switch (state) {
case SUSCEPTIBLE -> "S";
case EXPOSED -> "E";
case INFECTED -> "I";
case RECOVERED -> "R";
};
}
} }
package sma.agents;
import sma.agents.states.InfectedState;
import sma.agents.states.State;
import sma.agents.states.SuceptibleState;
import sma.environment.Environment;
import utils.YamlReader;
import view.GraphicEnvironment;
import java.awt.Point;
import java.util.Random;
public class RandomWalkingAgent implements Agent {
private Point position;
private Random r;
private GraphicEnvironment environment;
private State state;
private Boolean exposedThisCycle;
private Boolean infectedThisCycle;
public RandomWalkingAgent(Point position, int seed, GraphicEnvironment environment) {
this.position = position;
this.state = new SuceptibleState(this);
this.environment = environment;
this.r = new Random(seed);
}
public void move() {
int move = r.nextInt(4);
Point newPosition = switch (move) {
case Environment.LEFT -> new Point(position.x-environment.RADIUS,position.y);
case Environment.RIGHT -> new Point(position.x+environment.RADIUS,position.y);
case Environment.UP -> new Point(position.x,position.y-environment.RADIUS);
case Environment.DOWN -> new Point(position.x,position.y+environment.RADIUS);
default -> throw new IllegalStateException("Unexpected value: " + move);
};
if (newPosition.x <= environment.getWidth()-1 && newPosition.x >= 0 && newPosition.y <= environment.getHeight()-1 && newPosition.y >=0 ) {
environment.notifyNewPosition(position,newPosition,this);
position = newPosition;
}
}
@Override
public void changeState(State state) {
this.state = state;
}
@Override
public boolean contact() {
boolean isExposed = false;
for (RandomWalkingAgent neighbor: environment.getNeighbors(position)) {
if (neighbor.getState() instanceof InfectedState) {
int roll = r.nextInt(100);
if (roll <= YamlReader.getParams().getInfectionChance()*100) {
isExposed = true;
}
}
}
return isExposed;
}
@Override
public boolean incubate() {
boolean isSick = false;
int roll = r.nextInt(100);
if (roll <= YamlReader.getParams().getIncubationRate()*100) {
isSick = true;
}
return isSick;
}
@Override
public boolean recover() {
boolean isHealed = false;
int roll = r.nextInt(100);
if (roll <= YamlReader.getParams().getRecoveryRate()*100) {
isHealed = true;
}
return isHealed;
}
public State getState() {
return this.state;
}
public void wakeUp() {
exposedThisCycle = false;
infectedThisCycle = false;
state.onMovement();
}
public Point getPosition() {
return position;
}
}
package sma.agents.states;
import sma.agents.Agent;
public class ExposedState extends State{
public ExposedState(Agent agent) {
super(agent);
}
@Override
public void onMovement() {
agent.move();
if (agent.incubate()) {
agent.changeState(new InfectedState(agent));
}
}
@Override
public String toString() {
return "EXPOSED";
}
}
package sma.agents.states;
import sma.agents.Agent;
public class InfectedState extends State{
public InfectedState(Agent agent) {
super(agent);
}
@Override
public void onMovement() {
agent.move();
if (agent.recover()) {
agent.changeState(new RecoveredState(agent));
}
}
@Override
public String toString() {
return "INFECTED";
}
}
package sma.agents.states;
import sma.agents.Agent;
public class RecoveredState extends State{
public RecoveredState(Agent agent) {
super(agent);
}
@Override
public void onMovement() {
agent.move();
}
@Override
public String toString() {
return "RECOVERED";
}
}
package sma.agents.states;
import sma.agents.Agent;
public abstract class State {
public final static String EXPOSED = "EXPOSED";
public final static String INFECTED = "INFECTED";
public final static String SUCEPTIBLE = "SUCEPTIBLE";
public final static String RECOVERED = "RECOVERED";
protected Agent agent;
State(Agent agent) {
this.agent = agent;
}
public void onMovement(){}
}
package sma.agents.states;
import sma.agents.Agent;
public class SuceptibleState extends State{
public SuceptibleState(Agent agent) {
super(agent);
}
@Override
public void onMovement() {
agent.move();
if (agent.contact()) {
agent.changeState(new ExposedState(agent));
}
}
@Override
public String toString() {
return "SUCEPTIBLE";
}
}
package sma.environment; package sma.environment;
import sma.agents.Agent; import sma.agents.RandomWalkingAgent;
import java.awt.Point; import java.awt.Point;
import java.util.List; import java.util.List;
...@@ -17,5 +17,5 @@ public interface Environment { ...@@ -17,5 +17,5 @@ public interface Environment {
int DOWN_RIGHT = 8; int DOWN_RIGHT = 8;
int MAX_CHUNK = 9; int MAX_CHUNK = 9;
List<Agent> getNeighbors(Point position); List<RandomWalkingAgent> getNeighbors(Point position);
} }
package sma.scheduler; package sma.scheduler;
import sma.agents.Agent; import sma.agents.RandomWalkingAgent;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
...@@ -12,16 +12,16 @@ import java.util.stream.Collectors; ...@@ -12,16 +12,16 @@ import java.util.stream.Collectors;
public class FairAsynchronousScheduler implements Scheduler{ public class FairAsynchronousScheduler implements Scheduler{
private ExecutorService executor = Executors.newSingleThreadExecutor(); private ExecutorService executor = Executors.newSingleThreadExecutor();
private Queue<Agent> queue; private Queue<RandomWalkingAgent> queue;
public FairAsynchronousScheduler(Agent[] agents) { public FairAsynchronousScheduler(RandomWalkingAgent[] agents) {
this.queue = new ConcurrentLinkedQueue<>(Arrays.stream(agents).toList()); this.queue = new ConcurrentLinkedQueue<>(Arrays.stream(agents).toList());
} }
public void nextCycle() { public void nextCycle() {
List<Future<Agent>> 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.wakeUp(); return agent;})).collect(Collectors.toList());
Function<Future<Agent>,Agent> futureTreatment = futureAgent -> { Function<Future<RandomWalkingAgent>, RandomWalkingAgent> futureTreatment = futureAgent -> {
try { try {
return futureAgent.get(); return futureAgent.get();
} catch (ExecutionException e) { } catch (ExecutionException e) {
...@@ -32,7 +32,7 @@ public class FairAsynchronousScheduler implements Scheduler{ ...@@ -32,7 +32,7 @@ public class FairAsynchronousScheduler implements Scheduler{
return null; return null;
}; };
List<Agent> nextQueue = results.parallelStream().map(futureTreatment).collect(Collectors.toList()); List<RandomWalkingAgent> nextQueue = results.parallelStream().map(futureTreatment).collect(Collectors.toList());
queue = new ConcurrentLinkedQueue<>(nextQueue); queue = new ConcurrentLinkedQueue<>(nextQueue);
} }
......
package sma.scheduler; package sma.scheduler;
import sma.agents.Agent; import sma.agents.RandomWalkingAgent;
import java.util.*; import java.util.*;
public class FairSynchronousScheduler implements Scheduler { public class FairSynchronousScheduler implements Scheduler {
private Agent[] agents; private RandomWalkingAgent[] agents;
private Stack<Integer> executionOrder; private Stack<Integer> executionOrder;
private Random r; private Random r;
public FairSynchronousScheduler(Agent[] agents, int seed) { public FairSynchronousScheduler(RandomWalkingAgent[] agents, int seed) {
this.agents = agents; this.agents = agents;
r = new Random(seed); r = new Random(seed);
executionOrder = new Stack<>(); executionOrder = new Stack<>();
......
package utils;
import sma.agents.Agent;
import java.awt.*;
import java.util.HashMap;
public class DataAdapter {
public static HashMap<String,String> adaptData(HashMap<Agent.State,Pair<Integer, Color>> data) {
var map = new HashMap<String,String>();
map.put("SUCEPTIBLE",data.get(Agent.State.SUSCEPTIBLE).getFirst().toString());
map.put("EXPOSED",data.get(Agent.State.EXPOSED).getFirst().toString());
map.put("INFECTED",data.get(Agent.State.INFECTED).getFirst().toString());
map.put("RECOVERED",data.get(Agent.State.RECOVERED).getFirst().toString());
return map;
}
}
...@@ -10,7 +10,7 @@ public class StatsRecorder { ...@@ -10,7 +10,7 @@ public class StatsRecorder {
private static int nbOfCycles = 0; private static int nbOfCycles = 0;
private static ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); private static ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
public static void writeToCSV(HashMap<String,String> data, String outputFile) throws IOException { public static void writeToCSV(HashMap<String,Integer> data, String outputFile) throws IOException {
if (!outputFile.endsWith(".csv")) { if (!outputFile.endsWith(".csv")) {
throw new InvalidParameterException("outputFile is not a .csv file."); throw new InvalidParameterException("outputFile is not a .csv file.");
......
package view; package view;
import sma.agents.Agent; import sma.agents.RandomWalkingAgent;
import sma.agents.states.*;
import sma.environment.Environment; import sma.environment.Environment;
import utils.Pair; import utils.Pair;
import java.awt.*; import java.awt.*;
import java.security.InvalidParameterException;
import java.util.*; import java.util.*;
import java.util.List; import java.util.List;
...@@ -14,14 +16,14 @@ public class GraphicEnvironment extends Canvas implements Environment { ...@@ -14,14 +16,14 @@ public class GraphicEnvironment extends Canvas implements Environment {
public final static int CHUNK_SIZE = 2*RADIUS; public final static int CHUNK_SIZE = 2*RADIUS;
private Agent[] agents; private RandomWalkingAgent[] agents;
private List<Agent>[][] chunks; private List<RandomWalkingAgent>[][] chunks;
private int windowWidth; private int windowWidth;
private int windowHeight; private int windowHeight;
public GraphicEnvironment(int width,int height,Agent[] agents) { public GraphicEnvironment(int width, int height, RandomWalkingAgent[] agents) {
this.windowWidth = width; this.windowWidth = width;
this.windowHeight = height; this.windowHeight = height;
this.agents = agents; this.agents = agents;
...@@ -36,14 +38,14 @@ public class GraphicEnvironment extends Canvas implements Environment { ...@@ -36,14 +38,14 @@ public class GraphicEnvironment extends Canvas implements Environment {
chunks[i][j] = new ArrayList<>(); chunks[i][j] = new ArrayList<>();
} }
} }
for (Agent agent : agents) { for (RandomWalkingAgent agent : agents) {
int x = agent.getPosition().x/CHUNK_SIZE; int x = agent.getPosition().x/CHUNK_SIZE;
int y = agent.getPosition().y/CHUNK_SIZE; int y = agent.getPosition().y/CHUNK_SIZE;
chunks[x][y].add(agent); chunks[x][y].add(agent);
} }
} }
public void notifyNewPosition(Point oldPosition, Point newPosition, Agent agent) { public void notifyNewPosition(Point oldPosition, Point newPosition, RandomWalkingAgent agent) {
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(agent); chunks[oldPosition.x/CHUNK_SIZE][oldPosition.y/CHUNK_SIZE].remove(agent);
chunks[newPosition.x/CHUNK_SIZE][newPosition.y/CHUNK_SIZE].add(agent); chunks[newPosition.x/CHUNK_SIZE][newPosition.y/CHUNK_SIZE].add(agent);
...@@ -70,8 +72,8 @@ public class GraphicEnvironment extends Canvas implements Environment { ...@@ -70,8 +72,8 @@ public class GraphicEnvironment extends Canvas implements Environment {
} }
@Override @Override
public List<Agent> getNeighbors(Point p) { public List<RandomWalkingAgent> getNeighbors(Point p) {
var neighbors = new ArrayList<Agent>(); var neighbors = new ArrayList<RandomWalkingAgent>();
for (int i = 0; i < MAX_CHUNK; i++) { for (int i = 0; i < MAX_CHUNK; i++) {
neighbors.addAll(getChunkNeighbors(i,p)); neighbors.addAll(getChunkNeighbors(i,p));
...@@ -79,7 +81,7 @@ public class GraphicEnvironment extends Canvas implements Environment { ...@@ -79,7 +81,7 @@ public class GraphicEnvironment extends Canvas implements Environment {
return neighbors; return neighbors;
} }
private List<Agent> getChunkNeighbors(int relativeTo, Point p) { private List<RandomWalkingAgent> getChunkNeighbors(int relativeTo, Point p) {
var x = p.x/CHUNK_SIZE; var x = p.x/CHUNK_SIZE;
var y = p.y/CHUNK_SIZE; var y = p.y/CHUNK_SIZE;
switch (relativeTo) { switch (relativeTo) {
...@@ -95,9 +97,9 @@ public class GraphicEnvironment extends Canvas implements Environment { ...@@ -95,9 +97,9 @@ public class GraphicEnvironment extends Canvas implements Environment {
default -> throw new IllegalStateException("Unexpected value: " + relativeTo); default -> throw new IllegalStateException("Unexpected value: " + relativeTo);
}; };
var neighbors = new ArrayList<Agent>(); var neighbors = new ArrayList<RandomWalkingAgent>();
try{ try{
for (Agent agent : chunks[x][y]) { for (RandomWalkingAgent agent : chunks[x][y]) {
if (detectCollision(p, agent.getPosition())) { if (detectCollision(p, agent.getPosition())) {
neighbors.add(agent); neighbors.add(agent);
} }
...@@ -108,27 +110,19 @@ public class GraphicEnvironment extends Canvas implements Environment { ...@@ -108,27 +110,19 @@ public class GraphicEnvironment extends Canvas implements Environment {
return neighbors; return neighbors;
} }
public HashMap<Agent.State,Pair<Integer,Color>> getAgentStatus() { public HashMap<String,Integer> getAgentStatus() {
Pair<Integer,Color> susceptible = new Pair<>(0,Color.GRAY); var map = new HashMap<String,Integer>();
Pair<Integer,Color> exposed = new Pair<>(0,Color.YELLOW); map.put(State.EXPOSED,0);
Pair<Integer,Color> infected = new Pair<>(0,Color.RED); map.put(State.INFECTED,0);
Pair<Integer,Color> recovered = new Pair<>(0,Color.green); map.put(State.RECOVERED,0);
map.put(State.SUCEPTIBLE,0);
for (Agent agent : agents) { for (RandomWalkingAgent agent : agents) {
switch (agent.getState()) { String state = agent.getState().toString();
case SUSCEPTIBLE -> susceptible.setFirst(susceptible.getFirst()+1); map.put(state,map.get(state)+1);
case EXPOSED -> exposed.setFirst(exposed.getFirst()+1);
case INFECTED -> infected.setFirst(infected.getFirst()+1);
case RECOVERED -> recovered.setFirst(recovered.getFirst()+1);
}
} }
var result = new HashMap<Agent.State,Pair<Integer,Color>>(); return map;
result.put(Agent.State.SUSCEPTIBLE,susceptible);
result.put(Agent.State.EXPOSED,exposed);
result.put(Agent.State.INFECTED,infected);
result.put(Agent.State.RECOVERED,recovered);
return result;
} }
private Boolean detectCollision(Point pos1, Point pos2) { private Boolean detectCollision(Point pos1, Point pos2) {
...@@ -138,12 +132,13 @@ public class GraphicEnvironment extends Canvas implements Environment { ...@@ -138,12 +132,13 @@ public class GraphicEnvironment extends Canvas implements Environment {
return distanceSquared < (2*RADIUS) * (2*RADIUS); return distanceSquared < (2*RADIUS) * (2*RADIUS);
} }
private void colorAgent(Graphics g,Agent a) { private void colorAgent(Graphics g, RandomWalkingAgent a) {
switch (a.getState()) { var state = a.getState();
case SUSCEPTIBLE -> g.setColor(Color.GRAY); switch (state.toString()) {
case EXPOSED -> g.setColor(Color.YELLOW); case State.SUCEPTIBLE-> g.setColor(Color.GRAY);
case INFECTED -> g.setColor(Color.RED); case State.EXPOSED -> g.setColor(Color.YELLOW);
case RECOVERED -> g.setColor(Color.GREEN); case State.INFECTED -> g.setColor(Color.RED);
case State.RECOVERED -> g.setColor(Color.GREEN);
} }
} }
} }
package view; package view;
import sma.agents.Agent; import sma.agents.RandomWalkingAgent;
import sma.agents.states.State;
import utils.Pair; import utils.Pair;
import utils.YamlReader; import utils.YamlReader;
...@@ -11,7 +12,7 @@ public class StatisticsCanvas extends Canvas { ...@@ -11,7 +12,7 @@ public class StatisticsCanvas extends Canvas {
private int canvasWidth; private int canvasWidth;
private int canvasHeight; private int canvasHeight;
private HashMap<Agent.State,Pair<Integer,Color>> values; private HashMap<String,Integer> values;
private int total; private int total;
public StatisticsCanvas(int width,int height) { public StatisticsCanvas(int width,int height) {
...@@ -23,28 +24,36 @@ public class StatisticsCanvas extends Canvas { ...@@ -23,28 +24,36 @@ public class StatisticsCanvas extends Canvas {
setVisible(true); setVisible(true);
} }
public void updateValues(HashMap<Agent.State,Pair<Integer,Color>> values ) { private Color stringToColor(String str) {
return switch (str){
case State.EXPOSED -> Color.YELLOW;
case State.SUCEPTIBLE -> Color.GRAY;
case State.INFECTED -> Color.RED;
case State.RECOVERED -> Color.GREEN;
default -> throw new IllegalStateException("Illegal state : "+str);
};
}
public void updateValues(HashMap<String,Integer> values ) {
this.values = values; this.values = values;
} }
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
int start = 0; int start = 0;
for (String state : values.keySet()) {
}
for (int i=0 ; i <values.keySet().size();i++) { for (int i=0 ; i <values.keySet().size();i++) {
Agent.State state = values.keySet().stream().toList().get(i); String state = (String) values.keySet().toArray()[i];
Pair<Integer,Color> subpopulation = values.get(state); int value = values.get(state);
g.setColor(subpopulation.getSecond()); g.setColor(stringToColor(state));
float height = ((float)subpopulation.getFirst()/total)*canvasHeight; float height = ((float)value/total)*canvasHeight;
g.fillRect(10,start,canvasWidth/4,start+(int)height); g.fillRect(10,start,canvasWidth/4,start+(int)height);
start +=height; start +=height;
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
switch (state) { g.drawString(state + " : "+value,canvasWidth/2,canvasHeight/values.keySet().size()*(1+i)-100);
case SUSCEPTIBLE -> g.drawString("SUSCEPTIBLE : " + subpopulation.getFirst(),canvasWidth/2,canvasHeight/values.keySet().size()*(1+i)-100);
case EXPOSED -> g.drawString("EXPOSED : " + subpopulation.getFirst(),canvasWidth/2,canvasHeight/values.keySet().size()*(1+i)-100);
case INFECTED -> g.drawString("INFECTED : " + subpopulation.getFirst(),canvasWidth/2,canvasHeight/values.keySet().size()*(1+i)-100);
case RECOVERED -> g.drawString("RECOVERED : " + subpopulation.getFirst(),canvasWidth/2,canvasHeight/values.keySet().size()*(1+i)-100);
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment