|
@@ -5,6 +5,11 @@ import java.awt.Component;
|
|
import java.awt.Dimension;
|
|
import java.awt.Dimension;
|
|
import java.awt.FlowLayout;
|
|
import java.awt.FlowLayout;
|
|
import java.awt.image.BufferedImage;
|
|
import java.awt.image.BufferedImage;
|
|
|
|
+import java.io.BufferedWriter;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.OutputStreamWriter;
|
|
import java.math.RoundingMode;
|
|
import java.math.RoundingMode;
|
|
import java.text.NumberFormat;
|
|
import java.text.NumberFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
@@ -19,6 +24,7 @@ import javax.swing.BorderFactory;
|
|
import javax.swing.ImageIcon;
|
|
import javax.swing.ImageIcon;
|
|
import javax.swing.JButton;
|
|
import javax.swing.JButton;
|
|
import javax.swing.JCheckBox;
|
|
import javax.swing.JCheckBox;
|
|
|
|
+import javax.swing.JFileChooser;
|
|
import javax.swing.JFormattedTextField;
|
|
import javax.swing.JFormattedTextField;
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JLabel;
|
|
@@ -36,6 +42,7 @@ import classes.CpsUpperNode;
|
|
import classes.HolonElement;
|
|
import classes.HolonElement;
|
|
import classes.HolonObject;
|
|
import classes.HolonObject;
|
|
import classes.HolonSwitch;
|
|
import classes.HolonSwitch;
|
|
|
|
+import exampleAlgorithms.GaAlgorithm.RunDataBase;
|
|
import ui.controller.Control;
|
|
import ui.controller.Control;
|
|
import ui.model.DecoratedGroupNode;
|
|
import ui.model.DecoratedGroupNode;
|
|
import ui.model.DecoratedState;
|
|
import ui.model.DecoratedState;
|
|
@@ -74,10 +81,9 @@ public class AcoAlgorithm implements Algorithm {
|
|
//Parameter defined by Algo
|
|
//Parameter defined by Algo
|
|
private HashMap<Integer, AccessWrapper> access;
|
|
private HashMap<Integer, AccessWrapper> access;
|
|
LinkedList<List<Boolean>> resetChain = new LinkedList<List<Boolean>>();
|
|
LinkedList<List<Boolean>> resetChain = new LinkedList<List<Boolean>>();
|
|
- private List<Boolean> initialState;
|
|
|
|
private List<HolonSwitch> switchList;
|
|
private List<HolonSwitch> switchList;
|
|
private List<HolonObject> objectList;
|
|
private List<HolonObject> objectList;
|
|
-
|
|
|
|
|
|
+ private boolean append = false;
|
|
//Gui Part:
|
|
//Gui Part:
|
|
private Control control;
|
|
private Control control;
|
|
private Console console = new Console();
|
|
private Console console = new Console();
|
|
@@ -86,9 +92,10 @@ public class AcoAlgorithm implements Algorithm {
|
|
private JProgressBar progressBar = new JProgressBar();
|
|
private JProgressBar progressBar = new JProgressBar();
|
|
private int progressBarCount = 0;
|
|
private int progressBarCount = 0;
|
|
private long startTime;
|
|
private long startTime;
|
|
- private Thread runThread;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+ private Thread runThread = new Thread();
|
|
|
|
+ private RunDataBase db;
|
|
|
|
+ //Parameter for Plotting (Default Directory in Constructor)
|
|
|
|
+ private JFileChooser fileChooser = new JFileChooser();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -107,7 +114,6 @@ public class AcoAlgorithm implements Algorithm {
|
|
newFrame.pack();
|
|
newFrame.pack();
|
|
newFrame.setVisible(true);
|
|
newFrame.setVisible(true);
|
|
newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
-
|
|
|
|
}
|
|
}
|
|
public AcoAlgorithm() {
|
|
public AcoAlgorithm() {
|
|
content.setLayout(new BorderLayout());
|
|
content.setLayout(new BorderLayout());
|
|
@@ -116,6 +122,8 @@ public class AcoAlgorithm implements Algorithm {
|
|
splitPane.setResizeWeight(0.0);
|
|
splitPane.setResizeWeight(0.0);
|
|
content.add(splitPane, BorderLayout.CENTER);
|
|
content.add(splitPane, BorderLayout.CENTER);
|
|
content.setPreferredSize(new Dimension(800,800));
|
|
content.setPreferredSize(new Dimension(800,800));
|
|
|
|
+ fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
|
|
|
|
+ fileChooser.setSelectedFile(new File("plott.txt"));
|
|
}
|
|
}
|
|
public JPanel createOptionPanel() {
|
|
public JPanel createOptionPanel() {
|
|
JPanel optionPanel = new JPanel(new BorderLayout());
|
|
JPanel optionPanel = new JPanel(new BorderLayout());
|
|
@@ -260,6 +268,13 @@ public class AcoAlgorithm implements Algorithm {
|
|
}
|
|
}
|
|
public JPanel createButtonPanel() {
|
|
public JPanel createButtonPanel() {
|
|
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
|
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
|
|
|
+ JButton fitnessButton = new JButton("Fitness");
|
|
|
|
+ fitnessButton.setToolTipText("Fitness for the current state.");
|
|
|
|
+ fitnessButton.addActionListener(actionEvent -> fitness());
|
|
|
|
+ buttonPanel.add(fitnessButton);
|
|
|
|
+ JButton plottButton = new JButton("Plott");
|
|
|
|
+ plottButton.addActionListener(actionEvent -> plott());
|
|
|
|
+ buttonPanel.add(plottButton);
|
|
JButton resetButton = new JButton("ResetAll");
|
|
JButton resetButton = new JButton("ResetAll");
|
|
resetButton.setToolTipText("Resets the State to before the Algorithm has runed.");
|
|
resetButton.setToolTipText("Resets the State to before the Algorithm has runed.");
|
|
resetButton.addActionListener(actionEvent -> resetAll());
|
|
resetButton.addActionListener(actionEvent -> resetAll());
|
|
@@ -291,6 +306,16 @@ public class AcoAlgorithm implements Algorithm {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private void fitness() {
|
|
|
|
+ if(runThread.isAlive()) {
|
|
|
|
+ println("Run have to be cancelled First.");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ double currentFitness = evaluatePosition(extractPositionAndAccess(), false);
|
|
|
|
+ resetChain.removeLast();
|
|
|
|
+ console.println("Actual Fitnessvalue: " + currentFitness);
|
|
|
|
+ }
|
|
|
|
+
|
|
private void run() {
|
|
private void run() {
|
|
cancel = false;
|
|
cancel = false;
|
|
disableGuiInput(true);
|
|
disableGuiInput(true);
|
|
@@ -332,7 +357,15 @@ public class AcoAlgorithm implements Algorithm {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+ private void plott() {
|
|
|
|
+ if(db!=null) {
|
|
|
|
+ console.println("Plott..");
|
|
|
|
+ db.initFileStream();
|
|
|
|
+ }else {
|
|
|
|
+ console.println("No run inistialized.");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
private void disableGuiInput(boolean bool) {
|
|
private void disableGuiInput(boolean bool) {
|
|
control.guiDiable(bool);
|
|
control.guiDiable(bool);
|
|
@@ -396,12 +429,14 @@ public class AcoAlgorithm implements Algorithm {
|
|
startTimer();
|
|
startTimer();
|
|
Individual runBest = new Individual();
|
|
Individual runBest = new Individual();
|
|
runBest.fitness = Double.MAX_VALUE;
|
|
runBest.fitness = Double.MAX_VALUE;
|
|
|
|
+ db = new RunDataBase();
|
|
for(int r = 0; r < rounds; r++)
|
|
for(int r = 0; r < rounds; r++)
|
|
{
|
|
{
|
|
- Individual roundBest = executeAcoAlgo();
|
|
|
|
- if(cancel)return;
|
|
|
|
- resetState();
|
|
|
|
- if(roundBest.fitness < runBest.fitness) runBest = roundBest;
|
|
|
|
|
|
+ List<Double> runList = db.insertNewRun();
|
|
|
|
+ Individual roundBest = executeAcoAlgo(runList);
|
|
|
|
+ if(cancel)return;
|
|
|
|
+ resetState();
|
|
|
|
+ if(roundBest.fitness < runBest.fitness) runBest = roundBest;
|
|
}
|
|
}
|
|
printElapsedTime();
|
|
printElapsedTime();
|
|
setState(runBest.position);
|
|
setState(runBest.position);
|
|
@@ -431,11 +466,12 @@ public class AcoAlgorithm implements Algorithm {
|
|
* }
|
|
* }
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- private Individual executeAcoAlgo() {
|
|
|
|
|
|
+ private Individual executeAcoAlgo(List<Double> runList) {
|
|
Individual best = new Individual();
|
|
Individual best = new Individual();
|
|
best.position = extractPositionAndAccess();
|
|
best.position = extractPositionAndAccess();
|
|
if(moreInformation)println("Bit-Array_length: " + best.position.size());
|
|
if(moreInformation)println("Bit-Array_length: " + best.position.size());
|
|
best.fitness = evaluatePosition(best.position, false);
|
|
best.fitness = evaluatePosition(best.position, false);
|
|
|
|
+ runList.add(best.fitness);
|
|
console.print("Start with: " + best.fitness);
|
|
console.print("Start with: " + best.fitness);
|
|
if(moreInformation)println("");
|
|
if(moreInformation)println("");
|
|
int problemSize = best.position.size();
|
|
int problemSize = best.position.size();
|
|
@@ -452,6 +488,7 @@ public class AcoAlgorithm implements Algorithm {
|
|
if(moreInformation)println("Fitness" + i.fitness);
|
|
if(moreInformation)println("Fitness" + i.fitness);
|
|
if(i.fitness < best.fitness) best = i;
|
|
if(i.fitness < best.fitness) best = i;
|
|
}
|
|
}
|
|
|
|
+ runList.add(best.fitness);
|
|
if(moreInformation)println("________________");
|
|
if(moreInformation)println("________________");
|
|
vaporizeIntensifiePheromons(pheromones, best.position, problemSize);
|
|
vaporizeIntensifiePheromons(pheromones, best.position, problemSize);
|
|
double cf = calculateConvergenceFactor(pheromones, problemSize);
|
|
double cf = calculateConvergenceFactor(pheromones, problemSize);
|
|
@@ -552,7 +589,7 @@ public class AcoAlgorithm implements Algorithm {
|
|
Model model = control.getModel();
|
|
Model model = control.getModel();
|
|
switchList = new ArrayList<HolonSwitch>();
|
|
switchList = new ArrayList<HolonSwitch>();
|
|
objectList = new ArrayList<HolonObject>();
|
|
objectList = new ArrayList<HolonObject>();
|
|
- initialState = new ArrayList<Boolean>();
|
|
|
|
|
|
+ List<Boolean> initialState = new ArrayList<Boolean>();
|
|
access= new HashMap<Integer, AccessWrapper>();
|
|
access= new HashMap<Integer, AccessWrapper>();
|
|
rollOutNodes((useGroupNode && (dGroupNode != null))? dGroupNode.getModel().getNodes() :model.getObjectsOnCanvas(), initialState, model.getCurIteration());
|
|
rollOutNodes((useGroupNode && (dGroupNode != null))? dGroupNode.getModel().getNodes() :model.getObjectsOnCanvas(), initialState, model.getCurIteration());
|
|
resetChain.add(initialState);
|
|
resetChain.add(initialState);
|
|
@@ -675,7 +712,55 @@ public class AcoAlgorithm implements Algorithm {
|
|
return type;
|
|
return type;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ /**
|
|
|
|
+ * A Database for all Global Best(G<sub>Best</sub>) Values in a execution of a the Algo. For Easy Printing.
|
|
|
|
+ */
|
|
|
|
+ public class RunDataBase {
|
|
|
|
+ List<List<Double>> allRuns = new ArrayList<List<Double>>();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Initialize The Stream before you can write to a File.
|
|
|
|
+ */
|
|
|
|
+ public void initFileStream() {
|
|
|
|
+ File file = fileChooser.getSelectedFile();
|
|
|
|
+ try {
|
|
|
|
+ file.createNewFile();
|
|
|
|
+ BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
|
|
|
|
+ new FileOutputStream(file, append), "UTF-8"));
|
|
|
|
+ printToStream(out);
|
|
|
|
+ out.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ console.println(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void printToStream(BufferedWriter out) throws IOException {
|
|
|
|
+ try {
|
|
|
|
+ out.write(maxGenerations + 1 + "," + allRuns.size() + "," + popsize);
|
|
|
|
+ out.newLine();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ catch(IOException e) {
|
|
|
|
+ console.println(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ allRuns.forEach(run -> {
|
|
|
|
+ try {
|
|
|
|
+ out.write( run.stream().map(Object::toString).collect(Collectors.joining(", ")));
|
|
|
|
+ out.newLine();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ console.println(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ } );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public List<Double> insertNewRun(){
|
|
|
|
+ List<Double> newRun = new ArrayList<Double>();
|
|
|
|
+ allRuns.add(newRun);
|
|
|
|
+ return newRun;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
private class RunResult {
|
|
private class RunResult {
|
|
public int activatedFlex = 0;
|
|
public int activatedFlex = 0;
|