Browse Source

Cancel Button + ProgressBar fix

Tom Troppmann 5 years ago
parent
commit
1c1a17ceca
1 changed files with 36 additions and 12 deletions
  1. 36 12
      src/exampleAlgorithms/PSOAlgotihm.java

+ 36 - 12
src/exampleAlgorithms/PSOAlgotihm.java

@@ -86,6 +86,9 @@ public class PSOAlgotihm implements Algorithm {
 	private JProgressBar progressBar = new JProgressBar();
 	private int progressBarCount = 0;
 	private long startTime;
+	private Thread runThread;
+	private boolean cancel = false;
+	
 	
 	public static void main(String[] args)
 	{
@@ -267,6 +270,10 @@ public class PSOAlgotihm implements Algorithm {
 	}
 	public JPanel createButtonPanel() {
 		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
+		JButton cancelButton =  new JButton("Cancel Run");
+		cancelButton.addActionListener(actionEvent -> cancel());
+		buttonPanel.add(cancelButton);
+		
 		JButton clearButton =  new JButton("Clear Console");
 		clearButton.addActionListener(actionEvent -> clear());
 		buttonPanel.add(clearButton);
@@ -285,21 +292,36 @@ public class PSOAlgotihm implements Algorithm {
 		buttonPanel.add(resetButton);
 		JButton runButton =  new JButton("Run");
 		runButton.addActionListener(actionEvent -> {
-			Runnable task = () -> {
-				startTimer();
-				executePsoAlgoWithCurrentParameters();
-				printElapsedTime();
-			};
-			Thread thread = new Thread(task);
-			thread.start();
+			Runnable task = () -> run();
+			runThread = new Thread(task);
+			runThread.start();
 		});
 		buttonPanel.add(runButton);
 		return buttonPanel;
 	}
+	private void run() {
+		cancel = false;
+		startTimer();
+		executePsoAlgoWithCurrentParameters();
+		if(cancel) {
+			reset();
+			return;
+		}
+		printElapsedTime();
+	}
 	
+	private void cancel() {
+		if(runThread.isAlive()) {
+			println("");
+			println("Cancel run.");
+			cancel = true;
+		} else {
+			println("Nothing to cancel.");
+		}
+	}
 	private void fitness() {
 		initDependentParameter();
-		double currentFitness = evaluatePosition(extractPositionAndAccess(control.getModel()));
+		double currentFitness = evaluatePosition(extractPositionAndAccess(control.getModel()), false);
 		println("Actual Fitnessvalue: " + currentFitness);
 	}
 	private void setSaveFile() {
@@ -395,6 +417,7 @@ public class PSOAlgotihm implements Algorithm {
           
 		  List<Double> runList = db.insertNewRun();
 		  Best lastRunBest = executePSOoneTime(runList);
+		  if(cancel)return;
 		  resetState();
 		  if(lastRunBest.value < runBest.value) runBest = lastRunBest;
 		}
@@ -447,7 +470,7 @@ public class PSOAlgotihm implements Algorithm {
 	private Best executePSOoneTime(List<Double> runList) {
 		Best globalBest = new Best();
 		globalBest.position = extractPositionAndAccess(control.getModel());
-		globalBest.value = evaluatePosition(globalBest.position);
+		globalBest.value = evaluatePosition(globalBest.position, true);
 		print("Start Value:" + globalBest.value);
 		int dimensions = globalBest.position.size();
 		List<Particle> swarm= initializeParticles(dimensions);
@@ -464,6 +487,7 @@ public class PSOAlgotihm implements Algorithm {
 					decode(particle, index);
 				}
 			}
+			if(cancel)return null;
 			evaluation(globalBest, swarm);
 			runList.add(globalBest.value);
 		}
@@ -548,7 +572,7 @@ public class PSOAlgotihm implements Algorithm {
 	 */
 	private void evaluation(Best globalBest, List<Particle> swarm) {
 		for(Particle p: swarm) {
-			double localEvaluationValue = evaluatePosition(p.xPhenotype);
+			double localEvaluationValue = evaluatePosition(p.xPhenotype, true);
 			p.checkNewEvaluationValue(localEvaluationValue);
 			if(localEvaluationValue < globalBest.value) {
 				globalBest.value = localEvaluationValue;
@@ -561,9 +585,9 @@ public class PSOAlgotihm implements Algorithm {
 	 * @param position
 	 * @return
 	 */
-	private double evaluatePosition(List<Boolean> position) {
+	private double evaluatePosition(List<Boolean> position, boolean doIncreaseCounter) {
 		setState(position);
-		progressBarStep();
+		if(doIncreaseCounter)progressBarStep();
 		control.calculateStateForCurrentTimeStep();
 		DecoratedState actualstate = control.getSimManager().getActualDecorState();		
 		return getFitnessValueForState(actualstate);