1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Xml;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.Random;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.SimOpt {
|
---|
11 | public class TranslocationPermutationAdaptiveManipulator : SimOptManipulationOperatorBase {
|
---|
12 | public override string Description {
|
---|
13 | get { return @"Move a certain number of consecutive elements to a different part in an IntArray or Permutation.
|
---|
14 | Uses a shaking factor to provide an upper bound on the length of consecutive elments moved."; }
|
---|
15 | }
|
---|
16 |
|
---|
17 | public TranslocationPermutationAdaptiveManipulator()
|
---|
18 | : base() {
|
---|
19 | 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));
|
---|
20 | }
|
---|
21 |
|
---|
22 | protected override void Apply(IScope scope, IRandom random, IItem item) {
|
---|
23 | double shakingFactor = GetVariableValue<DoubleData>("ShakingFactor", scope, true).Data;
|
---|
24 | if (item is Permutation.Permutation || item is IntArrayData) {
|
---|
25 | IntArrayData data = (item as IntArrayData);
|
---|
26 | int l = random.Next(1, (int)Math.Max(Math.Min((int)shakingFactor, data.Data.Length - 1), 2));
|
---|
27 | int x = random.Next(data.Data.Length - l);
|
---|
28 | int y;
|
---|
29 | do {
|
---|
30 | y = random.Next(data.Data.Length - l);
|
---|
31 | } while (x == y);
|
---|
32 |
|
---|
33 | int[] h = new int[l];
|
---|
34 | for (int i = 0; i < h.Length; i++)
|
---|
35 | h[i] = data.Data[x + i];
|
---|
36 |
|
---|
37 | if (x > y) {
|
---|
38 | while (x > y) {
|
---|
39 | x--;
|
---|
40 | data.Data[x + l] = data.Data[x];
|
---|
41 | }
|
---|
42 | } else {
|
---|
43 | while (x < y) {
|
---|
44 | data.Data[x] = data.Data[x + l];
|
---|
45 | x++;
|
---|
46 | }
|
---|
47 | }
|
---|
48 | for (int i = 0; i < h.Length; i++)
|
---|
49 | data.Data[y + i] = h[i];
|
---|
50 | } else throw new InvalidOperationException("ERROR: PermutationTranslocationManipulator does not know how to work with " + ((item != null) ? (item.GetType().ToString()) : ("null")) + " data");
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|