diff --git a/src/example/philosophes/Fork.java b/src/example/philosophes/Fork.java index 3e150ea5c2f1723c9276a0afb1a1bcb9d1ce38e0..8d09fc233b71952ed826a3c08fcf8540bfb70155 100644 --- a/src/example/philosophes/Fork.java +++ b/src/example/philosophes/Fork.java @@ -2,9 +2,9 @@ package example.philosophes; public class Fork { - private Philosophe takenBy; + private Philosopher takenBy; - public synchronized boolean tryTake(Philosophe asker){ + public synchronized boolean tryTake(Philosopher asker){ if(takenBy != null){ return false; } @@ -12,19 +12,19 @@ public class Fork { return true; } - public synchronized void release(Philosophe asker){ + public synchronized void release(Philosopher asker){ if(takenBy == asker){ takenBy = null; } } - public synchronized boolean owned(Philosophe asker){ + public synchronized boolean owned(Philosopher asker){ return takenBy == asker; } - public Philosophe getTakenBy() { + public Philosopher getTakenBy() { if (takenBy == null) - return new Philosophe(999, null, null, null, null); + return new Philosopher(999, null, null, null, null); return takenBy; } } diff --git a/src/example/philosophes/MainPhilosophe.java b/src/example/philosophes/MainPhilosophe.java index 83acbcb244e4adaea6edc4b64fff998d421c0ff9..d70319a581b801b6a18bb8e2fd3cdba470686327 100644 --- a/src/example/philosophes/MainPhilosophe.java +++ b/src/example/philosophes/MainPhilosophe.java @@ -11,7 +11,7 @@ public class MainPhilosophe { int nAgents = 6; - Philosophe[] philosophes = new Philosophe[nAgents]; + Philosopher[] philosophers = new Philosopher[nAgents]; Fork[] forks = new Fork[nAgents]; for (int i = 0; i<nAgents ; i++){ @@ -19,15 +19,15 @@ public class MainPhilosophe { } for(int i = 0; i<nAgents ; i++){ - philosophes[i] = new Philosophe(i, forks[(((i-1) % nAgents) + nAgents) % nAgents], forks[i], null, null); + philosophers[i] = new Philosopher(i, forks[(((i-1) % nAgents) + nAgents) % nAgents], forks[i], null, null); } for (int i = 0; i<nAgents ; i++){ - philosophes[i].setLeftPhilosophe(philosophes[(((i-1) % nAgents) + nAgents) % nAgents]); - philosophes[i].setRightPhilosophe(philosophes[(i+1) % nAgents]); + philosophers[i].setLeftPhilosopher(philosophers[(((i-1) % nAgents) + nAgents) % nAgents]); + philosophers[i].setRightPhilosopher(philosophers[(i+1) % nAgents]); } - Schedulable scheduler = new FairCycling(philosophes); + Schedulable scheduler = new FairCycling(philosophers); scheduler.setSleep(2); scheduler.start(); diff --git a/src/example/philosophes/Philosophe.java b/src/example/philosophes/Philosopher.java similarity index 67% rename from src/example/philosophes/Philosophe.java rename to src/example/philosophes/Philosopher.java index 8e69a2f883dfa593621e42f48b0c0d2b2c82aeef..d683258d2dd1275c52af528c7b260f0593f1deb0 100644 --- a/src/example/philosophes/Philosophe.java +++ b/src/example/philosophes/Philosopher.java @@ -3,19 +3,17 @@ package example.philosophes; import mas.core.Agent; import mas.core.Schedulable; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; +import java.util.*; -public class Philosophe extends Agent { +public class Philosopher extends Agent { private Fork leftFork; private Fork rightFork; - private Philosophe rightPhilosophe; + private Philosopher rightPhilosopher; - private Philosophe leftPhilosophe; + private Philosopher leftPhilosopher; /** * The amount of time (in cycle) the philosopher haven't ate (while in state @@ -57,12 +55,12 @@ public class Philosophe extends Agent { private double criticallity; - public Philosophe(int _id, Fork _leftFork, Fork _rightFork, Philosophe _rightPhilosophe, Philosophe _leftPhilosophe) { + public Philosopher(int _id, Fork _leftFork, Fork _rightFork, Philosopher _rightPhilosopher, Philosopher _leftPhilosopher) { id = _id; leftFork = _leftFork; rightFork = _rightFork; - rightPhilosophe = _rightPhilosophe; - leftPhilosophe = _leftPhilosophe; + rightPhilosopher = _rightPhilosopher; + leftPhilosopher = _leftPhilosopher; } @Override @@ -124,16 +122,22 @@ public class Philosophe extends Agent { ); } - private Philosophe getMostCriticalNeighbor() { - if(leftPhilosophe.getCriticallity() < rightPhilosophe.getCriticallity()){ - if (rightPhilosophe.getCriticallity() < this.getCriticallity()) - return this; - return rightPhilosophe; - } else { - if (leftPhilosophe.getCriticallity() < this.getCriticallity()) - return this; - return leftPhilosophe; + private Philosopher getMostCriticalNeighbor() { + List<Philosopher> criticalest = new ArrayList<>(); + double maxCriticality = getHungerDuration(); + + criticalest.add(this); + + for(Map.Entry<Philosopher,Double> e : getNeighborsCriticallity()){ + if(e.getValue() > maxCriticality){ + criticalest.clear(); + maxCriticality = e.getValue(); + criticalest.add(e.getKey()); + } else if(e.getValue() == maxCriticality){ + criticalest.add(e.getKey()); + } } + return criticalest.get(new Random().nextInt(criticalest.size())); } private double computeCriticallity(){ @@ -147,7 +151,7 @@ public class Philosophe extends Agent { @Override public void act() { //System.out.println("Philosopher num " + id + " act"); - scheduler.addCyclable(new Dechets(id)); + scheduler.addCyclable(new Waste(id)); } @Override @@ -155,6 +159,18 @@ public class Philosophe extends Agent { return false; } + public Set<Map.Entry<Philosopher,Double>> getNeighborsCriticallity(){ + Map<Philosopher, Double> criticalities = new HashMap<>(); + + Philosopher leftPhilosopher = getLeftPhilosopher(); + Philosopher rightPhilosopher = getRightPhilosopher(); + + criticalities.put(leftPhilosopher,leftPhilosopher.getCriticallity()); + criticalities.put(rightPhilosopher, rightPhilosopher.getCriticallity()); + + return criticalities.entrySet(); + } + public int getId() { return id; } @@ -175,24 +191,24 @@ public class Philosophe extends Agent { return rightFork; } - public Philosophe getLeftPhilosophe() { - return leftPhilosophe; + public Philosopher getLeftPhilosopher() { + return leftPhilosopher; } - public Philosophe getRightPhilosophe() { - return rightPhilosophe; + public Philosopher getRightPhilosopher() { + return rightPhilosopher; } public double getCriticallity() { return criticallity; } - public void setLeftPhilosophe(Philosophe leftPhilosophe) { - this.leftPhilosophe = leftPhilosophe; + public void setLeftPhilosopher(Philosopher leftPhilosopher) { + this.leftPhilosopher = leftPhilosopher; } - public void setRightPhilosophe(Philosophe rightPhilosophe) { - this.rightPhilosophe = rightPhilosophe; + public void setRightPhilosopher(Philosopher rightPhilosopher) { + this.rightPhilosopher = rightPhilosopher; } public void setScheduler(Schedulable scheduler) { @@ -205,6 +221,6 @@ public class Philosophe extends Agent { @Override public String toString() { - return "Philosophe " + id; + return "Philosopher " + id; } } diff --git a/src/example/philosophes/Dechets.java b/src/example/philosophes/Waste.java similarity index 73% rename from src/example/philosophes/Dechets.java rename to src/example/philosophes/Waste.java index 9a6028d3382fcba772704add9dae66a24f189a03..01670d571216ede3aebfe4013b1b74c5cc43e7dc 100644 --- a/src/example/philosophes/Dechets.java +++ b/src/example/philosophes/Waste.java @@ -3,19 +3,19 @@ package example.philosophes; import mas.core.Cyclable; import mas.core.Schedulable; -public class Dechets implements Cyclable { +public class Waste implements Cyclable { private int id; Schedulable scheduler = null; - public Dechets(int _id){ + public Waste(int _id){ id = _id; } @Override public void cycle() { - /*System.out.println("je suis le dechet n°" + id);*/ + /*System.out.println("I'm the waste n°" + id);*/ } @Override @@ -30,6 +30,6 @@ public class Dechets implements Cyclable { @Override public String toString() { - return "Dechet " + id; + return "Waste " + id; } } diff --git a/src/example/randomants/MainAnt.java b/src/example/randomants/MainAnt.java index 9afcb72c75ab6641cae77b8985243cdf9cb5bd82..f7fc3fab5c43a6fc9ce42de40a5f5a281ebcd8e4 100644 --- a/src/example/randomants/MainAnt.java +++ b/src/example/randomants/MainAnt.java @@ -1,6 +1,7 @@ package example.randomants; import mas.core.Agent; +import mas.core.Cyclable; import mas.environment.TwoDContinuosGrid; import mas.implementation.base.schedulers.TwoDCycling; import mas.ui.MainWindow; @@ -10,6 +11,21 @@ public class MainAnt { public static void main(String[] args) { + class MyTwoDCycling extends TwoDCycling{ + + AntHill antHill; + + public MyTwoDCycling(AntHill _anthill, Cyclable... _cyclables){ + super(_cyclables); + antHill = _anthill; + } + + @Override + protected void onCycleEnds() { + antHill.getAntsCountLabel().setText("Ants count " + Ant.getNumberOfAnts()); + } + } + int widht = 800; int height = 600; @@ -26,7 +42,7 @@ public class MainAnt { ants[i] = new Ant(i+1,0,0,env); } - TwoDCycling scheduler = new TwoDCycling(ants); + MyTwoDCycling scheduler = new MyTwoDCycling(hill, ants); MainWindow.instance(); MainWindow.addToolbar(new SchedulerToolbar("Amas", scheduler)); diff --git a/src/mas/core/ThreeStepCyclable.java b/src/mas/core/ThreeStepCyclable.java index 99a01727b03ac903efed80a4d7c33a1c3259d4b7..8a9d0b586aec5d5dc8162921a390fc7f5d322392 100644 --- a/src/mas/core/ThreeStepCyclable.java +++ b/src/mas/core/ThreeStepCyclable.java @@ -1,7 +1,5 @@ package mas.core; -import mas.core.Cyclable; - /** * TODO */ diff --git a/src/mas/core/TwoStepCyclable.java b/src/mas/core/TwoStepCyclable.java index 6324ccd5f2b9f12d5aa354d3e12ebebc097b3614..398864e43d4c74dbafe6bc651401216fc340bf4f 100644 --- a/src/mas/core/TwoStepCyclable.java +++ b/src/mas/core/TwoStepCyclable.java @@ -1,7 +1,5 @@ package mas.core; -import mas.core.Cyclable; - public interface TwoStepCyclable extends Cyclable { @Override diff --git a/src/mas/implementation/base/schedulers/AsyncCycling.java b/src/mas/implementation/base/schedulers/AsyncCycling.java index 9d131094c09d0b295fb00282cdbcd2574775777e..343f2fe3f1e0928c52a3062a60a2709970cf8fad 100644 --- a/src/mas/implementation/base/schedulers/AsyncCycling.java +++ b/src/mas/implementation/base/schedulers/AsyncCycling.java @@ -29,7 +29,6 @@ public class AsyncCycling implements Schedulable { @Override public void start() { - System.out.println("Je fait start"); for (Cyclable cyclable : cyclables){ executor.execute(() -> { manageCyclable(cyclable); @@ -39,7 +38,6 @@ public class AsyncCycling implements Schedulable { @Override public void stop() { - System.out.println("Je fait stop"); mustStop = true; executor.shutdown(); try { @@ -51,13 +49,11 @@ public class AsyncCycling implements Schedulable { @Override public void pause() { - System.out.println("Je fait pause"); executor.pause(); } @Override public void resume() { - System.out.println("Je fait resume"); executor.resume(); } diff --git a/src/mas/implementation/base/schedulers/FairCycling.java b/src/mas/implementation/base/schedulers/FairCycling.java index 6d283eb96e462665e53b10b674baaf9a383c8cf2..468d9890f859713820c646f0766f7c79d950650a 100644 --- a/src/mas/implementation/base/schedulers/FairCycling.java +++ b/src/mas/implementation/base/schedulers/FairCycling.java @@ -5,7 +5,6 @@ import mas.core.Schedulable; import java.util.*; import java.util.concurrent.*; -import java.util.function.Consumer; /** * Chaque agent execute exactement 1 cycle pour chaque cycle systeme @@ -15,15 +14,13 @@ public class FairCycling implements Schedulable { private Set<Cyclable> cyclables = new LinkedHashSet<>(); private Queue<Cyclable> pendingToAddCyclables = new ConcurrentLinkedQueue<>(); - //private Queue<Cyclable> pendingToRemoveCyclables = new ConcurrentLinkedQueue<>(); - private int sleep = DEFAULT_SLEEP; private int nbOfCycles = 0; - boolean mustStop = false; + protected boolean mustStop = false; - boolean mustPause = false; + protected boolean mustPause = false; ExecutorService executor = Executors.newCachedThreadPool(); @@ -39,29 +36,23 @@ public class FairCycling implements Schedulable { @Override public void start() { - //System.out.println("Je fait start"); - executor.execute(() -> { - doCycle(); - }); + executor.execute(() -> doCycle()); } @Override public void stop() { - //System.out.println("Je fait stop"); mustStop = true; executor.shutdown(); } @Override public void pause() { - //System.out.println("Je fait pause"); pauseLatch = new CountDownLatch(1); mustPause = true; } @Override public void resume() { - //System.out.println("Je fait resume"); if(pauseLatch != null){ pauseLatch.countDown(); } @@ -78,7 +69,6 @@ public class FairCycling implements Schedulable { @Override public void addCyclable(Cyclable cyclable){ - //System.out.println("Je fait addCyclebles : " + cyclable.toString()); cyclable.setScheduler(this); pendingToAddCyclables.add(cyclable); } @@ -94,9 +84,7 @@ public class FairCycling implements Schedulable { } protected void step() { - //System.out.println("Je fait step"); nbOfCycles++; - System.out.println("cycle systeme " + nbOfCycles); treatPendingCyclables(); @@ -133,7 +121,6 @@ public class FairCycling implements Schedulable { } protected void doCycle() { - //System.out.println("Je fait doCycle"); step(); if(stopCondition()){ this.stop(); @@ -156,4 +143,8 @@ public class FairCycling implements Schedulable { cyclables.addAll(buffer); pendingToAddCyclables.clear(); } + + public int getNbOfCycles() { + return nbOfCycles; + } } diff --git a/src/mas/implementation/base/schedulers/TwoDCycling.java b/src/mas/implementation/base/schedulers/TwoDCycling.java index f3d1478cb1e33eb51310059e5fc1a425e3d4c1a4..45e9ffbc86632c7f9e0f833669bb81155f2a83fa 100644 --- a/src/mas/implementation/base/schedulers/TwoDCycling.java +++ b/src/mas/implementation/base/schedulers/TwoDCycling.java @@ -1,24 +1,15 @@ package mas.implementation.base.schedulers; import mas.core.Cyclable; -import mas.ui.VUI; -import java.util.ArrayList; -import java.util.List; import java.util.concurrent.CountDownLatch; -import java.util.function.Consumer; public class TwoDCycling extends FairCycling{ /** * The state of the scheduler {@link State} */ - private State state = State.PENDING_START; - - /** - * Method that is called when the scheduler stops - */ - private Consumer<TwoDCycling> onStop; + protected State state = State.PENDING_START; /** * The methods called when the speed is changed. Useful to change the value of @@ -40,34 +31,11 @@ public class TwoDCycling extends FairCycling{ IDLE } - private List<Consumer<TwoDCycling>> onChange = new ArrayList<>(); public TwoDCycling(Cyclable... _cyclables){ super(_cyclables); } - /** - * Set the method that must be executed when the system is stopped - * - * @param _onStop - * Consumer method - */ - public final void setOnStop(Consumer<TwoDCycling> _onStop) { - this.onStop = _onStop; - } - - /** - * Add a method that must be executed when the scheduler speed is changed - * - * @param _onChange - * Consumer method - */ - public final void addOnChange(Consumer<TwoDCycling> _onChange) { - synchronized (onChange) { - this.onChange.add(_onChange); - } - } - public void doOneCycle() { executor.execute(() -> { step(); @@ -80,8 +48,8 @@ public class TwoDCycling extends FairCycling{ } }); } + public void startWithSleep(int _sleep){ - //System.out.println("Je commence startWithSleep(" + _sleep + ")"); setSleep(_sleep);