Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.DataAnalysis.Symbolic/3.3/SymbolicDataAnalysisIslandGeneticAlgorithm.cs @ 14559

Last change on this file since 14559 was 10591, checked in by mkommend, 11 years ago

#1997: Added reevaluation of all indidviduals after migration to island algorithms and fixed symbolic data analysis evaluators for island algorithms.

File size: 10.9 KB
RevLine 
[9051]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[9067]23using System.Collections.Generic;
[9051]24using System.Linq;
25using HeuristicLab.Algorithms.GeneticAlgorithm;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
[10177]29using HeuristicLab.Operators;
[9077]30using HeuristicLab.Optimization;
[9051]31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34
35namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
[10142]36  [Item("Symbolic Data Analysis Island Genetic Algorithm", "A symbolic data analysis island genetic algorithm.")]
[9182]37  [Creatable("Data Analysis")]
[9051]38  [StorableClass]
[9067]39  public sealed class SymbolicDataAnalysisIslandGeneticAlgorithm : IslandGeneticAlgorithm {
40    private const string FixedSamplesParameterName = "NumberOfFixedSamples";
[9077]41    private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
[9067]42    private const string FixedSamplesPartitionsParameterName = "FixedSamplesPartitions";
[9077]43    private const string EvaluatorParameterName = "IslandEvaluator";
[10177]44    private const string IslandIndexParameterName = "IslandIndex";
[9077]45    private const string ProblemEvaluatorParameterName = "ProblemEvaluator";
[9051]46
47    #region Problem Properties
48    public override Type ProblemType {
49      get { return typeof(ISymbolicDataAnalysisSingleObjectiveProblem); }
50    }
51    public new ISymbolicDataAnalysisSingleObjectiveProblem Problem {
52      get { return (ISymbolicDataAnalysisSingleObjectiveProblem)base.Problem; }
53      set { base.Problem = value; }
54    }
55    #endregion
56
[9067]57    #region parameters
[10353]58    public IFixedValueParameter<PercentValue> FixedSamplesParameter {
59      get { return (IFixedValueParameter<PercentValue>)Parameters[FixedSamplesParameterName]; }
[9051]60    }
[9067]61    public IValueParameter<ItemArray<IntRange>> FixedSamplesPartitionsParameter {
62      get { return (IValueParameter<ItemArray<IntRange>>)Parameters[FixedSamplesPartitionsParameterName]; }
[9051]63    }
[10177]64    public IValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator> EvaluatorParameter {
65      get { return (IValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator>)Parameters[EvaluatorParameterName]; }
[9051]66    }
[9077]67    private ILookupParameter<ISingleObjectiveEvaluator> ProblemEvaluatorParameter {
68      get { return (ILookupParameter<ISingleObjectiveEvaluator>)Parameters[ProblemEvaluatorParameterName]; }
69    }
[9051]70    #endregion
71
[9067]72    #region properties
[10353]73    public double FixedSamples {
[9067]74      get { return FixedSamplesParameter.Value.Value; }
75      set { FixedSamplesParameter.Value.Value = value; }
[9051]76    }
[9067]77    public ItemArray<IntRange> FixedSamplesPartitions {
78      get { return FixedSamplesPartitionsParameter.Value; }
79      set { FixedSamplesPartitionsParameter.Value = value; }
[9051]80    }
[10357]81
82    private readonly ScopeTreeAssigner<IntValue> islandIndexAssigner;
[9051]83    #endregion
84
85    [StorableConstructor]
86    private SymbolicDataAnalysisIslandGeneticAlgorithm(bool deserializing) : base(deserializing) { }
87    [StorableHook(HookType.AfterDeserialization)]
88    private void AfterDeserialization() {
[9067]89      RegisterParameterEvents();
[9051]90    }
91    private SymbolicDataAnalysisIslandGeneticAlgorithm(SymbolicDataAnalysisIslandGeneticAlgorithm original, Cloner cloner)
92      : base(original, cloner) {
[9067]93      RegisterParameterEvents();
[9051]94    }
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new SymbolicDataAnalysisIslandGeneticAlgorithm(this, cloner);
97    }
98
99    public SymbolicDataAnalysisIslandGeneticAlgorithm()
100      : base() {
[10353]101      Parameters.Add(new FixedValueParameter<PercentValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island.", new PercentValue(0.2)));
[9067]102      Parameters.Add(new ValueParameter<ItemArray<IntRange>>(FixedSamplesPartitionsParameterName, "The fixed samples partitions used for fitness calculation for every island."));
[10177]103      Parameters.Add(new OptionalValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator>(EvaluatorParameterName, "The evaluator of the algorithm."));
[9077]104      Parameters.Add(new LookupParameter<ISingleObjectiveEvaluator>(ProblemEvaluatorParameterName, "Internal parameter for name translation", "Evaluator"));
[9051]105
[10357]106      islandIndexAssigner = new ScopeTreeAssigner<IntValue>();
[10177]107      islandIndexAssigner.Name = "Insert island index";
108      islandIndexAssigner.LeftSideParameter.ActualName = IslandIndexParameterName;
109      var readonlyIslandIndexes = Enumerable.Range(0, NumberOfIslands.Value).Select(x => (IntValue)new IntValue(x).AsReadOnly());
110      islandIndexAssigner.RightSideParameter.Value = new ItemArray<IntValue>(readonlyIslandIndexes);
111
[9077]112      ScopeTreeAssigner<IntRange> fixedSamplesPartitionCreator = new ScopeTreeAssigner<IntRange>();
[10142]113      fixedSamplesPartitionCreator.Name = "Create fixed evaluation partition";
[9077]114      fixedSamplesPartitionCreator.LeftSideParameter.ActualName = FixedSamplesPartitionParameterName;
115      fixedSamplesPartitionCreator.RightSideParameter.ActualName = FixedSamplesPartitionsParameterName;
116
[10177]117      SubScopesCreator insertionPoint = OperatorGraph.Iterate().OfType<SubScopesCreator>().First();
118      islandIndexAssigner.Successor = fixedSamplesPartitionCreator;
[9077]119      fixedSamplesPartitionCreator.Successor = insertionPoint.Successor;
[10177]120      insertionPoint.Successor = islandIndexAssigner;
[9077]121
[10591]122      ReevaluateImmigrants = true;
123      ReevaluteElites = true;
[10177]124
[9067]125      RegisterParameterEvents();
126      RecalculateFixedSamplesPartitions();
127    }
[9051]128
[9172]129    private void RegisterParameterEvents() {
130      if (Problem != null) Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
131      NumberOfIslandsParameter.ValueChanged += NumberOfIslandsParameter_ValueChanged;
[10357]132      NumberOfIslandsParameter.Value.ValueChanged += (o, ev) => NumberOfIslandsParameterValue_Changed();
[10156]133      FixedSamplesParameter.Value.ValueChanged += (o, e) => {
134        RecalculateFixedSamplesPartitions();
[10230]135        ReevaluateImmigrants = FixedSamples < Problem.FitnessCalculationPartition.Size;
[10156]136      };
[9172]137      Analyzer.Operators.PropertyChanged += (o, e) => ParameterizeAnalyzers();
[10156]138      EvaluatorParameter.ValueChanged += (o, e) => ParameterizeEvaluator();
[9172]139    }
140
[9077]141    protected override void ParameterizeSolutionsCreator() {
142      base.ParameterizeSolutionsCreator();
143      SolutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameterName;
144    }
145
146    protected override void ParameterizeMainLoop() {
147      base.ParameterizeMainLoop();
148      MainLoop.EvaluatorParameter.ActualName = EvaluatorParameterName;
149      MainLoop.QualityParameter.ActualName = EvaluatorParameter.Value.QualityParameter.ActualName;
150    }
151
[10142]152    protected override void ParameterizeAnalyzers() {
153      base.ParameterizeAnalyzers();
[9172]154      foreach (var analyzer in Analyzer.Operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
155        IParameter evaluatorParameter;
156        if (analyzer.Parameters.TryGetValue("Evaluator", out evaluatorParameter)) {
157          ILookupParameter param = evaluatorParameter as ILookupParameter;
158          if (evaluatorParameter != null) param.ActualName = ProblemEvaluatorParameterName;
159        }
160      }
[9067]161    }
[9051]162
[10156]163    private void ParameterizeEvaluator() {
164      var evaluator = EvaluatorParameter.Value;
[10230]165      evaluator.IterationsParameter.ActualName = "Generations";
166      evaluator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
167      evaluator.DataMigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
[10177]168
[10230]169      ParameterizeStochasticOperatorForIsland(evaluator);
[10156]170    }
171
[9067]172    private void NumberOfIslandsParameter_ValueChanged(object sender, EventArgs e) {
[10357]173      NumberOfIslands.ValueChanged += (o, ev) => NumberOfIslandsParameterValue_Changed();
174      NumberOfIslandsParameterValue_Changed();
175    }
176    private void NumberOfIslandsParameterValue_Changed() {
177      var readonlyIslandIndexes = Enumerable.Range(0, NumberOfIslands.Value).Select(x => (IntValue)new IntValue(x).AsReadOnly());
178      islandIndexAssigner.RightSideParameter.Value = new ItemArray<IntValue>(readonlyIslandIndexes);
[9067]179      RecalculateFixedSamplesPartitions();
[9051]180    }
181
[10356]182    protected override void Problem_Reset(object sender, EventArgs e) {
183      base.Problem_Reset(sender, e);
184      RecalculateFixedSamplesPartitions();
185    }
186
[9051]187    protected override void OnProblemChanged() {
[9172]188      Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
[9077]189
[10156]190      if (Problem != null && EvaluatorParameter.Value == null) {
[10177]191        EvaluatorParameter.Value = new RandomSamplesEvaluator();
[10156]192      } else if (Problem == null)
[9077]193        EvaluatorParameter.Value = null;
194
[10156]195      ParameterizeStochasticOperator(EvaluatorParameter.Value);
[9067]196      RecalculateFixedSamplesPartitions();
[9077]197      base.OnProblemChanged();
[9051]198    }
199
[9067]200    private void RecalculateFixedSamplesPartitions() {
201      if (Problem == null) {
202        FixedSamplesPartitions = new ItemArray<IntRange>(Enumerable.Repeat(new IntRange(), NumberOfIslands.Value));
203        return;
204      }
205      var samplesStart = Problem.FitnessCalculationPartition.Start;
206      var samplesEnd = Problem.FitnessCalculationPartition.End;
207      var totalSamples = Problem.FitnessCalculationPartition.Size;
[10353]208      var fixedSamples = (int)(FixedSamples * totalSamples);
[9067]209      var islands = NumberOfIslands.Value;
[9051]210
[10353]211      double shift = (double)((totalSamples - fixedSamples)) / (islands - 1);
212      int offset = (int)Math.Floor(shift);
213      double remainder = shift - offset;
214
[9067]215      List<IntRange> partitions = new List<IntRange>();
216      for (int i = 0; i < islands; i++) {
[10353]217        var partitionStart = samplesStart + offset * i + (int)(remainder * i);
[9067]218        partitions.Add(new IntRange(partitionStart, partitionStart + fixedSamples));
[9051]219      }
[9067]220
[10353]221      //if the last partitions exceeds the allowed samples move the last partition forward.
[9067]222      int exceedsSamples = partitions[partitions.Count - 1].End - samplesEnd;
223      if (exceedsSamples > 0) {
224        partitions[partitions.Count - 1].Start -= exceedsSamples;
225        partitions[partitions.Count - 1].End -= exceedsSamples;
[9051]226      }
[9067]227      FixedSamplesPartitions = new ItemArray<IntRange>(partitions);
[9051]228    }
229
230  }
231}
Note: See TracBrowser for help on using the repository browser.