Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10356 was 10356, checked in by mkommend, 10 years ago

#1997: Added code to recalculate island partitions when the problem changes.

File size: 10.6 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    #endregion
82
83    [StorableConstructor]
84    private SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm(bool deserializing) : base(deserializing) { }
85    [StorableHook(HookType.AfterDeserialization)]
86    private void AfterDeserialization() {
87      RegisterParameterEvents();
88    }
89    private SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm(SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm original, Cloner cloner)
90      : base(original, cloner) {
91      RegisterParameterEvents();
92    }
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm(this, cloner);
95    }
96
97    public SymbolicDataAnalysisIslandOffspringSelectionGeneticAlgorithm()
98      : base() {
99      Parameters.Add(new FixedValueParameter<PercentValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island.", new PercentValue(0.2)));
100      Parameters.Add(new ValueParameter<ItemArray<IntRange>>(FixedSamplesPartitionsParameterName, "The fixed samples partitions used for fitness calculation for every island."));
101      Parameters.Add(new OptionalValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator>(EvaluatorParameterName, "The evaluator of the algorithm."));
102      Parameters.Add(new LookupParameter<ISingleObjectiveEvaluator>(ProblemEvaluatorParameterName, "Internal parameter for name translation", "Evaluator"));
103
104      ScopeTreeAssigner<IntValue> islandIndexAssigner = new ScopeTreeAssigner<IntValue>();
105      islandIndexAssigner.Name = "Insert island index";
106      islandIndexAssigner.LeftSideParameter.ActualName = IslandIndexParameterName;
107      var readonlyIslandIndexes = Enumerable.Range(0, NumberOfIslands.Value).Select(x => (IntValue)new IntValue(x).AsReadOnly());
108      islandIndexAssigner.RightSideParameter.Value = new ItemArray<IntValue>(readonlyIslandIndexes);
109
110      ScopeTreeAssigner<IntRange> fixedSamplesPartitionCreator = new ScopeTreeAssigner<IntRange>();
111      fixedSamplesPartitionCreator.Name = "Create fixed evaluation partition";
112      fixedSamplesPartitionCreator.LeftSideParameter.ActualName = FixedSamplesPartitionParameterName;
113      fixedSamplesPartitionCreator.RightSideParameter.ActualName = FixedSamplesPartitionsParameterName;
114
115      SubScopesCreator insertionPoint = OperatorGraph.Iterate().OfType<SubScopesCreator>().First();
116      islandIndexAssigner.Successor = fixedSamplesPartitionCreator;
117      fixedSamplesPartitionCreator.Successor = insertionPoint.Successor;
118      insertionPoint.Successor = islandIndexAssigner;
119
120      RegisterParameterEvents();
121      RecalculateFixedSamplesPartitions();
122    }
123
124    private void RegisterParameterEvents() {
125      if (Problem != null) Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
126      NumberOfIslandsParameter.ValueChanged += NumberOfIslandsParameter_ValueChanged;
127      NumberOfIslandsParameter.Value.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
128      FixedSamplesParameter.Value.ValueChanged += (o, e) => {
129        RecalculateFixedSamplesPartitions();
130        ReevaluateImmigrants = FixedSamples < Problem.FitnessCalculationPartition.Size;
131      };
132      Analyzer.Operators.PropertyChanged += (o, e) => ParameterizeAnalyzers();
133      EvaluatorParameter.ValueChanged += (o, e) => ParameterizeEvaluator();
134    }
135
136    protected override void ParameterizeSolutionsCreator() {
137      base.ParameterizeSolutionsCreator();
138      SolutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameterName;
139    }
140
141    protected override void ParameterizeMainLoop() {
142      base.ParameterizeMainLoop();
143      MainLoop.EvaluatorParameter.ActualName = EvaluatorParameterName;
144      MainLoop.QualityParameter.ActualName = EvaluatorParameter.Value.QualityParameter.ActualName;
145    }
146
147    protected override void ParameterizeAnalyzers() {
148      base.ParameterizeAnalyzers();
149      foreach (var analyzer in Analyzer.Operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
150        IParameter evaluatorParameter;
151        if (analyzer.Parameters.TryGetValue("Evaluator", out evaluatorParameter)) {
152          ILookupParameter param = evaluatorParameter as ILookupParameter;
153          if (evaluatorParameter != null) param.ActualName = ProblemEvaluatorParameterName;
154        }
155      }
156    }
157
158    private void ParameterizeEvaluator() {
159      var evaluator = EvaluatorParameter.Value;
160      evaluator.IterationsParameter.ActualName = "Generations";
161      evaluator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
162      evaluator.DataMigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
163
164      ParameterizeStochasticOperator(evaluator);
165    }
166
167    private void NumberOfIslandsParameter_ValueChanged(object sender, EventArgs e) {
168      NumberOfIslands.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
169      RecalculateFixedSamplesPartitions();
170    }
171
172    protected override void Problem_Reset(object sender, EventArgs e) {
173      base.Problem_Reset(sender, e);
174      RecalculateFixedSamplesPartitions();
175    }
176
177    protected override void OnProblemChanged() {
178      Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
179
180      if (Problem != null && EvaluatorParameter.Value == null) {
181        EvaluatorParameter.Value = new RandomSamplesEvaluator();
182      } else if (Problem == null)
183        EvaluatorParameter.Value = null;
184
185      ParameterizeStochasticOperator(EvaluatorParameter.Value);
186      RecalculateFixedSamplesPartitions();
187      base.OnProblemChanged();
188    }
189
190    private void RecalculateFixedSamplesPartitions() {
191      if (Problem == null) {
192        FixedSamplesPartitions = new ItemArray<IntRange>(Enumerable.Repeat(new IntRange(), NumberOfIslands.Value));
193        return;
194      }
195      var samplesStart = Problem.FitnessCalculationPartition.Start;
196      var samplesEnd = Problem.FitnessCalculationPartition.End;
197      var totalSamples = Problem.FitnessCalculationPartition.Size;
198      var fixedSamples = (int)(FixedSamples * totalSamples);
199      var islands = NumberOfIslands.Value;
200
201      double shift = (double)((totalSamples - fixedSamples)) / (islands - 1);
202      int offset = (int)Math.Floor(shift);
203      double remainder = shift - offset;
204
205      List<IntRange> partitions = new List<IntRange>();
206      for (int i = 0; i < islands; i++) {
207        var partitionStart = samplesStart + offset * i + (int)(remainder * i);
208        partitions.Add(new IntRange(partitionStart, partitionStart + fixedSamples));
209      }
210
211      //it can be the case that the last partitions exceeds the allowed samples
212      //move the last partition forward.
213      int exceedsSamples = partitions[partitions.Count - 1].End - samplesEnd;
214      if (exceedsSamples > 0) {
215        partitions[partitions.Count - 1].Start -= exceedsSamples;
216        partitions[partitions.Count - 1].End -= exceedsSamples;
217      }
218      FixedSamplesPartitions = new ItemArray<IntRange>(partitions);
219    }
220
221  }
222}
Note: See TracBrowser for help on using the repository browser.