Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/TwoOpt/TwoOptTabuMoveEvaluator.cs @ 3098

Last change on this file since 3098 was 3074, checked in by abeham, 14 years ago

updated tabu search #840

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.PermutationEncoding {
31  [Item("TwoOptTabuMoveEvaluator", "Evaluates whether a given 2-opt move is tabu.")]
32  [StorableClass]
33  public class TwoOptTabuMoveEvaluator : SingleSuccessorOperator, ITwoOptPermutationMoveOperator, ITabuMoveEvaluator {
34    public ILookupParameter<TwoOptMove> TwoOptMoveParameter {
35      get { return (LookupParameter<TwoOptMove>)Parameters["TwoOptMove"]; }
36    }
37    public ILookupParameter<Permutation> PermutationParameter {
38      get { return (LookupParameter<Permutation>)Parameters["Permutation"]; }
39    }
40    public LookupParameter<ItemList<IItem>> TabuListParameter {
41      get { return (LookupParameter<ItemList<IItem>>)Parameters["TabuList"]; }
42    }
43    public LookupParameter<BoolValue> MoveTabuParameter {
44      get { return (LookupParameter<BoolValue>)Parameters["MoveTabu"]; }
45    }
46    private ScopeParameter CurrentScopeParameter {
47      get { return (ScopeParameter)Parameters["CurrentScope"]; }
48    }
49
50    public TwoOptTabuMoveEvaluator()
51      : base() {
52      Parameters.Add(new LookupParameter<TwoOptMove>("TwoOptMove", "The move to evaluate."));
53      Parameters.Add(new LookupParameter<BoolValue>("MoveTabu", "The variable to store if a move was tabu."));
54      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
55      Parameters.Add(new LookupParameter<ItemList<IItem>>("TabuList", "The tabu list."));
56      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope."));
57    }
58
59    public override IOperation Apply() {
60      ItemList<IItem> tabuList = TabuListParameter.ActualValue;
61      TwoOptMove move = TwoOptMoveParameter.ActualValue;
62      Permutation permutation = PermutationParameter.ActualValue;
63      int length = permutation.Length;
64      int E1S = permutation.GetCircular(move.Index1 - 1);
65      int E1T = permutation[move.Index1];
66      int E2S = permutation[move.Index2];
67      int E2T = permutation.GetCircular(move.Index2 + 1);
68      bool isTabu = false;
69      foreach (IItem tabuMove in tabuList) {
70        TwoOptTabuMoveAttribute attribute = (tabuMove as TwoOptTabuMoveAttribute);
71        if (attribute != null) {
72          // if previously deleted Edge1Source-Target is readded
73          if (attribute.Edge1Source == E1S && attribute.Edge1Target == E2S || attribute.Edge1Source == E2S && attribute.Edge1Target == E1S
74            // if previously deleted Edge2Source-Target is readded
75            || attribute.Edge2Source == E1T && attribute.Edge2Target == E2T || attribute.Edge2Source == E2T && attribute.Edge2Target == E1T
76            // if previously added Edge1Source-Edge2Source is deleted
77            || attribute.Edge1Source == E1S && attribute.Edge2Source == E1T || attribute.Edge1Source == E1T && attribute.Edge2Source == E1S
78            // if previously added Edge1Target-Edge2Target is deleted
79            || attribute.Edge1Target == E2S && attribute.Edge2Target == E2T || attribute.Edge1Target == E2T && attribute.Edge2Target == E2S) {
80            isTabu = true;
81            break;
82          }
83        }
84      }
85      MoveTabuParameter.ActualValue = new BoolValue(isTabu);
86      return base.Apply();
87    }
88
89    public override bool CanChangeName {
90      get { return false; }
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.