Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.DataAnalysis.Symbolic/3.3/SymbolicDataAnalysisIslandGeneticAlgorithm.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.8 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.Symbolic;
34
35namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
36  [Item("Symbolic Data Analysis Island Genetic Algorithm", "A symbolic data analysis island genetic algorithm.")]
37  [Creatable("Data Analysis")]
38  [StorableClass]
39  public sealed class SymbolicDataAnalysisIslandGeneticAlgorithm : IslandGeneticAlgorithm {
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 SymbolicDataAnalysisIslandGeneticAlgorithm(bool deserializing) : base(deserializing) { }
85    [StorableHook(HookType.AfterDeserialization)]
86    private void AfterDeserialization() {
87      RegisterParameterEvents();
88    }
89    private SymbolicDataAnalysisIslandGeneticAlgorithm(SymbolicDataAnalysisIslandGeneticAlgorithm original, Cloner cloner)
90      : base(original, cloner) {
91      RegisterParameterEvents();
92    }
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new SymbolicDataAnalysisIslandGeneticAlgorithm(this, cloner);
95    }
96
97    public SymbolicDataAnalysisIslandGeneticAlgorithm()
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      //RandomCreator insertionPoint = OperatorGraph.Iterate().OfType<RandomCreator>().Skip(1).First();
121      //fixedSamplesPartitionCreator.Successor = insertionPoint.Successor;
122      //insertionPoint.Successor = fixedSamplesPartitionCreator;
123
124      RegisterParameterEvents();
125      RecalculateFixedSamplesPartitions();
126    }
127
128    private void RegisterParameterEvents() {
129      if (Problem != null) Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
130      NumberOfIslandsParameter.ValueChanged += NumberOfIslandsParameter_ValueChanged;
131      NumberOfIslandsParameter.Value.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
132      FixedSamplesParameter.Value.ValueChanged += (o, e) => {
133        RecalculateFixedSamplesPartitions();
134        ReevaluateImmigrants = FixedSamples >= Problem.FitnessCalculationPartition.Size;
135      };
136      Analyzer.Operators.PropertyChanged += (o, e) => ParameterizeAnalyzers();
137      EvaluatorParameter.ValueChanged += (o, e) => ParameterizeEvaluator();
138    }
139
140    protected override void ParameterizeSolutionsCreator() {
141      base.ParameterizeSolutionsCreator();
142      SolutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameterName;
143    }
144
145    protected override void ParameterizeMainLoop() {
146      base.ParameterizeMainLoop();
147      MainLoop.EvaluatorParameter.ActualName = EvaluatorParameterName;
148      MainLoop.QualityParameter.ActualName = EvaluatorParameter.Value.QualityParameter.ActualName;
149    }
150
151    protected override void ParameterizeAnalyzers() {
152      base.ParameterizeAnalyzers();
153      foreach (var analyzer in Analyzer.Operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
154        IParameter evaluatorParameter;
155        if (analyzer.Parameters.TryGetValue("Evaluator", out evaluatorParameter)) {
156          ILookupParameter param = evaluatorParameter as ILookupParameter;
157          if (evaluatorParameter != null) param.ActualName = ProblemEvaluatorParameterName;
158        }
159      }
160    }
161
162    private void ParameterizeEvaluator() {
163      var evaluator = EvaluatorParameter.Value;
164
165      var randomEvaluator = evaluator as RandomSamplesEvaluator;
166      if (randomEvaluator != null) {
167        ReevaluteElites = randomEvaluator.RandomSamples != 0;
168      }
169
170      var consecutiveEvaluator = evaluator as ConsecutiveSamplesEvaluator;
171      if (consecutiveEvaluator != null) {
172        consecutiveEvaluator.DataMigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
173      }
174    }
175
176    private void NumberOfIslandsParameter_ValueChanged(object sender, EventArgs e) {
177      NumberOfIslands.ValueChanged += (o, ev) => RecalculateFixedSamplesPartitions();
178      RecalculateFixedSamplesPartitions();
179    }
180
181    protected override void Problem_Reset(object sender, EventArgs e) {
182      FixedSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
183      base.Problem_Reset(sender, e);
184    }
185
186    protected override void OnProblemChanged() {
187      Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
188      FixedSamples = Problem.FitnessCalculationPartition.Size / NumberOfIslands.Value;
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 = FixedSamples;
209      var islands = NumberOfIslands.Value;
210
211      int offset = (int)Math.Ceiling(((double)(totalSamples - fixedSamples)) / (islands - 1));
212      List<IntRange> partitions = new List<IntRange>();
213      for (int i = 0; i < islands; i++) {
214        var partitionStart = samplesStart + offset * i;
215        partitions.Add(new IntRange(partitionStart, partitionStart + fixedSamples));
216      }
217
218      //it can be the case that the last partitions exceeds the allowed samples
219      //move the last partition forward.
220      int exceedsSamples = partitions[partitions.Count - 1].End - samplesEnd;
221      if (exceedsSamples > 0) {
222        partitions[partitions.Count - 1].Start -= exceedsSamples;
223        partitions[partitions.Count - 1].End -= exceedsSamples;
224      }
225      FixedSamplesPartitions = new ItemArray<IntRange>(partitions);
226    }
227
228  }
229}
Note: See TracBrowser for help on using the repository browser.