Skip to content
Snippets Groups Projects
Commit 4d66106f authored by unknown's avatar unknown
Browse files

Creation of Sleepable interface + some minor fixes

parent 5e6d6605
No related branches found
No related tags found
No related merge requests found
Showing with 115 additions and 122 deletions
package example.philosophes; package example.philosophes;
import mas.core.Cyclable; import mas.core.Cyclable;
import mas.core.Schedulable;
import mas.implementation.schedulers.FairCycling; import mas.implementation.schedulers.FairCycling;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -11,7 +10,7 @@ public class MainPhilosophe { ...@@ -11,7 +10,7 @@ public class MainPhilosophe {
public static void main(String[] args) { public static void main(String[] args) {
class MyFairCycling extends FairCycling{ class MyFairCycling extends FairCycling {
long startTimeCycle = 0; long startTimeCycle = 0;
...@@ -37,13 +36,14 @@ public class MainPhilosophe { ...@@ -37,13 +36,14 @@ public class MainPhilosophe {
cycleTime.add(System.nanoTime() - startTimeCycle); cycleTime.add(System.nanoTime() - startTimeCycle);
} }
public String getCycleTime(){ public double getCycleTime(){
double moyenne = cycleTime.stream().mapToLong(value -> value).average().orElse(0.0); 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]; Philosopher[] philosophers = new Philosopher[nAgents];
Fork[] forks = new Fork[nAgents]; Fork[] forks = new Fork[nAgents];
...@@ -70,7 +70,7 @@ public class MainPhilosophe { ...@@ -70,7 +70,7 @@ public class MainPhilosophe {
long startTimeCycleSequential = 0; long startTimeCycleSequential = 0;
List<Long> cycleTimeSequential = new ArrayList<>(); List<Long> cycleTimeSequential = new ArrayList<>();
while(nbCycle < 1000){ while(nbCycle < nbCycles){
startTimeCycleSequential = System.nanoTime(); startTimeCycleSequential = System.nanoTime();
for(Philosopher philosopher : philosophers){ for(Philosopher philosopher : philosophers){
...@@ -84,8 +84,8 @@ public class MainPhilosophe { ...@@ -84,8 +84,8 @@ public class MainPhilosophe {
final long endTimeSequential = System.nanoTime(); final long endTimeSequential = System.nanoTime();
System.out.println("Iterative : Total execution time: " + (endTimeSequential / 1000000 - startTimeSequential / 1000000) + " microseconds"); 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) + " nanoseconds"); System.out.println("\tCycle : " + cycleTimeSequential.stream().mapToLong(value -> value).average().orElse(0.0) /1000 + " microseconds");
final long startTime = System.nanoTime(); final long startTime = System.nanoTime();
...@@ -119,7 +119,7 @@ public class MainPhilosophe { ...@@ -119,7 +119,7 @@ public class MainPhilosophe {
final long endTime = System.nanoTime(); final long endTime = System.nanoTime();
System.out.println("AMAk : Total execution time: " + (endTime / 1000000 - startTime / 1000000) + " microseconds"); System.out.println("AMAk : Total execution time: " + (endTime / 1000 - startTime / 1000) + " microseconds");
System.out.println(scheduler.getCycleTime()); System.out.println("\tCycle moyen : " + scheduler.getCycleTime() / 1000 + " microseconds");
} }
} }
package example.randomants; package example.randomants;
import mas.core.Agent; import mas.core.Agent;
import mas.environment.TwoDContinuosGrid; import mas.environment.TwoDCContinuousGrid;
import mas.ui.VUI; import mas.ui.VUI;
import mas.ui.drawables.DrawableImage; import mas.ui.drawables.DrawableImage;
...@@ -37,7 +37,7 @@ public class Ant extends Agent { ...@@ -37,7 +37,7 @@ public class Ant extends Agent {
private double angle = Math.random() * Math.PI * 2; private double angle = Math.random() * Math.PI * 2;
private DrawableImage image; private DrawableImage image;
TwoDContinuosGrid environment; TwoDCContinuousGrid environment;
/** /**
* Constructor of the ant. * Constructor of the ant.
...@@ -51,7 +51,7 @@ public class Ant extends Agent { ...@@ -51,7 +51,7 @@ public class Ant extends Agent {
* @param _environment * @param _environment
* environment of the ant * 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++; numberOfAnts++;
id = _id; id = _id;
dx = startX; dx = startX;
......
...@@ -2,7 +2,7 @@ package example.randomants; ...@@ -2,7 +2,7 @@ package example.randomants;
import mas.core.Agent; import mas.core.Agent;
import mas.core.Cyclable; import mas.core.Cyclable;
import mas.environment.TwoDContinuosGrid; import mas.environment.TwoDCContinuousGrid;
import mas.implementation.schedulers.variations.TwoDCycling; import mas.implementation.schedulers.variations.TwoDCycling;
import mas.ui.MainWindow; import mas.ui.MainWindow;
import mas.ui.SchedulerToolbar; import mas.ui.SchedulerToolbar;
...@@ -32,7 +32,7 @@ public class MainAnt { ...@@ -32,7 +32,7 @@ public class MainAnt {
AntHill hill = new AntHill(widht, height); AntHill hill = new AntHill(widht, height);
TwoDContinuosGrid env = new TwoDContinuosGrid(widht, height); TwoDCContinuousGrid env = new TwoDCContinuousGrid(widht, height);
int nAgents = 50; int nAgents = 50;
......
package mas.core; package mas.core;
/** /**
* A cyclable objet. * A cyclable objet. This objet must be scheduled by a {@link Schedulable}.
*/ */
public interface Cyclable { public interface Cyclable {
......
package mas.core; package mas.core;
/** /**
* A schedulable object. * A schedulable object. Made to code schedulers.
*
*/ */
public interface Schedulable { public interface Schedulable {
/**
* Default sleep time
*/
public static final int DEFAULT_SLEEP = 0;
/** /**
* Launch the scheduler if it is not running. * Launch the scheduler if it is not running.
*/ */
......
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();
}
...@@ -10,7 +10,7 @@ public interface ThreeStepCyclable extends Cyclable { ...@@ -10,7 +10,7 @@ public interface ThreeStepCyclable extends Cyclable {
perceive(); perceive();
decide(); decide();
act(); act();
}; }
/** /**
* This method represents the perception phase of the agent. * This method represents the perception phase of the agent.
......
...@@ -9,7 +9,7 @@ public interface TwoStepCyclable extends Cyclable { ...@@ -9,7 +9,7 @@ public interface TwoStepCyclable extends Cyclable {
default void cycle(){ default void cycle(){
perceive(); perceive();
decideAndAct(); decideAndAct();
}; }
/** /**
* This method represents the perception phase of the agent. * This method represents the perception phase of the agent.
......
package mas.environment; 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 final int width;
private int height; private final int height;
/** /**
* Constructor of TwoDContinuosGrid. * Constructor of TwoDContinuousGrid.
* *
* @param _width * @param _width
* width of the environment * width of the environment
* @param _height * @param _height
* height of the environment * height of the environment
*/ */
public TwoDContinuosGrid(int _width, int _height){ public TwoDCContinuousGrid(int _width, int _height){
height = _height; height = _height;
width = _width; width = _width;
} }
......
...@@ -2,6 +2,7 @@ package mas.implementation.schedulers; ...@@ -2,6 +2,7 @@ package mas.implementation.schedulers;
import mas.core.Cyclable; import mas.core.Cyclable;
import mas.core.Schedulable; import mas.core.Schedulable;
import mas.core.Sleepable;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
...@@ -12,7 +13,7 @@ import java.util.concurrent.*; ...@@ -12,7 +13,7 @@ import java.util.concurrent.*;
* *
* @author David Antunes * @author David Antunes
*/ */
public class AsyncCycling implements Schedulable { public class AsyncCycling implements Schedulable, Sleepable {
/** /**
* The cyclable objects handled by the scheduler. * The cyclable objects handled by the scheduler.
...@@ -20,7 +21,7 @@ public class AsyncCycling implements Schedulable { ...@@ -20,7 +21,7 @@ public class AsyncCycling implements Schedulable {
protected final Set<Cyclable> cyclables = new LinkedHashSet<>(); 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; private int sleep = DEFAULT_SLEEP;
...@@ -51,9 +52,7 @@ public class AsyncCycling implements Schedulable { ...@@ -51,9 +52,7 @@ public class AsyncCycling implements Schedulable {
@Override @Override
public void start() { public void start() {
for (Cyclable cyclable : cyclables){ for (Cyclable cyclable : cyclables){
executor.execute(() -> { executor.execute(() -> manageCyclable(cyclable));
manageCyclable(cyclable);
});
} }
} }
...@@ -84,9 +83,7 @@ public class AsyncCycling implements Schedulable { ...@@ -84,9 +83,7 @@ public class AsyncCycling implements Schedulable {
cyclable.setScheduler(this); cyclable.setScheduler(this);
if(!mustStop){ if(!mustStop){
executor.execute(() -> { executor.execute(() -> manageCyclable(cyclable));
manageCyclable(cyclable);
});
} }
} }
...@@ -109,25 +106,25 @@ public class AsyncCycling implements Schedulable { ...@@ -109,25 +106,25 @@ public class AsyncCycling implements Schedulable {
} }
} }
/** @Override
* Getter for the sleep time.
*
* @return the current time elapsed between each cyclable's cycle
*/
public int getSleep() { public int getSleep() {
return sleep; return sleep;
} }
/** @Override
* Setter for the sleep time.
*
* @param sleep
* The time between each cyclable's cycle
*/
public void setSleep(int sleep) { public void setSleep(int sleep) {
this.sleep = 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. * Executes a cycle and set up the next cycle if needed.
* *
...@@ -145,16 +142,4 @@ public class AsyncCycling implements Schedulable { ...@@ -145,16 +142,4 @@ public class AsyncCycling implements Schedulable {
cyclables.remove(cyclable); cyclables.remove(cyclable);
} }
} }
/**
* Performs the waiting time between two cycles.
*/
protected void doSleep(){
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
} }
...@@ -2,22 +2,18 @@ package mas.implementation.schedulers; ...@@ -2,22 +2,18 @@ package mas.implementation.schedulers;
import mas.core.Cyclable; import mas.core.Cyclable;
import mas.core.Schedulable; import mas.core.Schedulable;
import mas.core.Sleepable;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
/**
*
* Chaque agent execute exactement 1 cycle pour chaque cycle systeme.
*/
/** /**
* The FairCycling scheduler schedules tasks using a {@link Executors#newCachedThreadPool()}. * The FairCycling scheduler schedules tasks using a {@link Executors#newCachedThreadPool()}.
* Every cyclable executes its cycle once every system's cycle. * Every cyclable executes its cycle once every system's cycle.
* *
* @author David Antunes * @author David Antunes
*/ */
public class FairCycling implements Schedulable { public class FairCycling implements Schedulable, Sleepable {
/** /**
* The cyclable objects handled by the scheduler. * The cyclable objects handled by the scheduler.
...@@ -30,7 +26,7 @@ public class FairCycling implements Schedulable { ...@@ -30,7 +26,7 @@ public class FairCycling implements Schedulable {
protected Queue<Cyclable> pendingToAddCyclables = new ConcurrentLinkedQueue<>(); 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; protected int sleep = DEFAULT_SLEEP;
...@@ -79,7 +75,7 @@ public class FairCycling implements Schedulable { ...@@ -79,7 +75,7 @@ public class FairCycling implements Schedulable {
@Override @Override
public void start() { public void start() {
executor.execute(() -> doCycle()); executor.execute(this::doCycle);
} }
@Override @Override
...@@ -120,43 +116,45 @@ public class FairCycling implements Schedulable { ...@@ -120,43 +116,45 @@ public class FairCycling implements Schedulable {
} }
} }
/** @Override
* This method is called at the end of every system's cycle. public void addCyclable(Cyclable cyclable){
*/ cyclable.setScheduler(this);
protected void onCycleEnds() { pendingToAddCyclables.add(cyclable);
} }
/** @Override
* This method is called at the start of every system's cycle. public int getSleep() {
*/ return sleep;
protected void onCycleStarts(){ }
@Override
public void setSleep(int sleep) {
this.sleep = sleep;
} }
@Override @Override
public void addCyclable(Cyclable cyclable){ public void doSleep(){
cyclable.setScheduler(this); if (getSleep() != 0) {
pendingToAddCyclables.add(cyclable); try {
Thread.sleep(sleep);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
} }
/** /**
* Getter for the sleep time. * This method is called at the end of every system's cycle.
*
* @return the current time elapsed between each system's cycle
*/ */
public int getSleep() { protected void onCycleEnds() {
return sleep;
} }
/** /**
* Setter for the sleep time. * This method is called at the start of every system's cycle.
*
* @param sleep
* The time between each system's cycle
*/ */
public void setSleep(int sleep) { protected void onCycleStarts(){
this.sleep = sleep;
} }
/** /**
...@@ -212,7 +210,7 @@ public class FairCycling implements Schedulable { ...@@ -212,7 +210,7 @@ public class FairCycling implements Schedulable {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
executor.execute(() -> doCycle()); executor.execute(this::doCycle);
} }
} }
...@@ -225,19 +223,6 @@ public class FairCycling implements Schedulable { ...@@ -225,19 +223,6 @@ public class FairCycling implements Schedulable {
pendingToAddCyclables.clear(); 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. * Getter for the number of cycles.
* *
......
...@@ -2,11 +2,12 @@ package mas.implementation.schedulers; ...@@ -2,11 +2,12 @@ package mas.implementation.schedulers;
import mas.core.Cyclable; import mas.core.Cyclable;
import mas.core.Schedulable; import mas.core.Schedulable;
import mas.core.Sleepable;
/** /**
* Même que fair + equitable sur position des Round Robin * Même que fair + equitable sur position des Round Robin
*/ */
public class FairPosCycling implements Schedulable { public class FairPosCycling implements Schedulable, Sleepable {
@Override @Override
public void start() { public void start() {
...@@ -27,25 +28,21 @@ public class FairPosCycling implements Schedulable { ...@@ -27,25 +28,21 @@ public class FairPosCycling implements Schedulable {
} }
/** @Override
* Getter for the sleep time.
*
* @return the current time elapsed between each system's cycle
*/
public int getSleep() { public int getSleep() {
return 0; return 0;
} }
/** @Override
* Setter for the sleep time.
*
* @param sleep
* The time between each system's cycle
*/
public void setSleep(int sleep) { public void setSleep(int sleep) {
} }
@Override
public void doSleep() {
}
@Override @Override
public void addCyclable(Cyclable cyclable) { public void addCyclable(Cyclable cyclable) {
......
...@@ -12,14 +12,14 @@ import java.util.concurrent.locks.ReentrantLock; ...@@ -12,14 +12,14 @@ import java.util.concurrent.locks.ReentrantLock;
*/ */
public class PausableThreadPoolExecutor extends ThreadPoolExecutor { public class PausableThreadPoolExecutor extends ThreadPoolExecutor {
private boolean isPaused; private boolean isPaused;
private ReentrantLock lock; private final ReentrantLock lock;
private Condition condition; private final Condition condition;
/** /**
* {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)} * {@link ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue)}
*/ */
public PausableThreadPoolExecutor() { 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(); lock = new ReentrantLock();
condition = lock.newCondition(); condition = lock.newCondition();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment