浏览代码

ParameterStepping without Gui

Tom 5 年之前
父节点
当前提交
a19068305d
共有 2 个文件被更改,包括 162 次插入4 次删除
  1. 93 4
      src/api/AlgorithmFrameworkFlex.java
  2. 69 0
      src/exampleAlgorithms/AlgoTest.java

+ 93 - 4
src/api/AlgorithmFrameworkFlex.java

@@ -16,9 +16,11 @@ import java.util.ArrayList;
 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.DoubleConsumer;
 import java.util.function.IntConsumer;
+import java.util.function.Supplier;
 import java.util.stream.Collectors;
 
 import javax.swing.BorderFactory;
@@ -94,7 +96,9 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 	private RunPrinter printer = new RunPrinter(plottFileName(), true);
 
 
-	
+	//Parameter
+	LinkedList<ParameterStepping> parameterSteppingList= new LinkedList<ParameterStepping>();
+	protected boolean useStepping = false;
 	
 
 	
@@ -106,6 +110,11 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 		splitPane.setResizeWeight(0.0);
 		content.add(splitPane, BorderLayout.CENTER);
 		content.setPreferredSize(new Dimension(800,800));
+		
+		//Add rounds
+		ParameterStepping<Integer> roundStepping = new ParameterStepping<Integer>(IntInput -> rounds = IntInput, () -> rounds, (a,b) -> Integer.sum(a, b), (a,b) -> a * b, 10, 2);
+		roundStepping.useThisParameter = true;
+		parameterSteppingList.add(roundStepping);
 	}
 	
 	
@@ -352,11 +361,49 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 	private void run() {
 		cancel = false;
 		control.guiDisable(true);
-		executeAlgoWithParameter();
+		if(this.useStepping) {
+			initParameterStepping();
+			do {
+					executeAlgoWithParameter();
+					plott();
+					resetState();
+			}while(updateOneParameter());
+		}else {
+			executeAlgoWithParameter();
+			
+		}
 		updateVisual();
 		control.guiDisable(false);
 	}
 	
+	private void initParameterStepping() {
+		for(ParameterStepping param :this.parameterSteppingList) {
+			param.init();
+		}
+		
+	}
+
+
+
+
+
+
+	private boolean updateOneParameter() {
+		for(ParameterStepping param :this.parameterSteppingList) {
+			if(param.useThisParameter && param.canUpdate()) {
+				param.update();
+				return true;
+			}
+		}
+		//No Param can be updated 
+		return false;
+	}
+
+
+
+
+
+
 	private void executeAlgoWithParameter(){
 		runProgressbar.start();
 		int actualIteration = control.getModel().getCurIteration();
@@ -534,7 +581,6 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 	
 	
 	private String stringStatFromActualState() {
-		console.println("    (dGroupNode != null):" + (dGroupNode != null));
 		if(dGroupNode != null)
 		{
 			//GetActualDecoratedGroupNode
@@ -562,7 +608,7 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 				+ "]";
 		}
 		
-		return "[No GroupMode Use == No detailed Info]";
+		return "[No GroupNode Use == No detailed Info]";
 	}
 	
 	
@@ -790,4 +836,47 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 			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;
+		}
+	}
+	
+	
 }

+ 69 - 0
src/exampleAlgorithms/AlgoTest.java

@@ -0,0 +1,69 @@
+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";
+	}
+	
+}