From b78f9fc6b868f66fe4edb79738812e88553b7d14 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 31 Mar 2023 11:57:12 +0200 Subject: [PATCH 1/6] Cherry-pick plot/main.py in 'faeb49eb32e0c5ac30f68d60aedf435f32edb33a' to 'master' --- plot/main.py | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/plot/main.py b/plot/main.py index 5f91b25..a666898 100644 --- a/plot/main.py +++ b/plot/main.py @@ -8,53 +8,51 @@ nb_files = os.listdir(".." + os.sep + "export") size = len(nb_files) -def mean_mkn() -> np.ndarray: +def mean_mkn(arr: list[tuple[int, np.ndarray]]) -> np.ndarray: averages_mkn = np.empty((size, 2)) nb = 0 - for i in nb_files: - data = pd.read_csv(".." + os.sep + "export" + os.sep + i, delimiter=';').to_numpy() + for nb_users, data in arr: rb = data[:, 4] total = 0.0 for x in rb: total = total + x average = total / len(rb) - nb_users = i.split(".")[0] - averages_mkn[nb, 0] = int(nb_users) + averages_mkn[nb, 0] = nb_users averages_mkn[nb, 1] = average nb += 1 return averages_mkn -def rb_available() -> np.ndarray: +def rb_available(arr: list[tuple[int, np.ndarray]]) -> np.ndarray: available = np.zeros((size, 2)) nb = 0 - for i in nb_files: - data = pd.read_csv(".." + os.sep + "export" + os.sep + i, delimiter=';').to_numpy() - nb_users = i.split(".")[0] - available[nb, 0] = int(nb_users) + for nb_users, data in arr: + available[nb, 0] = nb_users available[nb, 1] = (data.shape[0] / (200 * 10000)) * 100 nb += 1 return available -def delay() -> np.ndarray: +def delay(arr: list[tuple[int, np.ndarray]]) -> np.ndarray: delays = np.zeros((size, 2)) nb = 0 - for i in nb_files: - data = pd.read_csv(".." + os.sep + "export" + os.sep + i, delimiter=';').to_numpy() - nb_users = i.split(".")[0] + for nb_users, data in arr: d = data[:, 5] for x in d: - delays[nb, 0] = int(nb_users) + delays[nb, 0] = nb_users delays[nb, 1] = float(x) nb += 1 return delays -averages = mean_mkn() -available = rb_available() -delays = delay() +np_arr: list[tuple[int, np.ndarray]] = list() +for i in nb_files: + np_arr.append((int(i.split(".")[0]), pd.read_csv(".." + os.sep + "export" + os.sep + i, delimiter=';').to_numpy())) + +averages = mean_mkn(np_arr) +available = rb_available(np_arr) +delays = delay(np_arr) delays.sort(axis=0) # Data for plotting averages.sort(axis=0) @@ -74,9 +72,8 @@ ax.set(xlabel='number of users', ylabel='RB utilisés', title='Pourcentage de RB ax.grid() plt.show() - fig, ax = plt.subplots() ax.scatter(delays[:, 0], delays[:, 1]) ax.set(xlabel='number of users', ylabel='delays(ms)', title='Delay') ax.grid() -plt.show() \ No newline at end of file +plt.show() From be44a1d098a6b3ffb13d432a8393f5a17c3819f4 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 31 Mar 2023 12:04:57 +0200 Subject: [PATCH 2/6] Display plots in one window --- plot/main.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/plot/main.py b/plot/main.py index a666898..8d7118f 100644 --- a/plot/main.py +++ b/plot/main.py @@ -57,23 +57,17 @@ delays.sort(axis=0) # Data for plotting averages.sort(axis=0) -fig, ax = plt.subplots() -ax.scatter(averages[:, 0], averages[:, 1]) +fig, ax = plt.subplots(2, 2) +ax[0, 0].scatter(averages[:, 0], averages[:, 1]) +ax[0, 0].set(xlabel='number of users', ylabel='Efficacité spectrale', title='Efficacité spectrale') +ax[0, 0].grid() -ax.set(xlabel='number of users', ylabel='Efficacité spectrale', title='Efficacité spectrale') -ax.grid() +ax[0, 1].scatter(available[:, 0], available[:, 1]) +ax[0, 1].set(xlabel='number of users', ylabel='RB utilisés', title='Pourcentage de RB utilisés') +ax[0, 1].grid() -# fig.savefig("test.png") -plt.show() +ax[1, 0].scatter(delays[:, 0], delays[:, 1]) +ax[1, 0].set(xlabel='number of users', ylabel='delays(ms)', title='Delay') +ax[1, 0].grid() -fig, ax = plt.subplots() -ax.scatter(available[:, 0], available[:, 1]) -ax.set(xlabel='number of users', ylabel='RB utilisés', title='Pourcentage de RB utilisés') -ax.grid() -plt.show() - -fig, ax = plt.subplots() -ax.scatter(delays[:, 0], delays[:, 1]) -ax.set(xlabel='number of users', ylabel='delays(ms)', title='Delay') -ax.grid() -plt.show() +plt.show() \ No newline at end of file From 83e4ccdd22f4960a3356279ed3db6f9a4161eced Mon Sep 17 00:00:00 2001 From: Tr1xt4n Date: Tue, 4 Apr 2023 08:19:36 +0200 Subject: [PATCH 3/6] +distance --- src/main/java/fr/ntr/AccessPoint.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/fr/ntr/AccessPoint.java b/src/main/java/fr/ntr/AccessPoint.java index 91e85bb..60c37d1 100644 --- a/src/main/java/fr/ntr/AccessPoint.java +++ b/src/main/java/fr/ntr/AccessPoint.java @@ -38,7 +38,7 @@ public class AccessPoint { public void startSimulation(int duration) { try{ this.outputDataFile = new FileOutputStream("export" + File.separator + this.users.size() + ".csv", true); - outputDataFile.write("tick;x;y;user;bandwidth;delay;\n".getBytes()); + outputDataFile.write("tick;x;y;user;bandwidth;delay;distance;\n".getBytes()); } catch(IOException e) { System.err.println(e.getClass().getSimpleName() + " : " + e.getMessage()); System.exit(1); @@ -96,7 +96,7 @@ public class AccessPoint { for(int j = 0; j < frame[i].length; j++) { ResourceBlock ur = frame[i][j]; if(ur.getUser() != null) { - String data = (tick + ";" + i + ";" + j + ";" + this.users.indexOf(ur.getUser()) + ";" + ur.getBandwidth() + ";" + delayAverage +";\n"); + String data = (tick + ";" + i + ";" + j + ";" + this.users.indexOf(ur.getUser()) + ";" + ur.getBandwidth() + ";" + delayAverage +";" + ur.getUser().getDistance() + ";" + "\n"); try{ outputDataFile.write(data.getBytes()); }catch(IOException e){ From af11fa37f3833386a3b92b3c0705db95ee6ae48c Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 4 Apr 2023 08:20:54 +0200 Subject: [PATCH 4/6] Lint in MaxSNR and removed useless condition on Scheduler --- src/main/java/fr/ntr/Main.java | 6 +++--- src/main/java/fr/ntr/scheduler/MaxSNR.java | 21 +++++++++---------- src/main/java/fr/ntr/scheduler/Scheduler.java | 14 ++----------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/main/java/fr/ntr/Main.java b/src/main/java/fr/ntr/Main.java index 68c86ff..bc78260 100644 --- a/src/main/java/fr/ntr/Main.java +++ b/src/main/java/fr/ntr/Main.java @@ -1,6 +1,6 @@ package fr.ntr; -import fr.ntr.scheduler.RoundRobin; +import fr.ntr.scheduler.MaxSNR; import fr.ntr.scheduler.Scheduler; import java.io.File; @@ -48,9 +48,9 @@ public class Main { for(int i = 2; i <= maximumLoad; i += 2) { List users = generateUsers(i, timeSlotNb, subCarrierNb); ResourceBlock[][] frame = new ResourceBlock[timeSlotNb][subCarrierNb]; - // Scheduler scheduler = new MaxSNR(frame, users); + Scheduler scheduler = new MaxSNR(frame, users); // Scheduler scheduler = new ProportionalFair(frame, users); - Scheduler scheduler = new RoundRobin(frame, users); + // Scheduler scheduler = new RoundRobin(frame, users); AccessPoint accessPoint = new AccessPoint(scheduler, frame, users); executor.submit(() -> accessPoint.startSimulation(numberOfTicks)); } diff --git a/src/main/java/fr/ntr/scheduler/MaxSNR.java b/src/main/java/fr/ntr/scheduler/MaxSNR.java index c56f9e5..7530bda 100644 --- a/src/main/java/fr/ntr/scheduler/MaxSNR.java +++ b/src/main/java/fr/ntr/scheduler/MaxSNR.java @@ -1,18 +1,18 @@ package fr.ntr.scheduler; import java.util.List; -import fr.ntr.Packets; +import fr.ntr.AccessPoint; import fr.ntr.ResourceBlock; import fr.ntr.User; public class MaxSNR extends Scheduler { - private List users; + private final List users; - private ResourceBlock[][] frame; + private final ResourceBlock[][] frame; - public MaxSNR( ResourceBlock[][] frame, List users) { + public MaxSNR(ResourceBlock[][] frame, List users) { this.frame = frame; this.users = users; } @@ -20,8 +20,8 @@ public class MaxSNR extends Scheduler { @Override public void scheduling(int ticks) { User userMax; - for(int ts = 0; ts < 2; ts++){ - for(int sp = 0; sp < 100; sp++){ + for(int ts = 0; ts < AccessPoint.getTimeSlotNb(); ts++){ + for(int sp = 0; sp < AccessPoint.getSubCarrierNb(); sp++){ userMax = userSelection(ts, sp); allocateRessource(userMax, frame, ts, sp, ticks); } @@ -36,11 +36,10 @@ public class MaxSNR extends Scheduler { double maxSnr = 0.0; User userMax = null; for(User u: users) { - if (!u.getPacketsToSend().isEmpty()) { - if (maxSnr < u.getBandwidthTable()[ts][sp]) { - maxSnr = u.getBandwidthTable()[ts][sp]; - userMax = u; - } + if (!u.getPacketsToSend().isEmpty() && (maxSnr < u.getBandwidthTable()[ts][sp])) { + maxSnr = u.getBandwidthTable()[ts][sp]; + userMax = u; + } } return userMax; diff --git a/src/main/java/fr/ntr/scheduler/Scheduler.java b/src/main/java/fr/ntr/scheduler/Scheduler.java index 29e2fe6..e3cb738 100644 --- a/src/main/java/fr/ntr/scheduler/Scheduler.java +++ b/src/main/java/fr/ntr/scheduler/Scheduler.java @@ -16,18 +16,8 @@ public abstract class Scheduler { protected void allocateRessource(User userMax, ResourceBlock[][] frame, int ts, int sp, int ticks) { if (userMax != null && !userMax.getPacketsToSend().isEmpty()) { Packets p = userMax.getPacketsToSend().get(userMax.getPacketsToSend().size()-1); - if(p.getBitsNumberRemaining() >= 0){ - 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); - } - } else { + p.decreaseBitsNumberRemaining((int) userMax.getBandwidthTable()[ts][sp]); + if(p.getBitsNumberRemaining() <= 0) { if(ticks == 0){ p.setDurationSending(1); }else { From 05d51acdc0a494802955dd5a2d2459f13330a16e Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 4 Apr 2023 08:57:40 +0200 Subject: [PATCH 5/6] Remove try-with-resources to make it work with jdk 1.8 --- src/main/java/fr/ntr/Main.java | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/fr/ntr/Main.java b/src/main/java/fr/ntr/Main.java index bc78260..407ebfe 100644 --- a/src/main/java/fr/ntr/Main.java +++ b/src/main/java/fr/ntr/Main.java @@ -10,6 +10,7 @@ import java.util.List; import java.util.Objects; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; public class Main { @@ -44,16 +45,23 @@ public class Main { }); AccessPoint.setTimeSlotNb(timeSlotNb); AccessPoint.setSubCarrierNb(subCarrierNb); - try(ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())) { - for(int i = 2; i <= maximumLoad; i += 2) { - List users = generateUsers(i, timeSlotNb, subCarrierNb); - ResourceBlock[][] frame = new ResourceBlock[timeSlotNb][subCarrierNb]; - Scheduler scheduler = new MaxSNR(frame, users); - // Scheduler scheduler = new ProportionalFair(frame, users); - // Scheduler scheduler = new RoundRobin(frame, users); - AccessPoint accessPoint = new AccessPoint(scheduler, frame, users); - executor.submit(() -> accessPoint.startSimulation(numberOfTicks)); + ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + for(int i = 2; i <= maximumLoad; i += 2) { + List users = generateUsers(i, timeSlotNb, subCarrierNb); + ResourceBlock[][] frame = new ResourceBlock[timeSlotNb][subCarrierNb]; + Scheduler scheduler = new MaxSNR(frame, users); + // Scheduler scheduler = new ProportionalFair(frame, users); + // Scheduler scheduler = new RoundRobin(frame, users); + AccessPoint accessPoint = new AccessPoint(scheduler, frame, users); + executor.submit(() -> accessPoint.startSimulation(numberOfTicks)); + } + executor.shutdown(); + try { + if(!executor.awaitTermination(2, TimeUnit.MINUTES)) { + executor.shutdownNow(); } + } catch (InterruptedException e) { + e.printStackTrace(); } } else { System.err.println("Please give launch arguments"); From b073d58f21bee2ddb83d4e40cd02731155ab752f Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 4 Apr 2023 09:27:06 +0200 Subject: [PATCH 6/6] Improve pf performances --- src/main/java/fr/ntr/Main.java | 5 +- .../fr/ntr/scheduler/ProportionalFair.java | 52 ++++++++----------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/main/java/fr/ntr/Main.java b/src/main/java/fr/ntr/Main.java index 407ebfe..8431425 100644 --- a/src/main/java/fr/ntr/Main.java +++ b/src/main/java/fr/ntr/Main.java @@ -1,6 +1,7 @@ package fr.ntr; import fr.ntr.scheduler.MaxSNR; +import fr.ntr.scheduler.ProportionalFair; import fr.ntr.scheduler.Scheduler; import java.io.File; @@ -49,8 +50,8 @@ public class Main { for(int i = 2; i <= maximumLoad; i += 2) { List users = generateUsers(i, timeSlotNb, subCarrierNb); ResourceBlock[][] frame = new ResourceBlock[timeSlotNb][subCarrierNb]; - Scheduler scheduler = new MaxSNR(frame, users); - // Scheduler scheduler = new ProportionalFair(frame, users); + // Scheduler scheduler = new MaxSNR(frame, users); + Scheduler scheduler = new ProportionalFair(frame, users); // Scheduler scheduler = new RoundRobin(frame, users); AccessPoint accessPoint = new AccessPoint(scheduler, frame, users); executor.submit(() -> accessPoint.startSimulation(numberOfTicks)); diff --git a/src/main/java/fr/ntr/scheduler/ProportionalFair.java b/src/main/java/fr/ntr/scheduler/ProportionalFair.java index 94e9403..6b33d51 100644 --- a/src/main/java/fr/ntr/scheduler/ProportionalFair.java +++ b/src/main/java/fr/ntr/scheduler/ProportionalFair.java @@ -3,57 +3,51 @@ package fr.ntr.scheduler; import java.util.ArrayList; import java.util.List; +import fr.ntr.AccessPoint; import fr.ntr.ResourceBlock; import fr.ntr.User; public class ProportionalFair extends Scheduler { + private final List users; - private List users; - - private ResourceBlock[][] frame; + private final ResourceBlock[][] frame; public ProportionalFair(ResourceBlock[][] frame, List users) { this.frame = frame; this.users = users; - } - + } + @Override public void scheduling(int ticks) { - ArrayList averageBandwiths = new ArrayList(); - for(User u : users){ - double avg = 0d; - double[][] bandwidthTable = u.getBandwidthTable(); - for(int i = 0; i < bandwidthTable.length; i++){ - for(int j = 0; j < bandwidthTable[i].length; j++){ - avg += bandwidthTable[i][j]; - } - } - 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); + for(int ts = 0; ts < AccessPoint.getTimeSlotNb(); ts++){ + for(int sp = 0; sp < AccessPoint.getSubCarrierNb(); sp++){ + User selectedUser = userSelection(ts, sp, users); allocateRessource(selectedUser, frame, ts, sp, ticks); } } } - - 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); + for (User u : users) { + double avg = 0d; + double[][] bandwidthTable = u.getBandwidthTable(); + for (double[] doubles : bandwidthTable) { + for (double aDouble : doubles) { + avg += aDouble; + } + } + avg = avg / (bandwidthTable.length * bandwidthTable[0].length); double mkn = u.getBandwidthTable()[ts][sp]; - double averageMkn = averageBandwiths.get(i); - double pf = mkn / averageMkn; - if (PF < pf){ + double pf = mkn / avg; + if (PF < pf) { PF = pf; selectedUser = u; } } return selectedUser; - } - + } + }