Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1847 added initial implementation of move operators for symbolic expression tree encoding

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<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
63      get { return (ILookupParameter<SymbolicExpressionTree>)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<SymbolicExpressionTree>("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 t = SymbolicExpressionTreeParameter.ActualValue;
84      var oldNode = move.Parent.GetSubtree(move.SubtreeIndex);
85      var children = new List<ISymbolicExpressionTreeNode>(oldNode.Subtrees);
86      while (oldNode.SubtreeCount > 0) oldNode.RemoveSubtree(0);
87      var newNode = move.NewChild;
88      foreach (var c in children)
89        newNode.AddSubtree(c);
90
91      move.Parent.RemoveSubtree(move.SubtreeIndex);
92      move.Parent.InsertSubtree(move.SubtreeIndex, newNode);
93
94      var problemData = ProblemDataParameter.ActualValue;
95      IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
96
97      var fitnessPartition = FitnessCalculationPartitionParameter.ActualValue;
98      var rows = Enumerable.Range(fitnessPartition.Start, fitnessPartition.End - fitnessPartition.Start);
99      var quality = evaluator.Evaluate(childContext, move.Tree, problemData, rows);
100
101      MoveQualityParameter.ActualValue = new DoubleValue(quality);
102      return base.Apply();
103    }
104
105    public override IDeepCloneable Clone(Cloner cloner) {
106      return new SymbolicRegressionMoveEvaluator(this, cloner);
107    }
108
109
110  }
111}
Note: See TracBrowser for help on using the repository browser.