Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11578 for branches/ALPS


Ignore:
Timestamp:
11/25/14 09:19:33 (10 years ago)
Author:
pfleck
Message:

#2269 Implemented wiring of ALPS-GA based on Island-GA wiring.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithm.cs

    r11568 r11578  
    2121
    2222using System;
     23using System.Linq;
    2324using HeuristicLab.Analysis;
    2425using HeuristicLab.Common;
     
    2728using HeuristicLab.Operators;
    2829using HeuristicLab.Optimization;
     30using HeuristicLab.Optimization.Operators;
    2931using HeuristicLab.Parameters;
    3032using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     33using HeuristicLab.PluginInfrastructure;
     34using HeuristicLab.Random;
     35using HeuristicLab.Selection;
    3136
    3237namespace HeuristicLab.Algorithms.ALPS {
     
    171176      set { ReevaluateElitesParameter.Value.Value = value; }
    172177    }
     178
     179    private RandomCreator GlobalRandomCreator {
     180      get { return (RandomCreator)OperatorGraph.InitialOperator; }
     181    }
     182    private SolutionsCreator SolutionsCreator {
     183      get { return OperatorGraph.Iterate().OfType<SolutionsCreator>().First(); }
     184    }
     185
     186    [Storable]
     187    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
     188    [Storable]
     189    private BestAverageWorstQualityAnalyzer layerQualityAnalyzer;
    173190    #endregion
    174191
     
    178195    private AlpsGeneticAlgorithm(AlpsGeneticAlgorithm original, Cloner cloner)
    179196      : base(original, cloner) {
    180       // TODO
     197      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
     198      layerQualityAnalyzer = cloner.Clone(original.layerQualityAnalyzer);
     199      Initialize();
    181200    }
    182201    public override IDeepCloneable Clone(Cloner cloner) {
     
    195214      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)));
    196215      Parameters.Add(new ValueParameter<IntArray>("AgeLimits", new IntArray(new[] { 1, 20, 45, 80, 125 })) { Hidden = true });
    197       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 });
     216      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 });
    198217      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
    199218      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
     
    202221      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
    203222      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 });
    204     }
     223
     224      var globalRandomCreator = new RandomCreator();
     225      var layer0Creator = new SubScopesCreator() { Name = "Create Layer Zero" };
     226      var layer0Processor = new SubScopesProcessor();
     227      var localRandomCreator = new LocalRandomCreator();
     228      var layerSolutionsCreator = new SolutionsCreator();
     229      var initializeAgeProcessor = new UniformSubScopesProcessor();
     230      var initializeAge = new VariableCreator() { Name = "Initialize Age" };
     231      var initializeLocalEvaluatedSolutions = new SubScopesCounter();
     232      var initializeGlobalEvaluatedSolutions = new DataReducer() { Name = "Initialize EvaluatedSolutions" };
     233      var resultsCollector = new ResultsCollector();
     234      var mainLoop = new CombinedOperator();
     235      OperatorGraph.InitialOperator = globalRandomCreator;
     236
     237      globalRandomCreator.RandomParameter.ActualName = "GlobalRandom";
     238      globalRandomCreator.SeedParameter.Value = null;
     239      globalRandomCreator.SetSeedRandomlyParameter.Value = null;
     240      globalRandomCreator.Successor = layer0Creator;
     241
     242      layer0Creator.NumberOfSubScopesParameter.Value = new IntValue(1);
     243      layer0Creator.Successor = layer0Processor;
     244
     245      layer0Processor.Operators.Add(localRandomCreator);
     246      layer0Processor.Successor = initializeGlobalEvaluatedSolutions;
     247
     248      localRandomCreator.Successor = layerSolutionsCreator;
     249
     250      layerSolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
     251      layerSolutionsCreator.Successor = initializeAgeProcessor;
     252
     253      initializeAgeProcessor.Operator = initializeAge;
     254      initializeAgeProcessor.Successor = initializeLocalEvaluatedSolutions;
     255
     256      initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("Age", new IntValue(0)));
     257      initializeAge.Successor = null;
     258
     259      initializeLocalEvaluatedSolutions.ValueParameter.ActualName = "EvaluatedSolutions";
     260      initializeLocalEvaluatedSolutions.Successor = null;
     261
     262      initializeGlobalEvaluatedSolutions.ReductionOperation.Value.Value = ReductionOperations.Sum;
     263      initializeGlobalEvaluatedSolutions.TargetOperation.Value.Value = ReductionOperations.Assign;
     264      initializeGlobalEvaluatedSolutions.ParameterToReduce.ActualName = "EvaluatedSolutions";
     265      initializeGlobalEvaluatedSolutions.TargetParameter.ActualName = "EvaluatedSolutions";
     266      initializeGlobalEvaluatedSolutions.Successor = resultsCollector;
     267
     268      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("EvaluatedSolutions"));
     269      resultsCollector.Successor = mainLoop;
     270
     271      foreach (var selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(s => !(s is IMultiObjectiveSelector)).OrderBy(s => Name))
     272        SelectorParameter.ValidValues.Add(selector);
     273      var porportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(s => s is ProportionalSelector);
     274      if (porportionalSelector != null) SelectorParameter.Value = porportionalSelector;
     275
     276      ParameterizeSelectors();
     277
     278      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
     279      layerQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
     280      ParameterizeAnalyzers();
     281      UpdateAnalyzers();
     282
     283      Initialize();
     284    }
     285
     286    public override void Prepare() {
     287      if (Problem != null)
     288        base.Prepare();
     289    }
     290
     291    #region Events
     292    protected override void OnProblemChanged() {
     293      ParameterizeStochasticOperator(Problem.SolutionCreator);
     294      ParameterizeStochasticOperatorForLayer(Problem.Evaluator);
     295      foreach (var @operator in Problem.Operators.OfType<IOperator>())
     296        ParameterizeStochasticOperator(@operator);
     297      ParameterizeSolutionsCreator();
     298      ParameterizeMainLoop();
     299      ParameterizeSelectors();
     300      ParameterizeAnalyzers();
     301      ParameterizeIterationBasedOperators();
     302      UpdateCrossovers();
     303      UpdateMutators();
     304      UpdateAnalyzers();
     305      Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
     306      base.OnProblemChanged();
     307    }
     308
     309    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
     310      ParameterizeStochasticOperator(Problem.SolutionCreator);
     311      ParameterizeSolutionsCreator();
     312      base.Problem_SolutionCreatorChanged(sender, e);
     313    }
     314    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
     315      ParameterizeStochasticOperatorForLayer(Problem.Evaluator);
     316      ParameterizeSolutionsCreator();
     317      ParameterizeMainLoop();
     318      ParameterizeSelectors();
     319      ParameterizeAnalyzers();
     320      Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
     321      base.Problem_EvaluatorChanged(sender, e);
     322    }
     323    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
     324      foreach (var @operator in Problem.Operators.OfType<IOperator>())
     325        ParameterizeStochasticOperator(@operator);
     326      ParameterizeIterationBasedOperators();
     327      UpdateCrossovers();
     328      UpdateMutators();
     329      UpdateAnalyzers();
     330      base.Problem_OperatorsChanged(sender, e);
     331    }
     332    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
     333      ParameterizeMainLoop();
     334      ParameterizeSelectors();
     335      ParameterizeAnalyzers();
     336    }
     337
     338    void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
     339      PopulationSizeParameter.ValueChanged += PopulationSize_ValueChanged;
     340      ParameterizeSelectors();
     341    }
     342    void PopulationSize_ValueChanged(object sender, EventArgs e) {
     343      ParameterizeSelectors();
     344    }
     345    void ElitesParameter_ValueChanged(object sender, EventArgs e) {
     346      Elites.ValueChanged += ElitesParameter_ValueChanged;
     347      ParameterizeSelectors();
     348    }
     349    void Elites_ValueChanged(object sender, EventArgs e) {
     350      ParameterizeSelectors();
     351    }
     352    #endregion
     353
     354    #region Parameterization
     355    private void Initialize() {
     356      PopulationSizeParameter.ValueChanged += PopulationSizeParameter_ValueChanged;
     357      PopulationSize.ValueChanged += PopulationSize_ValueChanged;
     358      ElitesParameter.ValueChanged += ElitesParameter_ValueChanged;
     359      Elites.ValueChanged += Elites_ValueChanged;
     360      if (Problem != null)
     361        Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
     362    }
     363    private void ParameterizeSolutionsCreator() {
     364      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
     365      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
     366    }
     367    private void ParameterizeMainLoop() {
     368      // TODO
     369    }
     370    private void ParameterizeSelectors() {
     371      foreach (var selector in SelectorParameter.ValidValues) {
     372        selector.CopySelected = new BoolValue(true);
     373        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
     374        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
     375        ParameterizeStochasticOperatorForLayer(selector);
     376      }
     377      if (Problem != null) {
     378        foreach (var selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
     379          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
     380          selector.MaximizationParameter.Hidden = true;
     381          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
     382          selector.QualityParameter.Hidden = true;
     383        }
     384      }
     385    }
     386    private void ParameterizeAnalyzers() {
     387      qualityAnalyzer.ResultsParameter.ActualName = "Results";
     388      qualityAnalyzer.ResultsParameter.Hidden = true;
     389      qualityAnalyzer.QualityParameter.Depth = 2;
     390      layerQualityAnalyzer.ResultsParameter.ActualName = "Results";
     391      layerQualityAnalyzer.ResultsParameter.Hidden = true;
     392      layerQualityAnalyzer.QualityParameter.Depth = 1;
     393      if (Problem != null) {
     394        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
     395        qualityAnalyzer.MaximizationParameter.Hidden = true;
     396        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
     397        qualityAnalyzer.QualityParameter.Hidden = true;
     398        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
     399        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
     400        layerQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
     401        layerQualityAnalyzer.MaximizationParameter.Hidden = true;
     402        layerQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
     403        layerQualityAnalyzer.QualityParameter.Hidden = true;
     404        layerQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
     405        layerQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
     406      }
     407    }
     408    private void ParameterizeIterationBasedOperators() {
     409      if (Problem != null) {
     410        foreach (var @operator in Problem.Operators.OfType<IIterationBasedOperator>()) {
     411          @operator.IterationsParameter.ActualName = "Generations";
     412          @operator.IterationsParameter.Hidden = true;
     413          @operator.MaximumIterationsParameter.ActualName = "MaximumGenerations";
     414          @operator.MaximumIterationsParameter.Hidden = true;
     415        }
     416      }
     417    }
     418    private void ParameterizeStochasticOperator(IOperator @operator) {
     419      var stochasticOperator = @operator as IStochasticOperator;
     420      if (stochasticOperator != null) {
     421        stochasticOperator.RandomParameter.ActualName = GlobalRandomCreator.RandomParameter.ActualName;
     422        stochasticOperator.RandomParameter.Hidden = true;
     423      }
     424    }
     425    private void ParameterizeStochasticOperatorForLayer(IOperator @operator) {
     426      var stochasticOperator = @operator as IStochasticOperator;
     427      if (stochasticOperator != null) {
     428        stochasticOperator.RandomParameter.ActualName = "LocalRandom";
     429        stochasticOperator.RandomParameter.Hidden = true;
     430      }
     431    }
     432
     433    #endregion
     434
     435    #region Updates
     436    private void UpdateCrossovers() {
     437      var oldCrossover = CrossoverParameter.Value;
     438      var defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
     439      CrossoverParameter.ValidValues.Clear();
     440      foreach (var crossover in Problem.Operators.OfType<ICrossover>().OrderBy(c => c.Name)) {
     441        ParameterizeStochasticOperatorForLayer(crossover);
     442        CrossoverParameter.ValidValues.Add(crossover);
     443      }
     444      if (oldCrossover != null) {
     445        var crossover = CrossoverParameter.ValidValues.FirstOrDefault(c => c.GetType() == oldCrossover.GetType());
     446        CrossoverParameter.Value = crossover;
     447      }
     448      if (oldCrossover == null && defaultCrossover != null)
     449        CrossoverParameter.Value = defaultCrossover;
     450    }
     451    private void UpdateMutators() {
     452      var oldMutator = MutatorParameter.Value;
     453      MutatorParameter.ValidValues.Clear();
     454      foreach (var mutator in Problem.Operators.OfType<IManipulator>().OrderBy(m => m.Name)) {
     455        ParameterizeStochasticOperatorForLayer(mutator);
     456        MutatorParameter.ValidValues.Add(mutator);
     457      }
     458      if (oldMutator != null) {
     459        var mutator = MutatorParameter.ValidValues.FirstOrDefault(m => m.GetType() == oldMutator.GetType());
     460        MutatorParameter.Value = mutator;
     461      }
     462    }
     463    private void UpdateAnalyzers() {
     464      Analyzer.Operators.Clear();
     465      LayerAnalyzer.Operators.Clear();
     466
     467      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
     468      LayerAnalyzer.Operators.Add(layerQualityAnalyzer, layerQualityAnalyzer.EnabledByDefault);
     469
     470      if (Problem != null) {
     471        foreach (var analyzer in Problem.Operators.OfType<IAnalyzer>()) {
     472          foreach (var parameter in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
     473            parameter.Depth = 2;
     474          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
     475        }
     476      }
     477    }
     478    #endregion
    205479  }
    206480}
Note: See TracChangeset for help on using the changeset viewer.