Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: unified namespaces

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