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

Last change on this file since 9182 was 9182, checked in by mkommend, 8 years ago

#1997: Added reevaluation of elits to symbolic data analysis island ga and changed evaluator to combine the fixed and random samples.

File size: 12.0 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.GeneticAlgorithm;
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;
34using HeuristicLab.Problems.DataAnalysis.Symbolic;
35using HeuristicLab.Random;
36using HeuristicLab.Selection;
37
38namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
39  [Item("Symbolic DataAnalysis Island Genetic Algorithm", "A symbolic data analysis island genetic algorithm.")]
40  [Creatable("Data Analysis")]
41  [StorableClass]
42  public sealed class SymbolicDataAnalysisIslandGeneticAlgorithm : IslandGeneticAlgorithm {
43    private const string FixedSamplesParameterName = "NumberOfFixedSamples";
44    private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
45    private const string FixedSamplesPartitionsParameterName = "FixedSamplesPartitions";
46    private const string RandomSamplesParameterName = "NumberOfRandomSamples";
47    private const string EvaluatorParameterName = "IslandEvaluator";
48    private const string ProblemEvaluatorParameterName = "ProblemEvaluator";
49
50    #region Problem Properties
51    public override Type ProblemType {
52      get { return typeof(ISymbolicDataAnalysisSingleObjectiveProblem); }
53    }
54    public new ISymbolicDataAnalysisSingleObjectiveProblem Problem {
55      get { return (ISymbolicDataAnalysisSingleObjectiveProblem)base.Problem; }
56      set { base.Problem = value; }
57    }
58    #endregion
59
60    #region parameters
61    public IFixedValueParameter<IntValue> FixedSamplesParameter {
62      get { return (IFixedValueParameter<IntValue>)Parameters[FixedSamplesParameterName]; }
63    }
64    public IValueParameter<ItemArray<IntRange>> FixedSamplesPartitionsParameter {
65      get { return (IValueParameter<ItemArray<IntRange>>)Parameters[FixedSamplesPartitionsParameterName]; }
66    }
67    public IFixedValueParameter<IntValue> RandomSamplesParameter {
68      get { return (IFixedValueParameter<IntValue>)Parameters[RandomSamplesParameterName]; }
69    }
70    public IValueParameter<ISymbolicDataAnalysisIslandGAEvaluator> EvaluatorParameter {
71      get { return (IValueParameter<ISymbolicDataAnalysisIslandGAEvaluator>)Parameters[EvaluatorParameterName]; }
72    }
73    private ILookupParameter<ISingleObjectiveEvaluator> ProblemEvaluatorParameter {
74      get { return (ILookupParameter<ISingleObjectiveEvaluator>)Parameters[ProblemEvaluatorParameterName]; }
75    }
76    #endregion
77
78    #region properties
79    public int FixedSamples {
80      get { return FixedSamplesParameter.Value.Value; }
81      set { FixedSamplesParameter.Value.Value = value; }
82    }
83    public ItemArray<IntRange> FixedSamplesPartitions {
84      get { return FixedSamplesPartitionsParameter.Value; }
85      set { FixedSamplesPartitionsParameter.Value = value; }
86    }
87    public int RandomSamples {
88      get { return RandomSamplesParameter.Value.Value; }
89      set { RandomSamplesParameter.Value.Value = value; }
90    }
91    #endregion
92
93    [StorableConstructor]
94    private SymbolicDataAnalysisIslandGeneticAlgorithm(bool deserializing) : base(deserializing) { }
95    [StorableHook(HookType.AfterDeserialization)]
96    private void AfterDeserialization() {
97      RegisterParameterEvents();
98    }
99    private SymbolicDataAnalysisIslandGeneticAlgorithm(SymbolicDataAnalysisIslandGeneticAlgorithm original, Cloner cloner)
100      : base(original, cloner) {
101      RegisterParameterEvents();
102    }
103    public override IDeepCloneable Clone(Cloner cloner) {
104      return new SymbolicDataAnalysisIslandGeneticAlgorithm(this, cloner);
105    }
106
107    public SymbolicDataAnalysisIslandGeneticAlgorithm()
108      : base() {
109      Parameters.Add(new FixedValueParameter<IntValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island.", new IntValue(0)));
110      Parameters.Add(new ValueParameter<ItemArray<IntRange>>(FixedSamplesPartitionsParameterName, "The fixed samples partitions used for fitness calculation for every island."));
111      Parameters.Add(new FixedValueParameter<IntValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island.", new IntValue(0)));
112      Parameters.Add(new OptionalValueParameter<ISymbolicDataAnalysisIslandGAEvaluator>(EvaluatorParameterName, "The evaluator of the algorithm."));
113      Parameters.Add(new LookupParameter<ISingleObjectiveEvaluator>(ProblemEvaluatorParameterName, "Internal parameter for name translation", "Evaluator"));
114
115      Elites.Value = 0;
116      ElitesParameter.Hidden = true;
117
118      ScopeTreeAssigner<IntRange> fixedSamplesPartitionCreator = new ScopeTreeAssigner<IntRange>();
119      fixedSamplesPartitionCreator.LeftSideParameter.ActualName = FixedSamplesPartitionParameterName;
120      fixedSamplesPartitionCreator.RightSideParameter.ActualName = FixedSamplesPartitionsParameterName;
121
122      RandomCreator insertionPoint = OperatorGraph.Iterate().OfType<RandomCreator>().Skip(1).First();
123      fixedSamplesPartitionCreator.Successor = insertionPoint.Successor;
124      insertionPoint.Successor = fixedSamplesPartitionCreator;
125
126      //necessary to reevaluate elites
127      var evaluatorPlaceHolder = new Placeholder();
128      evaluatorPlaceHolder.OperatorParameter.ActualName = "Evaluator";
129
130      UniformSubScopesProcessor subScopesProcessor = new UniformSubScopesProcessor();
131      subScopesProcessor.Name = "Reevaluate elites";
132      subScopesProcessor.Parallel.Value = true;
133      subScopesProcessor.Operator = evaluatorPlaceHolder;
134      OperatorGraph.Iterate().OfType<RightReducer>().First().Successor = subScopesProcessor;
135
136      RegisterParameterEvents();
137      RecalculateFixedSamplesPartitions();
138    }
139
140    private void RegisterParameterEvents() {
141      if (Problem != null) Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
142      NumberOfIslandsParameter.ValueChanged += NumberOfIslandsParameter_ValueChanged;
143      NumberOfIslandsParameter.Value.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
144      FixedSamplesParameter.Value.ValueChanged += (o, e) => RecalculateFixedSamplesPartitions();
145      Analyzer.Operators.PropertyChanged += (o, e) => ParameterizeAnalyzers();
146    }
147
148    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
149      ParameterizeProblemEvaluator();
150      base.Problem_EvaluatorChanged(sender, e);
151    }
152
153    private void ParameterizeProblemEvaluator() {
154      var regresssionEvaluator = Problem.Evaluator as ISymbolicDataAnalysisEvaluator<IRegressionProblemData>;
155      if (regresssionEvaluator != null) {
156        regresssionEvaluator.EvaluationPartitionParameter.ActualName = FixedSamplesPartitionParameterName;
157      }
158      var classificationEvaluator = Problem.Evaluator as ISymbolicDataAnalysisEvaluator<IClassificationProblemData>;
159      if (classificationEvaluator != null) {
160        classificationEvaluator.EvaluationPartitionParameter.ActualName = FixedSamplesPartitionParameterName;
161      }
162    }
163
164    protected override void ParameterizeSolutionsCreator() {
165      base.ParameterizeSolutionsCreator();
166      SolutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameterName;
167    }
168
169    protected override void ParameterizeMainLoop() {
170      base.ParameterizeMainLoop();
171      MainLoop.EvaluatorParameter.ActualName = EvaluatorParameterName;
172      MainLoop.QualityParameter.ActualName = EvaluatorParameter.Value.QualityParameter.ActualName;
173    }
174
175
176    private void ParameterizeAnalyzers() {
177      foreach (var analyzer in Analyzer.Operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
178        IParameter evaluatorParameter;
179        if (analyzer.Parameters.TryGetValue("Evaluator", out evaluatorParameter)) {
180          ILookupParameter param = evaluatorParameter as ILookupParameter;
181          if (evaluatorParameter != null) param.ActualName = ProblemEvaluatorParameterName;
182        }
183      }
184    }
185
186    private void NumberOfIslandsParameter_ValueChanged(object sender, EventArgs e) {
187      NumberOfIslands.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
188      RecalculateFixedSamplesPartitions();
189    }
190
191    protected override void Problem_Reset(object sender, EventArgs e) {
192      FixedSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
193      RandomSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
194      RecalculateFixedSamplesPartitions();
195      ParameterizeProblemEvaluator();
196      base.Problem_Reset(sender, e);
197    }
198
199    protected override void OnProblemChanged() {
200      Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
201      FixedSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
202      RandomSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
203
204      if (Problem is IRegressionProblem) {
205        var evaluator = new SymbolicDataAnalysisIslandGAEvaluator<IRegressionProblemData>();
206        evaluator.RandomSamplesParameter.ActualName = RandomSamplesParameterName;
207        EvaluatorParameter.Value = evaluator;
208      } else if (Problem is IClassificationProblem) {
209        var evaluator = new SymbolicDataAnalysisIslandGAEvaluator<IClassificationProblemData>();
210        evaluator.RandomSamplesParameter.ActualName = RandomSamplesParameterName;
211        EvaluatorParameter.Value = evaluator;
212      } else
213        EvaluatorParameter.Value = null;
214
215      ParameterizeProblemEvaluator();
216      ParameterizeStochasticOperatorForIsland(EvaluatorParameter.Value);
217
218      RecalculateFixedSamplesPartitions();
219      base.OnProblemChanged();
220    }
221
222    private void RecalculateFixedSamplesPartitions() {
223      if (Problem == null) {
224        FixedSamplesPartitions = new ItemArray<IntRange>(Enumerable.Repeat(new IntRange(), NumberOfIslands.Value));
225        return;
226      }
227      var samplesStart = Problem.FitnessCalculationPartition.Start;
228      var samplesEnd = Problem.FitnessCalculationPartition.End;
229      var totalSamples = Problem.FitnessCalculationPartition.Size;
230      var fixedSamples = FixedSamples;
231      var islands = NumberOfIslands.Value;
232
233      int offset = 0;
234      //fixed samples partition do not overlap
235      if (((double)totalSamples) / fixedSamples <= islands) {
236        offset = totalSamples / islands;
237      } else {
238        offset = (totalSamples - fixedSamples) / (islands - 1);
239      }
240
241      List<IntRange> partitions = new List<IntRange>();
242      for (int i = 0; i < islands; i++) {
243        var partitionStart = samplesStart + offset * i;
244        partitions.Add(new IntRange(partitionStart, partitionStart + fixedSamples));
245      }
246
247      //it can be the case that the last partitions exceeds the allowed samples
248      //move the last partition forward.
249      int exceedsSamples = partitions[partitions.Count - 1].End - samplesEnd;
250      if (exceedsSamples > 0) {
251        partitions[partitions.Count - 1].Start -= exceedsSamples;
252        partitions[partitions.Count - 1].End -= exceedsSamples;
253      }
254      FixedSamplesPartitions = new ItemArray<IntRange>(partitions);
255    }
256
257  }
258}
Note: See TracBrowser for help on using the repository browser.