Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10586 was 10357, checked in by mkommend, 11 years ago

#1997: Corrected insertion of island indexes in symbolic island algorithms.

File size: 11.1 KB
RevLine 
[9051]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;
[9067]23using System.Collections.Generic;
[9051]24using System.Linq;
25using HeuristicLab.Algorithms.GeneticAlgorithm;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
[10177]29using HeuristicLab.Operators;
[9077]30using HeuristicLab.Optimization;
[9051]31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34
35namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
[10142]36  [Item("Symbolic Data Analysis Island Genetic Algorithm", "A symbolic data analysis island genetic algorithm.")]
[9182]37  [Creatable("Data Analysis")]
[9051]38  [StorableClass]
[9067]39  public sealed class SymbolicDataAnalysisIslandGeneticAlgorithm : IslandGeneticAlgorithm {
40    private const string FixedSamplesParameterName = "NumberOfFixedSamples";
[9077]41    private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
[9067]42    private const string FixedSamplesPartitionsParameterName = "FixedSamplesPartitions";
[9077]43    private const string EvaluatorParameterName = "IslandEvaluator";
[10177]44    private const string IslandIndexParameterName = "IslandIndex";
[9077]45    private const string ProblemEvaluatorParameterName = "ProblemEvaluator";
[9051]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
[9067]57    #region parameters
[10353]58    public IFixedValueParameter<PercentValue> FixedSamplesParameter {
59      get { return (IFixedValueParameter<PercentValue>)Parameters[FixedSamplesParameterName]; }
[9051]60    }
[9067]61    public IValueParameter<ItemArray<IntRange>> FixedSamplesPartitionsParameter {
62      get { return (IValueParameter<ItemArray<IntRange>>)Parameters[FixedSamplesPartitionsParameterName]; }
[9051]63    }
[10177]64    public IValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator> EvaluatorParameter {
65      get { return (IValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator>)Parameters[EvaluatorParameterName]; }
[9051]66    }
[9077]67    private ILookupParameter<ISingleObjectiveEvaluator> ProblemEvaluatorParameter {
68      get { return (ILookupParameter<ISingleObjectiveEvaluator>)Parameters[ProblemEvaluatorParameterName]; }
69    }
[9051]70    #endregion
71
[9067]72    #region properties
[10353]73    public double FixedSamples {
[9067]74      get { return FixedSamplesParameter.Value.Value; }
75      set { FixedSamplesParameter.Value.Value = value; }
[9051]76    }
[9067]77    public ItemArray<IntRange> FixedSamplesPartitions {
78      get { return FixedSamplesPartitionsParameter.Value; }
79      set { FixedSamplesPartitionsParameter.Value = value; }
[9051]80    }
[10357]81
82    private readonly ScopeTreeAssigner<IntValue> islandIndexAssigner;
[9051]83    #endregion
84
85    [StorableConstructor]
86    private SymbolicDataAnalysisIslandGeneticAlgorithm(bool deserializing) : base(deserializing) { }
87    [StorableHook(HookType.AfterDeserialization)]
88    private void AfterDeserialization() {
[9067]89      RegisterParameterEvents();
[9051]90    }
91    private SymbolicDataAnalysisIslandGeneticAlgorithm(SymbolicDataAnalysisIslandGeneticAlgorithm original, Cloner cloner)
92      : base(original, cloner) {
[9067]93      RegisterParameterEvents();
[9051]94    }
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new SymbolicDataAnalysisIslandGeneticAlgorithm(this, cloner);
97    }
98
99    public SymbolicDataAnalysisIslandGeneticAlgorithm()
100      : base() {
[10353]101      Parameters.Add(new FixedValueParameter<PercentValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island.", new PercentValue(0.2)));
[9067]102      Parameters.Add(new ValueParameter<ItemArray<IntRange>>(FixedSamplesPartitionsParameterName, "The fixed samples partitions used for fitness calculation for every island."));
[10177]103      Parameters.Add(new OptionalValueParameter<ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator>(EvaluatorParameterName, "The evaluator of the algorithm."));
[9077]104      Parameters.Add(new LookupParameter<ISingleObjectiveEvaluator>(ProblemEvaluatorParameterName, "Internal parameter for name translation", "Evaluator"));
[9051]105
[10357]106      islandIndexAssigner = new ScopeTreeAssigner<IntValue>();
[10177]107      islandIndexAssigner.Name = "Insert island index";
108      islandIndexAssigner.LeftSideParameter.ActualName = IslandIndexParameterName;
109      var readonlyIslandIndexes = Enumerable.Range(0, NumberOfIslands.Value).Select(x => (IntValue)new IntValue(x).AsReadOnly());
110      islandIndexAssigner.RightSideParameter.Value = new ItemArray<IntValue>(readonlyIslandIndexes);
111
[9077]112      ScopeTreeAssigner<IntRange> fixedSamplesPartitionCreator = new ScopeTreeAssigner<IntRange>();
[10142]113      fixedSamplesPartitionCreator.Name = "Create fixed evaluation partition";
[9077]114      fixedSamplesPartitionCreator.LeftSideParameter.ActualName = FixedSamplesPartitionParameterName;
115      fixedSamplesPartitionCreator.RightSideParameter.ActualName = FixedSamplesPartitionsParameterName;
116
[10177]117      SubScopesCreator insertionPoint = OperatorGraph.Iterate().OfType<SubScopesCreator>().First();
118      islandIndexAssigner.Successor = fixedSamplesPartitionCreator;
[9077]119      fixedSamplesPartitionCreator.Successor = insertionPoint.Successor;
[10177]120      insertionPoint.Successor = islandIndexAssigner;
[9077]121
[10177]122      //RandomCreator insertionPoint = OperatorGraph.Iterate().OfType<RandomCreator>().Skip(1).First();
123      //fixedSamplesPartitionCreator.Successor = insertionPoint.Successor;
124      //insertionPoint.Successor = fixedSamplesPartitionCreator;
125
[9067]126      RegisterParameterEvents();
127      RecalculateFixedSamplesPartitions();
128    }
[9051]129
[9172]130    private void RegisterParameterEvents() {
131      if (Problem != null) Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
132      NumberOfIslandsParameter.ValueChanged += NumberOfIslandsParameter_ValueChanged;
[10357]133      NumberOfIslandsParameter.Value.ValueChanged += (o, ev) => NumberOfIslandsParameterValue_Changed();
[10156]134      FixedSamplesParameter.Value.ValueChanged += (o, e) => {
135        RecalculateFixedSamplesPartitions();
[10230]136        ReevaluateImmigrants = FixedSamples < Problem.FitnessCalculationPartition.Size;
[10156]137      };
[9172]138      Analyzer.Operators.PropertyChanged += (o, e) => ParameterizeAnalyzers();
[10156]139      EvaluatorParameter.ValueChanged += (o, e) => ParameterizeEvaluator();
[9172]140    }
141
[9077]142    protected override void ParameterizeSolutionsCreator() {
143      base.ParameterizeSolutionsCreator();
144      SolutionsCreator.EvaluatorParameter.ActualName = EvaluatorParameterName;
145    }
146
147    protected override void ParameterizeMainLoop() {
148      base.ParameterizeMainLoop();
149      MainLoop.EvaluatorParameter.ActualName = EvaluatorParameterName;
150      MainLoop.QualityParameter.ActualName = EvaluatorParameter.Value.QualityParameter.ActualName;
151    }
152
[10142]153    protected override void ParameterizeAnalyzers() {
154      base.ParameterizeAnalyzers();
[9172]155      foreach (var analyzer in Analyzer.Operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
156        IParameter evaluatorParameter;
157        if (analyzer.Parameters.TryGetValue("Evaluator", out evaluatorParameter)) {
158          ILookupParameter param = evaluatorParameter as ILookupParameter;
159          if (evaluatorParameter != null) param.ActualName = ProblemEvaluatorParameterName;
160        }
161      }
[9067]162    }
[9051]163
[10156]164    private void ParameterizeEvaluator() {
165      var evaluator = EvaluatorParameter.Value;
[10230]166      evaluator.IterationsParameter.ActualName = "Generations";
167      evaluator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
168      evaluator.DataMigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
[10177]169
[10230]170      ParameterizeStochasticOperatorForIsland(evaluator);
[10156]171    }
172
[9067]173    private void NumberOfIslandsParameter_ValueChanged(object sender, EventArgs e) {
[10357]174      NumberOfIslands.ValueChanged += (o, ev) => NumberOfIslandsParameterValue_Changed();
175      NumberOfIslandsParameterValue_Changed();
176    }
177    private void NumberOfIslandsParameterValue_Changed() {
178      var readonlyIslandIndexes = Enumerable.Range(0, NumberOfIslands.Value).Select(x => (IntValue)new IntValue(x).AsReadOnly());
179      islandIndexAssigner.RightSideParameter.Value = new ItemArray<IntValue>(readonlyIslandIndexes);
[9067]180      RecalculateFixedSamplesPartitions();
[9051]181    }
182
[10356]183    protected override void Problem_Reset(object sender, EventArgs e) {
184      base.Problem_Reset(sender, e);
185      RecalculateFixedSamplesPartitions();
186    }
187
[9051]188    protected override void OnProblemChanged() {
[9172]189      Problem.FitnessCalculationPartition.ValueChanged += Problem_Reset;
[9077]190
[10156]191      if (Problem != null && EvaluatorParameter.Value == null) {
[10177]192        EvaluatorParameter.Value = new RandomSamplesEvaluator();
[10156]193      } else if (Problem == null)
[9077]194        EvaluatorParameter.Value = null;
195
[10156]196      ParameterizeStochasticOperator(EvaluatorParameter.Value);
[9067]197      RecalculateFixedSamplesPartitions();
[9077]198      base.OnProblemChanged();
[9051]199    }
200
[9067]201    private void RecalculateFixedSamplesPartitions() {
202      if (Problem == null) {
203        FixedSamplesPartitions = new ItemArray<IntRange>(Enumerable.Repeat(new IntRange(), NumberOfIslands.Value));
204        return;
205      }
206      var samplesStart = Problem.FitnessCalculationPartition.Start;
207      var samplesEnd = Problem.FitnessCalculationPartition.End;
208      var totalSamples = Problem.FitnessCalculationPartition.Size;
[10353]209      var fixedSamples = (int)(FixedSamples * totalSamples);
[9067]210      var islands = NumberOfIslands.Value;
[9051]211
[10353]212      double shift = (double)((totalSamples - fixedSamples)) / (islands - 1);
213      int offset = (int)Math.Floor(shift);
214      double remainder = shift - offset;
215
[9067]216      List<IntRange> partitions = new List<IntRange>();
217      for (int i = 0; i < islands; i++) {
[10353]218        var partitionStart = samplesStart + offset * i + (int)(remainder * i);
[9067]219        partitions.Add(new IntRange(partitionStart, partitionStart + fixedSamples));
[9051]220      }
[9067]221
[10353]222      //if the last partitions exceeds the allowed samples move the last partition forward.
[9067]223      int exceedsSamples = partitions[partitions.Count - 1].End - samplesEnd;
224      if (exceedsSamples > 0) {
225        partitions[partitions.Count - 1].Start -= exceedsSamples;
226        partitions[partitions.Count - 1].End -= exceedsSamples;
[9051]227      }
[9067]228      FixedSamplesPartitions = new ItemArray<IntRange>(partitions);
[9051]229    }
230
231  }
232}
Note: See TracBrowser for help on using the repository browser.