1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace 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 | [StorableConstructor]
|
---|
48 | protected StochasticInsertionMultiMoveGenerator(bool deserializing) : base(deserializing) { }
|
---|
49 | protected StochasticInsertionMultiMoveGenerator(StochasticInsertionMultiMoveGenerator original, Cloner cloner) : base(original, cloner) { }
|
---|
50 | public StochasticInsertionMultiMoveGenerator() : base() {
|
---|
51 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
52 | Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate."));
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
56 | return new StochasticInsertionMultiMoveGenerator(this, cloner);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static TranslocationMove[] Apply(Permutation permutation, IRandom random, int sampleSize) {
|
---|
60 | int length = permutation.Length;
|
---|
61 | if (length == 1) throw new ArgumentException("ExhaustiveSingleInsertionMoveGenerator: There cannot be an insertion move given a permutation of length 1.", "permutation");
|
---|
62 | TranslocationMove[] moves = new TranslocationMove[sampleSize];
|
---|
63 | int count = 0;
|
---|
64 | HashSet<int> usedIndices = new HashSet<int>();
|
---|
65 | while (count < sampleSize) {
|
---|
66 |
|
---|
67 | int index = random.Next(length);
|
---|
68 |
|
---|
69 | if (usedIndices.Count != length)
|
---|
70 | while (usedIndices.Contains(index))
|
---|
71 | index = random.Next(length);
|
---|
72 | usedIndices.Add(index);
|
---|
73 |
|
---|
74 | if (permutation.PermutationType == PermutationTypes.Absolute) {
|
---|
75 | for (int j = 1; j <= length - 1; j++) {
|
---|
76 | moves[count++] = new TranslocationMove(index, index, (index + j) % length);
|
---|
77 | if (count == sampleSize) break;
|
---|
78 | }
|
---|
79 | } else {
|
---|
80 | if (length > 2) {
|
---|
81 | for (int j = 1; j < length - 1; j++) {
|
---|
82 | int insertPoint = (index + j) % length;
|
---|
83 | if (index + j >= length) insertPoint++;
|
---|
84 | moves[count++] = new TranslocationMove(index, index, insertPoint);
|
---|
85 | if (count == sampleSize) break;
|
---|
86 | }
|
---|
87 | } else { // doesn't make sense, but just create a dummy move to not crash the algorithms
|
---|
88 | moves = new TranslocationMove[1];
|
---|
89 | moves[0] = new TranslocationMove(0, 0, 1);
|
---|
90 | count = sampleSize;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | return moves;
|
---|
95 | }
|
---|
96 |
|
---|
97 | protected override TranslocationMove[] GenerateMoves(Permutation permutation) {
|
---|
98 | IRandom random = RandomParameter.ActualValue;
|
---|
99 | if (random == null) throw new InvalidOperationException(Name + ": No random number generator found.");
|
---|
100 | return Apply(permutation, random, SampleSizeParameter.ActualValue.Value);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|