From 85a33706f8901e923f58223a795f14ab569f1913 Mon Sep 17 00:00:00 2001
From: unknown <david.antunes-da-silva@etu.univ-tlse2.fr>
Date: Wed, 22 Jun 2022 10:41:28 +0200
Subject: [PATCH] Correction of Philosophers example(Critticallity is well
 calculed now) + Add new feature on Ants example(Count number of ants each
 cycle) + depuration of code in general

---
 src/example/philosophes/Fork.java             | 12 ++--
 src/example/philosophes/MainPhilosophe.java   | 10 +--
 .../{Philosophe.java => Philosopher.java}     | 72 +++++++++++--------
 .../philosophes/{Dechets.java => Waste.java}  |  8 +--
 src/example/randomants/MainAnt.java           | 18 ++++-
 src/mas/core/ThreeStepCyclable.java           |  2 -
 src/mas/core/TwoStepCyclable.java             |  2 -
 .../base/schedulers/AsyncCycling.java         |  4 --
 .../base/schedulers/FairCycling.java          | 23 ++----
 .../base/schedulers/TwoDCycling.java          | 36 +---------
 10 files changed, 85 insertions(+), 102 deletions(-)
 rename src/example/philosophes/{Philosophe.java => Philosopher.java} (67%)
 rename src/example/philosophes/{Dechets.java => Waste.java} (73%)

diff --git a/src/example/philosophes/Fork.java b/src/example/philosophes/Fork.java
index 3e150ea..8d09fc2 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 83acbcb..d70319a 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 8e69a2f..d683258 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 9a6028d..01670d5 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 9afcb72..f7fc3fa 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 99a0172..8a9d0b5 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 6324ccd..398864e 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 9d13109..343f2fe 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 6d283eb..468d989 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 f3d1478..45e9ffb 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);
 
 
-- 
GitLab