Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1997: Removed generic type parameter from SymbolicDataAnylsisIslandGAEvaluator and added wiring code for the reevaluation of immigrants.

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