TomTroppmann 2 years ago
parent
commit
8cf19e9260

+ 82 - 0
src/algorithm/binary/GreedyAlgorithm.java

@@ -0,0 +1,82 @@
+package algorithm.binary;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import api.AlgorithmFrameworkFlex;
+import utility.StringFormat;
+
+public class GreedyAlgorithm extends AlgorithmFrameworkFlex{
+
+	private int problemSize = 0;
+	private boolean moreInformation = true;
+	@Override
+	protected Individual executeAlgo() {
+		Individual best = new Individual();
+		best.position = extractPositionAndAccess();
+		println("Bit-Array_length: " + best.position.size());
+		best.fitness = evaluatePosition(best.position);
+		List<Double> runList = new ArrayList<Double>();
+		runList.add(best.fitness);
+		println("Start with: " + StringFormat.doubleFixedPlaces(2, best.fitness));
+		problemSize = best.position.size();
+		
+		
+		List<Integer> indexList = new ArrayList<Integer>();
+		for(int index = 0; index < problemSize; index++)
+		{
+			indexList.add(index);
+		}
+		
+		
+		Individual bestAfterRound = new Individual(best);
+		for(int round = 0; round < problemSize; round++)
+		{
+			Individual startInThisRound = new Individual(bestAfterRound);
+			Individual bestInThisRound = new Individual(); 
+			int indexToRemove = -1;
+			bestInThisRound.fitness = Float.MAX_VALUE;
+			for(Integer i : indexList) {
+				boolean actualValue = startInThisRound.position.get(i);
+				startInThisRound.position.set(i, !actualValue);
+				double fitness = evaluatePosition(startInThisRound.position);
+				if(fitness < bestInThisRound.fitness) {
+					bestInThisRound = new Individual(startInThisRound);	
+					bestInThisRound.fitness = fitness;
+					indexToRemove = i;
+				}
+				startInThisRound.position.set(i, actualValue);
+			}
+			if(cancel) return null;
+			bestAfterRound = bestInThisRound;
+			println("Fitness: " + bestInThisRound.fitness + "FlippedIndex = " + indexToRemove + "(" +indexList.size() + ")");
+			if(bestAfterRound.fitness < best.fitness) {
+				best = bestAfterRound;
+			}
+			indexList.remove(Integer.valueOf(indexToRemove));
+		}
+		
+		return best;
+	}
+
+	
+	
+	protected void println(String value) {
+		if(moreInformation)console.println(value);
+	}
+	
+	@Override
+	protected int getProgressBarMaxCount() {
+		return (problemSize * (problemSize + 1)) / 2;
+	}
+
+	@Override
+	protected String algoInformationToPrint() {
+		return "NoParameter";
+	}
+
+	@Override
+	protected String plottFileName() {
+		return "plottGreedy.txt";
+	}
+}

+ 71 - 0
src/algorithm/binary/GreedySinglePass.java

@@ -0,0 +1,71 @@
+package algorithm.binary;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import api.AlgorithmFrameworkFlex;
+import api.AlgorithmFrameworkFlex.Individual;
+import utility.StringFormat;
+
+public class GreedySinglePass extends AlgorithmFrameworkFlex{
+
+	private int problemSize = 0;
+	private boolean moreInformation = true;
+	@Override
+	protected Individual executeAlgo() {
+		Individual best = new Individual();
+		best.position = extractPositionAndAccess();
+		println("Bit-Array_length: " + best.position.size());
+		best.fitness = evaluatePosition(best.position);
+		List<Double> runList = new ArrayList<Double>();
+		runList.add(best.fitness);
+		println("Start with: " + StringFormat.doubleFixedPlaces(2, best.fitness));
+		problemSize = best.position.size();
+		
+		
+		Individual bestFound = new Individual(best);
+		bestFound.fitness = Float.MAX_VALUE;
+		Individual start = new Individual(best);
+		for(int index = 0; index < problemSize; index++)
+		{
+			boolean actualValue = start.position.get(index);
+			start.position.set(index, !actualValue);
+			double fitness = evaluatePosition(start.position);
+			if(fitness < bestFound.fitness) {
+				bestFound = new Individual(start);	
+				bestFound.fitness = fitness;
+			}
+			if(bestFound.fitness < best.fitness) {
+				best = bestFound;
+			}
+			
+			
+			if(cancel) return null;
+			println("Fitness: " + fitness + "FlippedIndex = " + index + "(" + problemSize + ")");
+		}
+		
+		
+		return best;
+	}
+
+	
+	
+	protected void println(String value) {
+		if(moreInformation)console.println(value);
+	}
+	
+	@Override
+	protected int getProgressBarMaxCount() {
+		return (problemSize * (problemSize + 1)) / 2;
+	}
+
+	@Override
+	protected String algoInformationToPrint() {
+		return "NoParameter";
+	}
+
+	@Override
+	protected String plottFileName() {
+		return "plottGreedy.txt";
+	}
+}

File diff suppressed because it is too large
+ 300 - 306
src/api/AlgorithmFrameworkFlex.java


Some files were not shown because too many files changed in this diff