Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.SimOpt/RandomPermutationInitializer.cs @ 9372

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

Put the GPL license in the files from the communication framework and simulation optimization project (branch 3.1)

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Random;
29
30namespace HeuristicLab.SimOpt {
31  public class RandomPermutationInitializer : SimOptInitializationOperatorBase {
32    public override string Description {
33      get { return @"Randomly initialize a permutation with values ranging from 0 to n-1"; }
34    }
35
36    /*private MersenneTwister myRandom;
37    public MersenneTwister Random {
38      get { return myRandom; }
39      set { myRandom = value; }
40    }*/
41
42    public RandomPermutationInitializer()
43      : base() {
44      //myRandom = new MersenneTwister();
45    }
46
47    protected override void Apply(IScope scope, IRandom random, IItem item) {
48      if (item is Permutation.Permutation || item is IntArrayData) {
49        IntArrayData data = (item as IntArrayData);
50        IList<int> number = new List<int>(data.Data.Length);
51        for (int i = 0; i < data.Data.Length; i++)
52          number.Add(i);
53        for (int i = 0; i < data.Data.Length; i++) {
54          int index = random.Next(number.Count);
55          data.Data[i] = number[index];
56          number.RemoveAt(index);
57        }
58      } else throw new InvalidOperationException("ERROR: RandomPermutationInitializer does not know how to work with " + ((item != null) ? (item.GetType().ToString()) : ("null")) + " data");
59    }
60
61    /*public override IView CreateView() {
62      return new RandomPermutationInitializerView(this);
63    }
64
65    public override void Visit(IntArrayData data) {
66      IList<int> number = new List<int>(data.Data.Length);
67      for (int i = 0; i < data.Data.Length; i++)
68        number.Add(i);
69      for (int i = 0; i < data.Data.Length; i++) {
70        int index = random.Next(number.Count);
71        data.Data[i] = number[index];
72        number.RemoveAt(index);
73      }
74    }
75
76    public override void Visit(ObjectData objectData) {
77      if (objectData is Permutation) {
78        Visit(objectData as IntArrayData);
79      } else throw new NotImplementedException();
80    }
81
82    #region clone & persistence
83    public override object Clone(IDictionary<Guid, object> clonedObjects) {
84      RandomPermutationInitializer clone = new RandomPermutationInitializer();
85      clonedObjects.Add(Guid, clone);
86      clone.Random = (MersenneTwister)myRandom.Clone(clonedObjects);
87      return clone;
88    }
89
90    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
91      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
92      XmlNode randomNode = PersistenceManager.Persist("Random", Random, document, persistedObjects);
93      node.AppendChild(randomNode);
94
95      return node;
96    }
97
98    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
99      base.Populate(node, restoredObjects);
100      myRandom = (MersenneTwister)PersistenceManager.Restore(node.SelectSingleNode("Random"), restoredObjects);
101    }
102    #endregion*/
103  }
104}
Note: See TracBrowser for help on using the repository browser.