Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10177 was 10177, checked in by mkommend, 9 years ago

#1997: Added different evaluators for symbolic island algorithms, unit test for consecutive samples evaluator and injected island index into the according scopes.

File size: 10.7 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<IntValue> FixedSamplesParameter {
59      get { return (IFixedValueParameter<IntValue>)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 int 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<IntValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island.", new IntValue(0)));
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
161      var randomEvaluator = evaluator as RandomSamplesEvaluator;
162      if (randomEvaluator != null) {
163        ReevaluteElites = randomEvaluator.RandomSamples != 0;
164      }
165
166      var consecutiveEvaluator = evaluator as ConsecutiveSamplesEvaluator;
167      if (consecutiveEvaluator != null) {
168        consecutiveEvaluator.DataMigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
169      }
170    }
171
172    private void NumberOfIslandsParameter_ValueChanged(object sender, EventArgs e) {
173      NumberOfIslands.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
174      RecalculateFixedSamplesPartitions();
175    }
176
177    protected override void Problem_Reset(object sender, EventArgs e) {
178      FixedSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
179      base.Problem_Reset(sender, e);
180    }
181
182    protected override void OnProblemChanged() {
183      Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
184      FixedSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
185
186      if (Problem != null && EvaluatorParameter.Value == null) {
187        EvaluatorParameter.Value = new RandomSamplesEvaluator();
188      } else if (Problem == null)
189        EvaluatorParameter.Value = null;
190
191      ParameterizeStochasticOperator(EvaluatorParameter.Value);
192      RecalculateFixedSamplesPartitions();
193      base.OnProblemChanged();
194    }
195
196    private void RecalculateFixedSamplesPartitions() {
197      if (Problem == null) {
198        FixedSamplesPartitions = new ItemArray<IntRange>(Enumerable.Repeat(new IntRange(), NumberOfIslands.Value));
199        return;
200      }
201      var samplesStart = Problem.FitnessCalculationPartition.Start;
202      var samplesEnd = Problem.FitnessCalculationPartition.End;
203      var totalSamples = Problem.FitnessCalculationPartition.Size;
204      var fixedSamples = FixedSamples;
205      var islands = NumberOfIslands.Value;
206
207      int offset = (int)Math.Ceiling(((double)(totalSamples - fixedSamples)) / (islands - 1));
208      List<IntRange> partitions = new List<IntRange>();
209      for (int i = 0; i < islands; i++) {
210        var partitionStart = samplesStart + offset * i;
211        partitions.Add(new IntRange(partitionStart, partitionStart + fixedSamples));
212      }
213
214      //it can be the case that the last partitions exceeds the allowed samples
215      //move the last partition forward.
216      int exceedsSamples = partitions[partitions.Count - 1].End - samplesEnd;
217      if (exceedsSamples > 0) {
218        partitions[partitions.Count - 1].Start -= exceedsSamples;
219        partitions[partitions.Count - 1].End -= exceedsSamples;
220      }
221      FixedSamplesPartitions = new ItemArray<IntRange>(partitions);
222    }
223
224  }
225}
Note: See TracBrowser for help on using the repository browser.