|
@@ -13,6 +13,7 @@ import java.io.FileOutputStream;
|
|
|
import java.io.FileWriter;
|
|
|
import java.io.IOException;
|
|
|
import java.io.PrintWriter;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Vector;
|
|
|
|
|
|
import javax.swing.JButton;
|
|
@@ -24,7 +25,9 @@ import javax.swing.JTextField;
|
|
|
import javax.swing.border.EmptyBorder;
|
|
|
|
|
|
import api.CpsAlgorithm;
|
|
|
+import classes.AbstractCpsObject;
|
|
|
import classes.CpsUpperNode;
|
|
|
+import classes.HolonElement;
|
|
|
import classes.HolonObject;
|
|
|
import classes.HolonSwitch;
|
|
|
import ui.controller.Control;
|
|
@@ -43,6 +46,12 @@ public class PSO implements CpsAlgorithm {
|
|
|
callPopUp(model, controller);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a binary representation of the current state of the network based on the state of the switches and holon elements
|
|
|
+ * @param model the overall model of the network
|
|
|
+ * @param controller the general controller
|
|
|
+ * @return Coordinate<Vector<Object>> where the first coordinate contains all switches states and the second coordinate all states of holon elements
|
|
|
+ */
|
|
|
public static Coordinate<Vector<Object>> extractInfo(Model model, Control controller) {
|
|
|
Coordinate<Vector<Object>> information = new Coordinate<Vector<Object>>();
|
|
|
// First only for Switches configuration
|
|
@@ -52,24 +61,24 @@ public class PSO implements CpsAlgorithm {
|
|
|
}
|
|
|
information.setCoord(temp, 0);
|
|
|
temp = new Vector<Object>();
|
|
|
- for (int i = 0; i < model.getObjectsOnCanvas().size(); i++) {
|
|
|
- if (model.getObjectsOnCanvas().get(i) instanceof HolonObject) {
|
|
|
- HolonObject obj = ((HolonObject) model.getObjectsOnCanvas().get(i));
|
|
|
- for (int j = 0; j < obj.getElements().size(); j++) {
|
|
|
- temp.add(obj.getElements().get(j).isActive());
|
|
|
+ ArrayList<AbstractCpsObject> canvas_objects = model.getObjectsOnCanvas();
|
|
|
+ for (int i = 0; i < canvas_objects.size(); i++) {
|
|
|
+ if (canvas_objects.get(i) instanceof HolonObject) {
|
|
|
+ HolonObject obj = ((HolonObject) canvas_objects.get(i));
|
|
|
+ ArrayList<HolonElement> obj_elements = obj.getElements();
|
|
|
+ for (int j = 0; j < obj_elements.size(); j++) {
|
|
|
+ temp.add(obj_elements.get(j).isActive());
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
- if(model.getObjectsOnCanvas().get(i) instanceof CpsUpperNode) {
|
|
|
- CpsUpperNode tmp = (CpsUpperNode) model.getObjectsOnCanvas().get(i);
|
|
|
- for( int j = 0; j < tmp.getNodes().size(); j++) {
|
|
|
- /*if(tmp.getNodes().get(j) instanceof HolonSwitch){
|
|
|
- HolonSwitch tmpSW = (HolonSwitch) tmp.getNodes().get(j);
|
|
|
- information.getCoord(0).addElement(tmpSW.getState())
|
|
|
- ;
|
|
|
- }*/
|
|
|
- if (tmp.getNodes().get(j) instanceof HolonObject) {
|
|
|
- HolonObject obj = ((HolonObject) tmp.getNodes().get(j));
|
|
|
+ if(canvas_objects.get(i) instanceof CpsUpperNode) {
|
|
|
+ CpsUpperNode tmp = (CpsUpperNode) canvas_objects.get(i);
|
|
|
+ ArrayList<AbstractCpsObject> upperNodecontent = tmp.getNodes();
|
|
|
+ for( int j = 0; j < upperNodecontent.size(); j++) {
|
|
|
+
|
|
|
+ //Does not work for recursiveness of more than two levels!!!!
|
|
|
+ if (upperNodecontent.get(j) instanceof HolonObject) {
|
|
|
+ HolonObject obj = ((HolonObject) upperNodecontent.get(j));
|
|
|
for (int k = 0; k < obj.getElements().size(); k++) {
|
|
|
temp.add(obj.getElements().get(k).isActive());
|
|
|
}
|
|
@@ -83,30 +92,40 @@ public class PSO implements CpsAlgorithm {
|
|
|
return information;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Applies the best configuration of the run on the model to make it visible on the canvas
|
|
|
+ * @param bestConfig
|
|
|
+ * @param model
|
|
|
+ * @param controller
|
|
|
+ */
|
|
|
public static void applyBestConfig(Coordinate<Vector<Object>> bestConfig, Model model, Control controller) {
|
|
|
System.out.println("Applying configuration: " + bestConfig.toString());
|
|
|
System.out.println("Config size: " + bestConfig.getCoord(1).size());
|
|
|
+ Vector<Object> switchstates = bestConfig.getCoord(0);
|
|
|
+ Vector<Object> elementstates = bestConfig.getCoord(1);
|
|
|
+
|
|
|
for (int i = 0; i < model.getSwitches().size(); i++) {
|
|
|
model.getSwitches().get(i).setManualMode(true);
|
|
|
- model.getSwitches().get(i).setManualState((boolean) bestConfig.getCoord(0).get(i));
|
|
|
+ model.getSwitches().get(i).setManualState((boolean) switchstates.get(i));
|
|
|
}
|
|
|
int position = 0;
|
|
|
for (int j = 0; j < model.getObjectsOnCanvas().size(); j++) {
|
|
|
if (model.getObjectsOnCanvas().get(j) instanceof HolonObject) {
|
|
|
for (int h = 0; h < ((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().size(); h++) {
|
|
|
((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().get(h)
|
|
|
- .setActive((boolean) bestConfig.getCoord(1).get(position));
|
|
|
+ .setActive((boolean) elementstates.get(position));
|
|
|
position++;
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
+ //Does not work for more than two recursive levels... seperate function recommended
|
|
|
if(model.getObjectsOnCanvas().get(j) instanceof CpsUpperNode) {
|
|
|
CpsUpperNode tmp = (CpsUpperNode) model.getObjectsOnCanvas().get(j);
|
|
|
for( int k = 0; k < tmp.getNodes().size(); k++) {
|
|
|
if (tmp.getNodes().get(k) instanceof HolonObject) {
|
|
|
HolonObject obj = ((HolonObject) tmp.getNodes().get(k));
|
|
|
for (int l = 0; l < obj.getElements().size(); l++) {
|
|
|
- obj.getElements().get(l).setActive((boolean) bestConfig.getCoord(1).get(position));
|
|
|
+ obj.getElements().get(l).setActive((boolean) elementstates.get(position));
|
|
|
position++;
|
|
|
|
|
|
}
|
|
@@ -119,6 +138,10 @@ public class PSO implements CpsAlgorithm {
|
|
|
controller.calculateStateForCurrentTimeStep();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Write stuff to file. Needs to get completely restructured.
|
|
|
+ */
|
|
|
public static void printChart(SimplePSO pso, PrintWriter out) {
|
|
|
int rounds = Constants.ROUNDS;
|
|
|
Vector<Double> record = null;
|
|
@@ -143,6 +166,11 @@ public class PSO implements CpsAlgorithm {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Unnecessary popup that is currently used to control the PSO runs, but should be replaced in the future
|
|
|
+ * @param model
|
|
|
+ * @param controller
|
|
|
+ */
|
|
|
public static void callPopUp(Model model, Control controller) {
|
|
|
System.out.println("Repeat");
|
|
|
// Declaration
|
|
@@ -262,7 +290,7 @@ public class PSO implements CpsAlgorithm {
|
|
|
|
|
|
|
|
|
JTextField runs = new JTextField();
|
|
|
- runs.setText("" + Constants.PHI);
|
|
|
+ runs.setText("");
|
|
|
runs.addKeyListener(new KeyListener() {
|
|
|
@Override
|
|
|
public void keyPressed(KeyEvent arg0) {
|
|
@@ -283,7 +311,7 @@ public class PSO implements CpsAlgorithm {
|
|
|
// Buttons
|
|
|
JButton run = new JButton("Run");
|
|
|
run.setActionCommand("Run");
|
|
|
- JButton nextIt = new JButton("Next It");
|
|
|
+ JButton nextIt = new JButton("Reset");
|
|
|
nextIt.setActionCommand("Next It.");
|
|
|
JCheckBox graph = new JCheckBox();
|
|
|
graph.setSelected(true);
|
|
@@ -349,7 +377,7 @@ public class PSO implements CpsAlgorithm {
|
|
|
out = new PrintWriter(new FileOutputStream(new File("gb_singlerun.txt"),true));
|
|
|
}else out = new PrintWriter("gb_singlerun.txt");
|
|
|
|
|
|
- printChart(pso, out);
|
|
|
+ //printChart(pso, out);
|
|
|
out.close();
|
|
|
} catch (IOException e1) {
|
|
|
// TODO Auto-generated catch block
|