Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/TwoOpt/PreventReaddTwoOptTabuMoveEvaluator.cs @ 3104

Last change on this file since 3104 was 3104, checked in by abeham, 15 years ago

Fixed changing of MoveTabuParameter and added two different tabu criteria for a move: One prevents only readding deleted edges, the other prevents both readding deleted edges and deleting added edges. #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("PreventReaddTwoOptTabuMoveEvaluator", "Prevents readding of previously deleted edges, but allows deleting previously added edges.")]
32  [StorableClass]
33  public class PreventReaddTwoOptTabuMoveEvaluator : 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 ILookupParameter<ItemList<IItem>> TabuListParameter {
41      get { return (ILookupParameter<ItemList<IItem>>)Parameters["TabuList"]; }
42    }
43    public ILookupParameter<BoolValue> MoveTabuParameter {
44      get { return (ILookupParameter<BoolValue>)Parameters["MoveTabu"]; }
45    }
46    private ScopeParameter CurrentScopeParameter {
47      get { return (ScopeParameter)Parameters["CurrentScope"]; }
48    }
49
50    public PreventReaddTwoOptTabuMoveEvaluator()
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            || attribute.Edge1Source == E1T && attribute.Edge1Target == E2T || attribute.Edge1Source == E2T && attribute.Edge1Target == E1T
75            // if previously deleted Edge2Source-Target is readded
76            || attribute.Edge2Source == E1T && attribute.Edge2Target == E2T || attribute.Edge2Source == E2T && attribute.Edge2Target == E1T
77            || attribute.Edge2Source == E1S && attribute.Edge2Target == E2S || attribute.Edge2Source == E2S && attribute.Edge2Target == E1S) {
78            isTabu = true;
79            break;
80          }
81        }
82      }
83      MoveTabuParameter.ActualValue = new BoolValue(isTabu);
84      return base.Apply();
85    }
86
87    public override bool CanChangeName {
88      get { return false; }
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.