Index: /trunk/sources/HeuristicLab.FixedOperators/3.2/FixedOperatorBase.cs
===================================================================
--- /trunk/sources/HeuristicLab.FixedOperators/3.2/FixedOperatorBase.cs	(revision 1994)
+++ /trunk/sources/HeuristicLab.FixedOperators/3.2/FixedOperatorBase.cs	(revision 1995)
@@ -27,7 +27,11 @@
 using System.Threading;
 using System.Diagnostics;
+using System.Text;
 
 namespace HeuristicLab.FixedOperators {
   class FixedOperatorBase : CombinedOperator {
+    protected ItemList<IOperation> persistedOperations;
+    protected Stack<IOperation> executionStack;
+
     /// <summary>
     /// Execution pointer shows which command actually is executed
@@ -40,4 +44,7 @@
     protected IntData persistedExecutionPointer;
 
+    protected int tempExePointer;
+    protected int tempPersExePointer;
+
     /// <summary>
     /// Current operator in execution.
@@ -45,6 +52,7 @@
     protected IOperator currentOperator;
 
-    public FixedOperatorBase() : base() {
-      //AddVariableInfo(new VariableInfo("ExecutionPointer", "Execution pointer for algorithm abortion", typeof(IntData), VariableKind.New));   
+    public FixedOperatorBase()
+      : base() {
+      executionStack = new Stack<IOperation>();
     } // FixedOperatorBase
 
@@ -53,10 +61,10 @@
     } // AlreadyExecuted
 
-    protected void ExecuteExitable(IOperator op, IScope scope) { 
-    
+    protected void ExecuteExitable(IOperator op, IScope scope) {
+
     } // ExecuteExitable
 
-    protected void SetRegion(string region) { 
-    
+    protected void SetRegion(string region) {
+
     } // SetRegion
 
@@ -65,8 +73,5 @@
         ExecuteOperation(op, scope);
         persistedExecutionPointer.Data++;
-        //Console.WriteLine("Execute: {0}", executionPointer);
       } // if not executed
-      //else
-        //Console.WriteLine("Skip Execute: {0}", executionPointer);
       executionPointer++;
 
@@ -77,35 +82,48 @@
     protected void ExecuteOperation(IOperator op, IScope scope) {
       IOperation operation;
-      currentOperator = op;
-      operation = op.Execute(scope);
-      if (operation != null) {
-        //IOperator currentOperator;
-        Stack<IOperation> executionStack = new Stack<IOperation>();
-        executionStack.Push(op.Execute(scope));
+       if (persistedOperations.Count == 0) {
+        currentOperator = op;
+        operation = op.Execute(scope);
+        if (operation != null) {
+          executionStack.Push(operation);
+        }
+      } else {
+        executionStack = new Stack<IOperation>(persistedOperations);  
+      }
 
-        while (executionStack.Count > 0) {
-          operation = executionStack.Pop();
-          if (operation is AtomicOperation) {
-            AtomicOperation atomicOperation = (AtomicOperation)operation;
-            IOperation next = null;
-            try {
-              currentOperator = atomicOperation.Operator;
-              next = currentOperator.Execute(atomicOperation.Scope);
-            }
-            catch (Exception) {
-              throw new InvalidOperationException("Invalid Operation occured in FixedBase.Execute");
-            }
-            if (next != null)
-              executionStack.Push(next);
-          } else if (operation is CompositeOperation) {
-            CompositeOperation compositeOperation = (CompositeOperation)operation;
-            for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
-              executionStack.Push(compositeOperation.Operations[i]);
-          } // else if
-        } // while
-      } // if (operation != null)
+      while (executionStack.Count > 0) {
+        operation = executionStack.Pop();
+        if (operation is AtomicOperation) {
+          AtomicOperation atomicOperation = (AtomicOperation)operation;
+          IOperation next = null;
+          try {
+            currentOperator = atomicOperation.Operator;
+            next = currentOperator.Execute(atomicOperation.Scope);
+          }
+          catch (Exception) {
+            throw new InvalidOperationException("Invalid Operation occured in FixedBase.Execute");
+          }
+          if (next != null)
+            executionStack.Push(next);
+        } else if (operation is CompositeOperation) {
+          CompositeOperation compositeOperation = (CompositeOperation)operation;
+          for (int i = compositeOperation.Operations.Count - 1; i >= 0; i--)
+            executionStack.Push(compositeOperation.Operations[i]);
+        } // else if
+
+        if (Canceled && executionStack.Count > 0) {
+          SaveExecutionStack(executionStack);
+          throw new CancelException();
+        }
+      } // while
     } // ExecuteOperation
 
+    private void SaveExecutionStack(Stack<IOperation> stack) {
+      persistedOperations = new ItemList<IOperation>();
+      persistedOperations.AddRange(stack.ToArray());
+    } // SaveExecutionStack
+
     public override IOperation Apply(IScope scope) {
+      base.Apply(scope);
       try {
         persistedExecutionPointer = scope.GetVariableValue<IntData>("ExecutionPointer", false);
@@ -115,6 +133,21 @@
         scope.AddVariable(new Variable("ExecutionPointer", persistedExecutionPointer));
       }
-      
+
+      try {
+        persistedOperations = scope.GetVariableValue<ItemList<IOperation>>("ExecutionStack", false);
+      }
+      catch (Exception) {
+        persistedOperations = new ItemList<IOperation>();
+        scope.AddVariable(new Variable("ExecutionStack", persistedOperations));
+      }
+
       executionPointer = 0;
+
+      for (int i = 0; i < SubOperators.Count; i++) {
+        if (scope.GetVariable(SubOperators[i].Name) != null)
+          scope.RemoveVariable(SubOperators[i].Name);
+        scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
+      }
+
       return null;
     } // Apply
@@ -123,11 +156,31 @@
       base.Abort();
       currentOperator.Abort();
-      //engineThread.Abort();
-    }
+    } // Abort
 
-  } // class FixedBase
+    /// <summary>
+    /// Saves the value of the execution pointers into temp variables
+    /// </summary>
+    protected void SaveExecutionPointer() {
+      tempExePointer = executionPointer;
+      tempPersExePointer = persistedExecutionPointer.Data;
+    } // SaveExecutionPointer
 
