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.Collections.Generic;
|
---|
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 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
33 | [Item("ReplaceBranchMoveSoftTabuCriterion", @"")]
|
---|
34 | [StorableClass]
|
---|
35 | public class ReplaceBranchMoveSoftTabuCriterion : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, ITabuChecker {
|
---|
36 | public override bool CanChangeName {
|
---|
37 | get { return false; }
|
---|
38 | }
|
---|
39 | public ILookupParameter<ReplaceBranchMove> ReplaceBranchMoveParameter {
|
---|
40 | get { return (LookupParameter<ReplaceBranchMove>)Parameters["ReplaceBranchMove"]; }
|
---|
41 | }
|
---|
42 | public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
43 | get { return (LookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
|
---|
44 | }
|
---|
45 | public ILookupParameter<ItemList<IItem>> TabuListParameter {
|
---|
46 | get { return (ILookupParameter<ItemList<IItem>>)Parameters["TabuList"]; }
|
---|
47 | }
|
---|
48 | public ILookupParameter<BoolValue> MoveTabuParameter {
|
---|
49 | get { return (ILookupParameter<BoolValue>)Parameters["MoveTabu"]; }
|
---|
50 | }
|
---|
51 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
52 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<DoubleValue> MoveQualityParameter {
|
---|
55 | get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
|
---|
56 | }
|
---|
57 | public ValueParameter<BoolValue> UseAspirationCriterionParameter {
|
---|
58 | get { return (ValueParameter<BoolValue>)Parameters["UseAspirationCriterion"]; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public BoolValue UseAspirationCriterion {
|
---|
62 | get { return UseAspirationCriterionParameter.Value; }
|
---|
63 | set { UseAspirationCriterionParameter.Value = value; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | [StorableConstructor]
|
---|
67 | protected ReplaceBranchMoveSoftTabuCriterion(bool deserializing) : base(deserializing) { }
|
---|
68 | protected ReplaceBranchMoveSoftTabuCriterion(ReplaceBranchMoveSoftTabuCriterion original, Cloner cloner) : base(original, cloner) { }
|
---|
69 | public ReplaceBranchMoveSoftTabuCriterion()
|
---|
70 | : base() {
|
---|
71 | Parameters.Add(new LookupParameter<ReplaceBranchMove>("ReplaceBranchMove", "The move to evaluate."));
|
---|
72 | Parameters.Add(new LookupParameter<BoolValue>("MoveTabu", "The variable to store if a move was tabu."));
|
---|
73 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The solution as symbolic expression tree."));
|
---|
74 | Parameters.Add(new LookupParameter<ItemList<IItem>>("TabuList", "The tabu list."));
|
---|
75 | Parameters.Add(new ValueParameter<BoolValue>("UseAspirationCriterion", "Whether to use the aspiration criterion or not.", new BoolValue(true)));
|
---|
76 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, else if it is a minimization problem."));
|
---|
77 | Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the current move."));
|
---|
78 | }
|
---|
79 |
|
---|
80 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
81 | return new ReplaceBranchMoveSoftTabuCriterion(this, cloner);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public override IOperation Apply() {
|
---|
85 | ItemList<IItem> tabuList = TabuListParameter.ActualValue;
|
---|
86 | var move = ReplaceBranchMoveParameter.ActualValue;
|
---|
87 | var tree = SymbolicExpressionTreeParameter.ActualValue;
|
---|
88 | bool isTabu = true;
|
---|
89 | bool useAspiration = UseAspirationCriterion.Value;
|
---|
90 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
91 | double moveQuality = MoveQualityParameter.ActualValue.Value;
|
---|
92 |
|
---|
93 | List<int> path = new List<int>();
|
---|
94 | path.Add(move.SubtreeIndex);
|
---|
95 | var parent = move.Parent.Parent;
|
---|
96 | var child = move.Parent;
|
---|
97 | while (parent != null) {
|
---|
98 | path.Add(parent.IndexOfSubtree(child));
|
---|
99 | child = parent;
|
---|
100 | parent = parent.Parent;
|
---|
101 | }
|
---|
102 | path.Reverse();
|
---|
103 |
|
---|
104 | isTabu = false;
|
---|
105 | for (int i = 0; i < tabuList.Count && isTabu == false; i++) {
|
---|
106 | var tabuAttribute = tabuList[i];
|
---|
107 | var absTabuAttribute = tabuAttribute as ChangeNodeTypeMoveAbsoluteAttribute;
|
---|
108 | if (!useAspiration
|
---|
109 | || maximization && moveQuality <= absTabuAttribute.MoveQuality
|
---|
110 | || !maximization && moveQuality >= absTabuAttribute.MoveQuality) {
|
---|
111 | if (absTabuAttribute.Path.Length == path.Count) {
|
---|
112 | isTabu = true;
|
---|
113 | for (int j = 0; j < path.Count; j++) {
|
---|
114 | if (absTabuAttribute.Path[j] != path[j]) {
|
---|
115 | isTabu = false;
|
---|
116 | break;
|
---|
117 | }
|
---|
118 | }
|
---|
119 | if (isTabu) {
|
---|
120 | // check correlations
|
---|
121 | // R(prev, next)
|
---|
122 | // R(current, next)
|
---|
123 | // tabu = R(prev,next) > R(current,next)
|
---|
124 | // == the new move would be closer to the original than the previous move
|
---|
125 | OnlineCalculatorError error;
|
---|
126 | double rPrevNext = OnlinePearsonsRSquaredCalculator.Calculate(absTabuAttribute.PreviousOutput, move.NewOutput, out error);
|
---|
127 | if (error != OnlineCalculatorError.None) rPrevNext = 0.0;
|
---|
128 | double rCurrentNext = OnlinePearsonsRSquaredCalculator.Calculate(absTabuAttribute.PreviousOutput, move.OriginalOutput,
|
---|
129 | out error);
|
---|
130 | if (error != OnlineCalculatorError.None) rCurrentNext = 0.0;
|
---|
131 | isTabu = rPrevNext >= rCurrentNext;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | MoveTabuParameter.ActualValue = new BoolValue(isTabu);
|
---|
138 | return base.Apply();
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|