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