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

Add randomants example + add 2DContinousGrid as environment + add 2DCycling...

Add randomants example + add 2DContinousGrid as environment + add 2DCycling Scheduler + Add UI options to create Swing GUIs for our MAS
parent 7cd8bc93
Branches
No related tags found
No related merge requests found
Showing
with 405 additions and 74 deletions
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/core.iml" filepath="$PROJECT_DIR$/core.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="d7618c8d-68f5-4749-ad72-9e4faa130ff9" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/src/example/randomants/Ant.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/example/randomants/AntHill.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/example/randomants/MainAnt.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/example/randomants/ressources/ant.png" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/example/randomants/ressources/ant_dead.png" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/mas/ui/VUI.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/mas/ui/drawables/Drawable.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/mas/ui/drawables/DrawableCircle.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/mas/ui/drawables/DrawableImage.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/mas/ui/drawables/DrawableLine.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/mas/ui/drawables/DrawableOval.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/mas/ui/drawables/DrawablePoint.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/mas/ui/drawables/DrawableRectangle.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/mas/ui/drawables/DrawableString.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/example/philosophes/Dechets.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/example/philosophes/Dechets.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/example/philosophes/MainPhilosophe.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/example/philosophes/MainPhilosophe.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/example/philosophes/Philosophe.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/example/philosophes/Philosophe.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/mas/core/Agent.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/mas/core/Agent.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/mas/core/Cyclable.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/mas/core/Cyclable.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/mas/environment/TwoDContinuosGrid.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/mas/environment/TwoDContinuosGrid.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/mas/implementation/base/schedulers/AsyncCycling.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/mas/implementation/base/schedulers/AsyncCycling.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/mas/implementation/base/schedulers/FairCycling.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/mas/implementation/base/schedulers/FairCycling.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/mas/implementation/base/schedulers/TwoDCycling.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/mas/implementation/base/schedulers/TwoDCycling.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/mas/ui/SchedulerToolbar.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/mas/ui/SchedulerToolbar.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2AsSPACnMPcfJVj1FTH6jpS1pV5" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"WebServerToolWindowFactoryState": "false",
"last_opened_file_path": "C:/Users/David/Desktop/AmakGit/core/src/mas",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm"
}
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="C:\Users\David\Desktop\AmakGit\core\src\mas" />
<recent name="C:\Users\David\Desktop\AmakGit\core\src\example\randomants" />
</key>
</component>
<component name="RunManager" selected="Application.MainAnt">
<configuration name="MainAnt" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="example.randomants.MainAnt" />
<module name="core" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="example.randomants.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Philosophes" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="example.philosophes.MainPhilosophe" />
<module name="core" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<list>
<item itemvalue="Application.Philosophes" />
<item itemvalue="Application.MainAnt" />
</list>
<recent_temporary>
<list>
<item itemvalue="Application.MainAnt" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="d7618c8d-68f5-4749-ad72-9e4faa130ff9" name="Changes" comment="" />
<created>1655796773469</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1655796773469</updated>
<workItem from="1655796774853" duration="87000" />
<workItem from="1655796884483" duration="12176000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project>
\ No newline at end of file
......@@ -3,13 +3,11 @@ package example.philosophes;
import mas.core.Cyclable;
import mas.core.Schedulable;
import java.util.concurrent.atomic.AtomicInteger;
public class Dechets implements Cyclable {
private int id;
Schedulable scheduleur = null;
Schedulable scheduler = null;
public Dechets(int _id){
id = _id;
......@@ -17,7 +15,7 @@ public class Dechets implements Cyclable {
}
@Override
public void cycle() {
System.out.println("je suis le dechet n°" + id);
/*System.out.println("je suis le dechet n°" + id);*/
}
@Override
......@@ -26,8 +24,8 @@ public class Dechets implements Cyclable {
}
@Override
public void setScheduleur(Schedulable _scheduleur) {
scheduleur = _scheduleur;
public void setScheduler(Schedulable _scheduler) {
scheduler = _scheduler;
}
@Override
......
package example.philosophes;
import mas.core.Schedulable;
import mas.implementation.base.schedulers.TwoDCycling;
import mas.ui.MainWindow;
import mas.ui.SchedulerToolbar;
import mas.implementation.base.schedulers.FairCycling;
public class MainPhilosophe {
......@@ -29,16 +27,16 @@ public class MainPhilosophe {
philosophes[i].setRightPhilosophe(philosophes[(i+1) % nAgents]);
}
Schedulable scheduleur = new FairCycling(philosophes);
//scheduleur.setSleep(500);
scheduleur.start();
Schedulable scheduler = new FairCycling(philosophes);
scheduler.setSleep(2);
scheduler.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
scheduleur.pause();
scheduler.pause();
try {
Thread.sleep(2000);
......@@ -46,18 +44,18 @@ public class MainPhilosophe {
throw new RuntimeException(e);
}
scheduleur.resume();
scheduler.resume();
try {
Thread.sleep(2000);
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
scheduleur.stop();
scheduler.stop();
final long endTime = System.nanoTime();
System.out.println("Total execution time: " + (endTime / 1000000 - startTime / 1000000) + " microseconds");*/
System.out.println("Total execution time: " + (endTime / 1000000 - startTime / 1000000) + " microseconds");
}
}
......@@ -32,7 +32,7 @@ public class Philosophe extends Agent {
*/
private int id;
private Schedulable scheduleur;
private Schedulable scheduler;
/**
* States philosophers can be in
......@@ -120,7 +120,7 @@ public class Philosophe extends Agent {
//System.out.println("Philospher n°" + id + " / State " + state);
System.out.println(
"\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()*/
);
}
......@@ -147,7 +147,7 @@ public class Philosophe extends Agent {
@Override
public void act() {
//System.out.println("Philosopher num " + id + " act");
scheduleur.addCyclable(new Dechets(id));
scheduler.addCyclable(new Dechets(id));
}
@Override
......@@ -195,12 +195,12 @@ public class Philosophe extends Agent {
this.rightPhilosophe = rightPhilosophe;
}
public void setScheduleur(Schedulable scheduleur) {
this.scheduleur = scheduleur;
public void setScheduler(Schedulable scheduler) {
this.scheduler = scheduler;
}
public Schedulable getScheduleur() {
return scheduleur;
public Schedulable getScheduler() {
return scheduler;
}
@Override
......
package example.randomants;
import mas.core.Agent;
import mas.environment.TwoDContinuosGrid;
import mas.ui.VUI;
import mas.ui.drawables.DrawableImage;
import java.util.Random;
public class Ant extends Agent {
private static int numberOfAnts = 0;
public int id;
/**
* X coordinate of the ant in the world
*/
public double dx;
/**
* Y coordinate of the ant in the world
*/
public double dy;
public boolean mustDie = false;
/**
* Angle in radians
*/
private double angle = Math.random() * Math.PI * 2;
private DrawableImage image;
TwoDContinuosGrid environnement;
/**
* Constructor of the ant
*
* @param startX
* Initial X coordinate
* @param startY
* Initial Y coordinate
*/
public Ant(int _id, double startX, double startY, TwoDContinuosGrid _environnement) {
numberOfAnts++;
id = _id;
dx = startX;
dy = startY;
environnement = _environnement;
image = VUI.get().createImage(dx, dy, "src/example/randomants/ressources/ant.png");
}
/**
* Move in a random direction
*/
@Override
public void decide() {
double random = new Random().nextGaussian();
angle += random * 0.1;
dx += Math.cos(angle);
dy += Math.sin(angle);
while (dx >= environnement.getWidth() / 2)
dx -= environnement.getWidth();
while (dy >= environnement.getHeight() / 2)
dy -= environnement.getHeight();
while (dx < -environnement.getWidth() / 2)
dx += environnement.getWidth();
while (dy < -environnement.getHeight() / 2)
dy += environnement.getHeight();
}
@Override
public boolean terminate() {
return mustDie;
}
@Override
public void act() {
image.move(dx,dy);
image.setAngle(angle);
if (new Random().nextDouble() < 0.001) {
scheduler.addCyclable(new Ant(id * 10,dx, dy, environnement));
}
if (new Random().nextDouble() < 0.001) {
image.setFilename("src/example/randomants/ressources/ant_dead.png");
numberOfAnts--;
mustDie = true;
}
//System.out.println("Ant n°" + id + " / x = " + dx + " / y = " + dy);
}
public static int getNumberOfAnts() {
return numberOfAnts;
}
}
package example.randomants;
import mas.ui.VUI;
import mas.ui.drawables.DrawableRectangle;
import mas.ui.drawables.DrawableString;
import java.awt.*;
public class AntHill {
public DrawableString antsCountLabel;
public AntHill(int _weight, int _height){
DrawableRectangle d = VUI.get().createRectangle(0, 0, _weight, _height);
d.setStrokeOnly();
VUI.get().createRectangle(90, 20, 180, 40).setColor(new Color(0.9f, 0.9f, 0.9f, 0.8f)).setFixed().setLayer(5);
VUI.get().createImage(20, 20, "src/example/randomants/ressources/ant.png").setFixed().setLayer(10);
antsCountLabel = (DrawableString) VUI.get().createString(45, 25, "Ants count").setFixed().setLayer(10);
}
public DrawableString getAntsCountLabel() {
return antsCountLabel;
}
public void setAntsCountLabel(DrawableString _antsCountLabel) {
antsCountLabel = _antsCountLabel;
}
}
package example.randomants;
import mas.core.Agent;
import mas.environment.TwoDContinuosGrid;
import mas.implementation.base.schedulers.TwoDCycling;
import mas.ui.MainWindow;
import mas.ui.SchedulerToolbar;
public class MainAnt {
public static void main(String[] args) {
int widht = 800;
int height = 600;
AntHill hill = new AntHill(widht, height);
TwoDContinuosGrid env = new TwoDContinuosGrid(widht, height);
int nAgents = 50;
Agent[] ants = new Ant[nAgents];
for(int i = 0; i<nAgents ; i++){
ants[i] = new Ant(i+1,0,0,env);
}
TwoDCycling scheduler = new TwoDCycling(ants);
MainWindow.instance();
MainWindow.addToolbar(new SchedulerToolbar("Amas", scheduler));
}
}
src/example/randomants/ressources/ant.png

352 B

src/example/randomants/ressources/ant_dead.png

214 B

......@@ -2,7 +2,7 @@ package mas.core;
public class Agent implements ThreeStepCyclable{
private Schedulable scheduleur;
protected Schedulable scheduler;
@Override
public void perceive() {
......@@ -25,7 +25,7 @@ public class Agent implements ThreeStepCyclable{
}
@Override
public void setScheduleur(Schedulable _scheduleur) {
scheduleur = _scheduleur;
public void setScheduler(Schedulable _scheduler) {
scheduler = _scheduler;
}
}
......@@ -19,5 +19,5 @@ public interface Cyclable {
/**
* TODO
*/
void setScheduleur(Schedulable _scheduleur);
void setScheduler(Schedulable _scheduler);
}
......@@ -2,4 +2,20 @@ package mas.environment;
public class TwoDContinuosGrid {
private int width;
private int height;
public TwoDContinuosGrid(int _width, int _height){
height = _height;
width = _width;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
}
......@@ -23,7 +23,7 @@ public class AsyncCycling implements Schedulable {
for(Cyclable cyclable : _cyclables){
cyclables.add(cyclable);
cyclable.setScheduleur(this);
cyclable.setScheduler(this);
}
}
......@@ -64,7 +64,7 @@ public class AsyncCycling implements Schedulable {
@Override
public void addCyclable(Cyclable cyclable) {
cyclables.add(cyclable);
cyclable.setScheduleur(this);
cyclable.setScheduler(this);
if(!mustStop){
executor.execute(() -> {
......
......@@ -3,10 +3,9 @@ package mas.implementation.base.schedulers;
import mas.core.Cyclable;
import mas.core.Schedulable;
import java.util.LinkedHashSet;
import java.util.Queue;
import java.util.Set;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.Consumer;
/**
* Chaque agent execute exactement 1 cycle pour chaque cycle systeme
......@@ -40,28 +39,32 @@ public class FairCycling implements Schedulable {
@Override
public void start() {
System.out.println("Je fait start");
new Thread(() -> doCycle()).start();
//System.out.println("Je fait start");
executor.execute(() -> {
doCycle();
});
}
@Override
public void stop() {
System.out.println("Je fait stop");
//System.out.println("Je fait stop");
mustStop = true;
executor.shutdown();
}
@Override
public void pause() {
System.out.println("Je fait pause");
//System.out.println("Je fait pause");
pauseLatch = new CountDownLatch(1);
mustPause = true;
}
@Override
public void resume() {
System.out.println("Je fait resume");
pauseLatch.countDown();
//System.out.println("Je fait resume");
if(pauseLatch != null){
pauseLatch.countDown();
}
}
@Override
......@@ -69,10 +72,14 @@ public class FairCycling implements Schedulable {
return false;
}
protected void onCycleEnds() {
}
@Override
public void addCyclable(Cyclable cyclable){
//System.out.println("Je fait addCyclebles : " + cyclable.toString());
cyclable.setScheduleur(this);
cyclable.setScheduler(this);
pendingToAddCyclables.add(cyclable);
}
......@@ -87,12 +94,10 @@ public class FairCycling implements Schedulable {
}
protected void step() {
System.out.println("Je fait step");
//System.out.println("Je fait step");
nbOfCycles++;
System.out.println("cycle systeme " + nbOfCycles);
//System.out.println(pendingToAddCyclables.size() + " : " + pendingToAddCyclables);
treatPendingCyclables();
latch = new CountDownLatch(cyclables.size());
......@@ -102,13 +107,10 @@ public class FairCycling implements Schedulable {
executor.execute(() -> {
cyclable.cycle();
if(!cyclable.terminate()){
//System.out.println("\t\t" + cyclable.toString() + " je fait pendingToAdd");
pendingToAddCyclables.add(cyclable);
}
latch.countDown();
});
}
if (getSleep() != 0) {
......@@ -126,10 +128,12 @@ public class FairCycling implements Schedulable {
}
cyclables.clear();
onCycleEnds();
}
protected void doCycle() {
System.out.println("Je fait doCycle");
//System.out.println("Je fait doCycle");
step();
if(stopCondition()){
this.stop();
......@@ -148,7 +152,8 @@ public class FairCycling implements Schedulable {
}
private void treatPendingCyclables() {
while (!pendingToAddCyclables.isEmpty())
cyclables.add(pendingToAddCyclables.poll());
Queue<Cyclable> buffer = new ConcurrentLinkedQueue<>(pendingToAddCyclables);
cyclables.addAll(buffer);
pendingToAddCyclables.clear();
}
}
package mas.implementation.base.schedulers;
import mas.core.Cyclable;
import mas.ui.VUI;
import java.util.ArrayList;
import java.util.List;
......@@ -12,7 +13,7 @@ public class TwoDCycling extends FairCycling{
/**
* The state of the scheduler {@link State}
*/
private State state;
private State state = State.PENDING_START;
/**
* Method that is called when the scheduler stops
......@@ -25,6 +26,10 @@ public class TwoDCycling extends FairCycling{
*/
public enum State {
/**
* The scheduler is waiting to start
*/
PENDING_START,
/**
* The scheduler is running
*/
......@@ -32,11 +37,7 @@ public class TwoDCycling extends FairCycling{
/**
* The scheduler is paused
*/
IDLE,
/**
* The scheduler is expected to stop at the end at the current cycle
*/
PENDING_STOP
IDLE
}
private List<Consumer<TwoDCycling>> onChange = new ArrayList<>();
......@@ -68,22 +69,30 @@ public class TwoDCycling extends FairCycling{
}
public void doOneCycle() {
System.out.println("Je fait doOneCycle");
step();
if(stopCondition()){
this.stop();
}
if (!mustStop) {
this.pause();
}
executor.execute(() -> {
step();
if(stopCondition()){
this.stop();
}
if (!mustStop) {
this.pause();
}
});
}
public void startWithSleep(int _sleep){
System.out.println("Je commence startWithSleep(" + _sleep + ")");
//System.out.println("Je commence startWithSleep(" + _sleep + ")");
setSleep(_sleep);
state = State.RUNNING;
resume();
executor.execute(() -> doCycle());
switch (state){
case PENDING_START -> {
start();
}
default -> {
resume();
}
}
}
......@@ -94,21 +103,21 @@ public class TwoDCycling extends FairCycling{
@Override
public void start() {
state = State.RUNNING;
new Thread(() -> doCycle()).start();
super.start();
}
@Override
public void pause() {
state = State.IDLE;
pauseLatch = new CountDownLatch(1);
if(pauseLatch == null){
pauseLatch = new CountDownLatch(1);
}
mustPause = true;
}
@Override
public void resume() {
if(pauseLatch != null){
pauseLatch.countDown();
}
super.resume();
state = State.RUNNING;
}
}
......@@ -3,7 +3,6 @@ package mas.ui;
import mas.implementation.base.schedulers.TwoDCycling;
import javax.swing.*;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.util.Hashtable;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment