source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/PackingSequence/Moves/Insertion/InsertionMove.cs @ 13032

Last change on this file since 13032 was 13032, checked in by gkronber, 6 years ago

#1966:

  • removed unused using
  • added/updated license headers
File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Joseph Helm and 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
22
23using System;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.BinPacking.Interfaces;
29
30namespace HeuristicLab.Encodings.PackingEncoding.PackingSequence {
31  [Item("Insertion Move", "A move on a Sequence vector that is specified by two group-assignment-indexes.")]
32  [StorableClass]
33  public class InsertionMove : SequenceMove, IPackingMove {
34    [Storable]
35    public int Index1 { get; protected set; }
36    [Storable]
37    public int Index2 { get; protected set; }
38
39    [StorableConstructor]
40    protected InsertionMove(bool deserializing) : base(deserializing) { }
41    protected InsertionMove(InsertionMove original, Cloner cloner)
42      : base(original, cloner) {
43      this.Index1 = original.Index1;
44      this.Index2 = original.Index2;
45    }
46    public InsertionMove(int index1, int index2, PackingSequenceEncoding PackingSequence)
47      : base(PackingSequence) {
48      Index1 = index1;
49      Index2 = index2;
50    }
51
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new InsertionMove(this, cloner);
54    }
55
56    public override IPackingSolutionEncoding GetSolutionAfterMove() {
57      PackingSequenceEncoding newSolution = new PackingSequenceEncoding();
58
59      Permutation original = PackingSequence.PackingSequence;
60      Permutation resultingPermutation = (Permutation)PackingSequence.PackingSequence.Clone();
61      int cutIndex, insertIndex, number;
62
63      cutIndex = Index1;
64      insertIndex = Index2;
65      number = original[cutIndex];
66
67      int i = 0;  // index in new permutation
68      int j = 0;  // index in old permutation
69      while (i < PackingSequence.PackingSequence.Length) {
70        if (j == cutIndex) {
71          j++;
72        }
73        if (i == insertIndex) {
74          resultingPermutation[i] = number;
75          i++;
76        }
77        if ((i < original.Length) && (j < original.Length)) {
78          resultingPermutation[i] = original[j];
79          i++;
80          j++;
81        }
82      }
83
84      newSolution.PackingSequence = resultingPermutation;
85      return newSolution;
86    }
87
88    public override Type GetMoveAttributeType() {
89      return typeof(InsertionMoveAttribute);
90    }
91
92    public override SequenceMoveAttribute GetAttribute(double quality) {
93      return new InsertionMoveAttribute(Index1, Index2, PackingSequence.PackingSequence[Index1], PackingSequence.PackingSequence[Index2], quality);
94    }
95
96    public override bool BelongsToAttribute(SequenceMoveAttribute attribute, bool hardCriterion) {
97      var attr = attribute as InsertionMoveAttribute;
98      if (attr != null) {
99        if (hardCriterion) {
100          if (Index1 == attr.Index1 || Index2 == attr.Index2)
101            return true;
102        } else
103          if (Index1 == attr.Index1 && Index2 == attr.Index2)
104            return true;
105      }
106      return false;
107    }
108
109    public override string ToString() {
110      return "IM(i1="+Index1 + ", i2=" + Index2  +")";
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.