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

Last change on this file since 14128 was 14128, checked in by gkronber, 3 years ago

#1966: refactoring of bin packing implementation

File size: 3.7 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;
29
30
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 {
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, Permutation 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 Permutation GetSolutionAfterMove() {
58      Permutation original = PackingSequence;
59      Permutation resultingPermutation = (Permutation)PackingSequence.Clone();
60      int cutIndex, insertIndex, number;
61
62      cutIndex = Index1;
63      insertIndex = Index2;
64      number = original[cutIndex];
65
66      int i = 0;  // index in new permutation
67      int j = 0;  // index in old permutation
68      while (i < PackingSequence.Length) {
69        if (j == cutIndex) {
70          j++;
71        }
72        if (i == insertIndex) {
73          resultingPermutation[i] = number;
74          i++;
75        }
76        if ((i < original.Length) && (j < original.Length)) {
77          resultingPermutation[i] = original[j];
78          i++;
79          j++;
80        }
81      }
82
83      return resultingPermutation;
84    }
85
86    public override Type GetMoveAttributeType() {
87      return typeof(InsertionMoveAttribute);
88    }
89
90    public override SequenceMoveAttribute GetAttribute(double quality) {
91      return new InsertionMoveAttribute(Index1, Index2, PackingSequence[Index1], PackingSequence[Index2], quality);
92    }
93
94    public override bool BelongsToAttribute(SequenceMoveAttribute attribute, bool hardCriterion) {
95      var attr = attribute as InsertionMoveAttribute;
96      if (attr != null) {
97        if (hardCriterion) {
98          if (Index1 == attr.Index1 || Index2 == attr.Index2)
99            return true;
100        } else
101          if (Index1 == attr.Index1 && Index2 == attr.Index2)
102            return true;
103      }
104      return false;
105    }
106
107    public override string ToString() {
108      return "IM(i1=" + Index1 + ", i2=" + Index2 + ")";
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.