Skip to content
Snippets Groups Projects
Commit 85a33706 authored by unknown's avatar unknown
Browse files

Correction of Philosophers example(Critticallity is well calculed now) + Add...

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
parent e78974f8
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
}
......@@ -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();
......
......@@ -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;
}
}
......@@ -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;
}
}
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));
......
package mas.core;
import mas.core.Cyclable;
/**
* TODO
*/
......
package mas.core;
import mas.core.Cyclable;
public interface TwoStepCyclable extends Cyclable {
@Override
......
......@@ -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();
}
......
......@@ -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;
}
}
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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment