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