Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionMoveEvaluator.cs @ 10235

Last change on this file since 10235 was 8206, checked in by gkronber, 12 years ago

#1847: merged r8084:8205 from trunk into GP move operators branch

File size: 5.1 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
33  [Item("SymbolicRegressionMoveEvaluator", "")]
34  [StorableClass]
35  public class SymbolicRegressionMoveEvaluator : SingleSuccessorOperator, ISymbolicRegressionMoveEvaluator {
36
37    public override bool CanChangeName {
38      get { return false; }
39    }
40
41    public ILookupParameter<ChangeNodeTypeMove> MoveParameter {
42      get { return (ILookupParameter<ChangeNodeTypeMove>)Parameters["ChangeNodeTypeMove"]; }
43    }
44    public ILookupParameter<DoubleValue> QualityParameter {
45      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
46    }
47
48    public ILookupParameter<DoubleValue> MoveQualityParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
50    }
51
52    public ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator> EvaluatorParameter {
53      get { return (ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>)Parameters["Evaluator"]; }
54    }
55    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
56      get { return (ILookupParameter<IRegressionProblemData>)Parameters["ProblemData"]; }
57    }
58    public ILookupParameter<IntRange> FitnessCalculationPartitionParameter {
59      get { return (ILookupParameter<IntRange>)Parameters["FitnessCalculationPartition"]; }
60    }
61
62    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
63      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
64    }
65    [StorableConstructor]
66    protected SymbolicRegressionMoveEvaluator(bool deserializing) : base(deserializing) { }
67    protected SymbolicRegressionMoveEvaluator(SymbolicRegressionMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
68    public SymbolicRegressionMoveEvaluator()
69      : base() {
70      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of a symbolic regression solution."));
71      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The evaluated quality of a move on a symbolic regression solution."));
72      Parameters.Add(new LookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>("Evaluator", ""));
73      Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", ""));
74      Parameters.Add(new LookupParameter<IRegressionProblemData>("ProblemData", ""));
75      Parameters.Add(new LookupParameter<IntRange>("FitnessCalculationPartition", ""));
76      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", ""));
77    }
78
79    public override IOperation Apply() {
80      var evaluator = EvaluatorParameter.ActualValue;
81      // clone the move and all contained objects to make sure that the items in the scope are untouched
82      var move = (ChangeNodeTypeMove)MoveParameter.ActualValue.Clone();
83      var oldNode = move.Parent.GetSubtree(move.SubtreeIndex);
84      var children = new List<ISymbolicExpressionTreeNode>(oldNode.Subtrees);
85      while (oldNode.SubtreeCount > 0) oldNode.RemoveSubtree(0);
86      var newNode = move.NewChild;
87      foreach (var c in children)
88        newNode.AddSubtree(c);
89
90      move.Parent.RemoveSubtree(move.SubtreeIndex);
91      move.Parent.InsertSubtree(move.SubtreeIndex, newNode);
92
93      var problemData = ProblemDataParameter.ActualValue;
94      IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
95
96      var fitnessPartition = FitnessCalculationPartitionParameter.ActualValue;
97      var rows = Enumerable.Range(fitnessPartition.Start, fitnessPartition.End - fitnessPartition.Start);
98      var quality = evaluator.Evaluate(childContext, move.Tree, problemData, rows);
99
100      MoveQualityParameter.ActualValue = new DoubleValue(quality);
101      return base.Apply();
102    }
103
104    public override IDeepCloneable Clone(Cloner cloner) {
105      return new SymbolicRegressionMoveEvaluator(this, cloner);
106    }
107
108
109  }
110}
Note: See TracBrowser for help on using the repository browser.