Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.SimOpt/TranslocationPermutationAdaptiveManipulator.cs @ 583

Last change on this file since 583 was 583, checked in by abeham, 16 years ago

Adding communication framework to branch 3.1 (ticket #278)

File size: 2.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Xml;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using HeuristicLab.Random;
9
10namespace 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.
14Uses 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}
Note: See TracBrowser for help on using the repository browser.