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

Last change on this file since 14046 was 14046, checked in by gkronber, 5 years ago

#1966: unified namespaces

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