Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.DataAnalysis.Symbolic/3.3/SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm.cs @ 11729

Last change on this file since 11729 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: 11.1 KB
Line 
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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34
35namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
36  [Item("Symbolic Data Analysis Island Offspring Selection Genetic Algorithm", "A symbolic data analysis island offspring selection genetic algorithm.")]
37  [Creatable("Data Analysis")]
38  [StorableClass]
39  public sealed class SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm : IslandOffspringSelectionGeneticAlgorithm {
40    private const string FixedSamplesParameterName = "NumberOfFixedSamples";
41    private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
42    private const string FixedSamplesPartitionsParameterName = "FixedSamplesPartitions";
43    private const string EvaluatorParameterName = "IslandEvaluator";
44    private const string IslandIndexParameterName = "IslandIndex";
45    private const string ProblemEvaluatorParameterName = "ProblemEvaluator";
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
57    #region parameters
58    public IFixedValueParameter<PercentValue> FixedSamplesParameter {
59      get { return (IFixedValueParameter<PercentValue>)Parameters[FixedSamplesParameterName]; }
60    }
61    public IValueParameter<ItemArray<IntRange>> FixedSamplesPartitionsParameter {
62      get { return (IValueParameter<ItemArray<IntRange>>)Parameters[FixedSamplesPartitionsParameterName]; }
63    }
64    public IValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator> EvaluatorParameter {
65      get { return (IValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator>)Parameters[EvaluatorParameterName]; }
66    }
67    private ILookupParameter<ISingleObjectiveEvaluator> ProblemEvaluatorParameter {
68      get { return (ILookupParameter<ISingleObjectiveEvaluator>)Parameters[ProblemEvaluatorParameterName]; }
69    }
70    #endregion
71
72    #region properties
73    public double FixedSamples {
74      get { return FixedSamplesParameter.Value.Value; }
75      set { FixedSamplesParameter.Value.Value = value; }
76    }
77    public ItemArray<IntRange> FixedSamplesPartitions {
78      get { return FixedSamplesPartitionsParameter.Value; }
79      set { FixedSamplesPartitionsParameter.Value = value; }
80    }
81
82    private readonly ScopeTreeAssigner<IntValue> islandIndexAssigner;
83    #endregion
84
85    [StorableConstructor]
86    private SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm(bool deserializing) : base(deserializing) { }
87    [StorableHook(HookType.AfterDeserialization)]
88    private void AfterDeserialization() {
89      RegisterParameterEvents();
90    }
91    private SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm(SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm original, Cloner cloner)
92      : base(original, cloner) {
93      RegisterParameterEvents();
94    }
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm(this, cloner);
97    }
98
99    public SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm()
100      : base() {
101      Parameters.Add(new FixedValueParameter<PercentValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island.", new PercentValue(0.2)));
102      Parameters.Add(new ValueParameter<ItemArray<IntRange>>(FixedSamplesPartitionsParameterName, "The fixed samples partitions used for fitness calculation for every island."));
103      Parameters.Add(new OptionalValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator>(EvaluatorParameterName, "The evaluator of the algorithm."));
104      Parameters.Add(new LookupParameter<ISingleObjectiveEvaluator>(ProblemEvaluatorParameterName, "Internal parameter for name translation", "Evaluator"));
105
106      islandIndexAssigner = new ScopeTreeAssigner<IntValue>();
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
112      ScopeTreeAssigner<IntRange> fixedSamplesPartitionCreator = new ScopeTreeAssigner<IntRange>();
113      fixedSamplesPartitionCreator.Name = "Create fixed evaluation partition";
114      fixedSamplesPartitionCreator.LeftSideParameter.ActualName = FixedSamplesPartitionParameterName;
115      fixedSamplesPartitionCreator.RightSideParameter.ActualName = FixedSamplesPartitionsParameterName;
116
117      SubScopesCreator insertionPoint = OperatorGraph.Iterate().OfType<SubScopesCreator>().First();
118      islandIndexAssigner.Successor = fixedSamplesPartitionCreator;
119      fixedSamplesPartitionCreator.Successor = insertionPoint.Successor;
120      insertionPoint.Successor = islandIndexAssigner;
121
122      ReevaluateImmigrants = true;
123      ReevaluteElites = true;
124
125      RegisterParameterEvents();
126      RecalculateFixedSamplesPartitions();
127    }
128
129    private void RegisterParameterEvents() {
130      if (Problem != null) Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
131      NumberOfIslandsParameter.ValueChanged += NumberOfIslandsParameter_ValueChanged;
132      NumberOfIslandsParameter.Value.ValueChanged += (o, ev) => NumberOfIslandsParameterValue_Changed();
133      FixedSamplesParameter.Value.ValueChanged += (o, e) => {
134        RecalculateFixedSamplesPartitions();
135        ReevaluateImmigrants = FixedSamples < Problem.FitnessCalculationPartition.Size;
136      };
137      Analyzer.Operators.PropertyChanged += (o, e) => ParameterizeAnalyzers();
138      EvaluatorParameter.ValueChanged += (o, e) => ParameterizeEvaluator();
139    }
140
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
152    protected override void ParameterizeAnalyzers() {
153      base.ParameterizeAnalyzers();
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      }
161    }
162
163    private void ParameterizeEvaluator() {
164      var evaluator = EvaluatorParameter.Value;
165      evaluator.IterationsParameter.ActualName = "Generations";
166      evaluator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
167      evaluator.DataMigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
168
169      ParameterizeStochasticOperator(evaluator);
170    }
171
172    private void NumberOfIslandsParameter_ValueChanged(object sender, EventArgs e) {
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);
179      RecalculateFixedSamplesPartitions();
180    }
181
182    protected override void Problem_Reset(object sender, EventArgs e) {
183      base.Problem_Reset(sender, e);
184      RecalculateFixedSamplesPartitions();
185    }
186
187    protected override void OnProblemChanged() {
188      Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
189
190      if (Problem != null && EvaluatorParameter.Value == null) {
191        EvaluatorParameter.Value = new RandomSamplesEvaluator();
192      } else if (Problem == null)
193        EvaluatorParameter.Value = null;
194
195      ParameterizeStochasticOperator(EvaluatorParameter.Value);
196      RecalculateFixedSamplesPartitions();
197      base.OnProblemChanged();
198    }
199
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;
208      var fixedSamples = (int)(FixedSamples * totalSamples);
209      var islands = NumberOfIslands.Value;
210
211      double shift = (double)((totalSamples - fixedSamples)) / (islands - 1);
212      int offset = (int)Math.Floor(shift);
213      double remainder = shift - offset;
214
215      List<IntRange> partitions = new List<IntRange>();
216      for (int i = 0; i < islands; i++) {
217        var partitionStart = samplesStart + offset * i + (int)(remainder * i);
218        partitions.Add(new IntRange(partitionStart, partitionStart + fixedSamples));
219      }
220
221      //it can be the case that the last partitions exceeds the allowed samples
222      //move the last partition forward.
223      int exceedsSamples = partitions[partitions.Count - 1].End - samplesEnd;
224      if (exceedsSamples > 0) {
225        partitions[partitions.Count - 1].Start -= exceedsSamples;
226        partitions[partitions.Count - 1].End -= exceedsSamples;
227      }
228      FixedSamplesPartitions = new ItemArray<IntRange>(partitions);
229    }
230
231  }
232}
Note: See TracBrowser for help on using the repository browser.