Last change
on this file since 584 was
584,
checked in by abeham, 16 years ago
|
merged communication framework to trunk (ticket #279)
|
File size:
1.6 KB
|
Rev | Line | |
---|
[584] | 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 TranslocationPermutationManipulator : 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 | }
|
---|
| 15 |
|
---|
| 16 | public TranslocationPermutationManipulator()
|
---|
| 17 | : base() {
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | protected override void Apply(IScope scope, IRandom random, IItem item) {
|
---|
| 21 | if (item is Permutation.Permutation || item is IntArrayData) {
|
---|
| 22 | IntArrayData data = (item as IntArrayData);
|
---|
| 23 | int l = random.Next(1, data.Data.Length - 2);
|
---|
| 24 | int x = random.Next(data.Data.Length - l);
|
---|
| 25 | int y;
|
---|
| 26 | do {
|
---|
| 27 | y = random.Next(data.Data.Length - l);
|
---|
| 28 | } while (x == y);
|
---|
| 29 |
|
---|
| 30 | int[] h = new int[l];
|
---|
| 31 | for (int i = 0; i < h.Length; i++)
|
---|
| 32 | h[i] = data.Data[x + i];
|
---|
| 33 |
|
---|
| 34 | if (x > y) {
|
---|
| 35 | while (x > y) {
|
---|
| 36 | x--;
|
---|
| 37 | data.Data[x + l] = data.Data[x];
|
---|
| 38 | }
|
---|
| 39 | } else {
|
---|
| 40 | while (x < y) {
|
---|
| 41 | data.Data[x] = data.Data[x + l];
|
---|
| 42 | x++;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | for (int i = 0; i < h.Length; i++)
|
---|
| 46 | data.Data[y + i] = h[i];
|
---|
| 47 | } else throw new InvalidOperationException("ERROR: PermutationTranslocationManipulator does not know how to work with " + ((item != null) ? (item.GetType().ToString()) : ("null")) + " data");
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.