From 4093336d40f62df9bf2040593a6cc9d58e13c6e4 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 7 Apr 2023 08:57:56 +0200 Subject: [PATCH 1/3] =?UTF-8?q?Modification=20schedulers=20pour=20g=C3=A9r?= =?UTF-8?q?er=20les=20interf=C3=A9rences?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- src/main/java/fr/ntr/AccessPoint.java | 27 ++++++++++-- src/main/java/fr/ntr/Cell.java | 29 ++++++++++++- src/main/java/fr/ntr/Main.java | 22 +++------- src/main/java/fr/ntr/scheduler/MaxSNR.java | 30 ++++++++------ .../fr/ntr/scheduler/ProportionalFair.java | 36 ++++++++-------- .../java/fr/ntr/scheduler/RoundRobin.java | 41 ++++++++++--------- src/main/java/fr/ntr/scheduler/Scheduler.java | 35 +++++++--------- 8 files changed, 129 insertions(+), 93 deletions(-) diff --git a/build.gradle b/build.gradle index b0052ff..4b8b89a 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ plugins { } run { - args = ["10000", "17"] + args = ["10000", "20"] } application { diff --git a/src/main/java/fr/ntr/AccessPoint.java b/src/main/java/fr/ntr/AccessPoint.java index cb13e64..e752838 100644 --- a/src/main/java/fr/ntr/AccessPoint.java +++ b/src/main/java/fr/ntr/AccessPoint.java @@ -1,8 +1,6 @@ package fr.ntr; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; public class AccessPoint { @@ -23,10 +21,27 @@ public class AccessPoint { // Simulation cell1.reset(); cell2.reset(); + cell1.updateBandwidth(ticks); cell2.updateBandwidth(ticks); - cell1.schedule(ticks); - cell2.schedule(ticks); + for(int ts = 0; ts < 2; ts++) { + for (int sp = 0; sp < 100; sp++) { + cell1.preScheduling(); + cell2.preScheduling(); + + User user1 = cell1.schedule(ticks, ts, sp); + User user2 = cell2.schedule(ticks, ts, sp); + if(user1 == user2 && user1 != null) { + computeInterference(user1, ts, sp); + } + cell1.consumeResource(ticks, ts, sp); + cell2.consumeResource(ticks, ts, sp); + + cell1.postScheduling(user1); + cell2.postScheduling(user2); + } + } + // traite les données et les enregistre dans un fichier try { cell1.analyseData(ticks); @@ -37,4 +52,8 @@ public class AccessPoint { } } + + private void computeInterference(User user, int ts, int sp) { + + } } diff --git a/src/main/java/fr/ntr/Cell.java b/src/main/java/fr/ntr/Cell.java index f2cf382..30c3673 100644 --- a/src/main/java/fr/ntr/Cell.java +++ b/src/main/java/fr/ntr/Cell.java @@ -61,8 +61,33 @@ public class Cell { } } - public void schedule(int tick) { - scheduler.scheduling(tick); + public void preScheduling() { + scheduler.preScheduling(); + } + + public void postScheduling(User userSelected) { + scheduler.postScheduling(userSelected); + } + + public User schedule(int tick, int ts, int sp) { + return scheduler.scheduling(tick, ts, sp); + } + + public void consumeResource(int tick, int ts, int sp) { + ResourceBlock rb = frame[ts][sp]; + User user = rb.getUser(); + Packets p = user.getPacketsToSend().get(user.getPacketsToSend().size()-1); + p.decreaseBitsNumberRemaining((int) user.getBandwidthTable()[ts][sp]); + if(p.getBitsNumberRemaining() <= 0) { + if(tick == 0){ + p.setDurationSending(1); + }else { + p.setDurationSending(tick); + } + user.getPacketsSent().add(p); + user.getPacketsToSend().remove(p); + } + } public void analyseData(int tick) throws IOException { diff --git a/src/main/java/fr/ntr/Main.java b/src/main/java/fr/ntr/Main.java index 44989d9..aa66e12 100644 --- a/src/main/java/fr/ntr/Main.java +++ b/src/main/java/fr/ntr/Main.java @@ -13,7 +13,6 @@ import java.util.Objects; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; public class Main { @@ -73,13 +72,12 @@ public class Main { List usersCell1 = generateUsers(i, timeSlotNb, subCarrierNb); List usersCell2 = generateUsers(i, timeSlotNb, subCarrierNb); //copie de 1/4 des utilisateurs de cell1 vers cell2 + Random r = new Random(); for(int j = 0; j < i/4; j++){ - Random r = new Random(); int idxCopy = r.nextInt(i); User copiedUser = usersCell1.get(idxCopy); // On vérifie qu'on ne l'a pas déjà copié dans cell2 - while(usersCell2.contains(copiedUser)){ - r = new Random(); + if(usersCell2.contains(copiedUser)) { idxCopy = r.nextInt(i); copiedUser = usersCell1.get(idxCopy); } @@ -90,10 +88,9 @@ public class Main { ResourceBlock[][] frame1 = new ResourceBlock[timeSlotNb][subCarrierNb]; ResourceBlock[][] frame2 = new ResourceBlock[timeSlotNb][subCarrierNb]; - //TODO : changes schedulers //génération des schedulers - Scheduler schedulerCell1 = new RoundRobin(frame1, frame2, usersCell1); - Scheduler schedulerCell2 = new RoundRobin(frame2, frame1, usersCell2); + Scheduler schedulerCell1 = new RoundRobin(frame1, usersCell1); + Scheduler schedulerCell2 = new RoundRobin(frame2, usersCell2); try { //préparation à exportation des données de chaque cellule @@ -113,14 +110,7 @@ public class Main { System.exit(1); } } - executor.shutdown(); - try { - executor.awaitTermination(2, TimeUnit.MINUTES); - } - catch (InterruptedException e) { - e.printStackTrace(); - } - executor.shutdownNow(); + executor.close(); } else { System.err.println("Please give launch arguments"); @@ -135,7 +125,7 @@ public class Main { int half = nbUsers / 2; for (int i = 0; i < nbUsers; i++) { User user; - if(i >= half ){ + if(i >= half){ user = new User(500d, timeSlotNb, subCarrierNb); } else { diff --git a/src/main/java/fr/ntr/scheduler/MaxSNR.java b/src/main/java/fr/ntr/scheduler/MaxSNR.java index 46e02b7..891ac93 100644 --- a/src/main/java/fr/ntr/scheduler/MaxSNR.java +++ b/src/main/java/fr/ntr/scheduler/MaxSNR.java @@ -8,24 +8,28 @@ import fr.ntr.User; public class MaxSNR extends Scheduler { private final List users; - private final ResourceBlock[][] myFrame; - private final ResourceBlock[][] neighborFrame; - public MaxSNR(ResourceBlock[][] myFrame, ResourceBlock[][] neighborFrame, List users) { - this.myFrame = myFrame; - this.neighborFrame = neighborFrame; + public MaxSNR(ResourceBlock[][] myFrame, List users) { + super(myFrame); this.users = users; } @Override - public void scheduling(int ticks) { - User userMax; - for(int ts = 0; ts < 2; ts++){ - for(int sp = 0; sp < 100; sp++){ - userMax = userSelection(ts, sp); - allocateRessource(userMax, myFrame, neighborFrame, ts, sp, ticks); - } - } + public User scheduling(int ticks, int ts, int sp) { + User user = userSelection(ts, sp); + allocateRessource(user, ts, sp, ticks); + return user; + + } + + @Override + public void postScheduling(User userSelected) { + + } + + @Override + public void preScheduling() { + } /** diff --git a/src/main/java/fr/ntr/scheduler/ProportionalFair.java b/src/main/java/fr/ntr/scheduler/ProportionalFair.java index 0f67d61..93bbd3c 100644 --- a/src/main/java/fr/ntr/scheduler/ProportionalFair.java +++ b/src/main/java/fr/ntr/scheduler/ProportionalFair.java @@ -11,18 +11,28 @@ public class ProportionalFair extends Scheduler { private final List users; - private final ResourceBlock[][] myFrame; - private final ResourceBlock[][] neighborFrame; + private ArrayList averageBandwidth; - public ProportionalFair(ResourceBlock[][] myFrame, ResourceBlock[][] neighborFrame, List users) { - this.myFrame = myFrame; - this.neighborFrame = neighborFrame; + public ProportionalFair(ResourceBlock[][] myFrame, List users) { + super(myFrame); this.users = users; } @Override - public void scheduling(int ticks) { - ArrayList averageBandwiths = new ArrayList<>(); + public User scheduling(int ticks, int ts, int sp) { + User user = userSelection(ts, sp, users); + allocateRessource(user, ts, sp, ticks); + return user; + } + + @Override + public void postScheduling(User userSelected) { + + } + + @Override + public void preScheduling() { + averageBandwidth = new ArrayList<>(); for(User u : users){ double avg = 0d; double[][] bandwidthTable = u.getBandwidthTable(); @@ -32,23 +42,17 @@ public class ProportionalFair extends Scheduler { } } avg = avg / (bandwidthTable.length * bandwidthTable[0].length); - averageBandwiths.add(avg); - } - for(int ts = 0; ts < 2; ts++){ - for(int sp = 0; sp < 100; sp++){ - User selectedUser = userSelection(ts, sp, users, averageBandwiths); - allocateRessource(selectedUser, myFrame, neighborFrame, ts, sp, ticks); - } + averageBandwidth.add(avg); } } - private User userSelection(int ts, int sp, List users, List averageBandwiths) { + private User userSelection(int ts, int sp, List users) { double PF = 0.0; User selectedUser = null; for(int i = 0; i < users.size(); i++){ User u = users.get(i); double mkn = u.getBandwidthTable()[ts][sp]; - double averageMkn = averageBandwiths.get(i); + double averageMkn = averageBandwidth.get(i); double pf = mkn / averageMkn; if (PF < pf){ PF = pf; diff --git a/src/main/java/fr/ntr/scheduler/RoundRobin.java b/src/main/java/fr/ntr/scheduler/RoundRobin.java index f34aa42..20bca04 100644 --- a/src/main/java/fr/ntr/scheduler/RoundRobin.java +++ b/src/main/java/fr/ntr/scheduler/RoundRobin.java @@ -3,7 +3,6 @@ import java.util.Collections; import java.util.List; import java.util.stream.Collectors; -import fr.ntr.Cell; import fr.ntr.ResourceBlock; import fr.ntr.User; @@ -11,13 +10,11 @@ import fr.ntr.User; public class RoundRobin extends Scheduler { private final List users; - private final ResourceBlock[][] myFrame; - private final ResourceBlock[][] neighborFrame; + private List userCopy; - public RoundRobin(ResourceBlock[][] myFrame, ResourceBlock[][] neighborFrame, List users) { - this.myFrame = myFrame; - this.neighborFrame = neighborFrame; + public RoundRobin(ResourceBlock[][] myFrame, List users) { + super(myFrame); this.users = users; } @@ -27,22 +24,26 @@ public class RoundRobin extends Scheduler { * Return */ @Override - public void scheduling(int ticks) { - List userCopy = users.stream().filter(u -> !u.getPacketsToSend().isEmpty()).collect(Collectors.toList()); - Collections.shuffle(userCopy); + public User scheduling(int ticks, int ts, int sp) { - //Pour chaque time slot et sous porteuses - loop: for (int ts = 0; ts < Cell.getTimeSlotNb(); ts++) { - for(int sp = 0; sp < Cell.getSubCarrierNb(); sp++) { - if(userCopy.isEmpty()) { - break loop; - } - User userSelected = userSelection(userCopy); - allocateRessource(userSelected, myFrame, neighborFrame, ts, sp, ticks); - if(!userSelected.getPacketsToSend().isEmpty()) - userCopy.add(userSelected); - } + if(userCopy.isEmpty()) { + return null; } + User user = userSelection(userCopy); + allocateRessource(user, ts, sp, ticks); + return user; + } + + @Override + public void postScheduling(User userSelected) { + if(!userSelected.getPacketsToSend().isEmpty()) + userCopy.add(userSelected); + } + + @Override + public void preScheduling() { + userCopy = users.stream().filter(u -> !u.getPacketsToSend().isEmpty()).collect(Collectors.toList()); + Collections.shuffle(userCopy); } /** diff --git a/src/main/java/fr/ntr/scheduler/Scheduler.java b/src/main/java/fr/ntr/scheduler/Scheduler.java index 9ce246f..78376a4 100644 --- a/src/main/java/fr/ntr/scheduler/Scheduler.java +++ b/src/main/java/fr/ntr/scheduler/Scheduler.java @@ -6,29 +6,22 @@ import fr.ntr.User; public abstract class Scheduler { + protected final ResourceBlock[][] myFrame; - public abstract void scheduling(int ticks); + public Scheduler(ResourceBlock[][] myFrame) { + this.myFrame = myFrame; + } - protected void allocateRessource(User userMax, ResourceBlock[][] myFrame, ResourceBlock[][] neighborFrame, int ts, int sp, int ticks) { - ResourceBlock rb1 = myFrame[ts][sp]; - ResourceBlock rb2 = neighborFrame[ts][sp]; + + public abstract User scheduling(int ticks, int ts, int sp); + + public abstract void postScheduling(User userSelected); + + public abstract void preScheduling(); + + public void allocateRessource(User userMax, int ts, int sp, int ticks) { if (userMax != null && !userMax.getPacketsToSend().isEmpty()) { - User user2 = rb2.getUser(); - if ((userMax != null || user2 != null) && userMax == user2) { - double bandwidth1 = rb1.getBandwidth(); - //double bandwidth2 = rb2.getBandwidth(); - //User proche - if (userMax.getDistance() < 200d) { - rb1.getUser().getBandwidthTable()[ts][sp] = bandwidth1 / 2; - //rb2.getUser().getBandwidthTable()[ts][sp] = bandwidth2 / 2; - } - //User loin - else { - rb1.getUser().getBandwidthTable()[ts][sp] = bandwidth1 / 4; - //rb2.getUser().getBandwidthTable()[ts][sp] = bandwidth2 / 4; - } - } - Packets p = userMax.getPacketsToSend().get(userMax.getPacketsToSend().size()-1); + /* Packets p = userMax.getPacketsToSend().get(userMax.getPacketsToSend().size()-1); p.decreaseBitsNumberRemaining((int) userMax.getBandwidthTable()[ts][sp]); if(p.getBitsNumberRemaining() <= 0) { if(ticks == 0){ @@ -38,7 +31,7 @@ public abstract class Scheduler { } userMax.getPacketsSent().add(p); userMax.getPacketsToSend().remove(p); - } + } */ myFrame[ts][sp].setUser(userMax); myFrame[ts][sp].setBandwidth(userMax.getBandwidthTable()[ts][sp]); } From de59bc8047883102f7db936b6c0467bdce521c1f Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 7 Apr 2023 09:28:10 +0200 Subject: [PATCH 2/3] Fix execution --- build.gradle | 2 +- src/main/java/fr/ntr/AccessPoint.java | 9 ++++---- src/main/java/fr/ntr/Cell.java | 21 ++++++++++--------- src/main/java/fr/ntr/Main.java | 3 ++- .../java/fr/ntr/scheduler/RoundRobin.java | 3 +-- src/main/java/fr/ntr/scheduler/Scheduler.java | 11 ---------- 6 files changed, 19 insertions(+), 30 deletions(-) diff --git a/build.gradle b/build.gradle index 4b8b89a..e154be0 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ plugins { } run { - args = ["10000", "20"] + args = ["10000", "40"] } application { diff --git a/src/main/java/fr/ntr/AccessPoint.java b/src/main/java/fr/ntr/AccessPoint.java index e752838..edd06d7 100644 --- a/src/main/java/fr/ntr/AccessPoint.java +++ b/src/main/java/fr/ntr/AccessPoint.java @@ -24,11 +24,10 @@ public class AccessPoint { cell1.updateBandwidth(ticks); cell2.updateBandwidth(ticks); - for(int ts = 0; ts < 2; ts++) { - for (int sp = 0; sp < 100; sp++) { - cell1.preScheduling(); - cell2.preScheduling(); - + cell1.preScheduling(); + cell2.preScheduling(); + for(int ts = 0; ts < Cell.getTimeSlotNb(); ts++) { + for (int sp = 0; sp < Cell.getSubCarrierNb(); sp++) { User user1 = cell1.schedule(ticks, ts, sp); User user2 = cell2.schedule(ticks, ts, sp); if(user1 == user2 && user1 != null) { diff --git a/src/main/java/fr/ntr/Cell.java b/src/main/java/fr/ntr/Cell.java index 30c3673..9ba7bb8 100644 --- a/src/main/java/fr/ntr/Cell.java +++ b/src/main/java/fr/ntr/Cell.java @@ -76,18 +76,19 @@ public class Cell { public void consumeResource(int tick, int ts, int sp) { ResourceBlock rb = frame[ts][sp]; User user = rb.getUser(); - Packets p = user.getPacketsToSend().get(user.getPacketsToSend().size()-1); - p.decreaseBitsNumberRemaining((int) user.getBandwidthTable()[ts][sp]); - if(p.getBitsNumberRemaining() <= 0) { - if(tick == 0){ - p.setDurationSending(1); - }else { - p.setDurationSending(tick); + if(user != null && !user.getPacketsToSend().isEmpty()) { + Packets p = user.getPacketsToSend().get(0); + p.decreaseBitsNumberRemaining((int) rb.getBandwidth()); + if(p.getBitsNumberRemaining() <= 0) { + if(tick == 0){ + p.setDurationSending(1); + }else { + p.setDurationSending(tick); + } + user.getPacketsSent().add(p); + user.getPacketsToSend().remove(p); } - user.getPacketsSent().add(p); - user.getPacketsToSend().remove(p); } - } public void analyseData(int tick) throws IOException { diff --git a/src/main/java/fr/ntr/Main.java b/src/main/java/fr/ntr/Main.java index aa66e12..1f615c1 100644 --- a/src/main/java/fr/ntr/Main.java +++ b/src/main/java/fr/ntr/Main.java @@ -110,7 +110,8 @@ public class Main { System.exit(1); } } - executor.close(); + executor.close(); + System.out.println("Executor closed"); } else { System.err.println("Please give launch arguments"); diff --git a/src/main/java/fr/ntr/scheduler/RoundRobin.java b/src/main/java/fr/ntr/scheduler/RoundRobin.java index 20bca04..f553fc4 100644 --- a/src/main/java/fr/ntr/scheduler/RoundRobin.java +++ b/src/main/java/fr/ntr/scheduler/RoundRobin.java @@ -25,7 +25,6 @@ public class RoundRobin extends Scheduler { */ @Override public User scheduling(int ticks, int ts, int sp) { - if(userCopy.isEmpty()) { return null; } @@ -36,7 +35,7 @@ public class RoundRobin extends Scheduler { @Override public void postScheduling(User userSelected) { - if(!userSelected.getPacketsToSend().isEmpty()) + if(userSelected != null && !userSelected.getPacketsToSend().isEmpty()) userCopy.add(userSelected); } diff --git a/src/main/java/fr/ntr/scheduler/Scheduler.java b/src/main/java/fr/ntr/scheduler/Scheduler.java index 78376a4..3d30a88 100644 --- a/src/main/java/fr/ntr/scheduler/Scheduler.java +++ b/src/main/java/fr/ntr/scheduler/Scheduler.java @@ -21,17 +21,6 @@ public abstract class Scheduler { public void allocateRessource(User userMax, int ts, int sp, int ticks) { if (userMax != null && !userMax.getPacketsToSend().isEmpty()) { - /* Packets p = userMax.getPacketsToSend().get(userMax.getPacketsToSend().size()-1); - p.decreaseBitsNumberRemaining((int) userMax.getBandwidthTable()[ts][sp]); - if(p.getBitsNumberRemaining() <= 0) { - if(ticks == 0){ - p.setDurationSending(1); - }else { - p.setDurationSending(ticks); - } - userMax.getPacketsSent().add(p); - userMax.getPacketsToSend().remove(p); - } */ myFrame[ts][sp].setUser(userMax); myFrame[ts][sp].setBandwidth(userMax.getBandwidthTable()[ts][sp]); } From d534f3f61b80296c2982b5eec667a6be463f3cc4 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 7 Apr 2023 09:58:54 +0200 Subject: [PATCH 3/3] Add interference --- src/main/java/fr/ntr/AccessPoint.java | 14 ++++---------- src/main/java/fr/ntr/Cell.java | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main/java/fr/ntr/AccessPoint.java b/src/main/java/fr/ntr/AccessPoint.java index edd06d7..b6a9672 100644 --- a/src/main/java/fr/ntr/AccessPoint.java +++ b/src/main/java/fr/ntr/AccessPoint.java @@ -30,11 +30,9 @@ public class AccessPoint { for (int sp = 0; sp < Cell.getSubCarrierNb(); sp++) { User user1 = cell1.schedule(ticks, ts, sp); User user2 = cell2.schedule(ticks, ts, sp); - if(user1 == user2 && user1 != null) { - computeInterference(user1, ts, sp); - } - cell1.consumeResource(ticks, ts, sp); - cell2.consumeResource(ticks, ts, sp); + boolean haveInterference = user1 == user2 && user1 != null; + cell1.consumeResource(ticks, ts, sp, haveInterference); + cell2.consumeResource(ticks, ts, sp, haveInterference); cell1.postScheduling(user1); cell2.postScheduling(user2); @@ -44,15 +42,11 @@ public class AccessPoint { // traite les données et les enregistre dans un fichier try { cell1.analyseData(ticks); - //cell2.analyseData(ticks); + cell2.analyseData(ticks); } catch (IOException e) { System.out.println("Can't export data"); } } } - - private void computeInterference(User user, int ts, int sp) { - - } } diff --git a/src/main/java/fr/ntr/Cell.java b/src/main/java/fr/ntr/Cell.java index 9ba7bb8..f373ed6 100644 --- a/src/main/java/fr/ntr/Cell.java +++ b/src/main/java/fr/ntr/Cell.java @@ -73,11 +73,12 @@ public class Cell { return scheduler.scheduling(tick, ts, sp); } - public void consumeResource(int tick, int ts, int sp) { + public void consumeResource(int tick, int ts, int sp, boolean haveInterference) { ResourceBlock rb = frame[ts][sp]; User user = rb.getUser(); if(user != null && !user.getPacketsToSend().isEmpty()) { Packets p = user.getPacketsToSend().get(0); + rb.setBandwidth(updateBandwidth(haveInterference, rb)); p.decreaseBitsNumberRemaining((int) rb.getBandwidth()); if(p.getBitsNumberRemaining() <= 0) { if(tick == 0){ @@ -91,6 +92,19 @@ public class Cell { } } + private double updateBandwidth(boolean haveInterference, ResourceBlock rb) { + if(haveInterference) { + if(rb.getUser().getDistance() < 200d) { + // User proche + return rb.getBandwidth() / 2; + } + // User loin + return rb.getBandwidth() / 4; + } else { + return rb.getBandwidth(); + } + } + public void analyseData(int tick) throws IOException { double delayAverage = 0.0; int nbPacketsSent = 0;