Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/MultiComponentVector/Moves/MultiComponentVectorMoveGenerator.cs @ 13032

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

#1966:

  • removed unused using
  • added/updated license headers
File size: 6.5 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
22using System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.BinPacking.Interfaces;
30
31namespace HeuristicLab.Encodings.PackingEncoding.MultiComponentVector {
32  [Item("MultiComponentVectorMoveGenerator", "Base class for all multi-component vector move generators.")]
33  [StorableClass]
34  public abstract class MultiComponentVectorMoveGenerator : SingleSuccessorOperator, IMultiComponentVectorMoveOperator, IMoveGenerator {
35    public override bool CanChangeName {
36      get { return false; }
37    }
38    public ILookupParameter<MultiComponentVectorEncoding> MultiComponentVectorParameter {
39      get { return (ILookupParameter<MultiComponentVectorEncoding>)Parameters["MultiComponentVector"]; }
40    }
41    public ILookupParameter<IPackingMove> PackingMoveParameter {
42      get { return (LookupParameter<IPackingMove>)Parameters["PackingMove"]; }
43    }
44    protected ScopeParameter CurrentScopeParameter {
45      get { return (ScopeParameter)Parameters["CurrentScope"]; }
46    }               
47
48
49    [StorableConstructor]
50    protected MultiComponentVectorMoveGenerator(bool deserializing) : base(deserializing) { }
51    protected MultiComponentVectorMoveGenerator(MultiComponentVectorMoveGenerator original, Cloner cloner) : base(original, cloner) { }
52    public MultiComponentVectorMoveGenerator()
53      : base() {
54        Parameters.Add(new LookupParameter<MultiComponentVectorEncoding>("MultiComponentVector", "The multi component vector for which moves should be generated."));
55        Parameters.Add(new LookupParameter<IPackingMove>("PackingMove", "The moves that should be generated in subscopes."));
56      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes."));
57    }
58
59
60    public static int NrOfRotationMoves(MultiComponentVectorEncoding mcv) {
61      return mcv.NrOfItems;
62    }
63    public static IEnumerable<MultiComponentVectorMove> GenerateRotationMoves(MultiComponentVectorEncoding multiComponentVector) {
64      for (int binNr = 0; binNr < multiComponentVector.PackingInformations.Count; binNr++)
65        for (int index = 0; index < multiComponentVector.PackingInformations[binNr].Count; index++)
66          yield return new SingleItemRotationMove(binNr, index, multiComponentVector);
67    }
68
69    public static int NrOfSingleGroupingMoves(MultiComponentVectorEncoding mcv) {
70      return mcv.NrOfItems * (mcv.NrOfBins - 1);
71    }
72    public static IEnumerable<MultiComponentVectorMove> GenerateSingleGroupingMoves(MultiComponentVectorEncoding multiComponentVector) {
73      for (int oldBin = 0; oldBin < multiComponentVector.PackingInformations.Count; oldBin++)
74        for (int index = 0; index < multiComponentVector.PackingInformations[oldBin].Count; index++)
75          for (int newBin = 0; newBin < multiComponentVector.NrOfBins; newBin++)
76            if (newBin != oldBin)
77              yield return new SingleGroupingMove(oldBin, index, newBin, multiComponentVector);
78    }
79
80    public static int NrOfSwapPositionMoves(MultiComponentVectorEncoding mcv) {
81      int nrOfSwapPositionMoves = 0;
82      foreach (var groupInformations in mcv.PackingInformations) {
83        nrOfSwapPositionMoves += (groupInformations.Value.Count * (groupInformations.Value.Count - 1)) / 2;
84      }
85      return nrOfSwapPositionMoves;
86    }
87    public static IEnumerable<MultiComponentVectorMove> GenerateSwapPositionMoves(MultiComponentVectorEncoding multiComponentVector) {
88      for (int bin = 0; bin < multiComponentVector.PackingInformations.Count; bin++)
89        for (int oldIndex = 0; oldIndex < multiComponentVector.PackingInformations[bin].Count - 1; oldIndex++)
90          for (int newIndex = oldIndex + 1; newIndex < multiComponentVector.PackingInformations[bin].Count; newIndex++)
91            yield return new SwapPositionMove(bin, oldIndex, newIndex, multiComponentVector);
92    }
93
94    public static int NrOfChangePositionMoves(MultiComponentVectorEncoding mcv) {
95      int nrOfChangePositionMoves = 0;
96      foreach (var groupInformations in mcv.PackingInformations) {
97        nrOfChangePositionMoves += (groupInformations.Value.Count * (groupInformations.Value.Count - 1)) / 2;
98      }
99      return nrOfChangePositionMoves;
100    }
101    public static IEnumerable<MultiComponentVectorMove> GenerateChangePositionMoves(MultiComponentVectorEncoding multiComponentVector) {
102      for (int bin = 0; bin < multiComponentVector.PackingInformations.Count; bin++)
103        for (int oldIndex = 0; oldIndex < multiComponentVector.PackingInformations[bin].Count - 1; oldIndex++)
104          for (int newIndex = oldIndex + 1; newIndex < multiComponentVector.PackingInformations[bin].Count; newIndex++)
105            yield return new ChangePositionMove(bin, oldIndex, newIndex, multiComponentVector);
106    }
107
108    public override IOperation Apply() {
109      MultiComponentVectorEncoding mcv = MultiComponentVectorParameter.ActualValue;
110      MultiComponentVectorMove[] moves = GenerateMoves(mcv);
111      Scope[] moveScopes = new Scope[moves.Length];
112      for (int i = 0; i < moveScopes.Length; i++) {
113        moveScopes[i] = new Scope(i.ToString());
114        moveScopes[i].Variables.Add(new Variable(PackingMoveParameter.ActualName, moves[i]));
115      }
116      CurrentScopeParameter.ActualValue.SubScopes.AddRange(moveScopes);
117      return base.Apply();
118    }
119
120    protected abstract MultiComponentVectorMove[] GenerateMoves(MultiComponentVectorEncoding multiComponentVector);
121  }
122}
Note: See TracBrowser for help on using the repository browser.