source: branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.DataAnalysis.Symbolic/3.3/ConsecutiveSamplesEvaluator.cs @ 10178

Last change on this file since 10178 was 10178, checked in by mkommend, 6 years ago

#1997: Follow up commit to r10177. Added different evaluators for symbolic island algorithms, unit test for consecutive samples evaluator.

File size: 9.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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis;
32using HeuristicLab.Problems.DataAnalysis.Symbolic;
33
34namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
35  [StorableClass]
36  public sealed class ConsecutiveSamplesEvaluator : SingleSuccessorOperator, IIterationBasedOperator, ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator {
37    private const string ProblemDataParameterName = "ProblemData";
38    private const string EvaluatorParameterName = "ProblemEvaluator";
39    private const string QualityParameterName = "Quality";
40    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
41    private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
42    private const string ConsecutiveSamplesParameterName = "ConsecutiveSamples";
43    private const string OverlapParameterName = "Overlap";
44    private const string DataMigrationIntervalParameterName = "DataMigrationInterval";
45    private const string IslandIndexParameterName = "IslandIndex";
46    private const string IterationsParameterName = "Iterations";
47    private const string MaximumIterationsParameterName = "Maximum Iterations";
48
49
50    #region parameter properties
51    public ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
52      get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
53    }
54    public ILookupParameter<IOperator> EvaluatorParameter {
55      get { return (ILookupParameter<IOperator>)Parameters[EvaluatorParameterName]; }
56    }
57    public ILookupParameter<DoubleValue> QualityParameter {
58      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
59    }
60    public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
61      get { return (IValueLookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
62    }
63    public ILookupParameter<IntRange> FixedSamplesPartitionParameter {
64      get { return (ILookupParameter<IntRange>)Parameters[FixedSamplesPartitionParameterName]; }
65    }
66    public IFixedValueParameter<IntValue> ConsecutiveSamplesParameter {
67      get { return (IFixedValueParameter<IntValue>)Parameters[ConsecutiveSamplesParameterName]; }
68    }
69    public IFixedValueParameter<IntValue> OverlapParameter {
70      get { return (IFixedValueParameter<IntValue>)Parameters[OverlapParameterName]; }
71    }
72    public IValueLookupParameter<IntValue> DataMigrationIntervalParameter {
73      get { return (IValueLookupParameter<IntValue>)Parameters[DataMigrationIntervalParameterName]; }
74    }
75    public ILookupParameter<IntValue> IslandIndexParameter {
76      get { return (ILookupParameter<IntValue>)Parameters[IslandIndexParameterName]; }
77    }
78    public ILookupParameter<IntValue> IterationsParameter {
79      get { return (ILookupParameter<IntValue>)Parameters[IterationsParameterName]; }
80    }
81    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
82      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsParameterName]; }
83    }
84
85    #endregion
86
87    #region properties
88
89    public int ConsecutiveSamples {
90      get { return ConsecutiveSamplesParameter.Value.Value; }
91      set { ConsecutiveSamplesParameter.Value.Value = value; }
92    }
93
94    public int Overlap {
95      get { return OverlapParameter.Value.Value; }
96      set { OverlapParameter.Value.Value = value; }
97    }
98    #endregion
99
100    [StorableConstructor]
101    private ConsecutiveSamplesEvaluator(bool deserializing) : base(deserializing) { }
102    private ConsecutiveSamplesEvaluator(ConsecutiveSamplesEvaluator original, Cloner cloner)
103      : base(original, cloner) {
104    }
105    public override IDeepCloneable Clone(Cloner cloner) {
106      return new ConsecutiveSamplesEvaluator(this, cloner);
107    }
108
109    public ConsecutiveSamplesEvaluator()
110      : base() {
111      Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
112      Parameters.Add(new LookupParameter<IOperator>(EvaluatorParameterName, "The evaluator provided by the symbolic data analysis  problem."));
113      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality which is calculated by the encapsulated evaluator."));
114      Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, "The data partition used to calculate the fitness"));
115      Parameters.Add(new LookupParameter<IntRange>(FixedSamplesPartitionParameterName, "The data partition which is used to calculate the fitness on the fixed samples."));
116      Parameters.Add(new FixedValueParameter<IntValue>(ConsecutiveSamplesParameterName, "The number of consecutive samples used for fitness calculation in each island.", new IntValue()));
117      Parameters.Add(new FixedValueParameter<IntValue>(OverlapParameterName, "The overlap for the consecutive samples used for every island.", new IntValue()));
118      Parameters.Add(new ValueLookupParameter<IntValue>(DataMigrationIntervalParameterName, "The number of generations that should pass between data migration phases."));
119      Parameters.Add(new LookupParameter<IntValue>(IslandIndexParameterName, "The index of the current island."));
120      Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
121      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
122    }
123
124    public override IOperation Apply() {
125      var evaluator = EvaluatorParameter.ActualValue;
126      var problemData = ProblemDataParameter.ActualValue;
127      var samples = FitnessCalculationPartitionParameter.ActualValue;
128      var fixedSamples = FixedSamplesPartitionParameter.ActualValue;
129
130      //create fixed rows enumerable
131      var rows = Enumerable.Range(fixedSamples.Start, fixedSamples.Size);
132      //create consecutive rows enumerable
133      if (ConsecutiveSamples > 0) {
134        var dataMigrationInterval = DataMigrationIntervalParameter.ActualValue.Value;
135        var islandIndex = IslandIndexParameter.ActualValue.Value;
136        var generation = IterationsParameter.ActualValue.Value;
137        var iteration = islandIndex + (generation / dataMigrationInterval);
138        var consecutiveRows = GenerateRows(samples, fixedSamples, ConsecutiveSamples, Overlap, iteration);
139        rows = rows.Concat(consecutiveRows);
140      }
141      //filter out test rows
142      rows = rows.Where(r => r < problemData.TestPartition.Start || r > problemData.TestPartition.End);
143
144      //execution context is created manually to be able to clear the rows parameter easily
145      var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope);
146
147      //TODO change to lookup parameter
148      executionContext.Scope.Variables.Remove("Rows");
149      executionContext.Scope.Variables.Add(new HeuristicLab.Core.Variable("Rows", new EnumerableItem<int>(rows)));
150      var successor = evaluator.Execute(executionContext, this.CancellationToken);
151      return new OperationCollection(successor, base.Apply());
152    }
153
154    public static IEnumerable<int> GenerateRows(IntRange samples, IntRange fixedSamples, int consecutiveSamples, int overlap, int iteration) {
155      var consecutiveSamplesStart = (consecutiveSamples - overlap) * iteration;
156      consecutiveSamplesStart = consecutiveSamplesStart % (samples.Size - fixedSamples.Size);
157      var rows = Enumerable.Range(fixedSamples.End, samples.End - fixedSamples.End);
158      rows = rows.Concat(Enumerable.Range(samples.Start, fixedSamples.Start - samples.Start));
159      rows = rows.Concat(Enumerable.Range(fixedSamples.End, samples.End - fixedSamples.End));
160      rows = rows.Concat(Enumerable.Range(samples.Start, fixedSamples.Start - samples.Start));
161      return rows.Skip(consecutiveSamplesStart).Take(consecutiveSamples);
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.