1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using System.Xml;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Random;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.SimOpt {
|
---|
32 | public class TranslocationPermutationAdaptiveManipulator : SimOptManipulationOperatorBase {
|
---|
33 | public override string Description {
|
---|
34 | get { return @"Move a certain number of consecutive elements to a different part in an IntArray or Permutation.
|
---|
35 | Uses a shaking factor to provide an upper bound on the length of consecutive elments moved."; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public TranslocationPermutationAdaptiveManipulator()
|
---|
39 | : base() {
|
---|
40 | AddVariableInfo(new VariableInfo("ShakingFactor", "A shaking factor that determines the maximum size of subtours which are to be translocated. The actual value is drawn from a uniform distribution between 1 and this factor (rounded)", typeof(DoubleData), VariableKind.In));
|
---|
41 | }
|
---|
42 |
|
---|
43 | protected override void Apply(IScope scope, IRandom random, IItem item) {
|
---|
44 | double shakingFactor = GetVariableValue<DoubleData>("ShakingFactor", scope, true).Data;
|
---|
45 | if (item is Permutation.Permutation || item is IntArrayData) {
|
---|
46 | IntArrayData data = (item as IntArrayData);
|
---|
47 | int l = random.Next(1, (int)Math.Max(Math.Min((int)shakingFactor, data.Data.Length - 1), 2));
|
---|
48 | int x = random.Next(data.Data.Length - l);
|
---|
49 | int y;
|
---|
50 | do {
|
---|
51 | y = random.Next(data.Data.Length - l);
|
---|
52 | } while (x == y);
|
---|
53 |
|
---|
54 | int[] h = new int[l];
|
---|
55 | for (int i = 0; i < h.Length; i++)
|
---|
56 | h[i] = data.Data[x + i];
|
---|
57 |
|
---|
58 | if (x > y) {
|
---|
59 | while (x > y) {
|
---|
60 | x--;
|
---|
61 | data.Data[x + l] = data.Data[x];
|
---|
62 | }
|
---|
63 | } else {
|
---|
64 | while (x < y) {
|
---|
65 | data.Data[x] = data.Data[x + l];
|
---|
66 | x++;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | for (int i = 0; i < h.Length; i++)
|
---|
70 | data.Data[y + i] = h[i];
|
---|
71 | } else throw new InvalidOperationException("ERROR: PermutationTranslocationManipulator does not know how to work with " + ((item != null) ? (item.GetType().ToString()) : ("null")) + " data");
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|