Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/ThreeOpt/StochasticSingleInsertionMoveGenerator.cs @ 4046

Last change on this file since 4046 was 4046, checked in by abeham, 14 years ago

#1083

  • Added a stochastic move generator for insertion moves
File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using System.Collections.Generic;
30
31namespace HeuristicLab.Encodings.PermutationEncoding {
32  [Item("StochasticInsertionMultiMoveGenerator", "Generates all possible insertion moves (3-opt) from a few numbers in a given permutation.")]
33  [StorableClass]
34  public class StochasticInsertionMultiMoveGenerator : TranslocationMoveGenerator, IMultiMoveGenerator, IStochasticOperator {
35    public ILookupParameter<IRandom> RandomParameter {
36      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
37    }
38    public IValueLookupParameter<IntValue> SampleSizeParameter {
39      get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
40    }
41
42    public IntValue SampleSize {
43      get { return SampleSizeParameter.Value; }
44      set { SampleSizeParameter.Value = value; }
45    }
46
47    public StochasticInsertionMultiMoveGenerator() {
48      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
49      Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate."));
50    }
51
52    public static TranslocationMove[] Apply(Permutation permutation, IRandom random, int sampleSize) {
53      int length = permutation.Length;
54      if (length == 1) throw new ArgumentException("ExhaustiveSingleInsertionMoveGenerator: There cannot be an insertion move given a permutation of length 1.", "permutation");
55      TranslocationMove[] moves = new TranslocationMove[sampleSize];
56      int count = 0;
57      HashSet<int> usedIndices = new HashSet<int>();
58      while (count < sampleSize) {
59       
60        int index = random.Next(length);
61       
62        if (usedIndices.Count != length)
63          while (usedIndices.Contains(index))
64            index = random.Next(length);
65        usedIndices.Add(index);
66
67        if (permutation.PermutationType == PermutationTypes.Absolute) {
68          for (int j = 1; j <= length - 1; j++) {
69            moves[count++] = new TranslocationMove(index, index, (index + j) % length);
70            if (count == sampleSize) break;
71          }
72        } else {
73          if (length > 2) {
74            for (int j = 1; j < length - 1; j++) {
75              int insertPoint = (index + j) % length;
76              if (index + j >= length) insertPoint++;
77              moves[count++] = new TranslocationMove(index, index, insertPoint);
78              if (count == sampleSize) break;
79            }
80          } else { // doesn't make sense, but just create a dummy move to not crash the algorithms
81            moves = new TranslocationMove[1];
82            moves[0] = new TranslocationMove(0, 0, 1);
83            count = sampleSize;
84          }
85        }
86      }
87      return moves;
88    }
89
90    protected override TranslocationMove[] GenerateMoves(Permutation permutation) {
91      IRandom random = RandomParameter.ActualValue;
92      if (random == null) throw new InvalidOperationException(Name + ": No random number generator found.");
93      return Apply(permutation, random, SampleSizeParameter.ActualValue.Value);
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.