Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.SimOpt/Swap2PermutationManipulator.cs @ 588

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

Adding communication framework to branch 3.1 (ticket #278)

File size: 1.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7
8namespace HeuristicLab.SimOpt {
9  public class Swap2PermutationManipulator : SimOptManipulationOperatorBase {
10    public override string Description {
11      get { return @"Swap two elements of a permutation or IntArray"; }
12    }
13
14    public Swap2PermutationManipulator()
15      : base() {
16    }
17
18    protected override void Apply(IScope scope, IRandom random, IItem item) {
19      if (item is Permutation.Permutation || item is IntArrayData) {
20        IntArrayData data = (item as IntArrayData);
21        int x = random.Next(data.Data.Length);
22        int y;
23        do {
24          y = random.Next(data.Data.Length);
25        } while (x == y);
26
27        int h = data.Data[x];
28        data.Data[x] = data.Data[y];
29        data.Data[y] = h;
30      } else throw new InvalidOperationException("ERROR: Swap2PermutationManipulator does not know how to work with " + ((item != null) ? (item.GetType().ToString()) : ("null")) + " data");
31    }
32  }
33}
Note: See TracBrowser for help on using the repository browser.