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

ADD: multiple UI launch for learning

parent d495ebe8
Branches main
No related tags found
1 merge request!4Exp rein
package agents; package agents;
import agents.percept.Percept; import agents.percept.Percept;
import fr.irit.smac.amak.Agent; import fr.irit.smac.amak.Agent;
import fr.irit.smac.amak.tools.Loggable; import fr.irit.smac.amak.tools.Loggable;
import gui.RenderStrategy; import gui.RenderStrategy;
import kernel.AMOEBA; import kernel.AMOEBA;
import kernel.World; import kernel.World;
/** /**
* The base class for all AMOEBA agents * The base class for all AMOEBA agents
*/ */
public abstract class AmoebaAgent extends Agent<AMOEBA, World> implements Loggable { public abstract class AmoebaAgent extends Agent<AMOEBA, World> implements Loggable {
// Attributes // Attributes
protected String name; protected String name;
private boolean dying; private boolean dying;
protected RenderStrategy renderStrategy; protected RenderStrategy renderStrategy;
/** /**
* Instantiate a new agent attached to an amoeba * Instantiate a new agent attached to an amoeba
* @param the amoeba * @param the amoeba
*/ */
public AmoebaAgent(AMOEBA amas, Object... params) { public AmoebaAgent(AMOEBA amas, Object... params) {
super(amas, params); super(amas, params);
this.dying = false; this.dying = false;
} }
@Override @Override
protected void onReady() { protected void onReady() {
super.onReady(); super.onReady();
logger().debug("CYCLE "+getAmas().getCycle(), "Agent %s ready.", toString()); logger().debug("CYCLE "+getAmas().getCycle(), "Agent %s ready.", toString());
} }
@Override @Override
protected void onDecide() { protected void onDecide() {
} }
@Override @Override
protected void onRenderingInitialization() { protected void onRenderingInitialization() {
if(renderStrategy != null) { if(renderStrategy != null) {
renderStrategy.initialize(); renderStrategy.initialize(getAmas().getVUIMulti());
}
} }
}
@Override
public void onUpdateRender() { @Override
amas.getEnvironment().incrementNbActivatedAgent(); public void onUpdateRender() {
if(renderStrategy != null && !isDying()) { amas.getEnvironment().incrementNbActivatedAgent();
if (amas.isRenderUpdate()) { if(renderStrategy != null && !isDying()) {
renderStrategy.render(); if (amas.isRenderUpdate()) {
} renderStrategy.render();
} }
} }
}
/**
* Set the name of the agent. Useful for visualization, and essential for {@link Percept}. /**
* @param name * Set the name of the agent. Useful for visualization, and essential for {@link Percept}.
*/ * @param name
public void setName(String name) { */
this.name = name; public void setName(String name) {
} this.name = name;
}
@Override
public void destroy() { @Override
dying = true; public void destroy() {
if(renderStrategy != null) { dying = true;
renderStrategy.delete(); if(renderStrategy != null) {
} renderStrategy.delete();
super.destroy(); }
logger().debug("CYCLE "+getAmas().getCycle(), "Agent %s destroyed.", toString()); super.destroy();
} logger().debug("CYCLE "+getAmas().getCycle(), "Agent %s destroyed.", toString());
}
/**
* Get the name of the agent. Useful for visualization, and essential for {@link Percept}. /**
* @param name * Get the name of the agent. Useful for visualization, and essential for {@link Percept}.
*/ * @param name
public String getName() { */
return name; public String getName() {
} return name;
}
/**
* Tell if the agent is dying. A dying agent no longer perform any useful action, but is not yet removed from its system. /**
* @return * Tell if the agent is dying. A dying agent no longer perform any useful action, but is not yet removed from its system.
*/ * @return
public boolean isDying() { */
return dying; public boolean isDying() {
} return dying;
}
/**
* Set the render strategy of an agent.<br/> /**
* {@link RenderStrategy#delete()} the old one, and {@link RenderStrategy#initialize()} the new one. * Set the render strategy of an agent.<br/>
* @param renderStrategy * {@link RenderStrategy#delete()} the old one, and {@link RenderStrategy#initialize()} the new one.
* @see RenderStrategy * @param renderStrategy
*/ * @see RenderStrategy
public void setRenderStrategy(RenderStrategy renderStrategy) { */
if(this.renderStrategy != null) this.renderStrategy.delete(); public void setRenderStrategy(RenderStrategy renderStrategy) {
this.renderStrategy = renderStrategy; if(this.renderStrategy != null) this.renderStrategy.delete();
if(this.renderStrategy != null) this.renderStrategy.initialize(); this.renderStrategy = renderStrategy;
} if(this.renderStrategy != null) this.renderStrategy.initialize(getAmas().getVUIMulti());
}
/**
* Get the render strategy of an agent. /**
* @return * Get the render strategy of an agent.
*/ * @return
public RenderStrategy getRenderStrategy() { */
return renderStrategy; public RenderStrategy getRenderStrategy() {
} return renderStrategy;
} }
}
package experiments.nDimensionsLaunchers;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import experiments.FILE;
import fr.irit.smac.amak.Configuration;
import fr.irit.smac.amak.examples.randomantsMultiUi.AntHillExampleMultiUI;
import fr.irit.smac.amak.examples.randomantsMultiUi.WorldExampleMultiUI;
import fr.irit.smac.amak.ui.AmasMultiUIWindow;
import fr.irit.smac.amak.ui.VUI;
import fr.irit.smac.amak.ui.VUIMulti;
import gui.AmoebaMultiUIWindow;
import gui.AmoebaWindow;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.Slider;
import javafx.stage.Stage;
import kernel.AMOEBA;
import kernel.StudiedSystem;
import kernel.backup.BackupSystem;
import kernel.backup.IBackupSystem;
import kernel.backup.SaveHelperImpl;
/**
* The Class BadContextLauncherEasy.
*/
public class F_N_LauncherMultiUI extends Application implements Serializable {
public static final double oracleNoiseRange = 0.5;
public static final double learningSpeed = 0.01;
public static final int regressionPoints = 100;
public static final int dimension = 2;
public static final double spaceSize = 50.0 ;
public static final int nbOfModels = 3 ;
public static final int normType = 2 ;
public static final boolean randomExploration = true;
public static final boolean limitedToSpaceZone = true;
//public static final double mappingErrorAllowed = 0.07; // BIG SQUARE
public static double mappingErrorAllowed = 0.03; // MULTI
public static final double explorationIncrement = 1.0 ;
public static final double explorationWidht = 0.5 ;
public static final int nbCycle = 1000;
public static void main(String[] args) throws IOException {
Application.launch(args);
}
@Override
public void start(Stage arg0) throws Exception, IOException {
Configuration.multiUI=true;
Configuration.commandLineMode = false;
Configuration.allowedSimultaneousAgentsExecution = 1;
Configuration.waitForGUI = true;
Configuration.plotMilliSecondsUpdate = 20000;
VUIMulti amoebaVUI = VUIMulti.get("2D");
AmoebaMultiUIWindow amoebaUI = new AmoebaMultiUIWindow("ELLSA", amoebaVUI);
AMOEBA amoeba = new AMOEBA(amoebaUI, amoebaVUI);
StudiedSystem studiedSystem = new F_N_Manager(spaceSize, dimension, nbOfModels, normType, randomExploration, explorationIncrement,explorationWidht,limitedToSpaceZone, oracleNoiseRange);
amoeba.setStudiedSystem(studiedSystem);
IBackupSystem backupSystem = new BackupSystem(amoeba);
File file = new File("resources/twoDimensionsLauncher.xml");
backupSystem.load(file);
amoeba.saver = new SaveHelperImpl(amoeba);
amoeba.allowGraphicalScheduler(true);
amoeba.setRenderUpdate(true);
amoeba.data.learningSpeed = learningSpeed;
amoeba.data.numberOfPointsForRegression = regressionPoints;
amoeba.getEnvironment().setMappingErrorAllowed(mappingErrorAllowed);
// Exemple for adding a tool in the toolbar
Slider slider = new Slider(0.01, 0.1, mappingErrorAllowed);
slider.setShowTickLabels(true);
slider.setShowTickMarks(true);
slider.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
System.out.println("new Value "+newValue);
mappingErrorAllowed = (double)newValue;
amoeba.getEnvironment().setMappingErrorAllowed(mappingErrorAllowed);
}
});
amoebaUI.addToolbar(slider);
studiedSystem.playOneStep();
amoeba.learn(studiedSystem.getOutput());
VUIMulti amoebaVUI2 = VUIMulti.get("2D");
AmoebaMultiUIWindow amoebaUI2 = new AmoebaMultiUIWindow("ELLSA", amoebaVUI2);
AMOEBA amoeba2 = new AMOEBA(amoebaUI2, amoebaVUI2);
StudiedSystem studiedSystem2 = new F_N_Manager(spaceSize, dimension, nbOfModels, normType, randomExploration, explorationIncrement,explorationWidht,limitedToSpaceZone, oracleNoiseRange);
amoeba2.setStudiedSystem(studiedSystem2);
IBackupSystem backupSystem2 = new BackupSystem(amoeba2);
File file2 = new File("resources/twoDimensionsLauncher.xml");
backupSystem2.load(file2);
amoeba2.saver = new SaveHelperImpl(amoeba2);
amoeba2.allowGraphicalScheduler(true);
amoeba2.setRenderUpdate(true);
amoeba2.data.learningSpeed = learningSpeed;
amoeba2.data.numberOfPointsForRegression = regressionPoints;
amoeba2.getEnvironment().setMappingErrorAllowed(mappingErrorAllowed);
// Exemple for adding a tool in the toolbar
Slider slider2 = new Slider(0.01, 0.1, mappingErrorAllowed);
slider2.setShowTickLabels(true);
slider2.setShowTickMarks(true);
slider2.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
System.out.println("new Value "+newValue);
mappingErrorAllowed = (double)newValue;
amoeba2.getEnvironment().setMappingErrorAllowed(mappingErrorAllowed);
}
});
amoebaUI2.addToolbar(slider2);
studiedSystem2.playOneStep();
amoeba2.learn(studiedSystem2.getOutput());
}
public static void launch() throws IOException{
// Set AMAK configuration before creating an AMOEBA
AMOEBA amoeba = new AMOEBA(null, VUIMulti.get("2D"));
StudiedSystem studiedSystem = new F_N_Manager(spaceSize, dimension, nbOfModels, normType, randomExploration, explorationIncrement,explorationWidht,limitedToSpaceZone, oracleNoiseRange);
amoeba.setStudiedSystem(studiedSystem);
IBackupSystem backupSystem = new BackupSystem(amoeba);
File file = new File("resources/twoDimensionsLauncher.xml");
backupSystem.load(file);
amoeba.saver = new SaveHelperImpl(amoeba);
amoeba.allowGraphicalScheduler(true);
amoeba.setRenderUpdate(true);
amoeba.data.learningSpeed = learningSpeed;
amoeba.data.numberOfPointsForRegression = regressionPoints;
amoeba.getEnvironment().setMappingErrorAllowed(mappingErrorAllowed);
// Exemple for adding a tool in the toolbar
Slider slider = new Slider(0.01, 0.1, mappingErrorAllowed);
slider.setShowTickLabels(true);
slider.setShowTickMarks(true);
slider.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
System.out.println("new Value "+newValue);
mappingErrorAllowed = (double)newValue;
amoeba.getEnvironment().setMappingErrorAllowed(mappingErrorAllowed);
}
});
AmoebaWindow.addToolbar(slider);
studiedSystem.playOneStep();
amoeba.learn(studiedSystem.getOutput());
/* AUTOMATIC */
// long start = System.currentTimeMillis();
// for (int i = 0; i < nbCycle; ++i) {
// studiedSystem.playOneStep();
// amoeba.learn(studiedSystem.getOutput());
// }
// long end = System.currentTimeMillis();
// System.out.println("Done in : " + (end - start) );
//
// start = System.currentTimeMillis();
// for (int i = 0; i < nbCycle; ++i) {
// studiedSystem.playOneStep();
// amoeba.request(studiedSystem.getOutput());
// }
// end = System.currentTimeMillis();
// System.out.println("Done in : " + (end - start) );
// /* XP PIERRE */
//
// String fileName = fileName(new ArrayList<String>(Arrays.asList("GaussiennePierre")));
//
// FILE Pierrefile = new FILE("Pierre",fileName);
// for (int i = 0; i < nbCycle; ++i) {
// studiedSystem.playOneStep();
// amoeba.learn(studiedSystem.getOutput());
// if(amoeba.getHeadAgent().isActiveLearning()) {
// studiedSystem.setActiveLearning(true);
// studiedSystem.setSelfRequest(amoeba.getHeadAgent().getSelfRequest());
//
// }
// }
//
// for (int i = 0; i < 10; ++i) {
// studiedSystem.playOneStep();
// System.out.println(studiedSystem.getOutput());
// System.out.println(amoeba.request(studiedSystem.getOutput()));
//
//
// }
//
// Pierrefile.write(new ArrayList<String>(Arrays.asList("ID contexte","Coeff Cte","Coeff X0","Coeff X1","Min Value","Max Value")));
//
// for(Context ctxt : amoeba.getContexts()) {
//
// writeMessage(Pierrefile, ctxt.toStringArrayPierre());
//
// }
//
//
// Pierrefile.close();
}
public static String fileName(ArrayList<String> infos) {
String fileName = "";
for(String info : infos) {
fileName += info + "_";
}
return fileName;
}
public static void writeMessage(FILE file, ArrayList<String> message) {
file.initManualMessage();
for(String m : message) {
file.addManualMessage(m);
}
file.sendManualMessage();
}
}
package gui;
import java.util.HashMap;
import javax.management.InstanceAlreadyExistsException;
import fr.irit.smac.amak.tools.Log;
import fr.irit.smac.amak.tools.RunLaterHelper;
import fr.irit.smac.amak.ui.AmakPlot;
import fr.irit.smac.amak.ui.AmakPlot.ChartType;
import fr.irit.smac.amak.ui.AmasMultiUIWindow;
import fr.irit.smac.amak.ui.MainWindow;
import fr.irit.smac.amak.ui.SchedulerToolbar;
import fr.irit.smac.amak.ui.VUI;
import fr.irit.smac.amak.ui.VUIMulti;
import fr.irit.smac.amak.ui.drawables.Drawable;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.Slider;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.Tooltip;
import javafx.scene.paint.Color;
import kernel.AMOEBA;
import kernel.backup.SaveHelperImpl;
/**
* The main window for AMOEBA GUI.
* @author Hugo
*
*/
public class AmoebaMultiUIWindow extends AmasMultiUIWindow{
protected HashMap<String, AmakPlot> plots = new HashMap<>();
/**
* The main {@link VUI} for AMOEBA, by default it's the 2D representation of the contexts.
*/
public VUIMulti mainVUI;
public Drawable point;
public Drawable rectangle;
public ToggleButton toggleRender;
public SchedulerToolbar schedulerToolbar;
public DimensionSelector dimensionSelector;
public Menu windowMenu;
public AmoebaMultiUIWindow(String title, VUIMulti vui) {
super(title);
mainVUI = vui;
}
public void initialize(AMOEBA amoeba) {
mainVUI.setDefaultView(200, 0, 0);
addTabbedPanel("2D VUI", mainVUI.getPanel());
// scheduler toolbar
schedulerToolbar = new SchedulerToolbar("AMOEBA", amoeba.getScheduler());
addToolbar(schedulerToolbar);
// plots
point = mainVUI.createAndAddPoint(0, 0);
point.setName("Cursor");
rectangle = mainVUI.createAndAddRectangle(10, 10, 10, 10);
rectangle.setName("Neighborhood");
rectangle.setColor(new Color(1, 1, 1, 0));
plots.put("This loop NCS", new AmakPlot(this, "This loop NCS", ChartType.LINE, "Cycle", "Number of NCS"));
plots.put("All time NCS", new AmakPlot(this, "All time NCS", ChartType.LINE, "Cycle", "Number of NCS"));
plots.put("Number of agents", new AmakPlot(this, "Number of agents", ChartType.LINE, "Cycle", "Number of agents"));
plots.put("Errors", new AmakPlot(this, "Errors", ChartType.LINE, "Cycle", "Coefficients"));
plots.put("Distances to models", new AmakPlot(this, "Distances to models", ChartType.LINE, "Cycle", "Distances"));
plots.put("Global Mapping Criticality", new AmakPlot(this, "Global Mapping Criticality", ChartType.LINE, "Cycle", "Criticalities"));
plots.put("Time Execution", new AmakPlot(this, "Time Execution", ChartType.LINE, "Cycle", "Times"));
plots.put("Criticalities", new AmakPlot(this, "Criticalities", ChartType.LINE, "Cycle", "Criticalities"));
// update render button
toggleRender = new ToggleButton("Allow Rendering");
toggleRender.setOnAction(evt -> {
amoeba.setRenderUpdate(toggleRender.isSelected());
if(amoeba.isRenderUpdate()) {
amoeba.updateAgentsVisualisation();
amoeba.nextCycleRunAllAgents();
}
});
toggleRender.setSelected(amoeba.isRenderUpdate());
addToolbar(toggleRender);
// dimension selector
dimensionSelector = new DimensionSelector(amoeba.getPercepts(), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
amoeba.updateAgentsVisualisation();
}
});
RunLaterHelper.runLater(()->mainVUI.toolbar.getItems().add(dimensionSelector));
// contextMenu "Request Here" on VUI
new ContextMenuVUI(amoeba, mainVUI); //the ContextMenu add itself to the VUI
// manual save button
addToolbar(newManualSaveButton(amoeba));
Slider slider = new Slider(0, 0.1, 0.1);
slider.setShowTickLabels(true);
slider.setShowTickMarks(true);
slider.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
amoeba.getEnvironment().mappingErrorAllowed = newValue.doubleValue();
}
});
addToolbar(slider);
}
/**
* Get an existing {@link AmakPlot}.
* @param name name of the plot to get
* @return an existing plot.
* @see AmoebaMultiUIWindow#addPlot(String, AmakPlot)
*/
public AmakPlot getPlot(String name) {
return plots.get(name);
}
/**
* Add an {@link AmakPlot} to le map of plots. Allowing for easy access with {@code AmoebaWindow.instance().getPlot(name)}
* @param name name of the plot to add
* @param plot the plot to add
* @see AmoebaMultiUIWindow#getPlot(String)
*/
public void addPlot(String name, AmakPlot plot) {
plots.put(name, plot);
}
/**
* Create a button 'Quick Save' button, when clicked create a manual save point using an amoeba's saver.
* @param amoeba
* @return
* @see AMOEBA#saver
* @see SaveHelperImpl#newManualSave(String)
*/
public Button newManualSaveButton(AMOEBA amoeba) {
Button button = new Button("Quick save");
button.setTooltip(new Tooltip("Create a new save point. You will be able to find it in 'Save Explorer' -> 'Manual Saves'"));
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(amoeba.saver != null) {
amoeba.saver.newManualSave("manualSaveButton");
} else {
Log.defaultLog.error("Main Window", "Cannot make a save point of an amoeba without saver");
}
}
});
return button;
}
}
...@@ -6,6 +6,7 @@ import java.util.Optional; ...@@ -6,6 +6,7 @@ import java.util.Optional;
import agents.percept.Percept; import agents.percept.Percept;
import fr.irit.smac.amak.tools.Log; import fr.irit.smac.amak.tools.Log;
import fr.irit.smac.amak.ui.VUI; import fr.irit.smac.amak.ui.VUI;
import fr.irit.smac.amak.ui.VUIMulti;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.scene.control.Button; import javafx.scene.control.Button;
...@@ -38,7 +39,7 @@ public class ContextMenuVUI extends ContextMenu { ...@@ -38,7 +39,7 @@ public class ContextMenuVUI extends ContextMenu {
* @param amoeba the amoeba where {@link AMOEBA#request(HashMap)} and {@link AMOEBA#learn(HashMap)} will be executed. * @param amoeba the amoeba where {@link AMOEBA#request(HashMap)} and {@link AMOEBA#learn(HashMap)} will be executed.
* @param vui the {@link VUI} hosting the {@link ContextMenuVUI} * @param vui the {@link VUI} hosting the {@link ContextMenuVUI}
*/ */
public ContextMenuVUI(AMOEBA amoeba, VUI vui) { public ContextMenuVUI(AMOEBA amoeba, VUIMulti vui) {
// "request here" menu item // "request here" menu item
setupRequestHereMenuItem(amoeba, vui); setupRequestHereMenuItem(amoeba, vui);
...@@ -56,7 +57,7 @@ public class ContextMenuVUI extends ContextMenu { ...@@ -56,7 +57,7 @@ public class ContextMenuVUI extends ContextMenu {
}); });
} }
private void setupRequestHereMenuItem(AMOEBA amoeba, VUI vui) { private void setupRequestHereMenuItem(AMOEBA amoeba, VUIMulti vui) {
MenuItem reqHere = new MenuItem("Request Here"); MenuItem reqHere = new MenuItem("Request Here");
reqHere.setOnAction(new EventHandler<ActionEvent>() { reqHere.setOnAction(new EventHandler<ActionEvent>() {
@Override @Override
...@@ -78,7 +79,7 @@ public class ContextMenuVUI extends ContextMenu { ...@@ -78,7 +79,7 @@ public class ContextMenuVUI extends ContextMenu {
* @param amoeba * @param amoeba
* @param vui * @param vui
*/ */
private void reqTwoDimension(AMOEBA amoeba, VUI vui) { private void reqTwoDimension(AMOEBA amoeba, VUIMulti vui) {
double x = vui.screenToWorldX(reqHereX); double x = vui.screenToWorldX(reqHereX);
double y = vui.screenToWorldY(reqHereY); double y = vui.screenToWorldY(reqHereY);
HashMap<String, Double> req = new HashMap<String, Double>(); HashMap<String, Double> req = new HashMap<String, Double>();
...@@ -95,7 +96,7 @@ public class ContextMenuVUI extends ContextMenu { ...@@ -95,7 +96,7 @@ public class ContextMenuVUI extends ContextMenu {
* @param amoeba * @param amoeba
* @param vui * @param vui
*/ */
private void reqNDimension(AMOEBA amoeba, VUI vui) { private void reqNDimension(AMOEBA amoeba, VUIMulti vui) {
double x = vui.screenToWorldX(reqHereX); double x = vui.screenToWorldX(reqHereX);
double y = vui.screenToWorldY(reqHereY); double y = vui.screenToWorldY(reqHereY);
...@@ -143,7 +144,7 @@ public class ContextMenuVUI extends ContextMenu { ...@@ -143,7 +144,7 @@ public class ContextMenuVUI extends ContextMenu {
}); });
} }
private void setupLearnHereMenuItem(AMOEBA amoeba, VUI vui) { private void setupLearnHereMenuItem(AMOEBA amoeba, VUIMulti vui) {
MenuItem learnHere = new MenuItem("Learn Here"); MenuItem learnHere = new MenuItem("Learn Here");
learnHere.setOnAction(new EventHandler<ActionEvent>() { learnHere.setOnAction(new EventHandler<ActionEvent>() {
@Override @Override
...@@ -165,7 +166,7 @@ public class ContextMenuVUI extends ContextMenu { ...@@ -165,7 +166,7 @@ public class ContextMenuVUI extends ContextMenu {
* @param amoeba * @param amoeba
* @param vui * @param vui
*/ */
private void learnTwoDimension(AMOEBA amoeba, VUI vui) { private void learnTwoDimension(AMOEBA amoeba, VUIMulti vui) {
double x = vui.screenToWorldX(reqHereX); double x = vui.screenToWorldX(reqHereX);
double y = vui.screenToWorldY(reqHereY); double y = vui.screenToWorldY(reqHereY);
HashMap<String, Double> req = new HashMap<String, Double>(); HashMap<String, Double> req = new HashMap<String, Double>();
...@@ -181,7 +182,7 @@ public class ContextMenuVUI extends ContextMenu { ...@@ -181,7 +182,7 @@ public class ContextMenuVUI extends ContextMenu {
* @param amoeba * @param amoeba
* @param vui * @param vui
*/ */
private void learnNDimebsion(AMOEBA amoeba, VUI vui) { private void learnNDimebsion(AMOEBA amoeba, VUIMulti vui) {
double x = vui.screenToWorldX(reqHereX); double x = vui.screenToWorldX(reqHereX);
double y = vui.screenToWorldY(reqHereY); double y = vui.screenToWorldY(reqHereY);
......
...@@ -2,6 +2,7 @@ package gui; ...@@ -2,6 +2,7 @@ package gui;
import agents.context.Context; import agents.context.Context;
import agents.percept.Percept; import agents.percept.Percept;
import fr.irit.smac.amak.ui.VUIMulti;
import fr.irit.smac.amak.ui.drawables.DrawableRectangle; import fr.irit.smac.amak.ui.drawables.DrawableRectangle;
import gui.utils.ContextColor; import gui.utils.ContextColor;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
...@@ -64,8 +65,8 @@ public class ContextRendererFX extends RenderStrategy { ...@@ -64,8 +65,8 @@ public class ContextRendererFX extends RenderStrategy {
* window. * window.
*/ */
@Override @Override
public void initialize() { public void initialize(VUIMulti vui) {
getDrawable().setName(context.toString()); // create the drawable if it does not exist getDrawable(vui).setName(context.toString()); // create the drawable if it does not exist
} }
...@@ -81,10 +82,10 @@ public class ContextRendererFX extends RenderStrategy { ...@@ -81,10 +82,10 @@ public class ContextRendererFX extends RenderStrategy {
* *
* @return * @return
*/ */
public DrawableRectangle getDrawable() { public DrawableRectangle getDrawable(VUIMulti vui) {
if (!context.isDying() && drawable == null) { if (!context.isDying() && drawable == null) {
drawable = new DrawableContext(0, 0, 0, 0, context); drawable = new DrawableContext(0, 0, 0, 0, context);
AmoebaWindow.instance().mainVUI.add(drawable); vui.add(drawable);
} }
return drawable; return drawable;
} }
......
package gui; package gui;
import fr.irit.smac.amak.ui.VUIMulti;
/** /**
* Strategy on how to render an object. * Strategy on how to render an object.
* See {@link ContextRendererFX} for example on how to extends this class. * See {@link ContextRendererFX} for example on how to extends this class.
...@@ -16,7 +18,8 @@ public abstract class RenderStrategy { ...@@ -16,7 +18,8 @@ public abstract class RenderStrategy {
/** /**
* Called when the rendered object need to be initialized * Called when the rendered object need to be initialized
*/ */
abstract public void initialize(); //abstract public void initialize();
abstract public void initialize(VUIMulti vui);
/** /**
* Called to render the object. * Called to render the object.
......
...@@ -23,6 +23,8 @@ import fr.irit.smac.amak.Scheduling; ...@@ -23,6 +23,8 @@ import fr.irit.smac.amak.Scheduling;
import fr.irit.smac.amak.tools.Log; import fr.irit.smac.amak.tools.Log;
import fr.irit.smac.amak.tools.RunLaterHelper; import fr.irit.smac.amak.tools.RunLaterHelper;
import fr.irit.smac.amak.ui.AmakPlot; import fr.irit.smac.amak.ui.AmakPlot;
import fr.irit.smac.amak.ui.VUIMulti;
import gui.AmoebaMultiUIWindow;
import gui.AmoebaWindow; import gui.AmoebaWindow;
import gui.DimensionSelector; import gui.DimensionSelector;
import kernel.backup.IBackupSystem; import kernel.backup.IBackupSystem;
...@@ -38,6 +40,9 @@ import utils.PrintOnce; ...@@ -38,6 +40,9 @@ import utils.PrintOnce;
*/ */
public class AMOEBA extends Amas<World> implements IAMOEBA { public class AMOEBA extends Amas<World> implements IAMOEBA {
// -- Attributes // -- Attributes
public VUIMulti vuiMulti;
/** /**
* Utility to save, autosave, and load amoebas. * Utility to save, autosave, and load amoebas.
*/ */
...@@ -48,6 +53,8 @@ public class AMOEBA extends Amas<World> implements IAMOEBA { ...@@ -48,6 +53,8 @@ public class AMOEBA extends Amas<World> implements IAMOEBA {
*/ */
public StudiedSystem studiedSystem; public StudiedSystem studiedSystem;
public AmoebaMultiUIWindow multiUIWindow;
private Head head; private Head head;
private TypeLocalModel localModel = TypeLocalModel.MILLER_REGRESSION; private TypeLocalModel localModel = TypeLocalModel.MILLER_REGRESSION;
private HashMap<String, Double> perceptions = new HashMap<String, Double>(); private HashMap<String, Double> perceptions = new HashMap<String, Double>();
...@@ -82,8 +89,9 @@ public class AMOEBA extends Amas<World> implements IAMOEBA { ...@@ -82,8 +89,9 @@ public class AMOEBA extends Amas<World> implements IAMOEBA {
* @param studiedSystem * @param studiedSystem
* the studied system * the studied system
*/ */
public AMOEBA() { public AMOEBA(AmoebaMultiUIWindow window, VUIMulti vui) {
super(new World(), Scheduling.HIDDEN); super(window, vui, new World(), Scheduling.HIDDEN);
vuiMulti = vui;
} }
/** /**
...@@ -91,8 +99,9 @@ public class AMOEBA extends Amas<World> implements IAMOEBA { ...@@ -91,8 +99,9 @@ public class AMOEBA extends Amas<World> implements IAMOEBA {
* *
* @param path path to the config file. * @param path path to the config file.
*/ */
public AMOEBA(String path, StudiedSystem studiedSystem) { public AMOEBA(AmoebaMultiUIWindow window, VUIMulti vui, String path, StudiedSystem studiedSystem) {
super(new World(), Scheduling.HIDDEN); super(window, vui, new World(), Scheduling.HIDDEN);
vuiMulti = vui;
this.studiedSystem = studiedSystem; this.studiedSystem = studiedSystem;
setRenderUpdate(true); setRenderUpdate(true);
saver = new SaveHelperImpl(this); saver = new SaveHelperImpl(this);
...@@ -112,23 +121,22 @@ public class AMOEBA extends Amas<World> implements IAMOEBA { ...@@ -112,23 +121,22 @@ public class AMOEBA extends Amas<World> implements IAMOEBA {
@Override @Override
protected void onRenderingInitialization() { protected void onRenderingInitialization() {
AmoebaWindow.instance().initialize(this); ((AmoebaMultiUIWindow) amasMultiUIWindow).initialize(this);
} }
@Override @Override
protected void onUpdateRender() { protected void onUpdateRender() {
// Update statistics // Update statistics
if(AmoebaWindow.isInstance()) { if(amasMultiUIWindow!=null) {
AmoebaWindow window = AmoebaWindow.instance();
AmakPlot loopNCS = ((AmoebaMultiUIWindow)amasMultiUIWindow).getPlot("This loop NCS");
AmakPlot loopNCS = window.getPlot("This loop NCS"); AmakPlot allNCS = ((AmoebaMultiUIWindow)amasMultiUIWindow).getPlot("All time NCS");
AmakPlot allNCS = window.getPlot("All time NCS"); AmakPlot nbAgent = ((AmoebaMultiUIWindow)amasMultiUIWindow).getPlot("Number of agents");
AmakPlot nbAgent = window.getPlot("Number of agents"); AmakPlot errors = ((AmoebaMultiUIWindow)amasMultiUIWindow).getPlot("Errors");
AmakPlot errors = window.getPlot("Errors"); AmakPlot distancesToModels = ((AmoebaMultiUIWindow)amasMultiUIWindow).getPlot("Distances to models");
AmakPlot distancesToModels = window.getPlot("Distances to models"); AmakPlot gloabalMappingCriticality = ((AmoebaMultiUIWindow)amasMultiUIWindow).getPlot("Global Mapping Criticality");
AmakPlot gloabalMappingCriticality = window.getPlot("Global Mapping Criticality"); AmakPlot timeExecution = ((AmoebaMultiUIWindow)amasMultiUIWindow).getPlot("Time Execution");
AmakPlot timeExecution = window.getPlot("Time Execution"); AmakPlot criticalities = ((AmoebaMultiUIWindow)amasMultiUIWindow).getPlot("Criticalities");
AmakPlot criticalities = window.getPlot("Criticalities");
boolean notify = isRenderUpdate(); boolean notify = isRenderUpdate();
...@@ -174,7 +182,7 @@ public class AMOEBA extends Amas<World> implements IAMOEBA { ...@@ -174,7 +182,7 @@ public class AMOEBA extends Amas<World> implements IAMOEBA {
} }
if (isRenderUpdate()) { if (isRenderUpdate()) {
AmoebaWindow.instance().mainVUI.updateCanvas(); ((AmoebaMultiUIWindow)amasMultiUIWindow).mainVUI.updateCanvas();
updateAgentsVisualisation(); updateAgentsVisualisation();
RunLaterHelper.runLater(() -> {resetCycleWithoutRender();}); RunLaterHelper.runLater(() -> {resetCycleWithoutRender();});
} }
...@@ -454,7 +462,7 @@ public class AMOEBA extends Amas<World> implements IAMOEBA { ...@@ -454,7 +462,7 @@ public class AMOEBA extends Amas<World> implements IAMOEBA {
*/ */
public void allowGraphicalScheduler(boolean allow) { public void allowGraphicalScheduler(boolean allow) {
if (!Configuration.commandLineMode) { if (!Configuration.commandLineMode) {
AmoebaWindow.instance().schedulerToolbar.setDisable(!allow); ((AmoebaMultiUIWindow)amasMultiUIWindow).schedulerToolbar.setDisable(!allow);
} }
} }
...@@ -476,7 +484,7 @@ public class AMOEBA extends Amas<World> implements IAMOEBA { ...@@ -476,7 +484,7 @@ public class AMOEBA extends Amas<World> implements IAMOEBA {
super.addPendingAgents(); super.addPendingAgents();
nextCycleRunAllAgents(); nextCycleRunAllAgents();
if(!Configuration.commandLineMode) { if(!Configuration.commandLineMode) {
AmoebaWindow.instance().dimensionSelector.update(getPercepts()); ((AmoebaMultiUIWindow)amasMultiUIWindow).dimensionSelector.update(getPercepts());
updateAgentsVisualisation(); updateAgentsVisualisation();
} }
} }
...@@ -504,7 +512,7 @@ public class AMOEBA extends Amas<World> implements IAMOEBA { ...@@ -504,7 +512,7 @@ public class AMOEBA extends Amas<World> implements IAMOEBA {
public void setRenderUpdate(boolean renderUpdate) { public void setRenderUpdate(boolean renderUpdate) {
if (!Configuration.commandLineMode) { if (!Configuration.commandLineMode) {
this.renderUpdate = renderUpdate; this.renderUpdate = renderUpdate;
AmoebaWindow.instance().toggleRender.setSelected(renderUpdate); ((AmoebaMultiUIWindow)amasMultiUIWindow).toggleRender.setSelected(renderUpdate);
if(renderUpdate == true) if(renderUpdate == true)
nextCycleRunAllAgents(); nextCycleRunAllAgents();
} }
...@@ -595,13 +603,13 @@ public class AMOEBA extends Amas<World> implements IAMOEBA { ...@@ -595,13 +603,13 @@ public class AMOEBA extends Amas<World> implements IAMOEBA {
for(Agent<? extends Amas<World>, World> a : getAgents()) { for(Agent<? extends Amas<World>, World> a : getAgents()) {
a.onUpdateRender(); a.onUpdateRender();
} }
AmoebaWindow.instance().point.move(AmoebaWindow.instance().dimensionSelector.d1().getValue(), AmoebaWindow.instance().dimensionSelector.d2().getValue()); ((AmoebaMultiUIWindow)amasMultiUIWindow).point.move(((AmoebaMultiUIWindow)amasMultiUIWindow).dimensionSelector.d1().getValue(), ((AmoebaMultiUIWindow)amasMultiUIWindow).dimensionSelector.d2().getValue());
AmoebaWindow.instance().rectangle.setHeight(2*getEnvironment().getContextCreationNeighborhood(null, AmoebaWindow.instance().dimensionSelector.d2())); ((AmoebaMultiUIWindow)amasMultiUIWindow).rectangle.setHeight(2*getEnvironment().getContextCreationNeighborhood(null, ((AmoebaMultiUIWindow)amasMultiUIWindow).dimensionSelector.d2()));
AmoebaWindow.instance().rectangle.setWidth(2*getEnvironment().getContextCreationNeighborhood(null, AmoebaWindow.instance().dimensionSelector.d1())); ((AmoebaMultiUIWindow)amasMultiUIWindow).rectangle.setWidth(2*getEnvironment().getContextCreationNeighborhood(null, ((AmoebaMultiUIWindow)amasMultiUIWindow).dimensionSelector.d1()));
AmoebaWindow.instance().rectangle.move(AmoebaWindow.instance().dimensionSelector.d1().getValue() - getEnvironment().getContextCreationNeighborhood(null, AmoebaWindow.instance().dimensionSelector.d1()), AmoebaWindow.instance().dimensionSelector.d2().getValue() - getEnvironment().getContextCreationNeighborhood(null, AmoebaWindow.instance().dimensionSelector.d2())); ((AmoebaMultiUIWindow)amasMultiUIWindow).rectangle.move(((AmoebaMultiUIWindow)amasMultiUIWindow).dimensionSelector.d1().getValue() - getEnvironment().getContextCreationNeighborhood(null, ((AmoebaMultiUIWindow)amasMultiUIWindow).dimensionSelector.d1()), ((AmoebaMultiUIWindow)amasMultiUIWindow).dimensionSelector.d2().getValue() - getEnvironment().getContextCreationNeighborhood(null, ((AmoebaMultiUIWindow)amasMultiUIWindow).dimensionSelector.d2()));
AmoebaWindow.instance().mainVUI.updateCanvas(); ((AmoebaMultiUIWindow)amasMultiUIWindow).mainVUI.updateCanvas();
AmoebaWindow.instance().point.toFront(); ((AmoebaMultiUIWindow)amasMultiUIWindow).point.toFront();
AmoebaWindow.instance().point.setInfo(getCursorInfo()); ((AmoebaMultiUIWindow)amasMultiUIWindow).point.setInfo(getCursorInfo());
} }
/** /**
...@@ -609,7 +617,7 @@ public class AMOEBA extends Amas<World> implements IAMOEBA { ...@@ -609,7 +617,7 @@ public class AMOEBA extends Amas<World> implements IAMOEBA {
* @return * @return
*/ */
public DimensionSelector getDimensionSelector() { public DimensionSelector getDimensionSelector() {
return AmoebaWindow.instance().dimensionSelector; return ((AmoebaMultiUIWindow)amasMultiUIWindow).dimensionSelector;
} }
/** /**
......
...@@ -11,6 +11,7 @@ import java.util.List; ...@@ -11,6 +11,7 @@ import java.util.List;
import fr.irit.smac.amak.Configuration; import fr.irit.smac.amak.Configuration;
import fr.irit.smac.amak.ui.MainWindow; import fr.irit.smac.amak.ui.MainWindow;
import gui.AmoebaMultiUIWindow;
import gui.AmoebaWindow; import gui.AmoebaWindow;
import gui.saveExplorer.SaveExplorer; import gui.saveExplorer.SaveExplorer;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
...@@ -33,6 +34,9 @@ public class SaveHelperImpl implements ISaveHelper{ ...@@ -33,6 +34,9 @@ public class SaveHelperImpl implements ISaveHelper{
public static final String autosaveDirName = "autosave"; public static final String autosaveDirName = "autosave";
public static final String manualsaveDirName = "manual"; public static final String manualsaveDirName = "manual";
public AmoebaMultiUIWindow amoebaMultiUIWindow;
/** /**
* The backup system used by the SaveHelper. * The backup system used by the SaveHelper.
*/ */
...@@ -116,6 +120,51 @@ public class SaveHelperImpl implements ISaveHelper{ ...@@ -116,6 +120,51 @@ public class SaveHelperImpl implements ISaveHelper{
setupGraphicalTool(); setupGraphicalTool();
} }
} }
public SaveHelperImpl(AMOEBA amoeba, AmoebaMultiUIWindow window) {
amoebaMultiUIWindow = window;
autoSave = !Configuration.commandLineMode;
this.amoeba = amoeba;
backupSystem = new BackupSystem(amoeba);
String dirName = amoeba.toString() + "_" + System.currentTimeMillis();
dir = Paths.get(savesRoot, dirName);
if (autoSave) {
dirAuto = Paths.get(dir.toString(), autosaveDirName);
try {
Files.createDirectories(dirAuto);
} catch (IOException e) {
e.printStackTrace();
System.err.println("Cannot create auto save directory. Auto saving is disabled.");
dirAuto = null;
autoSave = false;
}
}
dirManual = Paths.get(dir.toString(), manualsaveDirName);
try {
Files.createDirectories(dirManual);
} catch (IOException e) {
e.printStackTrace();
System.err.println("Cannot create manual save directory.");
dirManual = null;
}
// add graphical element if relevant
if (AmoebaWindow.isInstance()) {
SaveExplorer se = new SaveExplorer(amoeba);
AmoebaWindow.addTabbedPanel("Save Explorer", se);
AmoebaWindow.addOnCloseAction(()-> {
if(deleteFolderOnClose) {
try {
DeleteDirectory.deleteDirectoryRecursion(dir);
} catch (IOException e) {
e.printStackTrace();
System.err.println("Failed to delete saves files on close.");
}
}
});
setupGraphicalTool();
}
}
@Override @Override
public void load(String path) { public void load(String path) {
...@@ -187,7 +236,7 @@ public class SaveHelperImpl implements ISaveHelper{ ...@@ -187,7 +236,7 @@ public class SaveHelperImpl implements ISaveHelper{
* Add save/load options in the main window. * Add save/load options in the main window.
*/ */
private void setupGraphicalTool() { private void setupGraphicalTool() {
MainWindow mw = AmoebaWindow.instance(); AmoebaMultiUIWindow mw = amoebaMultiUIWindow;
// TODO remove if they exist items Save and Load in menu Option. // TODO remove if they exist items Save and Load in menu Option.
FileChooser fileChooser = new FileChooser(); FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("XML", "*.xml"), fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("XML", "*.xml"),
...@@ -198,7 +247,7 @@ public class SaveHelperImpl implements ISaveHelper{ ...@@ -198,7 +247,7 @@ public class SaveHelperImpl implements ISaveHelper{
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
amoeba.getScheduler().stop(); amoeba.getScheduler().stop();
File file = fileChooser.showOpenDialog(mw.stage); File file = fileChooser.showOpenDialog(mw);
if (file != null) if (file != null)
backupSystem.load(file); backupSystem.load(file);
} }
...@@ -210,7 +259,7 @@ public class SaveHelperImpl implements ISaveHelper{ ...@@ -210,7 +259,7 @@ public class SaveHelperImpl implements ISaveHelper{
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
amoeba.getScheduler().stop(); amoeba.getScheduler().stop();
File file = fileChooser.showSaveDialog(mw.stage); File file = fileChooser.showSaveDialog(mw);
if (file != null) if (file != null)
backupSystem.save(file); backupSystem.save(file);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment