Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ReplaceBranchMoveSoftTabuCriterion.cs @ 7832

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

#1847 added operator for replacing branches with semantically similar branches

File size: 10.3 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
32  [Item("ReplaceBranchMoveSoftTabuCriterion", @"")]
33  [StorableClass]
34  public class ReplaceBranchMoveSoftTabuCriterion : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, ITabuChecker {
35    public override bool CanChangeName {
36      get { return false; }
37    }
38    public ILookupParameter<ReplaceBranchMove> ReplaceBranchMoveParameter {
39      get { return (LookupParameter<ReplaceBranchMove>)Parameters["ReplaceBranchMove"]; }
40    }
41    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
42      get { return (LookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
43    }
44    public ILookupParameter<ItemList<IItem>> TabuListParameter {
45      get { return (ILookupParameter<ItemList<IItem>>)Parameters["TabuList"]; }
46    }
47    public ILookupParameter<BoolValue> MoveTabuParameter {
48      get { return (ILookupParameter<BoolValue>)Parameters["MoveTabu"]; }
49    }
50    public IValueLookupParameter<BoolValue> MaximizationParameter {
51      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
52    }
53    public ILookupParameter<DoubleValue> MoveQualityParameter {
54      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
55    }
56    public ValueParameter<BoolValue> UseAspirationCriterionParameter {
57      get { return (ValueParameter<BoolValue>)Parameters["UseAspirationCriterion"]; }
58    }
59
60    public BoolValue UseAspirationCriterion {
61      get { return UseAspirationCriterionParameter.Value; }
62      set { UseAspirationCriterionParameter.Value = value; }
63    }
64
65    [StorableConstructor]
66    protected ReplaceBranchMoveSoftTabuCriterion(bool deserializing) : base(deserializing) { }
67    protected ReplaceBranchMoveSoftTabuCriterion(ReplaceBranchMoveSoftTabuCriterion original, Cloner cloner) : base(original, cloner) { }
68    public ReplaceBranchMoveSoftTabuCriterion()
69      : base() {
70      Parameters.Add(new LookupParameter<ReplaceBranchMove>("ReplaceBranchMove", "The move to evaluate."));
71      Parameters.Add(new LookupParameter<BoolValue>("MoveTabu", "The variable to store if a move was tabu."));
72      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The solution as symbolic expression tree."));
73      Parameters.Add(new LookupParameter<ItemList<IItem>>("TabuList", "The tabu list."));
74      Parameters.Add(new ValueParameter<BoolValue>("UseAspirationCriterion", "Whether to use the aspiration criterion or not.", new BoolValue(true)));
75      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, else if it is a minimization problem."));
76      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the current move."));
77    }
78
79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new ReplaceBranchMoveSoftTabuCriterion(this, cloner);
81    }
82
83    public override IOperation Apply() {
84      ItemList<IItem> tabuList = TabuListParameter.ActualValue;
85      var move = ReplaceBranchMoveParameter.ActualValue;
86      var tree = SymbolicExpressionTreeParameter.ActualValue;
87      bool isTabu = true;
88      bool useAspiration = UseAspirationCriterion.Value;
89      bool maximization = MaximizationParameter.ActualValue.Value;
90      double moveQuality = MoveQualityParameter.ActualValue.Value;
91
92      List<int> path = new List<int>();
93      path.Add(move.SubtreeIndex);
94      var parent = move.Parent.Parent;
95      var child = move.Parent;
96      while (parent != null) {
97        path.Add(parent.IndexOfSubtree(child));
98        child = parent;
99        parent = parent.Parent;
100      }
101      path.Reverse();
102
103      isTabu = false;
104      for (int i = 0; i < tabuList.Count && isTabu == false; i++) {
105        var tabuAttribute = tabuList[i];
106        var absTabuAttribute = tabuAttribute as ChangeNodeTypeMoveAbsoluteAttribute;
107        if (!useAspiration
108              || maximization && moveQuality <= absTabuAttribute.MoveQuality
109              || !maximization && moveQuality >= absTabuAttribute.MoveQuality) {
110          if (absTabuAttribute != null && absTabuAttribute.Path.Length == path.Count) {
111            isTabu = true;
112            for (int j = 0; j < path.Count; j++) {
113              if (absTabuAttribute.Path[j] != path[j]) {
114                isTabu = false;
115                break;
116              }
117            }
118          }
119        }
120      }
121      //int length = permutation.Length;
122      //double moveQuality = MoveQualityParameter.ActualValue.Value;
123      //bool maximization = MaximizationParameter.ActualValue.Value;
124      //bool useAspiration = UseAspirationCriterion.Value;
125      //bool isTabu = false;
126
127      //if (permutation.PermutationType == PermutationTypes.Absolute) {
128      //  int count = move.Index2 - move.Index1 + 1;
129      //  int[] numbers = new int[count];
130      //  for (int i = move.Index1; i <= move.Index2; i++)
131      //    numbers[i - move.Index1] = permutation[i];
132
133      //  foreach (IItem tabuMove in tabuList) {
134      //    TranslocationMoveAbsoluteAttribute attribute = (tabuMove as TranslocationMoveAbsoluteAttribute);
135      //    if (attribute != null) {
136      //      if (!useAspiration
137      //        || maximization && moveQuality <= attribute.MoveQuality
138      //        || !maximization && moveQuality >= attribute.MoveQuality) { // if the move quality is improving beyond what was recorded when the move in the tabu list was recorded the move is regarded as okay
139
140      //        for (int i = 0; i < count; i++) {
141      //          for (int j = 0; j < attribute.Number.Length; j++) {
142      //            if (attribute.Number[j] == numbers[i] && attribute.OldPosition + j == move.Index3 + i) {
143      //              isTabu = true;
144      //              break;
145      //            }
146      //          }
147      //          if (isTabu) break;
148      //        }
149      //      }
150      //    }
151      //    if (isTabu) break;
152      //  }
153      //} else {
154      //  int E1S = permutation.GetCircular(move.Index1 - 1);
155      //  int E1T = permutation[move.Index1];
156      //  int E2S = permutation[move.Index2];
157      //  int E2T = permutation.GetCircular(move.Index2 + 1);
158      //  int E3S, E3T;
159      //  if (move.Index3 > move.Index1) {
160      //    E3S = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1);
161      //    E3T = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1 + 1);
162      //  } else {
163      //    E3S = permutation.GetCircular(move.Index3 - 1);
164      //    E3T = permutation[move.Index3];
165      //  }
166      //  foreach (IItem tabuMove in tabuList) {
167      //    TranslocationMoveRelativeAttribute attribute = (tabuMove as TranslocationMoveRelativeAttribute);
168      //    if (attribute != null) {
169      //      if (!useAspiration
170      //        || maximization && moveQuality <= attribute.MoveQuality
171      //        || !maximization && moveQuality >= attribute.MoveQuality) {
172
173      //        // if previously deleted Edge1Source-Target is readded
174      //        if (permutation.PermutationType == PermutationTypes.RelativeUndirected) {
175      //          if (attribute.Edge1Source == E3S && attribute.Edge1Target == E1T || attribute.Edge1Source == E1T && attribute.Edge1Target == E3S
176      //            || attribute.Edge1Source == E2S && attribute.Edge1Target == E3T || attribute.Edge1Source == E3T && attribute.Edge1Target == E2S
177      //            // if previously deleted Edge2Source-Target is readded
178      //            || attribute.Edge2Source == E3S && attribute.Edge2Target == E1T || attribute.Edge2Source == E1T && attribute.Edge2Target == E3S
179      //            || attribute.Edge2Source == E2S && attribute.Edge2Target == E3T || attribute.Edge2Source == E3T && attribute.Edge2Target == E2S
180      //            // if previously deleted Edge3Source-Target is readded
181      //            || attribute.Edge3Source == E3S && attribute.Edge3Target == E1T || attribute.Edge3Source == E1T && attribute.Edge3Target == E3S
182      //            || attribute.Edge3Source == E2S && attribute.Edge3Target == E3T || attribute.Edge3Source == E3T && attribute.Edge3Target == E2S) {
183      //            isTabu = true;
184      //            break;
185      //          }
186      //        } else {
187      //          if (attribute.Edge1Source == E3S && attribute.Edge1Target == E1T
188      //            || attribute.Edge1Source == E2S && attribute.Edge1Target == E3T
189      //            // if previously deleted Edge2Source-Target is readded
190      //            || attribute.Edge2Source == E3S && attribute.Edge2Target == E1T
191      //            || attribute.Edge2Source == E2S && attribute.Edge2Target == E3T
192      //            // if previously deleted Edge3Source-Target is readded
193      //            || attribute.Edge3Source == E3S && attribute.Edge3Target == E1T
194      //            || attribute.Edge3Source == E2S && attribute.Edge3Target == E3T) {
195      //            isTabu = true;
196      //            break;
197      //          }
198      //        }
199      //      }
200      //    }
201      //  }
202      //}
203      MoveTabuParameter.ActualValue = new BoolValue(isTabu);
204      return base.Apply();
205    }
206  }
207}
Note: See TracBrowser for help on using the repository browser.