Tom Troppmann 4 years ago
parent
commit
891ec61670

+ 1 - 1
src/exampleAlgorithms/InformationPanel.java → src/addOns/InformationPanel.java

@@ -1,4 +1,4 @@
-package exampleAlgorithms;
+package addOns;
 
 import java.awt.BorderLayout;
 import java.awt.Dimension;

+ 1 - 1
src/exampleAlgorithms/RandomPriority.java → src/addOns/RandomPriority.java

@@ -1,4 +1,4 @@
-package exampleAlgorithms;
+package addOns;
 
 import java.awt.BorderLayout;
 import java.awt.Dimension;

+ 1 - 1
src/exampleAlgorithms/RandomSwitch.java → src/addOns/RandomSwitch.java

@@ -1,4 +1,4 @@
-package exampleAlgorithms;
+package addOns;
 
 import java.awt.BorderLayout;
 import java.awt.Dimension;

+ 1 - 1
src/exampleAlgorithms/Randomizer.java → src/addOns/Randomizer.java

@@ -1,4 +1,4 @@
-package exampleAlgorithms;
+package addOns;
 
 import java.awt.BorderLayout;
 import java.awt.Dimension;

+ 2 - 1
src/exampleAlgorithms/AcoAlgorithm.java → src/algorithm/binary/AcoAlgorithm.java

@@ -1,4 +1,4 @@
-package exampleAlgorithms;
+package algorithm.binary;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -6,6 +6,7 @@ import java.util.ListIterator;
 
 import javax.swing.JFrame;
 
+import algorithm.objectiveFunction.ObjectiveFunctionByCarlos;
 import api.AlgorithmFrameworkFlex;
 import ui.model.DecoratedState;
 

+ 2 - 1
src/exampleAlgorithms/GaAlgorithm.java → src/algorithm/binary/GaAlgorithm.java

@@ -1,4 +1,4 @@
-package exampleAlgorithms;
+package algorithm.binary;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -7,6 +7,7 @@ import java.util.TreeSet;
 
 import javax.swing.JFrame;
 
+import algorithm.objectiveFunction.ObjectiveFunctionByCarlos;
 import api.AlgorithmFrameworkFlex;
 import ui.model.DecoratedState;
 

+ 2 - 2
src/exampleAlgorithms/PsoAlgorithm.java → src/algorithm/binary/PsoAlgorithm.java

@@ -1,4 +1,4 @@
-package exampleAlgorithms;
+package algorithm.binary;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -7,7 +7,7 @@ import java.util.stream.Collectors;
 
 import javax.swing.JFrame;
 
-
+import algorithm.objectiveFunction.ObjectiveFunctionByCarlos;
 import api.AlgorithmFrameworkFlex;
 import ui.model.DecoratedState;
 

+ 1 - 1
src/exampleAlgorithms/BaseLine.java → src/algorithm/example/BaseLine.java

@@ -1,4 +1,4 @@
-package exampleAlgorithms;
+package algorithm.example;
 
 import java.awt.BorderLayout;
 import java.awt.Component;

+ 1 - 1
src/exampleAlgorithms/DemoAlgo.java → src/algorithm/example/DemoAlgo.java

@@ -1,4 +1,4 @@
-package exampleAlgorithms;
+package algorithm.example;
 
 import java.awt.BorderLayout;
 import java.awt.Component;

+ 1 - 1
src/exampleAlgorithms/FlexExample.java → src/algorithm/example/FlexExample.java

@@ -1,4 +1,4 @@
-package exampleAlgorithms;
+package algorithm.example;
 
 import java.awt.BorderLayout;
 import java.awt.Component;

+ 1 - 1
src/exampleAlgorithms/Evaluation.java → src/algorithm/objectiveFunction/Evaluation.java

@@ -1,4 +1,4 @@
-package exampleAlgorithms;
+package algorithm.objectiveFunction;
 
 import classes.HolonElement.Priority;
 import classes.HolonObject;

+ 1 - 1
src/exampleAlgorithms/ObjectiveFunctionByCarlos.java → src/algorithm/objectiveFunction/ObjectiveFunctionByCarlos.java

@@ -1,4 +1,4 @@
-package exampleAlgorithms;
+package algorithm.objectiveFunction;
 
 import ui.model.DecoratedNetwork;
 import ui.model.DecoratedState;

+ 929 - 0
src/api/TopologieAlgorithmFramework.java

