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

drastically improved performances by rendering chunks and looking only...

drastically improved performances by rendering chunks and looking only collision in close chunks instead of looking collision for all agents everywhere
parent bd6000b6
No related branches found
No related tags found
No related merge requests found
SUCEPTIBLE,EXPOSED,RECOVERED,INFECTED
9995,4,0,1
9989,8,0,3
9968,23,0,9
9901,79,0,20
9826,124,0,50
9726,161,7,106
9576,230,13,181
9368,320,23,289
9140,379,46,435
8866,454,72,608
8541,543,110,806
8215,599,165,1021
7859,647,217,1277
7449,732,287,1532
7019,771,390,1820
6517,879,514,2090
6029,892,654,2425
5545,906,812,2737
5076,911,990,3023
4613,925,1164,3298
4097,977,1381,3545
3618,941,1616,3825
3126,956,1847,4071
2695,867,2086,4352
2286,849,2351,4514
1886,836,2637,4641
1513,761,2916,4810
1199,709,3209,4883
886,670,3509,4935
632,585,3815,4968
454,451,4097,4998
318,360,4398,4924
212,280,4695,4813
142,196,4985,4677
78,161,5270,4491
34,120,5554,4292
7,84,5784,4125
0,49,6046,3905
0,27,6305,3668
0,14,6522,3464
0,6,6726,3268
0,3,6944,3053
0,2,7118,2880
0,1,7318,2681
0,0,7477,2523
0,0,7621,2379
0,0,7754,2246
0,0,7879,2121
0,0,8013,1987
0,0,8122,1878
0,0,8253,1747
0,0,8360,1640
0,0,8468,1532
0,0,8577,1423
0,0,8660,1340
0,0,8737,1263
0,0,8815,1185
0,0,8894,1106
0,0,8961,1039
0,0,9027,973
0,0,9083,917
0,0,9144,856
0,0,9198,802
0,0,9245,755
0,0,9296,704
0,0,9342,658
0,0,9385,615
0,0,9428,572
0,0,9457,543
0,0,9490,510
0,0,9518,482
0,0,9541,459
0,0,9580,420
0,0,9608,392
0,0,9630,370
0,0,9647,353
0,0,9674,326
0,0,9690,310
0,0,9708,292
......@@ -40,7 +40,8 @@ public class Agent {
case IEnvironment.DOWN -> new Point(position.x,position.y+environment.RADIUS);
default -> throw new IllegalStateException("Unexpected value: " + move);
};
if (newPosition.x <= environment.getWidth() && newPosition.x >= 0 && newPosition.y <= environment.getHeight() && newPosition.y >=0 ) {
if (newPosition.x <= environment.getWidth()-1 && newPosition.x >= 0 && newPosition.y <= environment.getHeight()-1 && newPosition.y >=0 ) {
environment.notifyNewPosition(position,newPosition,this);
position = newPosition;
}
}
......
......@@ -8,5 +8,12 @@ public interface IEnvironment {
int RIGHT = 1;
int UP = 2;
int DOWN = 3;
int CENTER = 4;
int UP_LEFT = 5;
int UP_RIGHT = 6;
int DOWN_LEFT = 7;
int DOWN_RIGHT = 8;
int MAX_CHUNK = 9;
List<Agent> getNeighbors(Point position);
}
......@@ -39,7 +39,7 @@ public class SMA {
private void populateEnvironment() {
for (int i = 0; i<parameters.getPopulation();i++) {
Point position = new Point(r.nextInt(parameters.getSize()+1),r.nextInt(parameters.getSize()+1));
Point position = new Point(r.nextInt(parameters.getSize()),r.nextInt(parameters.getSize()));
Agent agent = new Agent(position,parameters.getSeed()+i,environment);
agents[i] = agent;
}
......@@ -54,6 +54,7 @@ public class SMA {
public void init() {
environment = new GraphicEnvironment(parameters.getSize(),parameters.getSize(),agents);
populateEnvironment();
environment.initiateChunks();
infectPatientZero();
frameBuilder.addComponent(environment,FrameBuilder.TOP);
......@@ -61,15 +62,17 @@ public class SMA {
frameBuilder.buildWindow();
scheduler = new Scheduler(agents, parameters.getSeed());
statisticsCanvas.updateValues(environment.getAgentStatus());
statisticsCanvas.repaint();
}
private void updateGraphics() {
private void updateGraphics(){
statisticsCanvas.updateValues(stats);
statisticsCanvas.repaint();
environment.repaint();
statisticsCanvas.updateValues(stats);
}
public void run() throws InterruptedException, IOException {
public void run() throws IOException, InterruptedException {
while (true) {
scheduler.nextCycle();
stats = environment.getAgentStatus();
......
package sma;
import view.GraphicEnvironment;
import java.util.*;
public class Scheduler{
......
......@@ -15,13 +15,8 @@ public class StatsRecorder {
if (!outputFile.endsWith(".csv")) {
throw new InvalidParameterException("outputFile is not a .csv file.");
}
File file;
try {
file = new File(classLoader.getResource(outputFile).getFile());
file.createNewFile();
}catch (NullPointerException e){
throw new FileNotFoundException("No file called "+outputFile+" found in resources folder");
}
File file = new File(outputFile);
file.createNewFile();
if (nbOfCycles == 0) {
PrintWriter writer = new PrintWriter(file);
......
......@@ -5,16 +5,19 @@ import sma.IEnvironment;
import utils.Pair;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.*;
import java.util.List;
public class GraphicEnvironment extends Canvas implements IEnvironment {
public final static int RADIUS = 10;
public final static int CHUNK_SIZE = 2*RADIUS;
private Agent[] agents;
private List<Agent>[][] chunks;
private int windowWidth;
private int windowHeight;
......@@ -26,6 +29,33 @@ public class GraphicEnvironment extends Canvas implements IEnvironment {
setVisible(true);
}
public void initiateChunks() {
chunks = new ArrayList[(windowWidth/CHUNK_SIZE)][(windowHeight/CHUNK_SIZE)];
for (int i = 0; i < chunks.length; i++) {
for (int j = 0; j < chunks[i].length; j++) {
chunks[i][j] = new ArrayList<>();
}
}
for (Agent agent : agents) {
int x = agent.getPosition().x/CHUNK_SIZE;
int y = agent.getPosition().y/CHUNK_SIZE;
chunks[x][y].add(agent);
}
}
public void notifyNewPosition(Point oldPosition, Point newPosition, Agent agent) {
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[newPosition.x/CHUNK_SIZE][newPosition.y/CHUNK_SIZE].add(agent);
}
}
private void drawCenteredCircle(Graphics g, int x, int y, int r) {
x = x-(r/2);
y = y-(r/2);
g.fillOval(x,y,r,r);
}
@Override
public void paint(Graphics g) {
......@@ -34,7 +64,8 @@ public class GraphicEnvironment extends Canvas implements IEnvironment {
var agent = agents[i];
if (agent != null) {
colorAgent(g,agent);
g.fillOval(agent.getPosition().x,agent.getPosition().y, RADIUS, RADIUS);
drawCenteredCircle(g,agent.getPosition().x,agent.getPosition().y,RADIUS);
//g.fillOval(agent.getPosition().x,agent.getPosition().y, RADIUS, RADIUS);
}
}
}
......@@ -42,10 +73,44 @@ public class GraphicEnvironment extends Canvas implements IEnvironment {
@Override
public List<Agent> getNeighbors(Point p) {
var neighbors = new ArrayList<Agent>();
for (Agent agent : agents) {
if (detectCollision(p, agent.getPosition())) {
neighbors.add(agent);
for (int i = 0; i < MAX_CHUNK; i++) {
neighbors.addAll(getChunkNeighbors(i,p));
}
//for (Agent agent : agents) {
// if (detectCollision(p, agent.getPosition())) {
// neighbors.add(agent);
// }
//}
return neighbors;
}
private List<Agent> getChunkNeighbors(int relativeTo, Point p) {
var x = p.x/CHUNK_SIZE;
var y = p.y/CHUNK_SIZE;
switch (relativeTo) {
case LEFT -> x-=1;
case RIGHT -> x+=1;
case UP -> y-=1;
case DOWN -> y+=1;
case CENTER -> {}
case UP_LEFT -> {x-=1;y-=1;}
case UP_RIGHT -> {x+=1;y-=1;}
case DOWN_LEFT -> {x-=1;y+=1;}
case DOWN_RIGHT -> {x+=1;y+=1;}
default -> throw new IllegalStateException("Unexpected value: " + relativeTo);
};
var neighbors = new ArrayList<Agent>();
try{
for (Agent agent : chunks[x][y]) {
if (detectCollision(p, agent.getPosition())) {
neighbors.add(agent);
}
}
}catch (Exception e) {
return neighbors;
}
return neighbors;
}
......@@ -77,9 +142,11 @@ public class GraphicEnvironment extends Canvas implements IEnvironment {
double xDif = pos1.x - pos2.x;
double yDif = pos1.y - pos2.y;
double distanceSquared = xDif * xDif + yDif * yDif;
return distanceSquared < (2* RADIUS) * (2* RADIUS);
return distanceSquared < (2*RADIUS) * (2*RADIUS);
}
//TODO : vérifier les collisions
private void colorAgent(Graphics g,Agent a) {
switch (a.getState()) {
case SUSCEPTIBLE -> g.setColor(Color.GRAY);
......
......@@ -5,9 +5,7 @@ import utils.Pair;
import utils.YamlReader;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class StatisticsCanvas extends Canvas {
......@@ -32,7 +30,6 @@ public class StatisticsCanvas extends Canvas {
@Override
public void paint(Graphics g) {
int start = 0;
for (int i=0 ; i <values.keySet().size();i++) {
Agent.State state = values.keySet().stream().toList().get(i);
Pair<Integer,Color> subpopulation = values.get(state);
......
SUCEPTIBLE,EXPOSED,RECOVERED,INFECTED
9993,6,0,1
9978,17,0,5
9935,52,0,13
9854,107,0,39
9704,200,1,95
9534,271,7,188
9316,343,13,328
9093,400,36,471
8843,447,59,651
8568,492,100,840
8316,477,143,1064
8045,502,209,1244
7785,503,290,1422
7535,508,368,1589
7247,538,474,1741
6972,542,582,1904
6668,575,690,2067
6395,574,816,2215
6131,534,961,2374
5829,559,1116,2496
5540,583,1271,2606
5256,580,1442,2722
4941,599,1612,2848
4611,630,1778,2981
4272,644,1954,3130
3909,678,2143,3270
3543,694,2340,3423
3190,700,2540,3570
2857,684,2770,3689
2581,607,2975,3837
2259,635,3205,3901
1984,581,3435,4000
1686,587,3660,4067
1418,578,3886,4118
1128,562,4170,4140
790,610,4420,4180
567,530,4675,4228
386,429,4925,4260
283,323,5194,4200
204,231,5447,4118
134,163,5695,4008
83,126,5931,3860
52,94,6180,3674
33,67,6426,3474
7,57,6609,3327
3,33,6819,3145
1,17,7015,2967
0,8,7200,2792
0,4,7363,2633
0,3,7513,2484
0,3,7668,2329
0,1,7801,2198
0,1,7936,2063
0,0,8083,1917
0,0,8188,1812
0,0,8293,1707
0,0,8410,1590
0,0,8518,1482
0,0,8600,1400
0,0,8688,1312
0,0,8769,1231
0,0,8850,1150
0,0,8937,1063
0,0,9014,986
0,0,9078,922
0,0,9125,875
0,0,9181,819
0,0,9232,768
0,0,9276,724
0,0,9319,681
0,0,9364,636
0,0,9401,599
0,0,9445,555
0,0,9472,528
0,0,9511,489
0,0,9545,455
0,0,9574,426
0,0,9604,396
0,0,9628,372
0,0,9649,351
0,0,9673,327
0,0,9697,303
0,0,9717,283
0,0,9726,274
0,0,9745,255
0,0,9758,242
0,0,9774,226
0,0,9784,216
0,0,9791,209
0,0,9805,195
0,0,9819,181
0,0,9836,164
0,0,9846,154
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment