Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9473 was 9473, checked in by jhelm, 11 years ago

#1966: Fixed some problems in MCV-move operators; Added parts of potvin-encoding implementation;

File size: 7.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Decoders;
30using HeuristicLab.Problems.BinPacking.Interfaces;
31using HeuristicLab.Problems.BinPacking.PackingBin;
32using HeuristicLab.Problems.BinPacking.PackingItem;
33
34namespace HeuristicLab.Encodings.PackingEncoding.MultiComponentVector {
35  [Item("MultiComponentVectorMoveGenerator", "Base class for all multi-component vector move generators.")]
36  [StorableClass]
37  public abstract class MultiComponentVectorMoveGenerator : SingleSuccessorOperator, IMultiComponentVectorMoveOperator, IMoveGenerator {
38    public override bool CanChangeName {
39      get { return false; }
40    }
41    public ILookupParameter<MultiComponentVectorEncoding> MultiComponentVectorParameter {
42      get { return (ILookupParameter<MultiComponentVectorEncoding>)Parameters["MultiComponentVector"]; }
43    }
44    public ILookupParameter<MultiComponentVectorMove> MultiComponentVectorMoveParameter {
45      get { return (LookupParameter<MultiComponentVectorMove>)Parameters["MultiComponentVectorMove"]; }
46    }
47    protected ScopeParameter CurrentScopeParameter {
48      get { return (ScopeParameter)Parameters["CurrentScope"]; }
49    }               
50
51
52    [StorableConstructor]
53    protected MultiComponentVectorMoveGenerator(bool deserializing) : base(deserializing) { }
54    protected MultiComponentVectorMoveGenerator(MultiComponentVectorMoveGenerator original, Cloner cloner) : base(original, cloner) { }
55    public MultiComponentVectorMoveGenerator()
56      : base() {
57        Parameters.Add(new LookupParameter<MultiComponentVectorEncoding>("MultiComponentVector", "The multi component vector for which moves should be generated."));
58      Parameters.Add(new LookupParameter<MultiComponentVectorMove>("MultiComponentVectorMove", "The moves that should be generated in subscopes."));
59      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes."));
60    }
61
62
63    public static int NrOfRotationMoves(MultiComponentVectorEncoding mcv) {
64      return mcv.PackingInformations.Count;
65    }
66    public static IEnumerable<MultiComponentVectorMove> GenerateRotationMoves(MultiComponentVectorEncoding multiComponentVector) {
67      for (int i = 0; i < multiComponentVector.PackingInformations.Count; i++)
68        yield return new SingleItemRotationMove(i, multiComponentVector);
69    }
70
71    public static int NrOfSingleGroupingMoves(MultiComponentVectorEncoding mcv) {
72      int nrOfBins = 0;
73      foreach (var pi in mcv.PackingInformations) {
74        if (pi.AssignedBin > nrOfBins)
75          nrOfBins = pi.AssignedBin;
76      }
77      nrOfBins++;
78      return mcv.PackingInformations.Count * (nrOfBins - 1);
79    }
80    public static IEnumerable<MultiComponentVectorMove> GenerateSingleGroupingMoves(MultiComponentVectorEncoding multiComponentVector) {
81      int nrOfBins = 0;
82      foreach (var pi in multiComponentVector.PackingInformations) {
83        if (pi.AssignedBin > nrOfBins)
84          nrOfBins = pi.AssignedBin;
85      }
86      nrOfBins++;
87      for (int i = 0; i < multiComponentVector.PackingInformations.Count; i++)
88        for (int j = 0; j < nrOfBins; j++) {
89          if (j != multiComponentVector.PackingInformations[i].AssignedBin)
90            yield return new SingleGroupingMove(i, j, multiComponentVector);
91        }
92    }
93
94    public static int NrOfSwapPositionMoves(MultiComponentVectorEncoding mcv) {
95      int nrOfBins = 0;
96      foreach (var pi in mcv.PackingInformations) {
97        if (pi.AssignedBin > nrOfBins)
98          nrOfBins = pi.AssignedBin;
99      }
100      nrOfBins++;
101      int[] itemsPerBin = new int[nrOfBins];
102      foreach (var pi in mcv.PackingInformations) {
103        itemsPerBin[pi.AssignedBin]++;
104      }
105      int nrOfSwapPositionMoves = 0;
106      foreach (int items in itemsPerBin) {
107        nrOfSwapPositionMoves += (items * (items - 1)) / 2;
108      }
109      return nrOfSwapPositionMoves;
110    }
111    public static IEnumerable<MultiComponentVectorMove> GenerateSwapPositionMoves(MultiComponentVectorEncoding multiComponentVector) {
112      Dictionary<int, List<int>> indexesPerBin = new Dictionary<int, List<int>>();
113      for (int i = 0; i < multiComponentVector.PackingInformations.Count; i++) {
114        int currentBin = multiComponentVector.PackingInformations[i].AssignedBin;
115        if (!indexesPerBin.ContainsKey(currentBin))
116          indexesPerBin[currentBin] = new List<int>();
117        indexesPerBin[currentBin].Add(i);
118      }
119      foreach (var entry in indexesPerBin) {
120        for (int i = 0; i < entry.Value.Count - 1; i++)
121          for (int j = i + 1; j < entry.Value.Count; j++)
122            yield return new SwapPositionMove(entry.Value[i], entry.Value[j], multiComponentVector);
123      }
124    }
125
126    public static int NrOfChangePositionMoves(MultiComponentVectorEncoding mcv) {
127      int nrOfBins = 0;
128      foreach (var pi in mcv.PackingInformations) {
129        if (pi.AssignedBin > nrOfBins)
130          nrOfBins = pi.AssignedBin;
131      }
132      nrOfBins++;
133      int[] itemsPerBin = new int[nrOfBins];
134      foreach (var pi in mcv.PackingInformations) {
135        itemsPerBin[pi.AssignedBin]++;
136      }
137      int nrOfSwapPositionMoves = 0;
138      foreach (int items in itemsPerBin) {
139        nrOfSwapPositionMoves += (items * (items - 1)) / 2;
140      }
141      return nrOfSwapPositionMoves;
142    }
143    public static IEnumerable<MultiComponentVectorMove> GenerateChangePositionMoves(MultiComponentVectorEncoding multiComponentVector) {
144      Dictionary<int, List<int>> indexesPerBin = new Dictionary<int, List<int>>();
145      for (int i = 0; i < multiComponentVector.PackingInformations.Count; i++) {
146        int currentBin = multiComponentVector.PackingInformations[i].AssignedBin;
147        if (!indexesPerBin.ContainsKey(currentBin))
148          indexesPerBin[currentBin] = new List<int>();
149        indexesPerBin[currentBin].Add(i);
150      }
151      foreach (var entry in indexesPerBin) {
152        for (int i = 0; i < entry.Value.Count - 1; i++)
153          for (int j = i + 1; j < entry.Value.Count; j++)
154            yield return new ChangePositionMove(entry.Value[i], entry.Value[j], multiComponentVector);
155      }
156    }
157
158    public override IOperation Apply() {
159      MultiComponentVectorEncoding mcv = MultiComponentVectorParameter.ActualValue;
160      MultiComponentVectorMove[] moves = GenerateMoves(mcv);
161      Scope[] moveScopes = new Scope[moves.Length];
162      for (int i = 0; i < moveScopes.Length; i++) {
163        moveScopes[i] = new Scope(i.ToString());
164        moveScopes[i].Variables.Add(new Variable(MultiComponentVectorMoveParameter.ActualName, moves[i]));
165      }
166      CurrentScopeParameter.ActualValue.SubScopes.AddRange(moveScopes);
167      return base.Apply();
168    }
169
170    protected abstract MultiComponentVectorMove[] GenerateMoves(MultiComponentVectorEncoding multiComponentVector);
171  }
172}
Note: See TracBrowser for help on using the repository browser.