Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16310 was 10825, checked in by mkommend, 11 years ago

#1997: Allowed fixed samples to be wrap around the samples partition.

File size: 9.4 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.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34
35namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
36  [StorableClass]
37  public sealed class ConsecutiveSamplesEvaluator : SingleSuccessorOperator, IIterationBasedOperator, ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator {
38    private const string ProblemDataParameterName = "ProblemData";
39    private const string EvaluatorParameterName = "ProblemEvaluator";
40    private const string QualityParameterName = "Quality";
41    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
42    private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
43    private const string ConsecutiveSamplesParameterName = "ConsecutiveSamples";
44    private const string OverlapParameterName = "Overlap";
45    private const string DataMigrationIntervalParameterName = "DataMigrationInterval";
46    private const string IslandIndexParameterName = "IslandIndex";
47    private const string IterationsParameterName = "Iterations";
48    private const string MaximumIterationsParameterName = "Maximum Iterations";
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 (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
62    }
63    public ILookupParameter<IntRange> FixedSamplesPartitionParameter {
64      get { return (ILookupParameter<IntRange>)Parameters[FixedSamplesPartitionParameterName]; }
65    }
66    public IFixedValueParameter<PercentValue> ConsecutiveSamplesParameter {
67      get { return (IFixedValueParameter<PercentValue>)Parameters[ConsecutiveSamplesParameterName]; }
68    }
69    public IFixedValueParameter<PercentValue> OverlapParameter {
70      get { return (IFixedValueParameter<PercentValue>)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 double ConsecutiveSamples {
90      get { return ConsecutiveSamplesParameter.Value.Value; }
91      set { ConsecutiveSamplesParameter.Value.Value = value; }
92    }
93
94    public double 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<PercentValue>(ConsecutiveSamplesParameterName, "The relative number of consecutive samples used for fitness calculation in each island.", new PercentValue()));
117      Parameters.Add(new FixedValueParameter<PercentValue>(OverlapParameterName, "The relative overlap for the consecutive samples used for every island.", new PercentValue()));
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      var dataMigrationInterval = DataMigrationIntervalParameter.ActualValue.Value;
131      var generationValue = IterationsParameter.ActualValue;
132      var generation = generationValue == null ? 0 : generationValue.Value;
133
134      if (ConsecutiveSamples > 0 && dataMigrationInterval == 0)
135        throw new ArgumentException("The data migration interval must not be 0 if consecutive samples are used.");
136
137      //create fixed rows enumerable
138      var rows = Enumerable.Range(fixedSamples.Start, fixedSamples.Size).Select(r => (r - samples.Start) % samples.Size + samples.Start);
139      //create consecutive rows enumerable
140      if (ConsecutiveSamples > 0) {
141        var islandIndex = IslandIndexParameter.ActualValue.Value;
142        var iteration = islandIndex + (generation / dataMigrationInterval);
143        var consecutiveSamples = (int)(ConsecutiveSamples * samples.Size);
144        var overlap = (int)Overlap * consecutiveSamples;
145        var consecutiveRows = GenerateRows(samples, fixedSamples, consecutiveSamples, overlap, iteration);
146        rows = rows.Concat(consecutiveRows);
147      }
148      //filter out test rows
149      rows = rows.Where(r => r < problemData.TestPartition.Start || r > problemData.TestPartition.End);
150
151      //TODO change to lookup parameter
152      ExecutionContext.Scope.Variables.Remove("Rows");
153      ExecutionContext.Scope.Variables.Add(new HeuristicLab.Core.Variable("Rows", new EnumerableItem<int>(rows)));
154
155      var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope);
156      var successor = evaluator.Execute(executionContext, this.CancellationToken);
157      return new OperationCollection(successor, base.Apply());
158    }
159
160    public static IEnumerable<int> GenerateRows(IntRange samples, IntRange fixedSamples, int consecutiveSamples, int overlap, int iteration) {
161      var consecutiveSamplesStart = (consecutiveSamples - overlap) * iteration;
162      consecutiveSamplesStart = consecutiveSamplesStart % (samples.Size - fixedSamples.Size);
163      var rows = Enumerable.Range(fixedSamples.End, samples.End - fixedSamples.End);
164      rows = rows.Concat(Enumerable.Range(samples.Start, fixedSamples.Start - samples.Start));
165      rows = rows.Concat(Enumerable.Range(fixedSamples.End, samples.End - fixedSamples.End));
166      rows = rows.Concat(Enumerable.Range(samples.Start, fixedSamples.Start - samples.Start));
167      return rows.Skip(consecutiveSamplesStart).Take(consecutiveSamples);
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.