@@ -0,0 +1,929 @@
+package api;
+
+import java.awt.BorderLayout;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.FlowLayout;
+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.text.NumberFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Locale;
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+import javax.swing.BorderFactory;
+import javax.swing.Box;
+import javax.swing.BoxLayout;
+import javax.swing.ImageIcon;
+import javax.swing.JButton;
+import javax.swing.JCheckBox;
+import javax.swing.JFileChooser;
+import javax.swing.JFormattedTextField;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JProgressBar;
+import javax.swing.JScrollPane;
+import javax.swing.JSplitPane;
+import javax.swing.text.NumberFormatter;
+
+import classes.AbstractCpsObject;
+import classes.CpsUpperNode;
+import classes.Flexibility;
+import classes.HolonElement;
+import classes.HolonObject;
+import classes.HolonSwitch;
+import classes.HolonElement.Priority;
+import ui.controller.Control;
+import ui.controller.FlexManager.FlexState;
+import ui.controller.FlexManager.FlexWrapper;
+import ui.model.DecoratedGroupNode;
+import ui.model.DecoratedState;
+import ui.model.Model;
+import ui.model.DecoratedHolonObject.HolonObjectState;
+import ui.model.DecoratedSwitch.SwitchState;
+import ui.model.DecoratedNetwork;
+import ui.view.Console;
+
+public abstract class TopologieAlgorithmFramework implements AddOn{
+	//Algo
+	protected int rounds = 1;
+	
+	
+	
+	//Panel
+	private JPanel content = new JPanel();
+	protected Console console = new Console();
+	private JPanel borderPanel = new JPanel();
+	
+	
+	//Settings groupNode
+	private DecoratedGroupNode dGroupNode = null;
+	
+	
+	//access
+	private ArrayList<AccessWrapper> access;
+	LinkedList<List<Integer>> resetChain = new LinkedList<List<Integer>>();
+	boolean algoUseElements = false, algoUseSwitches = true, algoUseFlexes = true;
+	
+	//time
+	private long startTime;
+	
+	
+	private RunProgressBar runProgressbar = new RunProgressBar();
+	
+	
+	
+	//concurrency
+	private Thread runThread = new Thread();
+	protected boolean cancel = false;
+
+	//holeg interaction
+	protected Control  control;
+
+	
+	//printing
+	private Printer runPrinter = new Printer(plottFileName());
+	protected List<Double> runList = new LinkedList<Double>();
+
+	//Parameter
+	@SuppressWarnings("rawtypes")
+	LinkedList<ParameterStepping> parameterSteppingList= new LinkedList<ParameterStepping>();
+	protected boolean useStepping = false;
+	
+
+	
+	
+	public TopologieAlgorithmFramework(){
+		content.setLayout(new BorderLayout());
+		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
+				createOptionPanel() , console);
+		splitPane.setResizeWeight(0.0);
+		content.add(splitPane, BorderLayout.CENTER);
+		content.setPreferredSize(new Dimension(1200,800));
+	}
+	
+	
+	
+	
+	
+	
+	private JPanel createOptionPanel() {
+		JPanel optionPanel = new JPanel(new BorderLayout());
+		JScrollPane scrollPane = new JScrollPane(createParameterPanel());
+		scrollPane.setBorder(BorderFactory.createTitledBorder("Parameter"));
+		optionPanel.add(scrollPane,  BorderLayout.CENTER);
+		optionPanel.add(createButtonPanel(), BorderLayout.PAGE_END);
+		return optionPanel;
+	}
+	
+	private JPanel createParameterPanel() {
+		JPanel parameterPanel = new JPanel(null);
+		parameterPanel.setPreferredSize(new Dimension(510,300));
+		borderPanel.setLayout(new BoxLayout(borderPanel, BoxLayout.PAGE_AXIS));
+		addIntParameter("Rounds", rounds, intInput -> rounds = intInput, () -> rounds, 1);
+		JScrollPane scrollPane = new JScrollPane(borderPanel);
+		scrollPane.setBounds(10, 0, 850, 292);
+		scrollPane.setBorder(BorderFactory.createEmptyBorder());
+		parameterPanel.add(scrollPane);	
+		
+		
+		JProgressBar progressBar = runProgressbar.getJProgressBar();
+		progressBar.setBounds(900, 35, 185, 20);
+		progressBar.setStringPainted(true);
+		parameterPanel.add(progressBar);
+		return parameterPanel;
+	}
+	private JPanel createButtonPanel() {
+		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
+		
+		JButton resetButton =  new JButton("Reset");
+		resetButton.setToolTipText("Resets the State to before the Algorithm has runed.");
+		resetButton.addActionListener(actionEvent -> reset());
+		buttonPanel.add(resetButton);
+		
+		JButton cancelButton =  new JButton("Cancel Run");
+		cancelButton.addActionListener(actionEvent -> cancel());
+		buttonPanel.add(cancelButton);
+		
+		JButton fitnessButton =  new JButton("Fitness");
+		fitnessButton.setToolTipText("Fitness for the current state.");
+		fitnessButton.addActionListener(actionEvent -> fitness());
+		buttonPanel.add(fitnessButton);
+		
+		JButton runButton =  new JButton("Run");
+		runButton.addActionListener(actionEvent -> {
+			Runnable task = () -> run();
+			runThread = new Thread(task);
+			runThread.start();
+		});
+		buttonPanel.add(runButton);
+		
+		
+		
+		return buttonPanel;
+	}
+	
+	
+	
+	//ParameterImports
+	
+	//int
+	protected void addIntParameter(String parameterName, int parameterValue, Consumer<Integer> setter, Supplier<Integer> getter) {
+		this.addIntParameter(parameterName, parameterValue, setter, getter, Integer.MIN_VALUE, Integer.MAX_VALUE);
+	}
+	
+	protected void addIntParameter(String parameterName, int parameterValue, Consumer<Integer> setter, Supplier<Integer> getter, int parameterMinValue) {
+		this.addIntParameter(parameterName, parameterValue, setter, getter, parameterMinValue, Integer.MAX_VALUE);
+	}
+	
+	protected void addIntParameter(String parameterName, int parameterValue, Consumer<Integer> setter, Supplier<Integer> getter, int parameterMinValue, int parameterMaxValue) {
+		JPanel singleParameterPanel = new JPanel();
+		singleParameterPanel.setLayout(new BoxLayout(singleParameterPanel, BoxLayout.LINE_AXIS));
+		singleParameterPanel.setAlignmentX(0.0f);
+		singleParameterPanel.add(new JLabel(parameterName + ": "));
+		singleParameterPanel.add(Box.createHorizontalGlue());
+		NumberFormat format = NumberFormat.getIntegerInstance();
+		format.setGroupingUsed(false);
+		format.setParseIntegerOnly(true);
+		NumberFormatter integerFormatter = new NumberFormatter(format);
+		integerFormatter.setMinimum(parameterMinValue);
+		integerFormatter.setMaximum(parameterMaxValue);
+		integerFormatter.setCommitsOnValidEdit(true);
+		JFormattedTextField singleParameterTextField = new  JFormattedTextField(integerFormatter);
+		singleParameterTextField.setValue(parameterValue);
+		String minValue = (parameterMinValue == Integer.MIN_VALUE)?"Integer.MIN_VALUE":String.valueOf(parameterMinValue);
+		String maxValue = (parameterMaxValue == Integer.MAX_VALUE)?"Integer.MAX_VALUE":String.valueOf(parameterMaxValue);
+		singleParameterTextField.setToolTipText("Only integer \u2208 [" + minValue + "," + maxValue + "]");
+		singleParameterTextField.addPropertyChangeListener(actionEvent -> setter.accept(Integer.parseInt(singleParameterTextField.getValue().toString())));
+		singleParameterTextField.setMaximumSize(new Dimension(200, 30));
+		singleParameterTextField.setPreferredSize(new Dimension(200, 30));
+		singleParameterPanel.add(singleParameterTextField);
+		
+		
+		ParameterStepping<Integer> intParameterStepping = new ParameterStepping<Integer>(setter, getter, Integer::sum , (a,b) -> a * b, 1, 1);
+		intParameterStepping.useThisParameter = false;
+		parameterSteppingList.add(intParameterStepping);
+		
+		JCheckBox useSteppingCheckBox = new JCheckBox();
+		useSteppingCheckBox.setSelected(false);
+		singleParameterPanel.add(useSteppingCheckBox);
+		
+		
+		
+		JLabel stepsLabel = new JLabel("Steps: ");
+		stepsLabel.setEnabled(false);
+		singleParameterPanel.add(stepsLabel);
+		
+		NumberFormatter stepFormatter = new NumberFormatter(format);
+		stepFormatter.setMinimum(1);
+		stepFormatter.setMaximum(Integer.MAX_VALUE);
+		stepFormatter.setCommitsOnValidEdit(true);
+		
+		
+		JFormattedTextField stepsTextField = new  JFormattedTextField(stepFormatter);
+		stepsTextField.setEnabled(false);
+		stepsTextField.setValue(1);
+		stepsTextField.setToolTipText("Only integer \u2208 [" + 1 + "," + Integer.MAX_VALUE + "]");
+		stepsTextField.addPropertyChangeListener(actionEvent -> intParameterStepping.stepps = Integer.parseInt(stepsTextField.getValue().toString()));
+		stepsTextField.setMaximumSize(new Dimension(40, 30));
+		stepsTextField.setPreferredSize(new Dimension(40, 30));
+		singleParameterPanel.add(stepsTextField);
+		
+		JLabel stepsSizeLabel = new JLabel("StepsSize: ");
+		stepsSizeLabel.setEnabled(false);
+		singleParameterPanel.add(stepsSizeLabel);
+		
+		JFormattedTextField stepsSizeTextField = new  JFormattedTextField(stepFormatter);
+		stepsSizeTextField.setEnabled(false);
+		stepsSizeTextField.setValue(1);
+		stepsSizeTextField.setToolTipText("Only integer \u2208 [" + 1 + "," + Integer.MAX_VALUE + "]");
+		stepsSizeTextField.addPropertyChangeListener(actionEvent -> intParameterStepping.stepSize = Integer.parseInt(stepsSizeTextField.getValue().toString()));
+		stepsSizeTextField.setMaximumSize(new Dimension(40, 30));
+		stepsSizeTextField.setPreferredSize(new Dimension(40, 30));
+		singleParameterPanel.add(stepsSizeTextField);
+		
+		useSteppingCheckBox.addActionListener(actionEvent -> {
+			boolean enabled = useSteppingCheckBox.isSelected();
+			intParameterStepping.useThisParameter = enabled;
+			this.useStepping = this.parameterSteppingList.stream().anyMatch(parameter -> parameter.useThisParameter);
+			stepsLabel.setEnabled(enabled);
+			stepsTextField.setEnabled(enabled);
+			stepsSizeLabel.setEnabled(enabled);
+			stepsSizeTextField.setEnabled(enabled);
+		});
+		
+		borderPanel.add(singleParameterPanel);
+	}
+	
+	
+	//double
+	protected void addDoubleParameter(String parameterName, double parameterValue, Consumer<Double> setter, Supplier<Double> getter) {
+		this.addDoubleParameter(parameterName, parameterValue, setter, getter, Double.MIN_VALUE, Double.MAX_VALUE);
+	}
+	
+	
+	protected void addDoubleParameter(String parameterName, double parameterValue, Consumer<Double> setter, Supplier<Double> getter, double parameterMinValue) {
+		this.addDoubleParameter(parameterName, parameterValue, setter, getter, parameterMinValue, Double.MAX_VALUE);
+	}
+	
+	
+	protected void addDoubleParameter(String parameterName, double parameterValue, Consumer<Double> setter, Supplier<Double> getter, double parameterMinValue, double parameterMaxValue) {
+		JPanel singleParameterPanel = new JPanel();
+		singleParameterPanel.setLayout(new BoxLayout(singleParameterPanel, BoxLayout.LINE_AXIS));
+		singleParameterPanel.setAlignmentX(0.0f);
+		singleParameterPanel.add(new JLabel(parameterName + ": "));
+		singleParameterPanel.add(Box.createHorizontalGlue());
+		NumberFormat doubleFormat = NumberFormat.getNumberInstance(Locale.US);
+		doubleFormat.setMinimumFractionDigits(1);
+		doubleFormat.setMaximumFractionDigits(10);
+		doubleFormat.setRoundingMode(RoundingMode.HALF_UP);
+		NumberFormatter doubleFormatter = new NumberFormatter(doubleFormat);
+		doubleFormatter.setMinimum(parameterMinValue);
+		doubleFormatter.setMaximum(parameterMaxValue);
+		doubleFormatter.setCommitsOnValidEdit(true);
+		JFormattedTextField singleParameterTextField = new  JFormattedTextField(doubleFormatter);
+		singleParameterTextField.setValue(parameterValue);
+		String minValue = (parameterMinValue == Double.MIN_VALUE)?"Double.MIN_VALUE":String.valueOf(parameterMinValue);
+		String maxValue = (parameterMaxValue == Double.MAX_VALUE)?"Double.MAX_VALUE":String.valueOf(parameterMaxValue);
+		singleParameterTextField.setToolTipText("Only double \u2208 [" + minValue + "," + maxValue + "]");
+		singleParameterTextField.addPropertyChangeListener(actionEvent -> setter.accept(Double.parseDouble(singleParameterTextField.getValue().toString())));
+		singleParameterTextField.setMaximumSize(new Dimension(200, 30));
+		singleParameterTextField.setPreferredSize(new Dimension(200, 30));
+		singleParameterPanel.add(singleParameterTextField);
+		
+		ParameterStepping<Double> doubleParameterStepping = new ParameterStepping<Double>(setter, getter, (a,b) -> a+b , (a,b) -> a * b, 1.0, 1);
+		doubleParameterStepping.useThisParameter = false;
+		parameterSteppingList.add(doubleParameterStepping);
+		
+		JCheckBox useSteppingCheckBox = new JCheckBox();
+		useSteppingCheckBox.setSelected(false);
+		singleParameterPanel.add(useSteppingCheckBox);
+		
+		
+		
+		JLabel stepsLabel = new JLabel("Steps: ");
+		stepsLabel.setEnabled(false);
+		singleParameterPanel.add(stepsLabel);
+		NumberFormat format = NumberFormat.getIntegerInstance();
+		format.setGroupingUsed(false);
+		format.setParseIntegerOnly(true);
+		NumberFormatter integerFormatter = new NumberFormatter(format);
+		integerFormatter.setMinimum(1);
+		integerFormatter.setMaximum(Integer.MAX_VALUE);
+		integerFormatter.setCommitsOnValidEdit(true);
+	
+		
+		
+		JFormattedTextField stepsTextField = new  JFormattedTextField(integerFormatter);
+		stepsTextField.setEnabled(false);
+		stepsTextField.setValue(1);
+		stepsTextField.setToolTipText("Only integer \u2208 [" + 1 + "," + Integer.MAX_VALUE + "]");
+		stepsTextField.addPropertyChangeListener(actionEvent -> doubleParameterStepping.stepps = Integer.parseInt(stepsTextField.getValue().toString()));
+		stepsTextField.setMaximumSize(new Dimension(40, 30));
+		stepsTextField.setPreferredSize(new Dimension(40, 30));
+		singleParameterPanel.add(stepsTextField);
+		
+		JLabel stepsSizeLabel = new JLabel("StepsSize: ");
+		stepsSizeLabel.setEnabled(false);
+		singleParameterPanel.add(stepsSizeLabel);
+		
+		NumberFormatter doubleFormatterForStepping = new NumberFormatter(doubleFormat);
+		doubleFormatterForStepping.setCommitsOnValidEdit(true);
+		JFormattedTextField stepsSizeTextField = new  JFormattedTextField(doubleFormatterForStepping);
+		stepsSizeTextField.setEnabled(false);
+		stepsSizeTextField.setValue(1.0);
+		stepsSizeTextField.setToolTipText("Only double");
+		stepsSizeTextField.addPropertyChangeListener(actionEvent -> doubleParameterStepping.stepSize = Double.parseDouble(stepsSizeTextField.getValue().toString()));
+		stepsSizeTextField.setMaximumSize(new Dimension(40, 30));
+		stepsSizeTextField.setPreferredSize(new Dimension(40, 30));
+		singleParameterPanel.add(stepsSizeTextField);
+		
+		useSteppingCheckBox.addActionListener(actionEvent -> {
+			boolean enabled = useSteppingCheckBox.isSelected();
+			doubleParameterStepping.useThisParameter = enabled;
+			this.useStepping = this.parameterSteppingList.stream().anyMatch(parameter -> parameter.useThisParameter);
+			stepsLabel.setEnabled(enabled);
+			stepsTextField.setEnabled(enabled);
+			stepsSizeLabel.setEnabled(enabled);
+			stepsSizeTextField.setEnabled(enabled);
+		});
+		
+		borderPanel.add(singleParameterPanel);
+	}
+	//boolean
+	protected void addBooleanParameter(String parameterName, boolean parameterValue, Consumer<Boolean> setter){
+		JPanel singleParameterPanel = new JPanel();
+		singleParameterPanel.setLayout(new BoxLayout(singleParameterPanel, BoxLayout.LINE_AXIS));
+		singleParameterPanel.setAlignmentX(0.0f);
+		singleParameterPanel.add(new JLabel(parameterName + ": "));
+		singleParameterPanel.add(Box.createHorizontalGlue());
+		JCheckBox useGroupNodeCheckBox = new JCheckBox();
+		useGroupNodeCheckBox.setSelected(parameterValue);
+		useGroupNodeCheckBox.addActionListener(actionEvent -> setter.accept(useGroupNodeCheckBox.isSelected()));
+		singleParameterPanel.add(useGroupNodeCheckBox);
+		borderPanel.add(singleParameterPanel);
+	}
+	
+
+	private void startTimer(){
+		startTime = System.currentTimeMillis();
+	}
+	private long printElapsedTime(){
+		long elapsedMilliSeconds = System.currentTimeMillis() - startTime;
+		console.println("Execution Time of Algo in Milliseconds:" + elapsedMilliSeconds);
+		return elapsedMilliSeconds;
+	}
+	
+	
+	
+	private void cancel() {
+		if(runThread.isAlive()) {
+			console.println("Cancel run.");
+			cancel = true;
+			runProgressbar.cancel();
+		} else {
+			console.println("Nothing to cancel.");
+		}
+	}
+	
+	
+	private void fitness() {
+		if(runThread.isAlive()) {
+			console.println("Run have to be cancelled First.");
+			return;
+		}
+		double currentFitness = evaluatePosition(extractPositionAndAccess());
+		resetChain.removeLast();
+		console.println("Actual Fitnessvalue: " + currentFitness);
+	}
+	
+	
+	protected double evaluatePosition(List<Integer> positionToEvaluate) {
+		runProgressbar.step();
+//		long startTime = System.currentTimeMillis(), endTime;
+		control.getSimManager().resetFlexManagerForTimeStep(control.getModel().getCurIteration()); // execution time critical
+//		endTime = System.currentTimeMillis();
+//		console.print(" a:" + (endTime - startTime)); 
+//		startTime = endTime;
+		setState(positionToEvaluate); // execution time critical
+
+		control.calculateStateOnlyForCurrentTimeStep();
+		DecoratedState actualstate = control.getSimManager().getActualDecorState();
+		double result = evaluateState(actualstate);
+		return result;
+	}
+
+	protected abstract double evaluateState(DecoratedState actualstate);
+
+	
+	private void run() {
+		cancel = false;
+		control.guiDisable(true);
+		runPrinter.openStream();
+		runPrinter.println("");
+		runPrinter.println("Start:" + stringStatFromActualState());
+		runPrinter.closeStream();
+		if(this.useStepping) {
+			initParameterStepping();
+			do {
+					executeAlgoWithParameter();
+					if(cancel) break;
+					resetState();
+			}while(updateOneParameter());
+			resetParameterStepping();
+		}else {
+			executeAlgoWithParameter();
+			
+		}
+		updateVisual();
+		runProgressbar.finishedCancel();
+		control.guiDisable(false);
+	}
+	@SuppressWarnings("rawtypes")
+	private void initParameterStepping() {
+		for(ParameterStepping param :this.parameterSteppingList) {
+			param.init();
+		}
+		
+	}
+	@SuppressWarnings("rawtypes")
+	private void resetParameterStepping() {
+		for(ParameterStepping param :this.parameterSteppingList) {
+			param.reset();
+		}
+		
+	}
+
+
+
+
+	@SuppressWarnings("rawtypes")
+	private boolean updateOneParameter() {
+		List<ParameterStepping> parameterInUseList = this.parameterSteppingList.stream().filter(param -> param.useThisParameter).collect(Collectors.toList());
+		Collections.reverse(parameterInUseList);
+		int lastParameter = parameterInUseList.size() - 1 ;
+		int actualParameter = 0;
+		for(ParameterStepping param : parameterInUseList) {
+			
+			if(param.canUpdate()) {
+				param.update();
+				return true;
+			}else {
+				if(actualParameter == lastParameter) break;
+				param.reset();
+			}
+			actualParameter++;
+		}
+		//No Param can be updated 
+		return false;
+	}
+
+
+
+
+
+
+	private void executeAlgoWithParameter(){
+		double startFitness = evaluatePosition(extractPositionAndAccess());
+		console.println("BitLength: " + access.size());
+		resetChain.removeLast();
+		runPrinter.openStream();
+		runPrinter.println("");
+		runPrinter.println(algoInformationToPrint());
+		console.println(algoInformationToPrint());
+		runPrinter.closeStream();
+		runProgressbar.start();
+		Individual runBest = new Individual();
+		runBest.fitness = Double.MAX_VALUE;
+		for(int r = 0; r < rounds; r++)
+		{	
+			
+			startTimer();	
+			Individual  roundBest = executeAlgo();
+			if(cancel)return;
+			long executionTime = printElapsedTime();
+			runPrinter.openStream();
+			runPrinter.println(runList.stream().map(Object::toString).collect(Collectors.joining(", ")));
+			runPrinter.println(stringStatFromActualState());
+			runPrinter.println("Result: " + roundBest.fitness + " ExecutionTime:" + executionTime);
+			runPrinter.closeStream();
+			resetState();
+			if(roundBest.fitness < runBest.fitness) runBest = roundBest;
+		}
+		
+		control.getSimManager().resetFlexManagerForTimeStep(control.getModel().getCurIteration());
+		this.extractPositionAndAccess();
+		setState(runBest.position);
+		updateVisual();
+		console.println("Start: " + startFitness);
+		console.println("AlgoResult: " + runBest.fitness);
+	}
+	
+	
+
+
+
+
+
+
+
+	protected abstract Individual executeAlgo();
+
+
+
+
+
+
+	private void reset() {
+		if(runThread.isAlive()) {
+			console.println("Run have to be cancelled First.");
+			return;
+		}
+		if(!resetChain.isEmpty()) {
+			console.println("Resetting..");
+			setState(resetChain.getFirst());
+			control.getSimManager().resetFlexManagerForTimeStep(control.getModel().getCurIteration());
+			resetChain.clear();
+			control.resetSimulation();
+			control.setCurIteration(0);
+			updateVisual();
+		}else {
+			console.println("No run inistialized.");
+		}
+	}
+
+
+	/**
+	 * To let the User See the current state without touching the Canvas.
+	 */
+	private void updateVisual() {
+		control.calculateStateAndVisualForCurrentTimeStep();
+	}
+	/**
+	 * Sets the Model back to its original State before the LAST run.
+	 */
+	private void resetState() {
+		control.getSimManager().resetFlexManagerForTimeStep(control.getModel().getCurIteration());
+		setState(resetChain.getLast());
+	}
+
+
+	/**
+	 * Sets the State out of the given position for calculation or to show the user.
+	 * @param position
+	 */
+	private void setState(List<Integer> position) {
+		int i = 0;
+		for(Integer integer: position) {
+			access.get(i++).setState(integer);
+		}
+		
+}
+
+
+	/**
+	 * Method to get the current Position alias a ListOf Booleans for aktive settings on the Objects on the Canvas.
+	 * Also initialize the Access Hashmap to swap faster positions.
+	 * @param model
+	 * @return
+	 */
+	protected List<Integer> extractPositionAndAccess() {
+		Model model = control.getModel();
+		access= new ArrayList<AccessWrapper>();
+		List<Integer> initialState = new ArrayList<Integer>();
+		rollOutNodes((dGroupNode != null)? dGroupNode.getModel().getNodes() :model.getObjectsOnCanvas(), initialState, model.getCurIteration());			
+		resetChain.add(initialState);
+		//console.println(access.stream().map(Object::toString).collect(Collectors.joining(", ")));
+		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) {
+		for(AbstractCpsObject aCps : nodes) {
+			if(aCps instanceof CpsUpperNode) {
+				rollOutNodes(((CpsUpperNode)aCps).getNodes(), positionToInit ,timeStep );
+			}
+		}
+	}
+	
+	
+	
+	
+	private String stringStatFromActualState() {
+		if(dGroupNode != null)
+		{
+			//GetActualDecoratedGroupNode
+			dGroupNode = control.getSimManager().getActualVisualRepresentationalState().getCreatedGroupNodes().get(dGroupNode.getModel());
+			int amountOfSupplier = dGroupNode.getAmountOfSupplier();
+			int amountOfConsumer = dGroupNode.getAmountOfConsumer();
+			int amountOfPassiv = dGroupNode.getAmountOfPassiv();
+			int amountOfObjects = amountOfSupplier + amountOfConsumer + amountOfPassiv;
+			int unSuppliedConsumer = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
+			int partiallySuppliedConsumer = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
+			int suppliedConsumer = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
+			int overSuppliedConsumer = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
+			
+			
+			int activeElements = dGroupNode.getAmountOfAktiveElemntsFromHolonObjects();
+			int elements = dGroupNode.getAmountOfElemntsFromHolonObjects();
+			return	"HolonObjects["
+				+	" Producer: " + amountOfSupplier  + "/" + amountOfObjects + "("+ (float)amountOfSupplier/(float)amountOfObjects * 100 + "%)"
+				+	" Unsupplied: " + unSuppliedConsumer  + "/" + amountOfObjects + "("+ (float)unSuppliedConsumer/(float)amountOfObjects * 100 + "%)"
+				+	" PartiallySupplied: " + partiallySuppliedConsumer  + "/" + amountOfObjects + "("+ (float)partiallySuppliedConsumer/(float)amountOfObjects * 100 + "%)"
+				+	" Supplied: " + suppliedConsumer  + "/" + amountOfObjects + "("+ (float)suppliedConsumer/(float)amountOfObjects * 100 + "%)"
+				+	" Passiv: " + overSuppliedConsumer  + "/" + amountOfObjects + "("+ (float)overSuppliedConsumer/(float)amountOfObjects * 100 + "%)"
+				+  "]" + "   HolonElemnts["
+				+	" Active: " + activeElements  + "/" + elements + "("+ (float)activeElements/(float)elements * 100 + "%)"
+				+ "]";
+		}
+		DecoratedState state = control.getSimManager().getActualDecorState();
+		int amountOfSupplier = 0, amountOfConsumer = 0, amountOfPassiv = 0, unSuppliedConsumer = 0, partiallySuppliedConsumer = 0, suppliedConsumer = 0, overSuppliedConsumer = 0;
+		int activeElements = 0, amountOfelements = 0;
+		int totalConsumption = 0, totalProduction = 0;
+		for(DecoratedNetwork net : state.getNetworkList()) {
+			amountOfConsumer += net.getAmountOfConsumer();
+			amountOfSupplier += net.getAmountOfSupplier();
+			amountOfPassiv += net.getAmountOfPassiv();
+			unSuppliedConsumer += net.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
+			partiallySuppliedConsumer += net.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
+			suppliedConsumer += net.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
+			overSuppliedConsumer += net.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
+			amountOfelements += net.getAmountOfElements();
+			activeElements += net.getAmountOfActiveElements();
+			totalConsumption += net.getTotalConsumption();
+			totalProduction += net.getTotalProduction();
+		}
+		int amountOfObjects = amountOfSupplier + amountOfConsumer + amountOfPassiv;
+		int difference = Math.abs(totalProduction - totalConsumption);
+		
+		
+		
+		List<Flexibility> flexActiveList = control.getSimManager().getActualFlexManager().getAllFlexWrapperWithState(FlexState.IN_USE).stream().map(flex -> flex.getFlex()).collect(Collectors.toList());
+		int amountActiveEssential = (int)flexActiveList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Essential).count();
+		int amountActiveHigh = (int)flexActiveList.stream().filter(flex -> flex.getElement().getPriority() == Priority.High).count();
+		int amountActiveMedium = (int)flexActiveList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Medium).count();
+		int amountActiveLow = (int)flexActiveList.stream().filter(flex -> flex.getElement().getPriority() == Priority.Low).count();
+		int amountActiveFlexibilities = amountActiveEssential + amountActiveHigh + amountActiveMedium + amountActiveLow;
+		int amountHolons = state.getNetworkList().size();
+		int amountSwitch = state.getDecoratedSwitches().size();
+		int amountActiveSwitch = (int)state.getDecoratedSwitches().stream().filter(dswitch -> (dswitch.getState() == SwitchState.Closed)).count();
+		
+		
+		return	"HolonObjects["
+			+	" Passiv: " + percentage(amountOfPassiv, amountOfObjects)
+			+	" Producer: " + percentage(amountOfSupplier, amountOfObjects)
+			+	" Consumer: " + percentage(amountOfConsumer, amountOfObjects)
+			+	" Unsupplied: " + percentage(unSuppliedConsumer, amountOfConsumer)
+			+	" PartiallySupplied: " + percentage(partiallySuppliedConsumer, amountOfObjects)
+			+	" Supplied: " + percentage(suppliedConsumer, amountOfConsumer)
+			+	" Over: " + percentage(overSuppliedConsumer, amountOfConsumer)
+			+  "]" + "   HolonElemnts["
+			+	" Active: " + percentage(activeElements, amountOfelements)
+			+ "]" + "Flexibilities_active[" 
+			+ " Essential: " + percentage(amountActiveEssential, amountActiveFlexibilities)
+			+ " High: " + percentage(amountActiveHigh, amountActiveFlexibilities)
+			+ " Medium: " + percentage(amountActiveMedium, amountActiveFlexibilities)
+			+ " Low: " + percentage(amountActiveLow, amountActiveFlexibilities)
+			+ "]" + " activeSwitches:" + percentage(amountActiveSwitch,amountSwitch)
+			+ " Holons: " + amountHolons
+			+ " totalConsumption: " + totalConsumption
+			+ " totalProduction: " + totalProduction
+			+ " difference: " + difference;
+		
+	}
+	
+	
+	private String percentage(int actual, int max) {
+		return  actual  + "/" + max + "("+ (float)actual/(float)max * 100 + "%)";
+	}
+	
+
+	
+	@Override
+	public JPanel getPanel() {
+		return content;
+	}
+
+	@Override
+	public void setController(Control control) {
+		this.control = control;
+	}
+	
+	
+	
+	private class RunProgressBar{
+		//progressbar
+		private JProgressBar progressBar = new JProgressBar();
+		private int count = 0;
+		private boolean isActive = false;
+		
+		public void step() {
+			if(isActive) progressBar.setValue(count++);
+		}
+		public void start() {
+			progressBar.setIndeterminate(false);
+			count = 0;
+			isActive = true;
+			progressBar.setValue(0);
+			progressBar.setMaximum(getProgressBarMaxCount());
+		}
+		public void cancel() {
+			isActive = false;
+			progressBar.setIndeterminate(true);
+		}
+		public void finishedCancel() {
+			progressBar.setIndeterminate(false);
+			progressBar.setValue(0);
+		}
+		public JProgressBar getJProgressBar(){
+			return progressBar;
+		}
+	}
+	
+	protected abstract int getProgressBarMaxCount();
+	
+	protected abstract String algoInformationToPrint();
+	protected abstract String plottFileName();
+	
+	
+	
+	public class Printer{
+		private JFileChooser fileChooser = new JFileChooser();
+		private BufferedWriter out;
+		public Printer(String filename){
+			fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
+			fileChooser.setSelectedFile(new File(filename));
+		}
+		public void openStream() {
+			File file = fileChooser.getSelectedFile();
+			try {
+				file.createNewFile();
+				out = new BufferedWriter(new OutputStreamWriter(
+					    new FileOutputStream(file, true), "UTF-8"));
+			} catch (IOException e) {
+				System.out.println(e.getMessage());
+			}
+		}
+		
+		
+		public void println(String stringToPrint) {
+			try {
+				out.write(stringToPrint);
+				out.newLine();
+			} catch (IOException e) {
+				System.out.println(e.getMessage());
+			}
+		}
+		public void closeStream() {
+			try {
+				out.close();
+			} catch (IOException e) {
+				System.out.println(e.getMessage());
+			}
+		}
+	}
+		
+	
+	/**
+	 * 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) {
+			
+		}
+		
+		public void setState(int state) {
+			//TODO set state
+		}
+		
+		
+		public String toString() {
+			return "Hollow";
+		}
+	}
+	
+	
+	
+	
+	/**
+	* To create Random and maybe switch the random generation in the future.
+	*/
+	protected static class Random{
+		private static java.util.Random random = new java.util.Random();
+		/**
+		 * True or false
+		 * @return the random boolean.
+		 */
+		public static boolean nextBoolean(){
+			return random.nextBoolean();
+		}
+		/**
+		 * Between 0.0(inclusive) and 1.0 (exclusive)
+		 * @return the random double.
+		 */
+		public static double nextDouble() {
+			return random.nextDouble();
+		}
+		
+		/**
+		 * Random Int in Range [min;max[ with UniformDistirbution
+		 * @param min
+		 * @param max
+		 * @return
+		 */
+		public static int nextIntegerInRange(int min, int max) {
+			int result = min;
+			try {
+				result = min + random.nextInt(max - min);
+			}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>{
+		public T object;
+		Handle(T object){
+			this.object = object;
+		}
+		public String toString() {
+			return object.toString();
+		}
+	}
+	public class Individual {
+		public double fitness;
+		public  List<Integer> position;
+		
+		public Individual(){};
+		/**
+		 *  Copy Constructor
+		 */
+		public Individual(Individual c){
+			position = c.position.stream().collect(Collectors.toList());
+			fitness = c.fitness;
+		}
+	}
+	
+	protected class ParameterStepping<T>{
+		boolean useThisParameter = false;
+		String paramaterName;
+		private int count = 0;
+		int stepps;
+		T stepSize;
+		T startValue;
+		Consumer<T> setter;
+		Supplier<T> getter;
+		BiFunction<Integer,T,T> multyply;
+		BiFunction<T,T,T> add;
+		ParameterStepping(Consumer<T> setter, Supplier<T> getter, BiFunction<T,T,T> add, BiFunction<Integer,T,T> multyply, T stepSize, int stepps){
+			this.setter = setter;
+			this.getter = getter;
+			this.multyply = multyply;
+			this.add = add;
+			this.stepSize = stepSize;
+			this.stepps = stepps;
+		}
+		
+		void init() {
+			startValue = getter.get();
+		}
+		
+		boolean canUpdate() {
+			return count  < stepps;
+		}
+		
+		void update(){
+			if(canUpdate()) {
+				setter.accept(add.apply(startValue, multyply.apply(count + 1, stepSize)));
+				count ++;
+			}
+		}
+		
+		void reset() {
+			setter.accept(startValue);
+			count = 0;
+		}
+	}
+	
+	
+}

+ 0 - 69
src/exampleAlgorithms/AlgoTest.java

@@ -1,69 +0,0 @@
-package exampleAlgorithms;
-
-import api.AlgorithmFrameworkFlex;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import java.util.ListIterator;
-import java.util.stream.Collectors;
-
-import ui.model.DecoratedState;
-
-public class AlgoTest extends AlgorithmFrameworkFlex{
-
-	
-	
-	
-	public AlgoTest(){
-		super();
-		
-	}
-	
-	
-	@Override
-	protected double evaluateState(DecoratedState actualstate) {
-		return Evaluation.getFitnessValueForState(actualstate);
-	}
-
-	@Override
-	protected Individual executeAlgo() {
-		
-		console.println(control.getModel().getAllHolonObjectsOnCanvas().stream().map(Object::toString).collect(Collectors.joining(", ")));
-		
-		
-		Individual best = new Individual();
-//		best.position = extractPositionAndAccess();
-//		List<Boolean> list=new ArrayList<Boolean>();
-//		for(boolean b: best.position) {
-//			list.add(true);
-//		}
-//		best.position = list;
-//		console.println(best.position.stream().map(Object::toString).collect(Collectors.joining(", ")));
-//		best.fitness = evaluatePosition(best.position);
-//		
-//		console.println("Fitness" +  best.fitness);
-		return best;
-	}
-
-	@Override
-	protected int getProgressBarMaxCount() {
-		// TODO Auto-generated method stub
-		return 0;
-	}
-
-
-	@Override
-	protected String algoInformationToPrint() {
-		return "TestAlgo";
-	}
-
-
-	@Override
-	protected String plottFileName() {
-		// TODO Auto-generated method stub
-		return "plottTestAlgo.txt";
-	}
-	
-}

+ 0 - 113
src/exampleAlgorithms/ExampleFitnessFunction.java

@@ -1,113 +0,0 @@
-package exampleAlgorithms;
-
-import java.awt.BorderLayout;
-import java.awt.Component;
-import java.awt.Dimension;
-
-import javax.swing.BoxLayout;
-import javax.swing.JButton;
-import javax.swing.JFrame;
-import javax.swing.JPanel;
-import javax.swing.JScrollPane;
-import javax.swing.JTextArea;
-
-import api.AddOn;
-import ui.controller.Control;
-import ui.model.DecoratedHolonObject.HolonObjectState;
-import ui.model.DecoratedNetwork;
-import ui.model.DecoratedState;
-
-public class ExampleFitnessFunction implements AddOn {
-	private Control  control;
-	private JTextArea textArea;
-	private JPanel content = new JPanel();
-//	public static void main(String[] args)
-//	{
-//	      JFrame newFrame = new JFrame("exampleWindow");
-//	      ExampleFitnessFunction instance = new ExampleFitnessFunction();
-//	      newFrame.setContentPane(instance.getAlgorithmPanel());
-//	      newFrame.pack();
-//	      newFrame.setVisible(true);
-//		  newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-//	}
-	public ExampleFitnessFunction(){
-		content.setLayout(new BorderLayout());
-		content.add(createParameterPanel(), BorderLayout.CENTER);
-		JButton buttonRun = new JButton("Run");
-		buttonRun.addActionListener(actionEvent -> run());
-		content.add(buttonRun, BorderLayout.PAGE_END);
-		content.setPreferredSize(new Dimension(300,300));
-	}
-	private void run() {
-		
-		float fitness = getFitnessValue();
-		println("" + fitness);
-	}
-	private JPanel createParameterPanel() {
-		JPanel parameterPanel = new JPanel();
-		parameterPanel.setLayout(new BoxLayout(parameterPanel, BoxLayout.PAGE_AXIS));
-		textArea = new JTextArea();
-		textArea.setEditable(false);
-		JScrollPane scrollPane = new JScrollPane(textArea);
-		parameterPanel.add(scrollPane);
-		return parameterPanel;
-	}
-	@Override
-	public JPanel getPanel() {
-		return content;
-	}
-
-	@Override
-	public void setController(Control control) {
-		this.control = control;
-	}
-	private void clear() {
-		textArea.setText("");
-	}
-	private void print(String message) {
-		textArea.append(message);
-	}
-	private void println(String message) {
-		textArea.append(message + "\n");
-	}
-	
-	private float getFitnessValue() {
-		float overallFitness = 0f;
-		//Calculate all timesteps
-		for(int timestep = 0; timestep <100; timestep++) {
-			control.calculateStateAndVisualForTimeStep(timestep);
-		}
-		//Calculate all timesteps
-		for(int timestep = 0; timestep <100; timestep++) {
-			overallFitness += getFitnessValueForState(control.getSimManager().getDecorState(timestep));
-		}
-		return overallFitness;
-	}
-
-	private float getFitnessValueForState(DecoratedState state) {
-		float fitnessOfTimestep = 0f;
-		for(DecoratedNetwork net : state.getNetworkList()) {
-			fitnessOfTimestep += net.getConsumerList().stream().map(con -> con.getState()).map(objectState -> StateToFloat(objectState)).reduce(0.0f, (a, b) -> (a + b));
-			fitnessOfTimestep += net.getConsumerSelfSuppliedList().stream().map(con -> 1f).reduce(0.0f, (a, b) -> (a + b));
-		}
-		return fitnessOfTimestep;
-	}
-	private float StateToFloat(HolonObjectState state) {
-		switch (state) {
-		case NOT_SUPPLIED:
-			return 10f;
-		case NO_ENERGY:
-			return 15f;
-		case OVER_SUPPLIED:
-			return 5f;
-		case PARTIALLY_SUPPLIED:
-			return 3f;
-		case PRODUCER:
-			return 2f;
-		case SUPPLIED:
-			return 0f;
-		default:
-			return 0f;
-		}
-	}
-}