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.Encodings.SymbolicExpressionTreeEncoding;
|
---|
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 | using HeuristicLab.Random;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
|
---|
36 | [StorableClass]
|
---|
37 | public sealed class SymbolicDataAnalysisIslandGAEvaluator<T> : SingleSuccessorOperator, IStochasticOperator, ISymbolicDataAnalysisIslandGAEvaluator
|
---|
38 | where T : class,IDataAnalysisProblemData {
|
---|
39 | private const string RandomParameterName = "Random";
|
---|
40 | private const string ProblemDataParameterName = "ProblemData";
|
---|
41 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
42 | private const string EvaluatorParameterName = "ProblemEvaluator";
|
---|
43 | private const string QualityParameterName = "Quality";
|
---|
44 | private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
|
---|
45 | private const string FixedSamplesPartitionParameterName = "FixedSamplesPartition";
|
---|
46 | private const string RandomSamplesParameterName = "RandomSamples";
|
---|
47 |
|
---|
48 | #region parameter properties
|
---|
49 | public ILookupParameter<IRandom> RandomParameter {
|
---|
50 | get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
|
---|
51 | }
|
---|
52 | public ILookupParameter<T> ProblemDataParameter {
|
---|
53 | get { return (ILookupParameter<T>)Parameters[ProblemDataParameterName]; }
|
---|
54 | }
|
---|
55 | public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
56 | get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
57 | }
|
---|
58 | public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> EvaluatorParameter {
|
---|
59 | get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>)Parameters[EvaluatorParameterName]; }
|
---|
60 | }
|
---|
61 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
62 | get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
63 | }
|
---|
64 | public IValueLookupParameter<IntRange> FitnessCalculationPartitionParameter {
|
---|
65 | get { return (IValueLookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
|
---|
66 | }
|
---|
67 | public ILookupParameter<IntRange> FixedSamplesPartitionParameter {
|
---|
68 | get { return (ILookupParameter<IntRange>)Parameters[FixedSamplesPartitionParameterName]; }
|
---|
69 | }
|
---|
70 | public ILookupParameter<IntValue> RandomSamplesParameter {
|
---|
71 | get { return (ILookupParameter<IntValue>)Parameters[RandomSamplesParameterName]; }
|
---|
72 | }
|
---|
73 | #endregion
|
---|
74 |
|
---|
75 |
|
---|
76 | [StorableConstructor]
|
---|
77 | private SymbolicDataAnalysisIslandGAEvaluator(bool deserializing) : base(deserializing) { }
|
---|
78 | private SymbolicDataAnalysisIslandGAEvaluator(SymbolicDataAnalysisIslandGAEvaluator<T> original, Cloner cloner)
|
---|
79 | : base(original, cloner) {
|
---|
80 | }
|
---|
81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
82 | return new SymbolicDataAnalysisIslandGAEvaluator<T>(this, cloner);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public SymbolicDataAnalysisIslandGAEvaluator()
|
---|
86 | : base() {
|
---|
87 | Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
|
---|
88 | Parameters.Add(new LookupParameter<T>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
|
---|
89 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic data analysis solution encoded as a symbolic expression tree."));
|
---|
90 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>(EvaluatorParameterName, "The evaluator provided by the symbolic data analysis problem."));
|
---|
91 | Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality which is calculated by the encapsulated evaluator."));
|
---|
92 | Parameters.Add(new ValueLookupParameter<IntRange>(FitnessCalculationPartitionParameterName, "The data partition used to calculate the fitness"));
|
---|
93 | Parameters.Add(new LookupParameter<IntRange>(FixedSamplesPartitionParameterName, "The data partition which is used to calculate the fitness on the fixed samples."));
|
---|
94 | Parameters.Add(new LookupParameter<IntValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island."));
|
---|
95 |
|
---|
96 | EvaluatorParameter.Hidden = true;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public override IOperation Apply() {
|
---|
100 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
101 | var tree = SymbolicExpressionTreeParameter.ActualValue;
|
---|
102 | var problemData = ProblemDataParameter.ActualValue;
|
---|
103 |
|
---|
104 | var samplesStart = FitnessCalculationPartitionParameter.ActualValue.Start;
|
---|
105 | var samplesEnd = FitnessCalculationPartitionParameter.ActualValue.End;
|
---|
106 | var fixedSamplesStart = FixedSamplesPartitionParameter.ActualValue.Start;
|
---|
107 | var fixedSamplesEnd = FixedSamplesPartitionParameter.ActualValue.End;
|
---|
108 | var randomSamples = RandomSamplesParameter.ActualValue.Value;
|
---|
109 | var maxRandomSamples = samplesEnd - samplesStart - fixedSamplesEnd + fixedSamplesStart;
|
---|
110 |
|
---|
111 | //create rows for evaluation
|
---|
112 | var fixedRows = Enumerable.Range(fixedSamplesStart, fixedSamplesEnd - fixedSamplesStart);
|
---|
113 | var randomRows = Enumerable.Range(samplesStart, samplesEnd - samplesStart).Where(r => r < fixedSamplesStart || r >= fixedSamplesEnd);
|
---|
114 | randomRows = randomRows.SampleRandomWithoutRepetition(RandomParameter.ActualValue, randomSamples, maxRandomSamples);
|
---|
115 | var rows = fixedRows.Concat(randomRows);
|
---|
116 |
|
---|
117 | var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope);
|
---|
118 | var fitness = evaluator.Evaluate(executionContext, tree, problemData, rows);
|
---|
119 | QualityParameter.ActualValue = new DoubleValue(fitness);
|
---|
120 | return base.Apply();
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|