Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/GroupingVector/Moves/SingleGrouping/ExhaustiveTargetBinSingleGroupingMoveGenerator.cs @ 9440

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

#1966: Implemented new encoding (MultiComponentVector/MCV); Implemented move-operators for MCV and GV encodings; Implemented new decoding-methods for PS, GV and MCV encodings (ExtremePoint-based packing);

File size: 3.3 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;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.PackingEncoding.GroupingVector {
30  [Item("Exhaustive Target Bin Single Grouping Move Generator", "Generates all possible single grouping moves for a specified target bin from a given groupingVector.")]
31  [StorableClass]
32  public class ExhaustiveTargetBinSingleGroupingMoveGenerator : SingleGroupingMoveGenerator, IExhaustiveMoveGenerator {
33    [StorableConstructor]
34    protected ExhaustiveTargetBinSingleGroupingMoveGenerator(bool deserializing) : base(deserializing) { }
35    protected ExhaustiveTargetBinSingleGroupingMoveGenerator(ExhaustiveTargetBinSingleGroupingMoveGenerator original, Cloner cloner) : base(original, cloner) { }
36    public ExhaustiveTargetBinSingleGroupingMoveGenerator() : base() { }
37
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new ExhaustiveTargetBinSingleGroupingMoveGenerator(this, cloner);
40    }
41
42    private static IEnumerable<SingleGroupingMove> Generate(GroupingVectorEncoding groupingVector, int lastBinNr, List<int> indexesOfItemsInLastBin) {
43      for (int i = 0; i < indexesOfItemsInLastBin.Count; i++)
44        for (int j = 0; j < lastBinNr; j++) {
45          yield return new SingleGroupingMove(i, j, groupingVector);
46        }
47    }
48
49    public static SingleGroupingMove[] Apply(GroupingVectorEncoding groupingVector) {
50      int lastBinNr = 0;
51      foreach (int binNr in groupingVector.GroupingVector) {
52        if (binNr > lastBinNr)
53          lastBinNr = binNr;
54      }
55      int nrOfBins = lastBinNr + 1;
56      List<int> indexesOfItemsInLastBin = new List<int>();
57      for (int i = 0; i < groupingVector.GroupingVector.Length; i++ ) {
58        if (groupingVector.GroupingVector[i] == lastBinNr)
59          indexesOfItemsInLastBin.Add(i);
60      }
61
62      int totalMoves = indexesOfItemsInLastBin.Count * (nrOfBins - 1);
63      SingleGroupingMove[] moves = new SingleGroupingMove[totalMoves];
64      int count = 0;
65      foreach (SingleGroupingMove move in Generate(groupingVector, lastBinNr, indexesOfItemsInLastBin)) {
66        moves[count++] = move;
67      }
68      return moves;
69    }
70
71    protected override SingleGroupingMove[] GenerateMoves(GroupingVectorEncoding groupingVector) {
72      return Apply(groupingVector);
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.