Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.Permutation/3.3/Moves/TwoOptMoveTabuEvaluator.cs @ 3026

Last change on this file since 3026 was 3017, checked in by epitzer, 15 years ago

Merge StorableClassType.Empty into StorableClassType.MarkedOnly and make it the default if not specified (#548)

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