Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/Scramble/StochasticScrambleMultiMoveGenerator.cs @ 14185

Last change on this file since 14185 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.PermutationEncoding {
32  [Item("StochasticScrambleMultiMoveGenerator", "Randomly samples n from all possible scramble moves from a given permutation.")]
33  [StorableClass]
34  public class StochasticScrambleMultiMoveGenerator : ScrambleMoveGenerator, 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 StochasticScrambleMultiMoveGenerator(bool deserializing) : base(deserializing) { }
49    protected StochasticScrambleMultiMoveGenerator(StochasticScrambleMultiMoveGenerator original, Cloner cloner) : base(original, cloner) { }
50    public StochasticScrambleMultiMoveGenerator()
51      : base() {
52      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
53      Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate."));
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new StochasticScrambleMultiMoveGenerator(this, cloner);
58    }
59
60    public static ScrambleMove GenerateRandomMove(Permutation permutation, IRandom random) {
61      int breakPoint1, breakPoint2;
62      int[] scrambledIndices;
63
64      breakPoint1 = random.Next(permutation.Length);
65      do {
66        breakPoint2 = random.Next(permutation.Length);
67      } while (Math.Abs(breakPoint2 - breakPoint1) <= 1);
68      if (breakPoint2 < breakPoint1) { int h = breakPoint1; breakPoint1 = breakPoint2; breakPoint2 = h; }
69
70      scrambledIndices = new int[breakPoint2 - breakPoint1 + 1];
71      for (int i = 0; i < scrambledIndices.Length; i++)
72        scrambledIndices[i] = i;
73      bool[] moved = new bool[scrambledIndices.Length];
74      bool changed = false;
75      do {
76        for (int i = scrambledIndices.Length - 1; i > 0; i--) {
77          int j = random.Next(i + 1);
78          int t = scrambledIndices[j];
79          scrambledIndices[j] = scrambledIndices[i];
80          scrambledIndices[i] = t;
81          if (scrambledIndices[j] == j) moved[j] = false;
82          else moved[j] = true;
83          if (scrambledIndices[i] == i) moved[i] = false;
84          else moved[i] = true;
85        }
86        changed = moved.Any(x => x);
87      } while (!changed);
88
89      return new ScrambleMove(breakPoint1, scrambledIndices);
90    }
91
92    public static ScrambleMove[] Apply(Permutation permutation, IRandom random, int sampleSize) {
93      int length = permutation.Length;
94      ScrambleMove[] moves = new ScrambleMove[sampleSize];
95      for (int i = 0; i < sampleSize; i++) {
96        moves[i] = GenerateRandomMove(permutation, random);
97      }
98      return moves;
99    }
100
101    protected override ScrambleMove[] GenerateMoves(Permutation permutation) {
102      IRandom random = RandomParameter.ActualValue;
103      return Apply(permutation, random, SampleSizeParameter.ActualValue.Value);
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.