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

Add ThreeStepCycling scheduler + fix minor bugs/mistakes

parent 521cb137
No related branches found
No related tags found
No related merge requests found
package example.philosophes; package example.philosophes;
import mas.core.Schedulable; import mas.core.Schedulable;
import mas.implementation.base.schedulers.FairCycling; import mas.implementation.schedulers.FairCycling;
import mas.implementation.schedulers.variations.ThreeStepCycling;
public class MainPhilosophe { public class MainPhilosophe {
...@@ -27,8 +28,8 @@ public class MainPhilosophe { ...@@ -27,8 +28,8 @@ public class MainPhilosophe {
philosophers[i].setRightPhilosopher(philosophers[(i+1) % nAgents]); philosophers[i].setRightPhilosopher(philosophers[(i+1) % nAgents]);
} }
Schedulable scheduler = new FairCycling(philosophers); Schedulable scheduler = new ThreeStepCycling(philosophers);
scheduler.setSleep(2); scheduler.setSleep(100);
scheduler.start(); scheduler.start();
try { try {
......
...@@ -65,19 +65,13 @@ public class Philosopher extends Agent { ...@@ -65,19 +65,13 @@ public class Philosopher extends Agent {
@Override @Override
public void perceive() { public void perceive() {
//System.out.println("Philosopher num " + id + " perceive"); System.out.println("Philosopher num " + id + " perceive");
criticallity = computeCriticallity(); criticallity = computeCriticallity();
} }
@Override @Override
public void decide() { public void decide() {
//System.out.println("Philosopher num " + id + " decide"); System.out.println("Philosopher num " + id + " decide");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
State nextState = state; State nextState = state;
switch (state) { switch (state) {
...@@ -116,12 +110,24 @@ public class Philosopher extends Agent { ...@@ -116,12 +110,24 @@ public class Philosopher extends Agent {
state = nextState; state = nextState;
//System.out.println("Philospher n°" + id + " / State " + state); //System.out.println("Philospher n°" + id + " / State " + state);
System.out.println( /*System.out.println(
"\tPhilosopher num " + id + " : " + state + " / " + criticallity + " / " + eatenPastas "\tPhilosopher num " + id + " : " + state + " / " + criticallity + " / " + eatenPastas
/* + "\n\t\t Right Fk : " + rightFork.getTakenBy().getId() + " / Left Fk : " + leftFork.getTakenBy().getId()*/ + "\n\t\t Right Fk : " + rightFork.getTakenBy().getId() + " / Left Fk : " + leftFork.getTakenBy().getId()
); );*/
}
@Override
public void act() {
System.out.println("Philosopher num " + id + " act");
scheduler.addCyclable(new Waste(id));
}
@Override
public boolean terminate() {
return false;
} }
private Philosopher getMostCriticalNeighbor() { private Philosopher getMostCriticalNeighbor() {
List<Philosopher> criticalest = new ArrayList<>(); List<Philosopher> criticalest = new ArrayList<>();
double maxCriticality = getHungerDuration(); double maxCriticality = getHungerDuration();
...@@ -148,17 +154,6 @@ public class Philosopher extends Agent { ...@@ -148,17 +154,6 @@ public class Philosopher extends Agent {
return -1; return -1;
} }
@Override
public void act() {
//System.out.println("Philosopher num " + id + " act");
scheduler.addCyclable(new Waste(id));
}
@Override
public boolean terminate() {
return false;
}
public Set<Map.Entry<Philosopher,Double>> getNeighborsCriticallity(){ public Set<Map.Entry<Philosopher,Double>> getNeighborsCriticallity(){
Map<Philosopher, Double> criticalities = new HashMap<>(); Map<Philosopher, Double> criticalities = new HashMap<>();
......
...@@ -3,7 +3,7 @@ package example.randomants; ...@@ -3,7 +3,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.TwoDContinuosGrid;
import mas.implementation.base.schedulers.TwoDCycling; import mas.implementation.schedulers.variations.TwoDCycling;
import mas.ui.MainWindow; import mas.ui.MainWindow;
import mas.ui.SchedulerToolbar; import mas.ui.SchedulerToolbar;
......
package mas.implementation.base.schedulers; package mas.implementation.schedulers;
import mas.core.Cyclable; import mas.core.Cyclable;
import mas.core.Schedulable; import mas.core.Schedulable;
...@@ -17,7 +17,7 @@ public class AsyncCycling implements Schedulable { ...@@ -17,7 +17,7 @@ public class AsyncCycling implements Schedulable {
private int sleep = DEFAULT_SLEEP; private int sleep = DEFAULT_SLEEP;
boolean mustStop = false; boolean mustStop = false;
private PausableThreadPoolExecutor executor = new PausableThreadPoolExecutor(); protected PausableThreadPoolExecutor executor = new PausableThreadPoolExecutor();
public AsyncCycling(Cyclable... _cyclables){ public AsyncCycling(Cyclable... _cyclables){
...@@ -84,14 +84,10 @@ public class AsyncCycling implements Schedulable { ...@@ -84,14 +84,10 @@ public class AsyncCycling implements Schedulable {
this.sleep = sleep; this.sleep = sleep;
} }
private void manageCyclable(Cyclable cyclable){ protected void manageCyclable(Cyclable cyclable){
cyclable.cycle(); cyclable.cycle();
try { doSleep();
Thread.sleep(sleep);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if(!cyclable.terminate() && !mustStop){ if(!cyclable.terminate() && !mustStop){
executor.execute(() -> manageCyclable(cyclable)); executor.execute(() -> manageCyclable(cyclable));
...@@ -100,6 +96,12 @@ public class AsyncCycling implements Schedulable { ...@@ -100,6 +96,12 @@ public class AsyncCycling implements Schedulable {
} }
} }
protected void doSleep(){
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
} }
package mas.implementation.base.schedulers; package mas.implementation.schedulers;
import mas.core.Cyclable; import mas.core.Cyclable;
import mas.core.Schedulable; import mas.core.Schedulable;
...@@ -11,21 +11,21 @@ import java.util.concurrent.*; ...@@ -11,21 +11,21 @@ import java.util.concurrent.*;
*/ */
public class FairCycling implements Schedulable { public class FairCycling implements Schedulable {
private Set<Cyclable> cyclables = new LinkedHashSet<>(); protected Set<Cyclable> cyclables = new LinkedHashSet<>();
private Queue<Cyclable> pendingToAddCyclables = new ConcurrentLinkedQueue<>(); protected Queue<Cyclable> pendingToAddCyclables = new ConcurrentLinkedQueue<>();
private int sleep = DEFAULT_SLEEP; protected int sleep = DEFAULT_SLEEP;
private int nbOfCycles = 0; protected int nbOfCycles = 0;
protected boolean mustStop = false; protected boolean mustStop = false;
protected boolean mustPause = false; protected boolean mustPause = false;
ExecutorService executor = Executors.newCachedThreadPool(); protected ExecutorService executor = Executors.newCachedThreadPool();
CountDownLatch pauseLatch; protected CountDownLatch pauseLatch;
CountDownLatch latch; protected CountDownLatch cycleLatch;
public FairCycling(Cyclable... _cyclables) { public FairCycling(Cyclable... _cyclables) {
...@@ -88,7 +88,7 @@ public class FairCycling implements Schedulable { ...@@ -88,7 +88,7 @@ public class FairCycling implements Schedulable {
treatPendingCyclables(); treatPendingCyclables();
latch = new CountDownLatch(cyclables.size()); cycleLatch = new CountDownLatch(cyclables.size());
for (Cyclable cyclable : cyclables) { for (Cyclable cyclable : cyclables) {
...@@ -97,20 +97,14 @@ public class FairCycling implements Schedulable { ...@@ -97,20 +97,14 @@ public class FairCycling implements Schedulable {
if(!cyclable.terminate()){ if(!cyclable.terminate()){
pendingToAddCyclables.add(cyclable); pendingToAddCyclables.add(cyclable);
} }
latch.countDown(); cycleLatch.countDown();
}); });
} }
if (getSleep() != 0) { doSleep();
try {
Thread.sleep(getSleep());
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
try { try {
latch.await(); cycleLatch.await();
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
...@@ -138,12 +132,22 @@ public class FairCycling implements Schedulable { ...@@ -138,12 +132,22 @@ public class FairCycling implements Schedulable {
} }
} }
private void treatPendingCyclables() { protected void treatPendingCyclables() {
Queue<Cyclable> buffer = new ConcurrentLinkedQueue<>(pendingToAddCyclables); Queue<Cyclable> buffer = new ConcurrentLinkedQueue<>(pendingToAddCyclables);
cyclables.addAll(buffer); cyclables.addAll(buffer);
pendingToAddCyclables.clear(); pendingToAddCyclables.clear();
} }
protected void doSleep(){
if (getSleep() != 0) {
try {
Thread.sleep(getSleep());
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
}
public int getNbOfCycles() { public int getNbOfCycles() {
return nbOfCycles; return nbOfCycles;
} }
......
package mas.implementation.base.schedulers; package mas.implementation.schedulers;
import mas.core.Cyclable; import mas.core.Cyclable;
import mas.core.Schedulable; import mas.core.Schedulable;
......
package mas.implementation.base.schedulers; package mas.implementation.schedulers;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Condition;
......
package mas.implementation.schedulers.variations;
import mas.core.ThreeStepCyclable;
import mas.implementation.schedulers.FairCycling;
import java.util.LinkedHashSet;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
/**
* Execution cadencé en 3 phases (tous perception puis tous decision puis tous action)
*/
public class ThreeStepCycling extends FairCycling {
protected Set<ThreeStepCyclable> threeStepCyclables = new LinkedHashSet<>();
protected Queue<ThreeStepCyclable> pendingToAddThreeStepCyclabes = new ConcurrentLinkedQueue<>();
protected CountDownLatch perceptionLatch;
protected CountDownLatch decicionLatch;
public ThreeStepCycling(ThreeStepCyclable... _threeStepCyclables){
for(ThreeStepCyclable threeStepCyclable : _threeStepCyclables){
addThreeStepCyclable(threeStepCyclable);
}
}
@Override
protected void step() {
nbOfCycles++;
treatPendingCyclables();
cycleLatch = new CountDownLatch(threeStepCyclables.size());
perceptionLatch = new CountDownLatch(threeStepCyclables.size());
decicionLatch = new CountDownLatch(threeStepCyclables.size());
for(ThreeStepCyclable threeStepCyclable : threeStepCyclables){
executor.execute(() -> {
threeStepCyclable.perceive();
waitPerception();
threeStepCyclable.decide();
waitDecision();
threeStepCyclable.act();
if(!threeStepCyclable.terminate()){
pendingToAddThreeStepCyclabes.add(threeStepCyclable);
}
cycleLatch.countDown();
});
}
doSleep();
try {
cycleLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
cyclables.clear();
onCycleEnds();
}
@Override
protected void treatPendingCyclables() {
Queue<ThreeStepCyclable> buffer = new ConcurrentLinkedQueue<>(pendingToAddThreeStepCyclabes);
threeStepCyclables.addAll(buffer);
pendingToAddThreeStepCyclabes.clear();
}
public void addThreeStepCyclable(ThreeStepCyclable threeStepCyclable){
threeStepCyclable.setScheduler(this);
pendingToAddThreeStepCyclabes.add(threeStepCyclable);
}
protected void waitPerception(){
perceptionLatch.countDown();
try {
perceptionLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
protected void waitDecision(){
decicionLatch.countDown();
try {
decicionLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
package mas.implementation.base.schedulers; package mas.implementation.schedulers.variations;
import mas.core.Cyclable; import mas.core.Cyclable;
import mas.implementation.schedulers.FairCycling;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer; import java.util.function.Consumer;
public class TwoDCycling extends FairCycling{ public class TwoDCycling extends FairCycling {
/** /**
* The state of the scheduler {@link State} * The state of the scheduler {@link State}
......
package mas.ui; package mas.ui;
import mas.implementation.base.schedulers.TwoDCycling; import mas.implementation.schedulers.variations.TwoDCycling;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
...@@ -99,9 +99,7 @@ public class SchedulerToolbar extends JToolBar { ...@@ -99,9 +99,7 @@ public class SchedulerToolbar extends JToolBar {
if(!source.getValueIsAdjusting()){ if(!source.getValueIsAdjusting()){
switch (runController.getValue()) { switch (runController.getValue()) {
case 0 -> { case 0 -> {
System.out.println("Je commence doOneCycle()");
scheduler.doOneCycle(); scheduler.doOneCycle();
System.out.println("Je termine doOneCycle()");
} }
case 2 -> { case 2 -> {
scheduler.startWithSleep(1000); scheduler.startWithSleep(1000);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment