|
@@ -14,9 +14,12 @@ import java.math.RoundingMode;
|
|
|
import java.text.NumberFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
|
import java.util.Locale;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.function.BiFunction;
|
|
|
import java.util.function.Consumer;
|
|
|
import java.util.function.Supplier;
|
|
@@ -37,8 +40,9 @@ import javax.swing.JProgressBar;
|
|
|
import javax.swing.JScrollPane;
|
|
|
import javax.swing.JSplitPane;
|
|
|
import javax.swing.text.NumberFormatter;
|
|
|
-
|
|
|
import classes.AbstractCpsObject;
|
|
|
+import classes.Category;
|
|
|
+import classes.CpsEdge;
|
|
|
import classes.CpsUpperNode;
|
|
|
import classes.Flexibility;
|
|
|
import classes.HolonElement;
|
|
@@ -50,7 +54,9 @@ import ui.controller.FlexManager.FlexState;
|
|
|
import ui.controller.FlexManager.FlexWrapper;
|
|
|
import ui.model.DecoratedGroupNode;
|
|
|
import ui.model.DecoratedState;
|
|
|
+import ui.model.IntermediateCableWithState;
|
|
|
import ui.model.Model;
|
|
|
+import ui.model.DecoratedCable.CableState;
|
|
|
import ui.model.DecoratedHolonObject.HolonObjectState;
|
|
|
import ui.model.DecoratedSwitch.SwitchState;
|
|
|
import ui.model.DecoratedNetwork;
|
|
@@ -59,7 +65,7 @@ import ui.view.Console;
|
|
|
public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
//Algo
|
|
|
protected int rounds = 1;
|
|
|
-
|
|
|
+ protected int amountOfNewCables = 3;
|
|
|
|
|
|
|
|
|
//Panel
|
|
@@ -73,8 +79,26 @@ public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
|
|
|
|
|
|
//access
|
|
|
- private ArrayList<AccessWrapper> access;
|
|
|
+ private ArrayList<AccessWrapper> accessWildcards = new ArrayList<AccessWrapper>();
|
|
|
LinkedList<List<Integer>> resetChain = new LinkedList<List<Integer>>();
|
|
|
+
|
|
|
+
|
|
|
+ private HashMap<Integer, AbstractCpsObject> accessIntToObject = new HashMap<Integer, AbstractCpsObject>();
|
|
|
+ private HashMap<AbstractCpsObject, Integer> accessObjectToInt = new HashMap<AbstractCpsObject, Integer>();
|
|
|
+ private HashMap<Integer, AbstractCpsObject> accessIntegerToWildcard = new HashMap<Integer, AbstractCpsObject>();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private HashSet<IndexCable> cableSet = new HashSet<IndexCable>();
|
|
|
+ private HashMap<IndexCable, CpsEdge> addedCableMap = new HashMap<IndexCable, CpsEdge>();
|
|
|
+ private int countForAccessMap = 0;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
boolean algoUseElements = false, algoUseSwitches = true, algoUseFlexes = true;
|
|
|
|
|
|
//time
|
|
@@ -133,6 +157,7 @@ public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
parameterPanel.setPreferredSize(new Dimension(510,300));
|
|
|
borderPanel.setLayout(new BoxLayout(borderPanel, BoxLayout.PAGE_AXIS));
|
|
|
addIntParameter("Rounds", rounds, intInput -> rounds = intInput, () -> rounds, 1);
|
|
|
+ addIntParameter("amountOfNewCables", amountOfNewCables, intInput -> amountOfNewCables = intInput, () -> amountOfNewCables, 0);
|
|
|
JScrollPane scrollPane = new JScrollPane(borderPanel);
|
|
|
scrollPane.setBounds(10, 0, 850, 292);
|
|
|
scrollPane.setBorder(BorderFactory.createEmptyBorder());
|
|
@@ -143,6 +168,11 @@ public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
progressBar.setBounds(900, 35, 185, 20);
|
|
|
progressBar.setStringPainted(true);
|
|
|
parameterPanel.add(progressBar);
|
|
|
+
|
|
|
+ JButton addCategoryButton = new JButton("Add Category");
|
|
|
+ addCategoryButton.setBounds(900, 65, 185, 30);
|
|
|
+ addCategoryButton.addActionListener(clicked -> createWildcardsCategory());
|
|
|
+ parameterPanel.add(addCategoryButton);
|
|
|
return parameterPanel;
|
|
|
}
|
|
|
private JPanel createButtonPanel() {
|
|
@@ -397,15 +427,25 @@ public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
console.println("Nothing to cancel.");
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+ private void createWildcardsCategory() {
|
|
|
+ Category category = control.searchCategory("Wildcards");
|
|
|
+ if(category == null) {
|
|
|
+ try {
|
|
|
+ control.addCategory("Wildcards");
|
|
|
+ } catch (IOException e) {
|
|
|
+ console.println("IO Exception - Creating WIldcards Category failed.");
|
|
|
+ System.out.println("IO Exception - Creating WIldcards Category failed.");
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
private void fitness() {
|
|
|
if(runThread.isAlive()) {
|
|
|
console.println("Run have to be cancelled First.");
|
|
|
return;
|
|
|
}
|
|
|
- double currentFitness = evaluatePosition(extractPositionAndAccess());
|
|
|
- resetChain.removeLast();
|
|
|
+ double currentFitness = evaluateNetwork();
|
|
|
console.println("Actual Fitnessvalue: " + currentFitness);
|
|
|
}
|
|
|
|
|
@@ -419,12 +459,16 @@ public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
// startTime = endTime;
|
|
|
setState(positionToEvaluate); // execution time critical
|
|
|
|
|
|
+ return evaluateNetwork();
|
|
|
+ }
|
|
|
+
|
|
|
+ private double evaluateNetwork() {
|
|
|
control.calculateStateOnlyForCurrentTimeStep();
|
|
|
DecoratedState actualstate = control.getSimManager().getActualDecorState();
|
|
|
- double result = evaluateState(actualstate);
|
|
|
- return result;
|
|
|
+ return evaluateState(actualstate);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
protected abstract double evaluateState(DecoratedState actualstate);
|
|
|
|
|
|
|
|
@@ -497,7 +541,6 @@ public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
|
|
|
private void executeAlgoWithParameter(){
|
|
|
double startFitness = evaluatePosition(extractPositionAndAccess());
|
|
|
- console.println("BitLength: " + access.size());
|
|
|
resetChain.removeLast();
|
|
|
runPrinter.openStream();
|
|
|
runPrinter.println("");
|
|
@@ -519,12 +562,11 @@ public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
runPrinter.println(stringStatFromActualState());
|
|
|
runPrinter.println("Result: " + roundBest.fitness + " ExecutionTime:" + executionTime);
|
|
|
runPrinter.closeStream();
|
|
|
- resetState();
|
|
|
+ //resetState();
|
|
|
if(roundBest.fitness < runBest.fitness) runBest = roundBest;
|
|
|
}
|
|
|
|
|
|
control.getSimManager().resetFlexManagerForTimeStep(control.getModel().getCurIteration());
|
|
|
- this.extractPositionAndAccess();
|
|
|
setState(runBest.position);
|
|
|
updateVisual();
|
|
|
console.println("Start: " + startFitness);
|
|
@@ -585,11 +627,14 @@ public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
* @param position
|
|
|
*/
|
|
|
private void setState(List<Integer> position) {
|
|
|
- int i = 0;
|
|
|
- for(Integer integer: position) {
|
|
|
- access.get(i++).setState(integer);
|
|
|
+ this.removeAllAddedCables();
|
|
|
+ for(int i = 0; i < this.amountOfNewCables; i++) {
|
|
|
+ generateCable(position.get(2 * i), position.get(2 * i + 1));
|
|
|
+ }
|
|
|
+ int count = 0;
|
|
|
+ for(int i = 2 * this.amountOfNewCables; i < position.size(); i++) {
|
|
|
+ accessWildcards.get(count++).setState(position.get(i));
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
|
|
@@ -601,29 +646,128 @@ public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
*/
|
|
|
protected List<Integer> extractPositionAndAccess() {
|
|
|
Model model = control.getModel();
|
|
|
- access= new ArrayList<AccessWrapper>();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //-->reset
|
|
|
+ accessWildcards.clear();
|
|
|
+ this.countForAccessMap = 0;
|
|
|
+ accessIntToObject.clear();
|
|
|
+ accessObjectToInt.clear();
|
|
|
+ cableSet.clear();
|
|
|
+ //<---
|
|
|
+ Category category = control.searchCategory("Wildcards");
|
|
|
+ if(category != null) {
|
|
|
+ for(int count = 0; count < category.getObjects().size(); count++ ) {
|
|
|
+ accessIntegerToWildcard.put(count + 1, category.getObjects().get(count));
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ console.println("No 'Wildcards' Category");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
List<Integer> initialState = new ArrayList<Integer>();
|
|
|
- rollOutNodes((dGroupNode != null)? dGroupNode.getModel().getNodes() :model.getObjectsOnCanvas(), initialState, model.getCurIteration());
|
|
|
+ generateAccess(model.getObjectsOnCanvas());
|
|
|
+ addCables(model.getEdgesOnCanvas());
|
|
|
+ getCables(model.getObjectsOnCanvas());
|
|
|
+ for(int i = 0; i < this.amountOfNewCables; i++) {
|
|
|
+ initialState.add(0);
|
|
|
+ initialState.add(0);
|
|
|
+ }
|
|
|
+ for(int i = 0; i < accessWildcards.size(); i++) {
|
|
|
+ initialState.add(0);
|
|
|
+ }
|
|
|
resetChain.add(initialState);
|
|
|
- //console.println(access.stream().map(Object::toString).collect(Collectors.joining(", ")));
|
|
|
+ //console.println(accessIntToObject.values().stream().map(hO -> hO.getName()).collect(Collectors.joining(", ")));
|
|
|
+ //console.println(cableSet.stream().map(Object::toString).collect(Collectors.f(", ")));
|
|
|
return initialState;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* Method to extract the Informations recursively out of the Model.
|
|
|
* @param nodes
|
|
|
* @param positionToInit
|
|
|
* @param timeStep
|
|
|
*/
|
|
|
- private void rollOutNodes(List<AbstractCpsObject> nodes, List<Integer> positionToInit, int timeStep) {
|
|
|
+ private void generateAccess(List<AbstractCpsObject> nodes) {
|
|
|
+ for(AbstractCpsObject aCps : nodes) {
|
|
|
+ if(aCps instanceof HolonObject) {
|
|
|
+ HolonObject hO = (HolonObject) aCps;
|
|
|
+ accessIntToObject.put(++countForAccessMap, hO);
|
|
|
+ accessObjectToInt.put(hO, countForAccessMap);
|
|
|
+ if(hO.getName().equals("Wildcard")) {
|
|
|
+ accessWildcards.add(new AccessWrapper(hO));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if(aCps instanceof CpsUpperNode) {
|
|
|
+ generateAccess(((CpsUpperNode)aCps).getNodes());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private void getCables(List<AbstractCpsObject> nodes) {
|
|
|
for(AbstractCpsObject aCps : nodes) {
|
|
|
if(aCps instanceof CpsUpperNode) {
|
|
|
- rollOutNodes(((CpsUpperNode)aCps).getNodes(), positionToInit ,timeStep );
|
|
|
+ CpsUpperNode aUpperNode = (CpsUpperNode)aCps;
|
|
|
+ addCables(aUpperNode.getNodeEdges());
|
|
|
+ addCables(aUpperNode.getOldEdges());
|
|
|
+ getCables(((CpsUpperNode)aCps).getNodes());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
+ protected void resetWildcards() {
|
|
|
+ this.accessWildcards.forEach(wrapper -> wrapper.resetState());
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * All Nodes have to be in the access map !!
|
|
|
+ * @param cables
|
|
|
+ */
|
|
|
+ private void addCables(List<CpsEdge> edges) {
|
|
|
+ for (CpsEdge edge : edges) {
|
|
|
+ edge.setUnlimitedCapacity(true);
|
|
|
+ //console.println("Cable from " + edge.getA().getName() + " to " + edge.getB().getName());
|
|
|
+ if(!accessObjectToInt.containsKey(edge.getA())) {
|
|
|
+ console.println("Node A from Edge[" + edge + "] not exist");
|
|
|
+ continue;
|
|
|
+ } else if (!accessObjectToInt.containsKey(edge.getB())) {
|
|
|
+ console.println("Node B from Edge[" + edge + "] not exist");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ cableSet.add(new IndexCable(accessObjectToInt.get(edge.getA()), accessObjectToInt.get(edge.getB())));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private void generateCable(int index1, int index2) {
|
|
|
+ //If cable isnt valid
|
|
|
+ if(index1 == 0 || index2 == 0 || index1 == index2) {
|
|
|
+ //console.println("Cable("+index1+","+index2+ ") isn't valid");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ IndexCable cable = new IndexCable(index1, index2);
|
|
|
+ //if cable is in existing cables
|
|
|
+ if(cableSet.contains(cable) || addedCableMap.containsKey(cable)) {
|
|
|
+ //console.println("Cable("+index1+","+index2+ ") already exist");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ CpsEdge edge = new CpsEdge(accessIntToObject.get(index1), accessIntToObject.get(index2));
|
|
|
+ edge.setUnlimitedCapacity(true);
|
|
|
+ control.getModel().getEdgesOnCanvas().add(edge);
|
|
|
+ addedCableMap.put(cable, edge);
|
|
|
+ }
|
|
|
+ private void removeAllAddedCables() {
|
|
|
+ control.getModel().getEdgesOnCanvas().removeAll(addedCableMap.values());
|
|
|
+ addedCableMap.clear();
|
|
|
+ }
|
|
|
|
|
|
|
|
|
private String stringStatFromActualState() {
|
|
@@ -726,6 +870,9 @@ public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
this.control = control;
|
|
|
}
|
|
|
|
|
|
+ public int getMaximumIndexObjects(int index) {
|
|
|
+ return (index < 2 * amountOfNewCables)? this.countForAccessMap: accessWildcards.size();
|
|
|
+ }
|
|
|
|
|
|
|
|
|
private class RunProgressBar{
|
|
@@ -805,18 +952,33 @@ public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
* A Wrapper Class for Access HolonElement and HolonSwitch in one Element and not have to split the List.
|
|
|
*/
|
|
|
private class AccessWrapper {
|
|
|
-
|
|
|
- public AccessWrapper(int a) {
|
|
|
-
|
|
|
+ int state = 0;
|
|
|
+ HolonObject wildcard;
|
|
|
+ public AccessWrapper(HolonObject wildcard) {
|
|
|
+ this.wildcard = wildcard;
|
|
|
}
|
|
|
|
|
|
public void setState(int state) {
|
|
|
- //TODO set state
|
|
|
+ if(this.state != state) {
|
|
|
+ this.state = state;
|
|
|
+ wildcard.getElements().clear();
|
|
|
+ if(state > 0) {
|
|
|
+ HolonObject hO = (HolonObject)accessIntegerToWildcard.get(state);
|
|
|
+ if(hO == null) {
|
|
|
+ console.println("null set state(" + state + ")");
|
|
|
+ }else {
|
|
|
+ wildcard.getElements().addAll(hO.getElements());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public void resetState() {
|
|
|
+ state = 0;
|
|
|
+ wildcard.getElements().clear();
|
|
|
}
|
|
|
-
|
|
|
|
|
|
public String toString() {
|
|
|
- return "Hollow";
|
|
|
+ return wildcard + "have state: " + state;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -859,6 +1021,26 @@ public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
+ /**
|
|
|
+ * Random Int in Range [min;max[ with UniformDistirbution
|
|
|
+ * @param min
|
|
|
+ * @param max
|
|
|
+ * @param valueBetween a value between min and max
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int nextIntegerInRangeExcept(int min, int max,int valueBetween) {
|
|
|
+ int result = min;
|
|
|
+ try {
|
|
|
+ result = min + random.nextInt((max - 1) - min);
|
|
|
+ if(result >= valueBetween) {
|
|
|
+ result++;
|
|
|
+ }
|
|
|
+ }catch(java.lang.IllegalArgumentException e){
|
|
|
+ System.err.println("min : " + min + " max : " + max);
|
|
|
+ System.err.println("max should be more then min");
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private class Handle<T>{
|
|
@@ -926,4 +1108,42 @@ public abstract class TopologieAlgorithmFramework implements AddOn{
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public class IndexCable{
|
|
|
+ public final Integer first;
|
|
|
+ public final Integer second;
|
|
|
+
|
|
|
+ public IndexCable(Integer first, Integer second) {
|
|
|
+ if(first.compareTo(second) == 0) {
|
|
|
+ throw new IllegalArgumentException("(" + first + "==" + second + ")"
|
|
|
+ + "Two ends of the cable are at the same Object");
|
|
|
+ } else if(first.compareTo(second) < 0) {
|
|
|
+ this.first = first;
|
|
|
+ this.second = second;
|
|
|
+ }else {
|
|
|
+ this.first = second;
|
|
|
+ this.second = first;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean equals(Object o) {
|
|
|
+ if (!(o instanceof IndexCable)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ IndexCable p = (IndexCable) o;
|
|
|
+ return Objects.equals(p.first, first) && Objects.equals(p.second, second);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int hashCode() {
|
|
|
+ return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return "{" + first + "," + second + "}";
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|