From aa9ea15beb86c5e062a5dd429e9a397c3863fe64 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 30 Mar 2023 08:24:25 +0200 Subject: [PATCH] Fix RR by removing random selection of the first user --- plot/main.py | 2 ++ src/main/java/fr/ntr/AccessPoint.java | 14 +------- src/main/java/fr/ntr/User.java | 9 +++--- .../java/fr/ntr/scheduler/RoundRobin.java | 32 +++++++------------ 4 files changed, 19 insertions(+), 38 deletions(-) diff --git a/plot/main.py b/plot/main.py index 656bea7..69e3350 100644 --- a/plot/main.py +++ b/plot/main.py @@ -37,6 +37,7 @@ def rb_available() -> np.ndarray: nb += 1 return available + def delay() -> np.ndarray: delays = np.zeros((size, 2)) nb = 0 @@ -51,6 +52,7 @@ def delay() -> np.ndarray: nb += 1 return delays + averages = mean_mkn() available = rb_available() delays = delay() diff --git a/src/main/java/fr/ntr/AccessPoint.java b/src/main/java/fr/ntr/AccessPoint.java index dd75f79..748bc25 100644 --- a/src/main/java/fr/ntr/AccessPoint.java +++ b/src/main/java/fr/ntr/AccessPoint.java @@ -69,7 +69,6 @@ public class AccessPoint { reset(); updateBandwidth(ticks); schedule(ticks); - computePacketDelay(ticks); // traite les données et les enregistre dans un fichier try { analyseData(ticks); @@ -91,7 +90,7 @@ public class AccessPoint { // On régénère les sources toutes les 0-50 ms if(ticks % timeInterval == 0){ - timeInterval = 50 + random.nextInt(51); + timeInterval = Math.max(1, random.nextInt(51)); n = user.createPackets(n, ticks); } } @@ -138,17 +137,6 @@ public class AccessPoint { } } - private void computePacketDelay(int ticks){ - for(int i = 0; i < this.users.size(); i++){ - User u = this.users.get(i); - if(!u.getPacketsToSend().isEmpty()){ - for(Packets p : u.getPacketsToSend()){ - - } - } - } - } - public ResourceBlock[][] getFrame() { return frame; } diff --git a/src/main/java/fr/ntr/User.java b/src/main/java/fr/ntr/User.java index 2096cf5..0d41e13 100644 --- a/src/main/java/fr/ntr/User.java +++ b/src/main/java/fr/ntr/User.java @@ -12,7 +12,7 @@ public class User { private final List packetsSent; private int leftForNextSource; - private Random random = new Random(); + private final Random random = new Random(); public User(double distance, int timeSlotNb, int subCarrierNb) { this.distance = distance; @@ -25,10 +25,10 @@ public class User { public void generateBandwidth() { for(int y = 0; y < bandwidthTable[0].length; y++) { - double random = this.random.nextDouble(); + double rand = this.random.nextDouble(); for(int x = 0; x < bandwidthTable.length; x++) { - double h = 1 * Math.sqrt(-2 * Math.log(1 - random)); - double gain = h * h * Math.pow(10, random * 1/10) * Math.pow(1 / this.distance, 3.5); + double h = 1 * Math.sqrt(-2 * Math.log(1 - rand)); + double gain = h * h * Math.pow(10, rand * 1/10) * Math.pow(1 / this.distance, 3.5); double spectralEfficiency = (20 * gain) / (15000 * (3.9*Math.pow(10, -21))); double mkn = Math.log(1 + spectralEfficiency) / Math.log(2); this.bandwidthTable[x][y] = mkn; @@ -43,7 +43,6 @@ public class User { * @return */ public int createPackets(int m, int ticks) { - Random random = new Random(); // On tire un nombre entre 0 et 2 * m int mbis = m == 0 ? 0 : random.nextInt(2 * (m + this.leftForNextSource)); // On calcule le nombre de paquets qu'on peut transmettre diff --git a/src/main/java/fr/ntr/scheduler/RoundRobin.java b/src/main/java/fr/ntr/scheduler/RoundRobin.java index 91d916c..1605d91 100644 --- a/src/main/java/fr/ntr/scheduler/RoundRobin.java +++ b/src/main/java/fr/ntr/scheduler/RoundRobin.java @@ -1,6 +1,7 @@ package fr.ntr.scheduler; import java.util.List; import java.util.Random; +import java.util.stream.Collectors; import fr.ntr.AccessPoint; import fr.ntr.ResourceBlock; @@ -9,17 +10,12 @@ import fr.ntr.User; public class RoundRobin extends Scheduler { - private final Random random; - private int index; - private final List users; private final ResourceBlock[][] frame; public RoundRobin(ResourceBlock[][] frame, List users) { - this.index = 0; this.frame = frame; this.users = users; - this.random = new Random(); } /** @@ -29,15 +25,18 @@ public class RoundRobin extends Scheduler { */ @Override public void scheduling(int ticks) { - //selection aléatoire du premier utilisateur - - index = random.nextInt(users.size()); + List userCopy = users.stream().filter(u -> !u.getPacketsToSend().isEmpty()).collect(Collectors.toList()); //Pour chaque time slot et sous porteuses - for (int ts = 0; ts < AccessPoint.getTimeSlotNb(); ts++) { + loop: for (int ts = 0; ts < AccessPoint.getTimeSlotNb(); ts++) { for(int sp = 0; sp < AccessPoint.getSubCarrierNb(); sp++) { - User userSelected = userSelection(); + if(userCopy.isEmpty()) { + break loop; + } + User userSelected = userSelection(userCopy); allocateRessource(userSelected, frame, ts, sp, ticks); + if(!userSelected.getPacketsToSend().isEmpty()) + userCopy.add(userSelected); } } } @@ -46,16 +45,9 @@ public class RoundRobin extends Scheduler { * Entry Time slot (int), Sous porteuse(int), and users ( List) * Return the user in function of TS and SP selected * - * @return the next user which have something to send, null otherwise + * @return the next user which have something to send */ - private User userSelection() { - for(int i = index; i < index + users.size(); i++) { - User user = users.get(index % (users.size())); - if(!user.getPacketsToSend().isEmpty()) { - index = i; - return user; - } - } - return null; + private User userSelection(List users) { + return users.remove(0); } }