source: branches/2936_GQAPIntegration/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Moves/ExhaustiveOneMoveGenerator.cs @ 16077

Last change on this file since 16077 was 16077, checked in by abeham, 12 months ago

#2936: Added integration branch

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Encodings.IntegerVectorEncoding;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Random;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
33  [Item("Exhaustive 1-Move MoveGenerator", "Exhaustively generates all possible 1-moves.")]
34  [StorableClass]
35  public class ExhaustiveOneMoveGenerator : GQAPNMoveGenerator, IStochasticOperator, IExhaustiveMoveGenerator {
36   
37    public ILookupParameter<IRandom> RandomParameter {
38      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
39    }
40
41    [StorableConstructor]
42    protected ExhaustiveOneMoveGenerator(bool deserializing) : base(deserializing) { }
43    protected ExhaustiveOneMoveGenerator(ExhaustiveOneMoveGenerator original, Cloner cloner) : base(original, cloner) { }
44    public ExhaustiveOneMoveGenerator()
45      : base() {
46      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator that should be used."));
47      NParameter.Value.Value = 1;
48      NParameter.Hidden = true;
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new ExhaustiveOneMoveGenerator(this, cloner);
53    }
54
55    public static IEnumerable<NMove> Generate(IntegerVector assignment, GQAPInstance problemInstance) {
56      var equipments = problemInstance.Demands.Length;
57      var locations = problemInstance.Capacities.Length;
58      var tmp = new int[equipments];
59      for (var e = 0; e < equipments; e++) {
60        var indices = new List<int> { e };
61        for (var l = 0; l < locations; l++) {
62          if (assignment[e] == l) continue;
63          var reassign = (int[])tmp.Clone();
64          reassign[e] = l + 1;
65          yield return new NMove(reassign, indices);
66        }
67      }
68    }
69
70    public static IEnumerable<NMove> GenerateAllNxM(GQAPInstance problemInstance) {
71      var equipments = problemInstance.Demands.Length;
72      var locations = problemInstance.Capacities.Length;
73      var tmp = new int[equipments];
74      for (var e = 0; e < equipments; e++) {
75        var indices = new List<int> { e };
76        for (var l = 0; l < locations; l++) {
77          var reassign = (int[])tmp.Clone();
78          reassign[e] = l + 1;
79          yield return new NMove(reassign, indices);
80        }
81      }
82    }
83
84    public override IEnumerable<NMove> GenerateMoves(IntegerVector assignment, int n, GQAPInstance problemInstance) {
85      if (n != 1) throw new ArgumentException("N must be equal to 1 for the exhaustive 1-move generator.");
86      var random = RandomParameter.ActualValue;
87      return Generate(assignment, problemInstance).Shuffle(random);
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.