Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/29/17 23:56:43 (7 years ago)
Author:
abeham
Message:

#1614: added additional algorithms

File:
1 copied

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Moves/ExhaustiveOneMoveGenerator.cs

    r15519 r15562  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    25 using HeuristicLab.Data;
    2626using HeuristicLab.Encodings.IntegerVectorEncoding;
    2727using HeuristicLab.Optimization;
    2828using HeuristicLab.Parameters;
    2929using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     30using HeuristicLab.Random;
    3031
    3132namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
    32   [Item("Stochastic N-Move MultiMoveGenerator", "Randomly samples a number of N-Moves.")]
     33  [Item("Exhaustive 1-Move MoveGenerator", "Exhaustively generates all possible 1-moves.")]
    3334  [StorableClass]
    34   public class StochasticNMoveMultiMoveGenerator : GQAPNMoveGenerator, IStochasticOperator, IMultiMoveGenerator {
     35  public class ExhaustiveOneMoveGenerator : GQAPNMoveGenerator, IStochasticOperator, IExhaustiveMoveGenerator {
    3536   
    3637    public ILookupParameter<IRandom> RandomParameter {
    3738      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    3839    }
    39     public IValueLookupParameter<IntValue> SampleSizeParameter {
    40       get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
    41     }
    4240
    4341    [StorableConstructor]
    44     protected StochasticNMoveMultiMoveGenerator(bool deserializing) : base(deserializing) { }
    45     protected StochasticNMoveMultiMoveGenerator(StochasticNMoveMultiMoveGenerator original, Cloner cloner) : base(original, cloner) { }
    46     public StochasticNMoveMultiMoveGenerator()
     42    protected ExhaustiveOneMoveGenerator(bool deserializing) : base(deserializing) { }
     43    protected ExhaustiveOneMoveGenerator(ExhaustiveOneMoveGenerator original, Cloner cloner) : base(original, cloner) { }
     44    public ExhaustiveOneMoveGenerator()
    4745      : base() {
    4846      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator that should be used."));
    49       Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate."));
     47      NParameter.Value.Value = 1;
     48      NParameter.Hidden = true;
    5049    }
    5150
    5251    public override IDeepCloneable Clone(Cloner cloner) {
    53       return new StochasticNMoveMultiMoveGenerator(this, cloner);
     52      return new ExhaustiveOneMoveGenerator(this, cloner);
    5453    }
    5554
    56     public static IEnumerable<NMove> Generate(IRandom random, IntegerVector assignment, int n, GQAPInstance problemInstance, int sampleSize) {
    57       for (int i = 0; i < sampleSize; i++)
    58         yield return StochasticNMoveSingleMoveGenerator.GenerateUpToN(random, assignment, n, problemInstance.Capacities);
     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      }
    5982    }
    6083
    6184    public override IEnumerable<NMove> GenerateMoves(IntegerVector assignment, int n, GQAPInstance problemInstance) {
    62       return Generate(RandomParameter.ActualValue, assignment, n, problemInstance, SampleSizeParameter.ActualValue.Value);
     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);
    6388    }
    6489  }
Note: See TracChangeset for help on using the changeset viewer.