Index: /branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithm.cs
===================================================================
--- /branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithm.cs	(revision 11577)
+++ /branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithm.cs	(revision 11578)
@@ -21,4 +21,5 @@
 
 using System;
+using System.Linq;
 using HeuristicLab.Analysis;
 using HeuristicLab.Common;
@@ -27,6 +28,10 @@
 using HeuristicLab.Operators;
 using HeuristicLab.Optimization;
+using HeuristicLab.Optimization.Operators;
 using HeuristicLab.Parameters;
 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+using HeuristicLab.PluginInfrastructure;
+using HeuristicLab.Random;
+using HeuristicLab.Selection;
 
 namespace HeuristicLab.Algorithms.ALPS {
@@ -171,4 +176,16 @@
       set { ReevaluateElitesParameter.Value.Value = value; }
     }
+
+    private RandomCreator GlobalRandomCreator {
+      get { return (RandomCreator)OperatorGraph.InitialOperator; }
+    }
+    private SolutionsCreator SolutionsCreator {
+      get { return OperatorGraph.Iterate().OfType<SolutionsCreator>().First(); }
+    }
+
+    [Storable]
+    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
+    [Storable]
+    private BestAverageWorstQualityAnalyzer layerQualityAnalyzer;
     #endregion
 
