Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Algorithms.DataAnalysis.Symbolic/3.3/SymbolicDataAnalysisIslandGAEvaluator.cs @ 10156

Last change on this file since 10156 was 10156, checked in by mkommend, 9 years ago

#1997: Removed generic type parameter from SymbolicDataAnylsisIslandGAEvaluator and added wiring code for the reevaluation of immigrants.

File size: 6.7 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31using HeuristicLab.Problems.DataAnalysis.Symbolic;
32using HeuristicLab.Random;
33
34namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic {
35  [StorableClass]
36  public sealed class SymbolicDataAnalysisIslandGAEvaluator : SingleSuccessorOperator, IStochasticOperator, ISymbolicDataAnalysisIslandGAEvaluator {
37    private const string RandomParameterName = "Random";
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 RandomSamplesParameterName = "RandomSamples";
44
45    #region parameter properties
46    public ILookupParameter<IRandom> RandomParameter {
47      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
48    }
49    public ILookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
50      get { return (ILookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
51    }
52    public ILookupParameter<IOperator> EvaluatorParameter {
53      get { return (ILookupParameter<IOperator>)Parameters[EvaluatorParameterName]; }
54    }
55    public ILookupParameter<DoubleValue> QualityParameter {
56      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
57    }
58    public IValueLookupParameter<IntRange> FitnessCalculationPartitionParameter {
59      get { return (IValueLookupParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
60    }
61    public ILookupParameter<IntRange> FixedSamplesPartitionParameter {
62      get { return (ILookupParameter<IntRange>)Parameters[FixedSamplesPartitionParameterName]; }
63    }
64    public ILookupParameter<IntValue> RandomSamplesParameter {
65      get { return (ILookupParameter<IntValue>)Parameters[RandomSamplesParameterName]; }
66    }
67    #endregion
68
69    [StorableConstructor]
70    private SymbolicDataAnalysisIslandGAEvaluator(bool deserializing) : base(deserializing) { }
71    private SymbolicDataAnalysisIslandGAEvaluator(SymbolicDataAnalysisIslandGAEvaluator original, Cloner cloner)
72      : base(original, cloner) {
73    }
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new SymbolicDataAnalysisIslandGAEvaluator(this, cloner);
76    }
77
78    public SymbolicDataAnalysisIslandGAEvaluator()
79      : base() {
80      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
81      Parameters.Add(new LookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
82      Parameters.Add(new LookupParameter<IOperator>(EvaluatorParameterName, "The evaluator provided by the symbolic data analysis  problem."));
83      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality which is calculated by the encapsulated evaluator."));
84      Parameters.Add(new ValueLookupParameter<IntRange>(FitnessCalculationPartitionParameterName, "The data partition used to calculate the fitness"));
85      Parameters.Add(new LookupParameter<IntRange>(FixedSamplesPartitionParameterName, "The data partition which is used to calculate the fitness on the fixed samples."));
86      Parameters.Add(new LookupParameter<IntValue>(RandomSamplesParameterName, "The number of random samples used for fitness calculation in each island."));
87
88      EvaluatorParameter.Hidden = true;
89    }
90
91    public override IOperation Apply() {
92      var evaluator = EvaluatorParameter.ActualValue;
93      var problemData = ProblemDataParameter.ActualValue;
94
95      var samples = FitnessCalculationPartitionParameter.ActualValue;
96      var fixedSamples = FixedSamplesPartitionParameter.ActualValue;
97      var randomSamples = RandomSamplesParameter.ActualValue.Value;
98
99      //create fixed rows enumerable
100      var rows = Enumerable.Range(fixedSamples.Start, fixedSamples.Size);
101      //create randomly chosen rows enumerable
102      if (randomSamples > 0) {
103        if (randomSamples > samples.Size - fixedSamples.Size) {
104          var error = string.Format("Could not select {0} random samples, because there are {1} total samples present from which {2} where used in the fixed partition. Please lower the number of random samples in the algorithm configuration.", randomSamples, samples.Size, fixedSamples.Size);
105          throw new OperatorExecutionException(this, error);
106        }
107        var randomRows = Enumerable.Range(samples.Start, samples.Size).Where(r => r < fixedSamples.Start || r >= fixedSamples.End);
108        randomRows = randomRows.SampleRandomWithoutRepetition(RandomParameter.ActualValue, randomSamples, samples.Size - fixedSamples.Size);
109
110        rows = rows.Concat(randomRows);
111      }
112      //filter out test rows
113      rows = rows.Where(r => r < problemData.TestPartition.Start || r > problemData.TestPartition.End);
114
115      //execution context is created manually to be able to clear the rows parameter easily
116      var executionContext = new ExecutionContext(ExecutionContext, evaluator, ExecutionContext.Scope);
117
118      //TODO change to lookup parameter
119      executionContext.Scope.Variables.Remove("Rows");
120      executionContext.Scope.Variables.Add(new HeuristicLab.Core.Variable("Rows", new EnumerableItem<int>(rows)));
121      var successor = evaluator.Execute(executionContext, this.CancellationToken);
122      return new OperationCollection(successor, base.Apply());
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.