Index: branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Experiment.cs
===================================================================
--- branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Experiment.cs	(revision 6646)
+++ branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Experiment.cs	(revision 6878)
@@ -104,5 +104,6 @@
     }
 
-    private bool stopPending;
+    private bool experimentStarted = false;
+    private bool experimentStopped = false;
 
     public Experiment()
@@ -114,5 +115,4 @@
       optimizers = new OptimizerList();
       Runs = new RunCollection();
-      stopPending = false;
       Initialize();
     }
@@ -124,5 +124,4 @@
       optimizers = new OptimizerList();
       Runs = new RunCollection();
-      stopPending = false;
       Initialize();
     }
@@ -133,12 +132,8 @@
       optimizers = new OptimizerList();
       Runs = new RunCollection();
-      stopPending = false;
       Initialize();
     }
     [StorableConstructor]
-    private Experiment(bool deserializing)
-      : base(deserializing) {
-      stopPending = false;
-    }
+    private Experiment(bool deserializing) : base(deserializing) { }
     [StorableHook(HookType.AfterDeserialization)]
     private void AfterDeserialization() {
@@ -151,5 +146,7 @@
       optimizers = cloner.Clone(original.optimizers);
       runs = cloner.Clone(original.runs);
-      stopPending = original.stopPending;
+
+      experimentStarted = original.experimentStarted;
+      experimentStopped = original.experimentStopped;
       Initialize();
     }
@@ -172,35 +169,40 @@
       if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
         throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
-      if (Optimizers.Count > 0) {
-        if (clearRuns) runs.Clear();
-        foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
+      if (Optimizers.Count == 0) return;
+
+      if (clearRuns) runs.Clear();
+      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
+        if (clearRuns || optimizer.ExecutionState != ExecutionState.Prepared)
           optimizer.Prepare(clearRuns);
-      }
     }
     public void Start() {
       if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
         throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
-      stopPending = false;
-      if (Optimizers.Count > 0) {
-        IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
-        if (optimizer != null) optimizer.Start();
-      }
+      if (Optimizers.Count == 0) return;
+
+      experimentStarted = true;
+      experimentStopped = false;
+      IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
+      if (optimizer != null) optimizer.Start();
     }
     public void Pause() {
       if (ExecutionState != ExecutionState.Started)
         throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
-      if (Optimizers.Count > 0) {
-        foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started))
-          optimizer.Pause();
-      }
+      if (Optimizers.Count == 0) return;
+
+      experimentStarted = false;
+      experimentStopped = false;
+      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started))
+        optimizer.Pause();
     }
     public void Stop() {
       if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
         throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
-      stopPending = true;
-      if (Optimizers.Count > 0) {
-        foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))
-          optimizer.Stop();
-      }
+      if (Optimizers.Count == 0) return;
+
+      experimentStarted = false;
+      experimentStopped = true;
+      foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))
+        optimizer.Stop();
     }
 
@@ -333,11 +335,9 @@
     }
     private void optimizer_Stopped(object sender, EventArgs e) {
-      if (!stopPending && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
+      if (experimentStopped && Optimizers.All(o => o.ExecutionState == ExecutionState.Stopped || o.ExecutionState == ExecutionState.Prepared)) OnStopped();
+      else if (Optimizers.All(o => o.ExecutionState == ExecutionState.Stopped)) OnStopped();
+      else if (Optimizers.Any(o => o.ExecutionState == ExecutionState.Paused) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused();
+      else if (experimentStarted && !experimentStopped && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
         Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start();
-      } else {
-        if (Optimizers.All(x => (x.ExecutionState != ExecutionState.Started) && (x.ExecutionState != ExecutionState.Paused))) {
-          stopPending = false;
-          OnStopped();
-        }
       }
     }
