Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16712 was 16712, checked in by gkronber, 5 years ago

#2936: adapted branch to new persistence (works with HL trunk r16711)

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