1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Random;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.SimOpt {
|
---|
10 | public class RandomPermutationInitializer : SimOptInitializationOperatorBase {
|
---|
11 | public override string Description {
|
---|
12 | get { return @"Randomly initialize a permutation with values ranging from 0 to n-1"; }
|
---|
13 | }
|
---|
14 |
|
---|
15 | /*private MersenneTwister myRandom;
|
---|
16 | public MersenneTwister Random {
|
---|
17 | get { return myRandom; }
|
---|
18 | set { myRandom = value; }
|
---|
19 | }*/
|
---|
20 |
|
---|
21 | public RandomPermutationInitializer()
|
---|
22 | : base() {
|
---|
23 | //myRandom = new MersenneTwister();
|
---|
24 | }
|
---|
25 |
|
---|
26 | protected override void Apply(IScope scope, IRandom random, IItem item) {
|
---|
27 | if (item is Permutation.Permutation || item is IntArrayData) {
|
---|
28 | IntArrayData data = (item as IntArrayData);
|
---|
29 | IList<int> number = new List<int>(data.Data.Length);
|
---|
30 | for (int i = 0; i < data.Data.Length; i++)
|
---|
31 | number.Add(i);
|
---|
32 | for (int i = 0; i < data.Data.Length; i++) {
|
---|
33 | int index = random.Next(number.Count);
|
---|
34 | data.Data[i] = number[index];
|
---|
35 | number.RemoveAt(index);
|
---|
36 | }
|
---|
37 | } else throw new InvalidOperationException("ERROR: RandomPermutationInitializer does not know how to work with " + ((item != null) ? (item.GetType().ToString()) : ("null")) + " data");
|
---|
38 | }
|
---|
39 |
|
---|
40 | /*public override IView CreateView() {
|
---|
41 | return new RandomPermutationInitializerView(this);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public override void Visit(IntArrayData data) {
|
---|
45 | IList<int> number = new List<int>(data.Data.Length);
|
---|
46 | for (int i = 0; i < data.Data.Length; i++)
|
---|
47 | number.Add(i);
|
---|
48 | for (int i = 0; i < data.Data.Length; i++) {
|
---|
49 | int index = random.Next(number.Count);
|
---|
50 | data.Data[i] = number[index];
|
---|
51 | number.RemoveAt(index);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override void Visit(ObjectData objectData) {
|
---|
56 | if (objectData is Permutation) {
|
---|
57 | Visit(objectData as IntArrayData);
|
---|
58 | } else throw new NotImplementedException();
|
---|
59 | }
|
---|
60 |
|
---|
61 | #region clone & persistence
|
---|
62 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
63 | RandomPermutationInitializer clone = new RandomPermutationInitializer();
|
---|
64 | clonedObjects.Add(Guid, clone);
|
---|
65 | clone.Random = (MersenneTwister)myRandom.Clone(clonedObjects);
|
---|
66 | return clone;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
70 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
71 | XmlNode randomNode = PersistenceManager.Persist("Random", Random, document, persistedObjects);
|
---|
72 | node.AppendChild(randomNode);
|
---|
73 |
|
---|
74 | return node;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
78 | base.Populate(node, restoredObjects);
|
---|
79 | myRandom = (MersenneTwister)PersistenceManager.Restore(node.SelectSingleNode("Random"), restoredObjects);
|
---|
80 | }
|
---|
81 | #endregion*/
|
---|
82 | }
|
---|
83 | }
|
---|