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.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
|
---|
33 | [StorableClass]
|
---|
34 | public sealed class GrowingRandomSamplesEvaluator : SingleSuccessorOperator, ISymbolicDataAnalysisIslandGeneticAlgorithmEvaluator {
|
---|
35 | private const string ProblemDataParameterName = "ProblemData";
|
---|
36 | private const string EvaluatorParameterName = "ProblemEvaluator";
|
---|
37 | private const string QualityParameterName = "Quality";
|
---|
38 | private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
|
---|
39 | private const string DataMigrationIntervalParameterName = "DataMigrationInterval";
|
---|
40 | private const string RandomSamplesParameterName = "RandomSamples";
|
---|
41 | private const string IslandIndexParameterName = "IslandIndex";
|
---|
42 | private const string IterationsParameterName = "Iterations";
|
---|
43 | private const string MaximumIterationsParameterName = "Maximum Iterations";
|
---|
44 |
|
---|
45 | #region parameter properties
|
---|
46 | public ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
|
---|
47 | get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
48 | }
|
---|
49 | public ILookupParameter<IOperator> EvaluatorParameter {
|
---|
50 | get { return (ILookupParameter<IOperator>)Parameters[EvaluatorParameterName]; }
|
---|
51 | }
|
---|
52 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
53 | get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
54 | }
|
---|
55 | public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
|
---|
56 | get { return (ILookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
|
---|
57 | }
|
---|
58 | public IValueLookupParameter<IntValue> DataMigrationIntervalParameter {
|
---|
59 | get { return (IValueLookupParameter<IntValue>)Parameters[DataMigrationIntervalParameterName]; }
|
---|
60 | }
|
---|
61 | public IFixedValueParameter<PercentValue> RandomSamplesParameter {
|
---|
62 | get { return (IFixedValueParameter<PercentValue>)Parameters[RandomSamplesParameterName]; }
|
---|
63 | }
|
---|
64 | public ILookupParameter<IntValue> IslandIndexParameter {
|
---|
65 | get { return (ILookupParameter<IntValue>)Parameters[IslandIndexParameterName]; }
|
---|
66 | }
|
---|
67 | public ILookupParameter<IntValue> IterationsParameter {
|
---|
68 | get { return (ILookupParameter<IntValue>)Parameters[IterationsParameterName]; }
|
---|
69 | }
|
---|
70 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
71 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsParameterName]; }
|
---|
72 | }
|
---|
73 | #endregion
|
---|
74 |
|
---|
75 | #region properties
|
---|
76 |
|
---|
77 | public double RandomSamples {
|
---|
78 | get { return RandomSamplesParameter.Value.Value; }
|
---|
79 | set { RandomSamplesParameter.Value.Value = value; }
|
---|
80 | }
|
---|
81 | #endregion
|
---|
82 |
|
---|
83 | [StorableConstructor]
|
---|
84 | private GrowingRandomSamplesEvaluator(bool deserializing) : base(deserializing) { }
|
---|
85 | private GrowingRandomSamplesEvaluator(GrowingRandomSamplesEvaluator original, Cloner cloner)
|
---|
86 | : base(original, cloner) {
|
---|
87 | }
|
---|
88 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
89 | return new GrowingRandomSamplesEvaluator(this, cloner);
|
---|
90 | }
|
---|
91 |
|
---|
92 | public GrowingRandomSamplesEvaluator()
|
---|
93 | : base() {
|
---|
94 | Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
|
---|
95 | Parameters.Add(new LookupParameter<IOperator>(EvaluatorParameterName, "The evaluator provided by the symbolic data analysis problem."));
|
---|
96 | Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality which is calculated by the encapsulated evaluator."));
|
---|
97 | Parameters.Add(new LookupParameter<IntRange>(FitnessCalculationPartitionParameterName, "The data partition used to calculate the fitness"));
|
---|
98 | Parameters.Add(new FixedValueParameter<PercentValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island.", new PercentValue()));
|
---|
99 | Parameters.Add(new ValueLookupParameter<IntValue>(DataMigrationIntervalParameterName, "The number of generations that should pass between data migration phases."));
|
---|
100 | Parameters.Add(new LookupParameter<IntValue>(IslandIndexParameterName, "The index of the current island."));
|
---|
101 | Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
|
---|
102 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
|
---|
103 | }
|
---|
104 |
|
---|
105 | public override IOperation Apply() {
|
---|
106 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
107 | var problemData = ProblemDataParameter.ActualValue;
|
---|
108 | var samples = FitnessCalculationPartitionParameter.ActualValue;
|
---|
109 |
|
---|
110 | var islandIndex = IslandIndexParameter.ActualValue.Value;
|
---|
111 | var dataMigrationInterval = DataMigrationIntervalParameter.ActualValue.Value;
|
---|
112 | var generationValue = IterationsParameter.ActualValue;
|
---|
113 | var generation = generationValue == null ? 0 : generationValue.Value;
|
---|
114 | var maximumGenerations = MaximumIterationsParameter.ActualValue.Value;
|
---|
115 |
|
---|
116 | var growth = (1.0 - RandomSamples) * ((double)dataMigrationInterval) / (maximumGenerations - dataMigrationInterval);
|
---|
117 | var randomSamples = (int)((RandomSamples + growth * ((int)generation / dataMigrationInterval)) * samples.Size);
|
---|
118 |
|
---|
119 | //var random = new FastRandom(islandIndex + generation / dataMigrationInterval);
|
---|
120 | //var rows = Enumerable.Range(samples.Start, samples.Size).SampleRandomWithoutRepetition(random, randomSamples, samples.Size);
|
---|
121 | var rows = Enumerable.Range(samples.Start, randomSamples);
|
---|
122 |
|
---|
123 | //filter out test rows
|
---|
124 | rows = rows.Where(r => r < problemData.TestPartition.Start || r > problemData.TestPartition.End);
|
---|
125 | //TODO change to lookup parameter
|
---|
126 | ExecutionContext.Scope.Variables.Remove("Rows");
|
---|
127 | ExecutionContext.Scope.Variables.Add(new HeuristicLab.Core.Variable("Rows", new EnumerableItem<int>(rows)));
|
---|
128 |
|
---|
129 | var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope);
|
---|
130 | var successor = evaluator.Execute(executionContext, this.CancellationToken);
|
---|
131 | return new OperationCollection(successor, base.Apply());
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|