From 4d66106fa62cb047dfbe8f9d22a6975b5a1313f9 Mon Sep 17 00:00:00 2001 From: unknown <david.antunes-da-silva@irit.fr> Date: Wed, 6 Jul 2022 14:44:37 +0200 Subject: [PATCH] Creation of Sleepable interface + some minor fixes --- src/example/philosophes/MainPhilosophe.java | 20 ++--- src/example/randomants/Ant.java | 6 +- src/example/randomants/MainAnt.java | 4 +- src/mas/core/Cyclable.java | 2 +- src/mas/core/Schedulable.java | 8 +- src/mas/core/Sleepable.java | 32 ++++++++ src/mas/core/ThreeStepCyclable.java | 2 +- src/mas/core/TwoStepCyclable.java | 2 +- ...nuosGrid.java => TwoDCContinuousGrid.java} | 12 +-- .../schedulers/AsyncCycling.java | 47 ++++-------- .../schedulers/FairCycling.java | 75 ++++++++----------- .../schedulers/FairPosCycling.java | 21 +++--- .../PausableThreadPoolExecutor.java | 6 +- 13 files changed, 115 insertions(+), 122 deletions(-) create mode 100644 src/mas/core/Sleepable.java rename src/mas/environment/{TwoDContinuosGrid.java => TwoDCContinuousGrid.java} (65%) diff --git a/src/example/philosophes/MainPhilosophe.java b/src/example/philosophes/MainPhilosophe.java index 03fed1c..3f35581 100644 --- a/src/example/philosophes/MainPhilosophe.java +++ b/src/example/philosophes/MainPhilosophe.java @@ -1,7 +1,6 @@ package example.philosophes; import mas.core.Cyclable; -import mas.core.Schedulable; import mas.implementation.schedulers.FairCycling; import java.util.ArrayList; @@ -11,7 +10,7 @@ public class MainPhilosophe { public static void main(String[] args) { - class MyFairCycling extends FairCycling{ + class MyFairCycling extends FairCycling { long startTimeCycle = 0; @@ -37,13 +36,14 @@ public class MainPhilosophe { cycleTime.add(System.nanoTime() - startTimeCycle); } - public String getCycleTime(){ + public double getCycleTime(){ double moyenne = cycleTime.stream().mapToLong(value -> value).average().orElse(0.0); - return "\tCycle moyen : " + moyenne + " nanoseconds"; + return moyenne; } } - int nAgents = 1000; + int nAgents = 100; + int nbCycles = 1000; Philosopher[] philosophers = new Philosopher[nAgents]; Fork[] forks = new Fork[nAgents]; @@ -70,7 +70,7 @@ public class MainPhilosophe { long startTimeCycleSequential = 0; List<Long> cycleTimeSequential = new ArrayList<>(); - while(nbCycle < 1000){ + while(nbCycle < nbCycles){ startTimeCycleSequential = System.nanoTime(); for(Philosopher philosopher : philosophers){ @@ -84,8 +84,8 @@ public class MainPhilosophe { final long endTimeSequential = System.nanoTime(); - System.out.println("Iterative : Total execution time: " + (endTimeSequential / 1000000 - startTimeSequential / 1000000) + " microseconds"); - System.out.println("\tCycle : " + cycleTimeSequential.stream().mapToLong(value -> value).average().orElse(0.0) + " nanoseconds"); + System.out.println("Iterative : Total execution time: " + (endTimeSequential / 1000 - startTimeSequential / 1000) + " microseconds"); + System.out.println("\tCycle : " + cycleTimeSequential.stream().mapToLong(value -> value).average().orElse(0.0) /1000 + " microseconds"); final long startTime = System.nanoTime(); @@ -119,7 +119,7 @@ public class MainPhilosophe { final long endTime = System.nanoTime(); - System.out.println("AMAk : Total execution time: " + (endTime / 1000000 - startTime / 1000000) + " microseconds"); - System.out.println(scheduler.getCycleTime()); + System.out.println("AMAk : Total execution time: " + (endTime / 1000 - startTime / 1000) + " microseconds"); + System.out.println("\tCycle moyen : " + scheduler.getCycleTime() / 1000 + " microseconds"); } } diff --git a/src/example/randomants/Ant.java b/src/example/randomants/Ant.java index 5cbc795..2cd62d3 100644 --- a/src/example/randomants/Ant.java +++ b/src/example/randomants/Ant.java @@ -1,7 +1,7 @@ package example.randomants; import mas.core.Agent; -import mas.environment.TwoDContinuosGrid; +import mas.environment.TwoDCContinuousGrid; import mas.ui.VUI; import mas.ui.drawables.DrawableImage; @@ -37,7 +37,7 @@ public class Ant extends Agent { private double angle = Math.random() * Math.PI * 2; private DrawableImage image; - TwoDContinuosGrid environment; + TwoDCContinuousGrid environment; /** * Constructor of the ant. @@ -51,7 +51,7 @@ public class Ant extends Agent { * @param _environment * environment of the ant */ - public Ant(int _id, double startX, double startY, TwoDContinuosGrid _environment) { + public Ant(int _id, double startX, double startY, TwoDCContinuousGrid _environment) { numberOfAnts++; id = _id; dx = startX; diff --git a/src/example/randomants/MainAnt.java b/src/example/randomants/MainAnt.java index 3944436..2451161 100644 --- a/src/example/randomants/MainAnt.java +++ b/src/example/randomants/MainAnt.java @@ -2,7 +2,7 @@ package example.randomants; import mas.core.Agent; import mas.core.Cyclable; -import mas.environment.TwoDContinuosGrid; +import mas.environment.TwoDCContinuousGrid; import mas.implementation.schedulers.variations.TwoDCycling; import mas.ui.MainWindow; import mas.ui.SchedulerToolbar; @@ -32,7 +32,7 @@ public class MainAnt { AntHill hill = new AntHill(widht, height); - TwoDContinuosGrid env = new TwoDContinuosGrid(widht, height); + TwoDCContinuousGrid env = new TwoDCContinuousGrid(widht, height); int nAgents = 50; diff --git a/src/mas/core/Cyclable.java b/src/mas/core/Cyclable.java index 1d88af6..c5785b3 100644 --- a/src/mas/core/Cyclable.java +++ b/src/mas/core/Cyclable.java @@ -1,7 +1,7 @@ package mas.core; /** - * A cyclable objet. + * A cyclable objet. This objet must be scheduled by a {@link Schedulable}. */ public interface Cyclable { diff --git a/src/mas/core/Schedulable.java b/src/mas/core/Schedulable.java index 1d88b13..acad3b2 100644 --- a/src/mas/core/Schedulable.java +++ b/src/mas/core/Schedulable.java @@ -1,16 +1,10 @@ package mas.core; /** - * A schedulable object. - * + * A schedulable object. Made to code schedulers. */ public interface Schedulable { - /** - * Default sleep time - */ - public static final int DEFAULT_SLEEP = 0; - /** * Launch the scheduler if it is not running. */ diff --git a/src/mas/core/Sleepable.java b/src/mas/core/Sleepable.java new file mode 100644 index 0000000..bbe0a8e --- /dev/null +++ b/src/mas/core/Sleepable.java @@ -0,0 +1,32 @@ +package mas.core; + +/** + * A sleepable object. Useful for schedulers which can have sleep time between cycles. + */ +public interface Sleepable { + + /** + * Default sleep time + */ + int DEFAULT_SLEEP = 0; + + /** + * Getter for the sleep time. + * + * @return the current time elapsed between each system's cycle + */ + int getSleep(); + + /** + * Setter for the sleep time. + * + * @param sleep + * The time between each system's cycle + */ + void setSleep(int sleep); + + /** + * Performs the waiting time between two cycles of the system. + */ + void doSleep(); +} diff --git a/src/mas/core/ThreeStepCyclable.java b/src/mas/core/ThreeStepCyclable.java index e2be1db..5368e8f 100644 --- a/src/mas/core/ThreeStepCyclable.java +++ b/src/mas/core/ThreeStepCyclable.java @@ -10,7 +10,7 @@ public interface ThreeStepCyclable extends Cyclable { perceive(); decide(); act(); - }; + } /** * This method represents the perception phase of the agent. diff --git a/src/mas/core/TwoStepCyclable.java b/src/mas/core/TwoStepCyclable.java index dc6a1da..2b9530c 100644 --- a/src/mas/core/TwoStepCyclable.java +++ b/src/mas/core/TwoStepCyclable.java @@ -9,7 +9,7 @@ public interface TwoStepCyclable extends Cyclable { default void cycle(){ perceive(); decideAndAct(); - }; + } /** * This method represents the perception phase of the agent. diff --git a/src/mas/environment/TwoDContinuosGrid.java b/src/mas/environment/TwoDCContinuousGrid.java similarity index 65% rename from src/mas/environment/TwoDContinuosGrid.java rename to src/mas/environment/TwoDCContinuousGrid.java index 8ae9bde..71a72ea 100644 --- a/src/mas/environment/TwoDContinuosGrid.java +++ b/src/mas/environment/TwoDCContinuousGrid.java @@ -1,22 +1,22 @@ package mas.environment; /** - * A 2 dimensions continuous grid. + * A 2 dimensions continuous grid representing the environment of a MAS. */ -public class TwoDContinuosGrid { +public class TwoDCContinuousGrid { - private int width; - private int height; + private final int width; + private final int height; /** - * Constructor of TwoDContinuosGrid. + * Constructor of TwoDContinuousGrid. * * @param _width * width of the environment * @param _height * height of the environment */ - public TwoDContinuosGrid(int _width, int _height){ + public TwoDCContinuousGrid(int _width, int _height){ height = _height; width = _width; } diff --git a/src/mas/implementation/schedulers/AsyncCycling.java b/src/mas/implementation/schedulers/AsyncCycling.java index 3e0941a..a99e265 100644 --- a/src/mas/implementation/schedulers/AsyncCycling.java +++ b/src/mas/implementation/schedulers/AsyncCycling.java @@ -2,6 +2,7 @@ package mas.implementation.schedulers; import mas.core.Cyclable; import mas.core.Schedulable; +import mas.core.Sleepable; import java.util.LinkedHashSet; import java.util.Set; @@ -12,7 +13,7 @@ import java.util.concurrent.*; * * @author David Antunes */ -public class AsyncCycling implements Schedulable { +public class AsyncCycling implements Schedulable, Sleepable { /** * The cyclable objects handled by the scheduler. @@ -20,7 +21,7 @@ public class AsyncCycling implements Schedulable { protected final Set<Cyclable> cyclables = new LinkedHashSet<>(); /** - * Time between two cycles. Default time in {@link Schedulable#DEFAULT_SLEEP}. + * Time between two cycles. Default time in {@link Sleepable#DEFAULT_SLEEP}. */ private int sleep = DEFAULT_SLEEP; @@ -51,9 +52,7 @@ public class AsyncCycling implements Schedulable { @Override public void start() { for (Cyclable cyclable : cyclables){ - executor.execute(() -> { - manageCyclable(cyclable); - }); + executor.execute(() -> manageCyclable(cyclable)); } } @@ -84,9 +83,7 @@ public class AsyncCycling implements Schedulable { cyclable.setScheduler(this); if(!mustStop){ - executor.execute(() -> { - manageCyclable(cyclable); - }); + executor.execute(() -> manageCyclable(cyclable)); } } @@ -109,25 +106,25 @@ public class AsyncCycling implements Schedulable { } } - /** - * Getter for the sleep time. - * - * @return the current time elapsed between each cyclable's cycle - */ + @Override public int getSleep() { return sleep; } - /** - * Setter for the sleep time. - * - * @param sleep - * The time between each cyclable's cycle - */ + @Override public void setSleep(int sleep) { this.sleep = sleep; } + @Override + public void doSleep(){ + try { + Thread.sleep(sleep); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + /** * Executes a cycle and set up the next cycle if needed. * @@ -145,16 +142,4 @@ public class AsyncCycling implements Schedulable { cyclables.remove(cyclable); } } - - /** - * Performs the waiting time between two cycles. - */ - protected void doSleep(){ - try { - Thread.sleep(sleep); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - } diff --git a/src/mas/implementation/schedulers/FairCycling.java b/src/mas/implementation/schedulers/FairCycling.java index 79d4ae5..f4ad3f9 100644 --- a/src/mas/implementation/schedulers/FairCycling.java +++ b/src/mas/implementation/schedulers/FairCycling.java @@ -2,22 +2,18 @@ package mas.implementation.schedulers; import mas.core.Cyclable; import mas.core.Schedulable; +import mas.core.Sleepable; import java.util.*; import java.util.concurrent.*; -/** - * - * Chaque agent execute exactement 1 cycle pour chaque cycle systeme. - */ - /** * The FairCycling scheduler schedules tasks using a {@link Executors#newCachedThreadPool()}. * Every cyclable executes its cycle once every system's cycle. * * @author David Antunes */ -public class FairCycling implements Schedulable { +public class FairCycling implements Schedulable, Sleepable { /** * The cyclable objects handled by the scheduler. @@ -30,7 +26,7 @@ public class FairCycling implements Schedulable { protected Queue<Cyclable> pendingToAddCyclables = new ConcurrentLinkedQueue<>(); /** - * Time between two cycles. Default time in {@link Schedulable#DEFAULT_SLEEP}. + * Time between two cycles. Default time in {@link Sleepable#DEFAULT_SLEEP}. */ protected int sleep = DEFAULT_SLEEP; @@ -79,7 +75,7 @@ public class FairCycling implements Schedulable { @Override public void start() { - executor.execute(() -> doCycle()); + executor.execute(this::doCycle); } @Override @@ -120,43 +116,45 @@ public class FairCycling implements Schedulable { } } - /** - * This method is called at the end of every system's cycle. - */ - protected void onCycleEnds() { - + @Override + public void addCyclable(Cyclable cyclable){ + cyclable.setScheduler(this); + pendingToAddCyclables.add(cyclable); } - /** - * This method is called at the start of every system's cycle. - */ - protected void onCycleStarts(){ + @Override + public int getSleep() { + return sleep; + } + @Override + public void setSleep(int sleep) { + this.sleep = sleep; } @Override - public void addCyclable(Cyclable cyclable){ - cyclable.setScheduler(this); - pendingToAddCyclables.add(cyclable); + public void doSleep(){ + if (getSleep() != 0) { + try { + Thread.sleep(sleep); + } catch (final InterruptedException e) { + e.printStackTrace(); + } + } } /** - * Getter for the sleep time. - * - * @return the current time elapsed between each system's cycle + * This method is called at the end of every system's cycle. */ - public int getSleep() { - return sleep; + protected void onCycleEnds() { + } /** - * Setter for the sleep time. - * - * @param sleep - * The time between each system's cycle + * This method is called at the start of every system's cycle. */ - public void setSleep(int sleep) { - this.sleep = sleep; + protected void onCycleStarts(){ + } /** @@ -212,7 +210,7 @@ public class FairCycling implements Schedulable { throw new RuntimeException(e); } } - executor.execute(() -> doCycle()); + executor.execute(this::doCycle); } } @@ -225,19 +223,6 @@ public class FairCycling implements Schedulable { pendingToAddCyclables.clear(); } - /** - * Performs the waiting time between two cycles of the system. - */ - protected void doSleep(){ - if (getSleep() != 0) { - try { - Thread.sleep(sleep); - } catch (final InterruptedException e) { - e.printStackTrace(); - } - } - } - /** * Getter for the number of cycles. * diff --git a/src/mas/implementation/schedulers/FairPosCycling.java b/src/mas/implementation/schedulers/FairPosCycling.java index 16d5284..791060c 100644 --- a/src/mas/implementation/schedulers/FairPosCycling.java +++ b/src/mas/implementation/schedulers/FairPosCycling.java @@ -2,11 +2,12 @@ package mas.implementation.schedulers; import mas.core.Cyclable; import mas.core.Schedulable; +import mas.core.Sleepable; /** * Même que fair + equitable sur position des Round Robin */ -public class FairPosCycling implements Schedulable { +public class FairPosCycling implements Schedulable, Sleepable { @Override public void start() { @@ -27,25 +28,21 @@ public class FairPosCycling implements Schedulable { } - /** - * Getter for the sleep time. - * - * @return the current time elapsed between each system's cycle - */ + @Override public int getSleep() { return 0; } - /** - * Setter for the sleep time. - * - * @param sleep - * The time between each system's cycle - */ + @Override public void setSleep(int sleep) { } + @Override + public void doSleep() { + + } + @Override public void addCyclable(Cyclable cyclable) { diff --git a/src/mas/implementation/schedulers/PausableThreadPoolExecutor.java b/src/mas/implementation/schedulers/PausableThreadPoolExecutor.java index 4664ece..b8d041f 100644 --- a/src/mas/implementation/schedulers/PausableThreadPoolExecutor.java +++ b/src/mas/implementation/schedulers/PausableThreadPoolExecutor.java @@ -12,14 +12,14 @@ import java.util.concurrent.locks.ReentrantLock; */ public class PausableThreadPoolExecutor extends ThreadPoolExecutor { private boolean isPaused; - private ReentrantLock lock; - private Condition condition; + private final ReentrantLock lock; + private final Condition condition; /** * {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)} */ public PausableThreadPoolExecutor() { - super(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); + super(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<>()); lock = new ReentrantLock(); condition = lock.newCondition(); } -- GitLab