Skip to content
Snippets Groups Projects
Commit d495ebe8 authored by BrunoDatoMeneses's avatar BrunoDatoMeneses
Browse files

ENH: multiUI for AMAKFX with randomAnt example

parent 00ef9fe9
No related branches found
No related tags found
1 merge request!4Exp rein
...@@ -315,6 +315,7 @@ public abstract class Agent<A extends Amas<E>, E extends Environment> implements ...@@ -315,6 +315,7 @@ public abstract class Agent<A extends Amas<E>, E extends Environment> implements
*/ */
@Override @Override
public void run() { public void run() {
ExecutionPolicy executionPolicy = amas.getExecutionPolicy(); ExecutionPolicy executionPolicy = amas.getExecutionPolicy();
if (executionPolicy == ExecutionPolicy.TWO_PHASES) { if (executionPolicy == ExecutionPolicy.TWO_PHASES) {
......
...@@ -169,14 +169,15 @@ public class Amas<E extends Environment> implements Schedulable { ...@@ -169,14 +169,15 @@ public class Amas<E extends Environment> implements Schedulable {
vuiMulti.addTabbedDefaultPanel(amasMultiUIWindow); vuiMulti.addTabbedDefaultPanel(amasMultiUIWindow);
executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(Configuration.allowedSimultaneousAgentsExecution); executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(Configuration.allowedSimultaneousAgentsExecution);
//this.scheduler = environment.getScheduler();
if (scheduling == Scheduling.DEFAULT) { if (scheduling == Scheduling.DEFAULT) {
//MainWindow.instance();
this.scheduler = Scheduler.getDefaultScheduler(window); this.scheduler = Scheduler.getDefaultMultiUIScheduler(window);
this.scheduler.add(this); this.scheduler.add(this);
} else { } else {
this.scheduler = new Scheduler(this); this.scheduler = new Scheduler(this);
if (scheduling == Scheduling.UI && !Configuration.commandLineMode) { if (scheduling == Scheduling.UI && !Configuration.commandLineMode) {
//MainWindow.instance();
amasMultiUIWindow.addToolbar(new SchedulerToolbar("Amas #" + id, getScheduler())); amasMultiUIWindow.addToolbar(new SchedulerToolbar("Amas #" + id, getScheduler()));
} }
} }
......
...@@ -67,22 +67,22 @@ public abstract class Environment implements Schedulable { ...@@ -67,22 +67,22 @@ public abstract class Environment implements Schedulable {
public Environment(AmasMultiUIWindow window, Scheduling _scheduling, Object... params) { public Environment(AmasMultiUIWindow window, Scheduling _scheduling, Object... params) {
amasMultiUIWindow = window; amasMultiUIWindow = window;
if (_scheduling == Scheduling.DEFAULT) { // if (_scheduling == Scheduling.DEFAULT) {
this.scheduler = Scheduler.getDefaultScheduler(window); // this.scheduler = Scheduler.getDefaultMultiUIScheduler(window);
this.scheduler.add(this); // this.scheduler.add(this);
} else { // } else {
this.scheduler = new Scheduler(this); // this.scheduler = new Scheduler(this);
if (_scheduling == Scheduling.UI && !Configuration.commandLineMode) // if (_scheduling == Scheduling.UI && !Configuration.commandLineMode)
amasMultiUIWindow.addToolbar(new SchedulerToolbar("Environment #" + id, getScheduler())); // amasMultiUIWindow.addToolbar(new SchedulerToolbar("Environment #" + id, getScheduler()));
} // }
//
this.scheduler.lock(); // this.scheduler.lock();
this.params = params; this.params = params;
onInitialization(); onInitialization();
onInitialEntitiesCreation(); onInitialEntitiesCreation();
if (!Configuration.commandLineMode) if (!Configuration.commandLineMode)
onRenderingInitialization(); onRenderingInitialization();
this.scheduler.unlock(); // this.scheduler.unlock();
} }
/** /**
...@@ -165,4 +165,6 @@ public abstract class Environment implements Schedulable { ...@@ -165,4 +165,6 @@ public abstract class Environment implements Schedulable {
@Override @Override
public void onSchedulingStops() { public void onSchedulingStops() {
} }
} }
...@@ -60,6 +60,8 @@ public class Scheduler implements Runnable, Serializable { ...@@ -60,6 +60,8 @@ public class Scheduler implements Runnable, Serializable {
* The default scheduler * The default scheduler
*/ */
private static Scheduler defaultScheduler; private static Scheduler defaultScheduler;
/** /**
* The schedulables that must be added * The schedulables that must be added
*/ */
...@@ -120,16 +122,17 @@ public class Scheduler implements Runnable, Serializable { ...@@ -120,16 +122,17 @@ public class Scheduler implements Runnable, Serializable {
return defaultScheduler; return defaultScheduler;
} }
public static Scheduler getDefaultScheduler(AmasMultiUIWindow window) {
if (defaultScheduler == null) {
defaultScheduler = new Scheduler();
public static Scheduler getDefaultMultiUIScheduler(AmasMultiUIWindow window) {
Scheduler multiUIScheduler = new Scheduler();
if (!Configuration.commandLineMode) { if (!Configuration.commandLineMode) {
//MainWindow.instance(); SchedulerToolbar st = new SchedulerToolbar("Default", multiUIScheduler);
SchedulerToolbar st = new SchedulerToolbar("Default", defaultScheduler);
window.addToolbar(st); window.addToolbar(st);
} }
} return multiUIScheduler;
return defaultScheduler;
} }
/** /**
......
package fr.irit.smac.amak.examples.randomants; package fr.irit.smac.amak.examples.randomants;
import fr.irit.smac.amak.Agent; import fr.irit.smac.amak.Agent;
import fr.irit.smac.amak.ui.VUI; import fr.irit.smac.amak.ui.VUI;
import fr.irit.smac.amak.ui.drawables.DrawableImage; import fr.irit.smac.amak.ui.drawables.DrawableImage;
public class AntExample extends Agent<AntHillExample, WorldExample> { public class AntExample extends Agent<AntHillExample, WorldExample> {
private boolean dead = false; private boolean dead = false;
/** /**
* X coordinate of the ant in the world * X coordinate of the ant in the world
*/ */
public double dx; public double dx;
/** /**
* Y coordinate of the ant in the world * Y coordinate of the ant in the world
*/ */
public double dy; public double dy;
/** /**
* Angle in radians * Angle in radians
*/ */
private double angle = Math.random() * Math.PI * 2; private double angle = Math.random() * Math.PI * 2;
private DrawableImage image; private DrawableImage image;
/** /**
* Constructor of the ant * Constructor of the ant
* *
* @param amas * @param amas
* the amas the ant belongs to * the amas the ant belongs to
* @param startX * @param startX
* Initial X coordinate * Initial X coordinate
* @param startY * @param startY
* Initial Y coordinate * Initial Y coordinate
*/ */
public AntExample(AntHillExample amas, double startX, double startY) { public AntExample(AntHillExample amas, double startX, double startY) {
super(amas, startX, startY); super(amas, startX, startY);
} }
@Override @Override
public void onInitialization() { public void onInitialization() {
dx = (double) params[0]; dx = (double) params[0];
dy = (double) params[1]; dy = (double) params[1];
} }
@Override @Override
protected void onRenderingInitialization() { protected void onRenderingInitialization() {
image = VUI.get().createAndAddImage(dx, dy, "file:resources/ant.png"); image = VUI.get().createAndAddImage(dx, dy, "file:resources/ant.png");
image.setName("Ant "+getId()); image.setName("Ant "+getId());
} }
/** /**
* Move in a random direction * Move in a random direction
*/ */
@Override @Override
protected void onDecideAndAct() { protected void onDecideAndAct() {
double random = amas.getEnvironment().getRandom().nextGaussian(); double random = amas.getEnvironment().getRandom().nextGaussian();
angle += random * 0.1; angle += random * 0.1;
dx += Math.cos(angle); dx += Math.cos(angle);
dy += Math.sin(angle); dy += Math.sin(angle);
while (dx >= getAmas().getEnvironment().getWidth() / 2) while (dx >= getAmas().getEnvironment().getWidth() / 2)
dx -= getAmas().getEnvironment().getWidth(); dx -= getAmas().getEnvironment().getWidth();
while (dy >= getAmas().getEnvironment().getHeight() / 2) while (dy >= getAmas().getEnvironment().getHeight() / 2)
dy -= getAmas().getEnvironment().getHeight(); dy -= getAmas().getEnvironment().getHeight();
while (dx < -getAmas().getEnvironment().getWidth() / 2) while (dx < -getAmas().getEnvironment().getWidth() / 2)
dx += getAmas().getEnvironment().getWidth(); dx += getAmas().getEnvironment().getWidth();
while (dy < -getAmas().getEnvironment().getHeight() / 2) while (dy < -getAmas().getEnvironment().getHeight() / 2)
dy += getAmas().getEnvironment().getHeight(); dy += getAmas().getEnvironment().getHeight();
if (amas.getEnvironment().getRandom().nextDouble() < 0.001) { if (amas.getEnvironment().getRandom().nextDouble() < 0.001) {
dead = true; dead = true;
destroy(); destroy();
} }
if (amas.getEnvironment().getRandom().nextDouble() < 0.001) { if (amas.getEnvironment().getRandom().nextDouble() < 0.001) {
new AntExample(getAmas(), dx, dy); new AntExample(getAmas(), dx, dy);
} }
} }
@Override @Override
public void onUpdateRender() { public void onUpdateRender() {
image.move(dx, dy); image.move(dx, dy);
image.setAngle(angle); image.setAngle(angle);
image.setInfo("Ant "+getId()+"\nPosition "+dx+" "+dy+"\nAngle "+angle); image.setInfo("Ant "+getId()+"\nPosition "+dx+" "+dy+"\nAngle "+angle);
if(dead) { if(dead) {
image.setFilename("file:Resources/ant_dead.png"); image.setFilename("file:Resources/ant_dead.png");
image.setInfo("Ant "+getId()+"\nPosition "+dx+" "+dy+"\nAngle "+angle+"\nDead"); image.setInfo("Ant "+getId()+"\nPosition "+dx+" "+dy+"\nAngle "+angle+"\nDead");
} }
} }
} }
...@@ -55,6 +55,7 @@ public class AntExampleMutliUI extends Agent<AntHillExampleMultiUI, WorldExample ...@@ -55,6 +55,7 @@ public class AntExampleMutliUI extends Agent<AntHillExampleMultiUI, WorldExample
*/ */
@Override @Override
protected void onDecideAndAct() { protected void onDecideAndAct() {
double random = amas.getEnvironment().getRandom().nextGaussian(); double random = amas.getEnvironment().getRandom().nextGaussian();
angle += random * 0.1; angle += random * 0.1;
dx += Math.cos(angle); dx += Math.cos(angle);
......
...@@ -35,8 +35,6 @@ public class AntsLaunchExampleMultiUI extends Application{ ...@@ -35,8 +35,6 @@ public class AntsLaunchExampleMultiUI extends Application{
AntHillExampleMultiUI amas1 = new AntHillExampleMultiUI(window, VUIMulti.getDefault(), env); AntHillExampleMultiUI amas1 = new AntHillExampleMultiUI(window, VUIMulti.getDefault(), env);
AntHillExampleMultiUI amas2 = new AntHillExampleMultiUI(window2, VUIMulti.getDefault(), env2); AntHillExampleMultiUI amas2 = new AntHillExampleMultiUI(window2, VUIMulti.getDefault(), env2);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment