Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/Moves/Swap/Swap2MoveMaker.cs @ 17800

Last change on this file since 17800 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HEAL.Attic;
31
32namespace HeuristicLab.Encodings.LinearLinkageEncoding {
33  [Item("Swap2MoveMaker", "Peforms a swap-2 move on a given grouping and updates the quality.")]
34  [StorableType("2A1F7169-5DD5-4FB6-A25C-BBC4C06BB15A")]
35  public class Swap2MoveMaker : SingleSuccessorOperator, ILinearLinkageSwap2MoveOperator, IMoveMaker, ISingleObjectiveOperator {
36    public override bool CanChangeName {
37      get { return false; }
38    }
39    public ILookupParameter<DoubleValue> QualityParameter {
40      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
41    }
42    public ILookupParameter<DoubleValue> MoveQualityParameter {
43      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
44    }
45    public ILookupParameter<Swap2Move> Swap2MoveParameter {
46      get { return (ILookupParameter<Swap2Move>)Parameters["Swap2Move"]; }
47    }
48    public ILookupParameter<LinearLinkage> LLEParameter {
49      get { return (ILookupParameter<LinearLinkage>)Parameters["LLE"]; }
50    }
51
52    [StorableConstructor]
53    protected Swap2MoveMaker(StorableConstructorFlag _) : base(_) { }
54    protected Swap2MoveMaker(Swap2MoveMaker original, Cloner cloner) : base(original, cloner) { }
55    public Swap2MoveMaker()
56      : base() {
57      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the solution."));
58      Parameters.Add(new LookupParameter<Swap2Move>("Swap2Move", "The move to apply."));
59      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The relative quality of the move."));
60      Parameters.Add(new LookupParameter<LinearLinkage>("LLE", "The linear linkage encoded solution to which the move should be applied."));
61    }
62
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new Swap2MoveMaker(this, cloner);
65    }
66
67    public static void Apply(LinearLinkage lle, Swap2Move move) {
68      var groups = lle.GetGroups().ToList();
69      int g1 = -1, g2 = -1, g1Idx = -1, g2Idx = -1;
70      for (var i = 0; i < groups.Count; i++) {
71        if (g1 < 0) {
72          g1Idx = groups[i].IndexOf(move.Item1);
73          if (g1Idx >= 0) {
74            g1 = i;
75            continue;
76          }
77        }
78        if (g2 < 0) {
79          g2Idx = groups[i].IndexOf(move.Item2);
80          if (g2Idx >= 0) g2 = i;
81        }
82      }
83
84      // can happen if (for some reason) the items belong to the same group
85      if (g1 < 0 || g2 < 0) throw new InvalidOperationException("Swap2MoveMaker: Cannot apply swap move, items are not found in different groups.");
86
87      var h = groups[g1][g1Idx];
88      groups[g1][g1Idx] = groups[g2][g2Idx];
89      groups[g2][g2Idx] = h;
90      lle.SetGroups(groups);
91    }
92
93    public override IOperation Apply() {
94      var move = Swap2MoveParameter.ActualValue;
95      var lle = LLEParameter.ActualValue;
96      var moveQuality = MoveQualityParameter.ActualValue;
97      var quality = QualityParameter.ActualValue;
98
99      Apply(lle, move);
100
101      quality.Value = moveQuality.Value;
102
103      return base.Apply();
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.