-  class CancelException : Exception { 
-  
+    protected void SetExecutionPointerToLastSaved() {
+      if (executionPointer != persistedExecutionPointer.Data)
+        persistedExecutionPointer.Data = tempPersExePointer;
+      else
+        persistedExecutionPointer.Data = tempExePointer;
+      executionPointer = tempExePointer;
+    } // SetExecutionPointerToLastSaved
+
+
+    protected void ResetExecutionPointer() {
+      executionPointer = 0;
+      persistedExecutionPointer.Data = 0;
+    } // ResetExecutionPointer
+  } // class FixedOperatorBase
+
+  class CancelException : Exception {
+
   } // class CancelException
 } // namespace HeuristicLab.FixedOperators
Index: /trunk/sources/HeuristicLab.FixedOperators/3.2/FixedSGAMain.cs
===================================================================
--- /trunk/sources/HeuristicLab.FixedOperators/3.2/FixedSGAMain.cs	(revision 1994)
+++ /trunk/sources/HeuristicLab.FixedOperators/3.2/FixedSGAMain.cs	(revision 1995)
@@ -34,4 +34,6 @@
 using HeuristicLab.Selection;
 using System.Threading;
+using System.IO;
+using HeuristicLab.Random;
 
 namespace HeuristicLab.FixedOperators {
@@ -53,4 +55,7 @@
     OperatorBase evaluator;
     SubScopesRemover sr;
+    StochasticBranch sb;
+
+    OperatorBase selector;
 
     // CreateReplacement
@@ -63,6 +68,11 @@
     Thread executionThread;
     Thread cancelThread;
-    StringBuilder output = new StringBuilder();
-
+    
+    // for testing only
+    QualityLogger ql;
+    BestAverageWorstQualityCalculator bawqc;
+    DataCollector dc;
+    ItemList<StringData> names;
+    LinechartInjector lci;
 
     //long[] timesExecuteCreateChildren;
@@ -79,9 +89,12 @@
       sorter.GetVariableInfo("Value").ActualName = "Quality";
 
-      InitVariablesForCreateChildren();
-      InitVariablesForCreateReplacement();
-    }
-
-    private void InitVariablesForCreateReplacement() {
+      InitCreateChildren();
+      InitReplacement();
+
+      sb = new StochasticBranch();
+      sb.GetVariableInfo("Probability").ActualName = "MutationRate";
+    }
+
+    private void InitReplacement() {
       ls = new LeftSelector();
       rr = new RightReducer();
@@ -92,8 +105,7 @@
       ls.GetVariableInfo("Selected").ActualName = "Elites";
       rs.GetVariableInfo("Selected").ActualName = "Elites";
-
-    }
-
-    protected void InitVariablesForCreateChildren() {
+    }
+
+    protected void InitCreateChildren() {
       // variables for create children
       ci = new ChildrenInitializer();
@@ -117,11 +129,6 @@
       Stopwatch swApply = new Stopwatch();
       swApply.Start();
-      for (int i = 0; i < SubOperators.Count; i++) {
-        if (scope.GetVariable(SubOperators[i].Name) != null)
-          scope.RemoveVariable(SubOperators[i].Name);
-        scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
-      }
-
-      OperatorBase selector = (OperatorBase)GetVariableValue("Selector", scope, true);
+
+      #region Initialization
       QualityLogger ql = new QualityLogger();
 
@@ -137,13 +144,7 @@
       lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3;
 
-      LessThanComparator ltc = new LessThanComparator();
-      ltc.GetVariableInfo("LeftSide").ActualName = "Generations";
-      ltc.GetVariableInfo("RightSide").ActualName = "MaximumGenerations";
-      ltc.GetVariableInfo("Result").ActualName = "GenerationsCondition";
-
       IntData maxGenerations = GetVariableValue<IntData>("MaximumGenerations", scope, true);
       IntData nrOfGenerations = GetVariableValue<IntData>("Generations", scope, true);
-      //nrOfGenerations.Data = 0;
-
+    
       IntData subscopeNr;
       try {
@@ -155,20 +156,22 @@
       }
 
-      EmptyOperator empty = new EmptyOperator();
+      ci = new ChildrenInitializer();
+      
+
+      GetOperatorsFromScope(scope);
+
+      try {
+        sb.RemoveSubOperator(0);
+      }
+      catch (Exception) {
+      }
+      sb.AddSubOperator(mutator);
+
 
       IScope s;
       IScope s2;
-      int tempExePointer = 0;
-      int tempPersExePointer = 0;
-      double randomNumber;
-      
-      // fetch variables from scope for create children
-      InitializeExecuteCreateChildren(scope);
+      #endregion
       try {
-        for (int i = nrOfGenerations.Data; i < maxGenerations.Data; i++) {
-          if (executionPointer == persistedExecutionPointer.Data)
-            persistedExecutionPointer.Data = 0;
-          executionPointer = 0;
-
+        for (; nrOfGenerations.Data < maxGenerations.Data; nrOfGenerations.Data++) {
           Execute(selector, scope);
 
@@ -178,33 +181,35 @@
           Execute(ci, s);
 
-          tempExePointer = executionPointer;
-          tempPersExePointer = persistedExecutionPointer.Data;
+          SaveExecutionPointer();
           // UniformSequentialSubScopesProcessor
-          for (int j = subscopeNr.Data; j < s.SubScopes.Count; j++) {
-            if (executionPointer == persistedExecutionPointer.Data)
-              persistedExecutionPointer.Data = tempExePointer;
-            executionPointer = tempExePointer;
-
-            s2 = s.SubScopes[j];
+          for (; subscopeNr.Data < s.SubScopes.Count; subscopeNr.Data++) {
+            SetExecutionPointerToLastSaved();
+
+            s2 = s.SubScopes[subscopeNr.Data];
             Execute(crossover, s2);
             // Stochastic Branch
-
-            randomNumber = random.NextDouble();
+            Execute(sb, s2);
+
+            // ganz böse!!!!!!!
+            // wird nach dem stochastic branch angehalten und später fortgesetzt,
+            // wird eine Zufallszahl erzeugt, die aber nicht verwendet wird.
+            // Dadurch kommt der GA auf ein anderes Endergebnis
+            // Lösung: Stochastic Branch Operator verwenden
+            //randomNumber = random.NextDouble();
             //output.AppendLine(randomNumber.ToString());
-            if (randomNumber < probability.Data)
-              Execute(mutator, s2);
-            else
-              Execute(empty, s2);
+            //if (randomNumber < probability.Data)
+            //  Execute(mutator, s2);
+            //else
+            //  Execute(empty, s2);
+
             Execute(evaluator, s2);
             Execute(sr, s2);
             Execute(counter, s2);
-            subscopeNr.Data++;
           } // foreach
-
 
           Execute(sorter, s);
           ////// END Create Children //////
 
-          ExecuteCreateReplacementWithFixedConstrolStructures(scope);
+          DoReplacement(scope);
           Execute(ql, scope);
           Execute(bawqc, scope);
@@ -212,21 +217,17 @@
           Execute(lci, scope);
           subscopeNr.Data = 0;
-          nrOfGenerations.Data++;
+          ResetExecutionPointer();
         } // for i
 
-
-
+        //TextWriter tw = new StreamWriter(DateTime.Now.ToFileTime() + ".txt");
+        //tw.Write(output.ToString());
+        //tw.Close();
+        //output = new StringBuilder();
+
+        swApply.Stop();
+        Console.WriteLine("SGAMain.Apply(): {0}", swApply.Elapsed);
       } // try
       catch (CancelException) {
         Console.WriteLine("Micro engine aborted by cancel flag.");
-      }
-      catch (Exception) { 
-          
-      }
-
-      swApply.Stop();
-      Console.WriteLine("SGAMain.Apply(): {0}", swApply.Elapsed);
-
-      if (Canceled) {
         return new AtomicOperation(this, scope);
       }
@@ -235,15 +236,11 @@
     } // Apply
 
-    private void WorkerMethod(object o) {
-     
-    } // Apply
-
-
-
     /// <summary>
-    /// Initializes some variables needed before the execution of create children
+    /// Fetch main operators like selector, crossover, mutator, ... from scope
+    /// and store them in instance variables.
     /// </summary>
     /// <param name="scope"></param>
-    private void InitializeExecuteCreateChildren(IScope scope) {
+    private void GetOperatorsFromScope(IScope scope) {
+      selector = (OperatorBase)GetVariableValue("Selector", scope, true);
       crossover = (OperatorBase)GetVariableValue("Crossover", scope, true);
       mutator = (OperatorBase)GetVariableValue("Mutator", scope, true);
@@ -252,6 +249,4 @@
       random = GetVariableValue<IRandom>("Random", scope, true);
       probability = GetVariableValue<DoubleData>("MutationRate", scope, true);
-
-      ci = new ChildrenInitializer();
     }
 
@@ -260,5 +255,5 @@
     /// </summary>
     /// <param name="scope"></param>
-    protected void ExecuteCreateChildrenWithFixedControlStructures(IScope scope) {
+    protected void CreateChildren(IScope scope) {
       // ChildrenInitializer
       Execute(ci, scope);
@@ -275,7 +270,7 @@
 
       Execute(sorter, scope);
-    } // ExecuteCreateChildrenHWCS
-
-    private void ExecuteCreateReplacementWithFixedConstrolStructures(IScope scope) {
+    } // CreateChildren
+
+    private void DoReplacement(IScope scope) {
       //// SequentialSubScopesProcessor
       Execute(ls, scope.SubScopes[0]);
@@ -287,7 +282,5 @@
       Execute(mr, scope);
       Execute(sorter, scope);
-    } // ExecuteCreateReplacementWithFixedConstrolStructures
-
-
+    } // DoReplacement
   } // class FixedSGAMain
 } // namespace HeuristicLab.FixedOperators