@@ -178,5 +195,7 @@
     private AlpsGeneticAlgorithm(AlpsGeneticAlgorithm original, Cloner cloner)
       : base(original, cloner) {
-      // TODO
+      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
+      layerQualityAnalyzer = cloner.Clone(original.layerQualityAnalyzer);
+      Initialize();
     }
     public override IDeepCloneable Clone(Cloner cloner) {
@@ -195,5 +214,5 @@
       Parameters.Add(new ValueParameter<IntValue>("AgeGap", "The frequency of reseeding the lowest layer and scaling factor for the age-limits for the layers", new IntValue(5)));
       Parameters.Add(new ValueParameter<IntArray>("AgeLimits", new IntArray(new[] { 1, 20, 45, 80, 125 })) { Hidden = true });
-      Parameters.Add(new ValueParameter<ReductionOperation>("AgeInheritance", "The operator for determining the age of an offspring based the parents age.", new ReductionOperation(ReductionOperations.Max)) { Hidden = true });
+      Parameters.Add(new ValueParameter<ReductionOperation>("AgeInheritance", "The operator for determining the age of an offspring based the parents' age.", new ReductionOperation(ReductionOperations.Max)) { Hidden = true });
       Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
       Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
@@ -202,5 +221,260 @@
       Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
       Parameters.Add(new FixedValueParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", new BoolValue(false)) { Hidden = true });
-    }
+
+      var globalRandomCreator = new RandomCreator();
+      var layer0Creator = new SubScopesCreator() { Name = "Create Layer Zero" };
+      var layer0Processor = new SubScopesProcessor();
+      var localRandomCreator = new LocalRandomCreator();
+      var layerSolutionsCreator = new SolutionsCreator();
+      var initializeAgeProcessor = new UniformSubScopesProcessor();
+      var initializeAge = new VariableCreator() { Name = "Initialize Age" };
+      var initializeLocalEvaluatedSolutions = new SubScopesCounter();
+      var initializeGlobalEvaluatedSolutions = new DataReducer() { Name = "Initialize EvaluatedSolutions" };
+      var resultsCollector = new ResultsCollector();
+      var mainLoop = new CombinedOperator();
+      OperatorGraph.InitialOperator = globalRandomCreator;
+
+      globalRandomCreator.RandomParameter.ActualName = "GlobalRandom";
+      globalRandomCreator.SeedParameter.Value = null;
+      globalRandomCreator.SetSeedRandomlyParameter.Value = null;
+      globalRandomCreator.Successor = layer0Creator;
+
+      layer0Creator.NumberOfSubScopesParameter.Value = new IntValue(1);
+      layer0Creator.Successor = layer0Processor;
+
+      layer0Processor.Operators.Add(localRandomCreator);
+      layer0Processor.Successor = initializeGlobalEvaluatedSolutions;
+
+      localRandomCreator.Successor = layerSolutionsCreator;
+
+      layerSolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
+      layerSolutionsCreator.Successor = initializeAgeProcessor;
+
+      initializeAgeProcessor.Operator = initializeAge;
+      initializeAgeProcessor.Successor = initializeLocalEvaluatedSolutions;
+
+      initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("Age", new IntValue(0)));
+      initializeAge.Successor = null;
+
+      initializeLocalEvaluatedSolutions.ValueParameter.ActualName = "EvaluatedSolutions";
+      initializeLocalEvaluatedSolutions.Successor = null;
+
+      initializeGlobalEvaluatedSolutions.ReductionOperation.Value.Value = ReductionOperations.Sum;
+      initializeGlobalEvaluatedSolutions.TargetOperation.Value.Value = ReductionOperations.Assign;
+      initializeGlobalEvaluatedSolutions.ParameterToReduce.ActualName = "EvaluatedSolutions";
+      initializeGlobalEvaluatedSolutions.TargetParameter.ActualName = "EvaluatedSolutions";
+      initializeGlobalEvaluatedSolutions.Successor = resultsCollector;
+
+      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("EvaluatedSolutions"));
+      resultsCollector.Successor = mainLoop;
+
+      foreach (var selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(s => !(s is IMultiObjectiveSelector)).OrderBy(s => Name))
+        SelectorParameter.ValidValues.Add(selector);
+      var porportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(s => s is ProportionalSelector);
+      if (porportionalSelector != null) SelectorParameter.Value = porportionalSelector;
+
+      ParameterizeSelectors();
+
+      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
+      layerQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
+      ParameterizeAnalyzers();
+      UpdateAnalyzers();
+
+      Initialize();
+    }
+
+    public override void Prepare() {
+      if (Problem != null)
+        base.Prepare();
+    }
+
+    #region Events
+    protected override void OnProblemChanged() {
+      ParameterizeStochasticOperator(Problem.SolutionCreator);
+      ParameterizeStochasticOperatorForLayer(Problem.Evaluator);
+      foreach (var @operator in Problem.Operators.OfType<IOperator>())
+        ParameterizeStochasticOperator(@operator);
+      ParameterizeSolutionsCreator();
+      ParameterizeMainLoop();
+      ParameterizeSelectors();
+      ParameterizeAnalyzers();
+      ParameterizeIterationBasedOperators();
+      UpdateCrossovers();
+      UpdateMutators();
+      UpdateAnalyzers();
+      Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
+      base.OnProblemChanged();
+    }
+
+    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
+      ParameterizeStochasticOperator(Problem.SolutionCreator);
+      ParameterizeSolutionsCreator();
+      base.Problem_SolutionCreatorChanged(sender, e);
+    }
+    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
+      ParameterizeStochasticOperatorForLayer(Problem.Evaluator);
+      ParameterizeSolutionsCreator();
+      ParameterizeMainLoop();
+      ParameterizeSelectors();
+      ParameterizeAnalyzers();
+      Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
+      base.Problem_EvaluatorChanged(sender, e);
+    }
+    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
+      foreach (var @operator in Problem.Operators.OfType<IOperator>())
+        ParameterizeStochasticOperator(@operator);
+      ParameterizeIterationBasedOperators();
+      UpdateCrossovers();
+      UpdateMutators();
+      UpdateAnalyzers();
+      base.Problem_OperatorsChanged(sender, e);
+    }
+    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
+      ParameterizeMainLoop();
+      ParameterizeSelectors();
+      ParameterizeAnalyzers();
+    }
+
+    void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
+      PopulationSizeParameter.ValueChanged += PopulationSize_ValueChanged;
+      ParameterizeSelectors();
+    }
+    void PopulationSize_ValueChanged(object sender, EventArgs e) {
+      ParameterizeSelectors();
+    }
+    void ElitesParameter_ValueChanged(object sender, EventArgs e) {
+      Elites.ValueChanged += ElitesParameter_ValueChanged;
+      ParameterizeSelectors();
+    }
+    void Elites_ValueChanged(object sender, EventArgs e) {
+      ParameterizeSelectors();
+    }
+    #endregion
+
+    #region Parameterization
+    private void Initialize() {
+      PopulationSizeParameter.ValueChanged += PopulationSizeParameter_ValueChanged;
+      PopulationSize.ValueChanged += PopulationSize_ValueChanged;
+      ElitesParameter.ValueChanged += ElitesParameter_ValueChanged;
+      Elites.ValueChanged += Elites_ValueChanged;
+      if (Problem != null)
+        Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
+    }
+    private void ParameterizeSolutionsCreator() {
+      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
+      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
+    }
+    private void ParameterizeMainLoop() {
+      // TODO
+    }
+    private void ParameterizeSelectors() {
+      foreach (var selector in SelectorParameter.ValidValues) {
+        selector.CopySelected = new BoolValue(true);
+        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
+        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
+        ParameterizeStochasticOperatorForLayer(selector);
+      }
+      if (Problem != null) {
+        foreach (var selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
+          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
+          selector.MaximizationParameter.Hidden = true;
+          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
+          selector.QualityParameter.Hidden = true;
+        }
+      }
+    }
+    private void ParameterizeAnalyzers() {
+      qualityAnalyzer.ResultsParameter.ActualName = "Results";
+      qualityAnalyzer.ResultsParameter.Hidden = true;
+      qualityAnalyzer.QualityParameter.Depth = 2;
+      layerQualityAnalyzer.ResultsParameter.ActualName = "Results";
+      layerQualityAnalyzer.ResultsParameter.Hidden = true;
+      layerQualityAnalyzer.QualityParameter.Depth = 1;
+      if (Problem != null) {
+        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
+        qualityAnalyzer.MaximizationParameter.Hidden = true;
+        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
+        qualityAnalyzer.QualityParameter.Hidden = true;
+        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
+        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
+        layerQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
+        layerQualityAnalyzer.MaximizationParameter.Hidden = true;
+        layerQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
+        layerQualityAnalyzer.QualityParameter.Hidden = true;
+        layerQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
+        layerQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
+      }
+    }
+    private void ParameterizeIterationBasedOperators() {
+      if (Problem != null) {
+        foreach (var @operator in Problem.Operators.OfType<IIterationBasedOperator>()) {
+          @operator.IterationsParameter.ActualName = "Generations";
+          @operator.IterationsParameter.Hidden = true;
+          @operator.MaximumIterationsParameter.ActualName = "MaximumGenerations";
+          @operator.MaximumIterationsParameter.Hidden = true;
+        }
+      }
+    }
+    private void ParameterizeStochasticOperator(IOperator @operator) {
+      var stochasticOperator = @operator as IStochasticOperator;
+      if (stochasticOperator != null) {
+        stochasticOperator.RandomParameter.ActualName = GlobalRandomCreator.RandomParameter.ActualName;
+        stochasticOperator.RandomParameter.Hidden = true;
+      }
+    }
+    private void ParameterizeStochasticOperatorForLayer(IOperator @operator) {
+      var stochasticOperator = @operator as IStochasticOperator;
+      if (stochasticOperator != null) {
+        stochasticOperator.RandomParameter.ActualName = "LocalRandom";
+        stochasticOperator.RandomParameter.Hidden = true;
+      }
+    }
+
+    #endregion
+
+    #region Updates
+    private void UpdateCrossovers() {
+      var oldCrossover = CrossoverParameter.Value;
+      var defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
+      CrossoverParameter.ValidValues.Clear();
+      foreach (var crossover in Problem.Operators.OfType<ICrossover>().OrderBy(c => c.Name)) {
+        ParameterizeStochasticOperatorForLayer(crossover);
+        CrossoverParameter.ValidValues.Add(crossover);
+      }
+      if (oldCrossover != null) {
+        var crossover = CrossoverParameter.ValidValues.FirstOrDefault(c => c.GetType() == oldCrossover.GetType());
+        CrossoverParameter.Value = crossover;
+      }
+      if (oldCrossover == null && defaultCrossover != null)
+        CrossoverParameter.Value = defaultCrossover;
+    }
+    private void UpdateMutators() {
+      var oldMutator = MutatorParameter.Value;
+      MutatorParameter.ValidValues.Clear();
+      foreach (var mutator in Problem.Operators.OfType<IManipulator>().OrderBy(m => m.Name)) {
+        ParameterizeStochasticOperatorForLayer(mutator);
+        MutatorParameter.ValidValues.Add(mutator);
+      }
+      if (oldMutator != null) {
+        var mutator = MutatorParameter.ValidValues.FirstOrDefault(m => m.GetType() == oldMutator.GetType());
+        MutatorParameter.Value = mutator;
+      }
+    }
+    private void UpdateAnalyzers() {
+      Analyzer.Operators.Clear();
+      LayerAnalyzer.Operators.Clear();
+
+      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
+      LayerAnalyzer.Operators.Add(layerQualityAnalyzer, layerQualityAnalyzer.EnabledByDefault);
+
+      if (Problem != null) {
+        foreach (var analyzer in Problem.Operators.OfType<IAnalyzer>()) {
+          foreach (var parameter in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
+            parameter.Depth = 2;
+          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
+        }
+      }
+    }
+    #endregion
   }
 }
