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

Last change on this file since 10142 was 10142, checked in by mkommend, 7 years ago

#1997: Worked on symbolic data analysis island algorithms:

  • Refactored and removed unnecessary code in symbolic island GA and symbolic island GA evaluator.
  • Added EnumerableItem to ease the passing of rows to the actual evaluator.
  • Added prototype of island offspring selection GA.
  • Corrected build configuration (plugin classes, project settings, ...)
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.GeneticAlgorithm;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34using HeuristicLab.Random;
35
36namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
37  [Item("Symbolic Data Analysis Island Genetic Algorithm", "A symbolic data analysis island genetic algorithm.")]
38  [Creatable("Data Analysis")]
39  [StorableClass]
40  public sealed class SymbolicDataAnalysisIslandGeneticAlgorithm : IslandGeneticAlgorithm {
41    private const string FixedSamplesParameterName = "NumberOfFixedSamples";
42    private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
43    private const string FixedSamplesPartitionsParameterName = "FixedSamplesPartitions";
44    private const string RandomSamplesParameterName = "NumberOfRandomSamples";
45    private const string EvaluatorParameterName = "IslandEvaluator";
46    private const string ProblemEvaluatorParameterName = "ProblemEvaluator";
47
48    #region Problem Properties
49    public override Type ProblemType {
50      get { return typeof(ISymbolicDataAnalysisSingleObjectiveProblem); }
51    }
52    public new ISymbolicDataAnalysisSingleObjectiveProblem Problem {
53      get { return (ISymbolicDataAnalysisSingleObjectiveProblem)base.Problem; }
54      set { base.Problem = value; }
55    }
56    #endregion
57
58    #region parameters
59    public IFixedValueParameter<IntValue> FixedSamplesParameter {
60      get { return (IFixedValueParameter<IntValue>)Parameters[FixedSamplesParameterName]; }
61    }
62    public IValueParameter<ItemArray<IntRange>> FixedSamplesPartitionsParameter {
63      get { return (IValueParameter<ItemArray<IntRange>>)Parameters[FixedSamplesPartitionsParameterName]; }
64    }
65    public IFixedValueParameter<IntValue> RandomSamplesParameter {
66      get { return (IFixedValueParameter<IntValue>)Parameters[RandomSamplesParameterName]; }
67    }
68    public IValueParameter<ISymbolicDataAnalysisIslandGAEvaluator> EvaluatorParameter {
69      get { return (IValueParameter<ISymbolicDataAnalysisIslandGAEvaluator>)Parameters[EvaluatorParameterName]; }
70    }
71    private ILookupParameter<ISingleObjectiveEvaluator> ProblemEvaluatorParameter {
72      get { return (ILookupParameter<ISingleObjectiveEvaluator>)Parameters[ProblemEvaluatorParameterName]; }
73    }
74    #endregion
75
76    #region properties
77    public int FixedSamples {
78      get { return FixedSamplesParameter.Value.Value; }
79      set { FixedSamplesParameter.Value.Value = value; }
80    }
81    public ItemArray<IntRange> FixedSamplesPartitions {
82      get { return FixedSamplesPartitionsParameter.Value; }
83      set { FixedSamplesPartitionsParameter.Value = value; }
84    }
85    public int RandomSamples {
86      get { return RandomSamplesParameter.Value.Value; }
87      set { RandomSamplesParameter.Value.Value = value; }
88    }
89    #endregion
90
91    [StorableConstructor]
92    private SymbolicDataAnalysisIslandGeneticAlgorithm(bool deserializing) : base(deserializing) { }
93    [StorableHook(HookType.AfterDeserialization)]
94    private void AfterDeserialization() {
95      RegisterParameterEvents();
96    }
97    private SymbolicDataAnalysisIslandGeneticAlgorithm(SymbolicDataAnalysisIslandGeneticAlgorithm original, Cloner cloner)
98      : base(original, cloner) {
99      RegisterParameterEvents();
100    }
101    public override IDeepCloneable Clone(Cloner cloner) {
102      return new SymbolicDataAnalysisIslandGeneticAlgorithm(this, cloner);
103    }
104
105    public SymbolicDataAnalysisIslandGeneticAlgorithm()
106      : base() {
107      Parameters.Add(new FixedValueParameter<IntValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island.", new IntValue(0)));
108      Parameters.Add(new ValueParameter<ItemArray<IntRange>>(FixedSamplesPartitionsParameterName, "The fixed samples partitions used for fitness calculation for every island."));
109      Parameters.Add(new FixedValueParameter<IntValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island.", new IntValue(0)));
110      Parameters.Add(new OptionalValueParameter<ISymbolicDataAnalysisIslandGAEvaluator>(EvaluatorParameterName, "The evaluator of the algorithm."));
111      Parameters.Add(new LookupParameter<ISingleObjectiveEvaluator>(ProblemEvaluatorParameterName, "Internal parameter for name translation", "Evaluator"));
112
113      ScopeTreeAssigner<IntRange> fixedSamplesPartitionCreator = new ScopeTreeAssigner<IntRange>();
114      fixedSamplesPartitionCreator.Name = "Create fixed evaluation partition";
115      fixedSamplesPartitionCreator.LeftSideParameter.ActualName = FixedSamplesPartitionParameterName;
116      fixedSamplesPartitionCreator.RightSideParameter.ActualName = FixedSamplesPartitionsParameterName;
117
118      RandomCreator insertionPoint = OperatorGraph.Iterate().OfType<RandomCreator>().Skip(1).First();
119      fixedSamplesPartitionCreator.Successor = insertionPoint.Successor;
120      insertionPoint.Successor = fixedSamplesPartitionCreator;
121
122      RegisterParameterEvents();
123      RecalculateFixedSamplesPartitions();
124    }
125
126    private void RegisterParameterEvents() {
127      if (Problem != null) Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
128      NumberOfIslandsParameter.ValueChanged += NumberOfIslandsParameter_ValueChanged;
129      NumberOfIslandsParameter.Value.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
130      FixedSamplesParameter.Value.ValueChanged += (o, e) => RecalculateFixedSamplesPartitions();
131      RandomSamplesParameter.Value.ValueChanged += (o, e) => { ReevaluteElites = RandomSamples != 0; };
132      Analyzer.Operators.PropertyChanged += (o, e) => ParameterizeAnalyzers();
133    }
134
135    protected override void ParameterizeSolutionsCreator() {
136      base.ParameterizeSolutionsCreator();
137      SolutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameterName;
138    }
139
140    protected override void ParameterizeMainLoop() {
141      base.ParameterizeMainLoop();
142      MainLoop.EvaluatorParameter.ActualName = EvaluatorParameterName;
143      MainLoop.QualityParameter.ActualName = EvaluatorParameter.Value.QualityParameter.ActualName;
144    }
145
146    protected override void ParameterizeAnalyzers() {
147      base.ParameterizeAnalyzers();
148      foreach (var analyzer in Analyzer.Operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
149        IParameter evaluatorParameter;
150        if (analyzer.Parameters.TryGetValue("Evaluator", out evaluatorParameter)) {
151          ILookupParameter param = evaluatorParameter as ILookupParameter;
152          if (evaluatorParameter != null) param.ActualName = ProblemEvaluatorParameterName;
153        }
154      }
155    }
156
157    private void NumberOfIslandsParameter_ValueChanged(object sender, EventArgs e) {
158      NumberOfIslands.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
159      RecalculateFixedSamplesPartitions();
160    }
161
162    protected override void Problem_Reset(object sender, EventArgs e) {
163      FixedSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
164      RandomSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
165      RecalculateFixedSamplesPartitions();
166      base.Problem_Reset(sender, e);
167    }
168
169    protected override void OnProblemChanged() {
170      Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
171      FixedSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
172      RandomSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
173
174      if (Problem is IRegressionProblem) {
175        var evaluator = new SymbolicDataAnalysisIslandGAEvaluator<IRegressionProblemData>();
176        evaluator.RandomSamplesParameter.ActualName = RandomSamplesParameterName;
177        EvaluatorParameter.Value = evaluator;
178      } else if (Problem is IClassificationProblem) {
179        var evaluator = new SymbolicDataAnalysisIslandGAEvaluator<IClassificationProblemData>();
180        evaluator.RandomSamplesParameter.ActualName = RandomSamplesParameterName;
181        EvaluatorParameter.Value = evaluator;
182      } else
183        EvaluatorParameter.Value = null;
184
185      ParameterizeStochasticOperatorForIsland(EvaluatorParameter.Value);
186
187      RecalculateFixedSamplesPartitions();
188      base.OnProblemChanged();
189    }
190
191    private void RecalculateFixedSamplesPartitions() {
192      if (Problem == null) {
193        FixedSamplesPartitions = new ItemArray<IntRange>(Enumerable.Repeat(new IntRange(), NumberOfIslands.Value));
194        return;
195      }
196      var samplesStart = Problem.FitnessCalculationPartition.Start;
197      var samplesEnd = Problem.FitnessCalculationPartition.End;
198      var totalSamples = Problem.FitnessCalculationPartition.Size;
199      var fixedSamples = FixedSamples;
200      var islands = NumberOfIslands.Value;
201
202      int offset = (int)Math.Ceiling(((double)(totalSamples - fixedSamples)) / (islands - 1));
203      List<IntRange> partitions = new List<IntRange>();
204      for (int i = 0; i < islands; i++) {
205        var partitionStart = samplesStart + offset * i;
206        partitions.Add(new IntRange(partitionStart, partitionStart + fixedSamples));
207      }
208
209      //it can be the case that the last partitions exceeds the allowed samples
210      //move the last partition forward.
211      int exceedsSamples = partitions[partitions.Count - 1].End - samplesEnd;
212      if (exceedsSamples > 0) {
213        partitions[partitions.Count - 1].Start -= exceedsSamples;
214        partitions[partitions.Count - 1].End -= exceedsSamples;
215      }
216      FixedSamplesPartitions = new ItemArray<IntRange>(partitions);
217    }
218
219  }
220}
Note: See TracBrowser for help on using the repository browser.