Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/MultiComponentVector/Moves/ThreeWay/ExhaustiveMCVTripleMoveGenerator.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: 3.6 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.MultiComponentVector {
30  [Item("Exhaustive MultiComponentVector Triple Move Generator", "Generates all possible multi component moves from a given multiComponentVector.")]
31  [StorableClass]
32  public class ExhaustiveMCVTripleMoveGenerator : MultiComponentVectorMoveGenerator, IExhaustiveMoveGenerator {
33    [StorableConstructor]
34    protected ExhaustiveMCVTripleMoveGenerator(bool deserializing) : base(deserializing) { }
35    protected ExhaustiveMCVTripleMoveGenerator(ExhaustiveMCVTripleMoveGenerator original, Cloner cloner) : base(original, cloner) { }
36    public ExhaustiveMCVTripleMoveGenerator() : base() { }
37
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new ExhaustiveMCVTripleMoveGenerator(this, cloner);
40    }
41
42    public static IEnumerable<MultiComponentVectorMove> GenerateMCVTripleMoves(MultiComponentVectorEncoding mcv) {
43      int nrOfBins = 0;
44      foreach (var pi in mcv.PackingInformations) {
45        if (pi.AssignedBin > nrOfBins)
46          nrOfBins = pi.AssignedBin;
47      }
48      nrOfBins++;
49      Dictionary<int, List<int>> indexesPerBin = new Dictionary<int, List<int>>();
50      for (int i = 0; i < mcv.PackingInformations.Count; i++) {
51        int currentBin = mcv.PackingInformations[i].AssignedBin;
52        if (!indexesPerBin.ContainsKey(currentBin))
53          indexesPerBin[currentBin] = new List<int>();
54        indexesPerBin[currentBin].Add(i);
55      }
56                                                       
57      for (int index = 0; index < mcv.PackingInformations.Count; index++) {
58        for (int group = 0; group < nrOfBins; group++) {
59          foreach (int targetIndex in indexesPerBin[group]) {
60            yield return new MCVTripleMove(index, false, group, targetIndex, mcv);
61            yield return new MCVTripleMove(index, true, group, targetIndex, mcv);
62          }
63        }
64      }
65    }
66
67    public static MultiComponentVectorMove[] Apply(MultiComponentVectorEncoding multiComponentVector) {
68      var generatedMoves = new List<MultiComponentVectorMove>(GenerateMCVTripleMoves(multiComponentVector));
69      int totalMoves = generatedMoves.Count;
70      MultiComponentVectorMove[] moves = new MultiComponentVectorMove[totalMoves];
71      int count = 0;
72      foreach (MultiComponentVectorMove move in generatedMoves) {
73        moves[count++] = move;
74      }
75      return moves;
76    } 
77
78    protected override MultiComponentVectorMove[] GenerateMoves(MultiComponentVectorEncoding multiComponentVector) {
79      return Apply(multiComponentVector);
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.