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 FixedSamplesParameterName = "FixedSamples";
|
---|
47 | private const string FixedSamplesFitnessWeightParameterName = "FixedSamplesFitnessWeight";
|
---|
48 | private const string RandomSamplesParameterName = "RandomSamples";
|
---|
49 | private const string RandomSamplesFitnessWeightParameterName = "RandomSamplesFitnessWeight";
|
---|
50 |
|
---|
51 | #region parameter properties
|
---|
52 | public ILookupParameter<IRandom> RandomParameter {
|
---|
53 | get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
|
---|
54 | }
|
---|
55 | public ILookupParameter<T> ProblemDataParameter {
|
---|
56 | get { return (ILookupParameter<T>)Parameters[ProblemDataParameterName]; }
|
---|
57 | }
|
---|
58 | public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
59 | get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
60 | }
|
---|
61 | public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> EvaluatorParameter {
|
---|
62 | get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>)Parameters[EvaluatorParameterName]; }
|
---|
63 | }
|
---|
64 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
65 | get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
66 | }
|
---|
67 | public IValueLookupParameter<IntRange> FitnessCalculationPartitionParameter {
|
---|
68 | get { return (IValueLookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
|
---|
69 | }
|
---|
70 | public ILookupParameter<IntRange> FixedSamplesPartitionParameter {
|
---|
71 | get { return (ILookupParameter<IntRange>)Parameters[FixedSamplesPartitionParameterName]; }
|
---|
72 | }
|
---|
73 | public ILookupParameter<IntValue> FixedSamplesParameter {
|
---|
74 | get { return (ILookupParameter<IntValue>)Parameters[FixedSamplesParameterName]; }
|
---|
75 | }
|
---|
76 | public IFixedValueParameter<DoubleValue> FixedSamplesFitnessWeightParameter {
|
---|
77 | get { return (IFixedValueParameter<DoubleValue>)Parameters[FixedSamplesFitnessWeightParameterName]; }
|
---|
78 | }
|
---|
79 | public ILookupParameter<IntValue> RandomSamplesParameter {
|
---|
80 | get { return (ILookupParameter<IntValue>)Parameters[RandomSamplesParameterName]; }
|
---|
81 | }
|
---|
82 | public IFixedValueParameter<DoubleValue> RandomSamplesFitnessWeightParameter {
|
---|
83 | get { return (IFixedValueParameter<DoubleValue>)Parameters[RandomSamplesFitnessWeightParameterName]; }
|
---|
84 | }
|
---|
85 | #endregion
|
---|
86 |
|
---|
87 | #region properties
|
---|
88 | public double FixedSamplesFitnessWeight {
|
---|
89 | get { return FixedSamplesFitnessWeightParameter.Value.Value; }
|
---|
90 | set { FixedSamplesFitnessWeightParameter.Value.Value = value; }
|
---|
91 | }
|
---|
92 | public double RandomSamplesFitnessWeight {
|
---|
93 | get { return RandomSamplesFitnessWeightParameter.Value.Value; }
|
---|
94 | set { RandomSamplesFitnessWeightParameter.Value.Value = value; }
|
---|
95 | }
|
---|
96 | #endregion
|
---|
97 |
|
---|
98 | [StorableConstructor]
|
---|
99 | private SymbolicDataAnalysisIslandGAEvaluator(bool deserializing) : base(deserializing) { }
|
---|
100 | private SymbolicDataAnalysisIslandGAEvaluator(SymbolicDataAnalysisIslandGAEvaluator<T> original, Cloner cloner)
|
---|
101 | : base(original, cloner) {
|
---|
102 | }
|
---|
103 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
104 | return new SymbolicDataAnalysisIslandGAEvaluator<T>(this, cloner);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public SymbolicDataAnalysisIslandGAEvaluator()
|
---|
108 | : base() {
|
---|
109 | Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
|
---|
110 | Parameters.Add(new LookupParameter<T>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
|
---|
111 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic data analysis solution encoded as a symbolic expression tree."));
|
---|
112 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>(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 ValueLookupParameter<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 LookupParameter<IntValue>(FixedSamplesParameterName, "The number of fixed samples used for fitness calculation in each island."));
|
---|
117 | Parameters.Add(new FixedValueParameter<DoubleValue>(FixedSamplesFitnessWeightParameterName, "The weight of the fitness obtained on the fixed samples.", new DoubleValue(1)));
|
---|
118 | Parameters.Add(new LookupParameter<IntValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island."));
|
---|
119 | Parameters.Add(new FixedValueParameter<DoubleValue>(RandomSamplesFitnessWeightParameterName, "The weight of the fitness obtained on the random samples.", new DoubleValue(1)));
|
---|
120 |
|
---|
121 | EvaluatorParameter.Hidden = true;
|
---|
122 | }
|
---|
123 |
|
---|
124 | public override IOperation Apply() {
|
---|
125 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
126 | //calculate fitness on fixed samples
|
---|
127 | if (QualityParameter.ActualValue == null) {
|
---|
128 | var operation = ExecutionContext.CreateOperation(evaluator, ExecutionContext.Scope);
|
---|
129 | return new OperationCollection() { operation, ExecutionContext.CreateOperation(this) };
|
---|
130 | }
|
---|
131 | //calculate fitness on random samples;
|
---|
132 | var samplesStart = FitnessCalculationPartitionParameter.ActualValue.Start;
|
---|
133 | var samplesEnd = FitnessCalculationPartitionParameter.ActualValue.End;
|
---|
134 | var fixedSamplesStart = FixedSamplesPartitionParameter.ActualValue.Start;
|
---|
135 | var fixedSamplesEnd = FixedSamplesPartitionParameter.ActualValue.End;
|
---|
136 | var randomSamples = RandomSamplesParameter.ActualValue.Value;
|
---|
137 | var maxRandomSamples = samplesEnd - samplesStart - fixedSamplesEnd + fixedSamplesStart;
|
---|
138 |
|
---|
139 | var rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart).Where(r => r < fixedSamplesStart || r >= fixedSamplesEnd);
|
---|
140 | rows = rows.SampleRandomWithoutRepetition(RandomParameter.ActualValue, randomSamples, maxRandomSamples);
|
---|
141 |
|
---|
142 | var fixedSamplesFitness = QualityParameter.ActualValue.Value;
|
---|
143 | var tree = SymbolicExpressionTreeParameter.ActualValue;
|
---|
144 | var problemData = ProblemDataParameter.ActualValue;
|
---|
145 |
|
---|
146 | var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope);
|
---|
147 | var randomSamplesFitness = evaluator.Evaluate(executionContext, tree, problemData, rows);
|
---|
148 | QualityParameter.ActualValue.Value = fixedSamplesFitness * FixedSamplesFitnessWeight + randomSamplesFitness * RandomSamplesFitnessWeight;
|
---|
149 | return base.Apply();
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|