Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SimOpt/NormalDoubleManipulator.cs @ 584

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: 4.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5using System.Text;
6using System.Xml;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Random;
10
11namespace HeuristicLab.SimOpt {
12  public class NormalDoubleManipulator : SimOptManipulationOperatorBase {
13    public override string Description {
14      get { return @"Perturbs a DoubleData or ConstrainedDoubleData by a value normal distributed around 0"; }
15    }
16    /*private NormalDistributedRandom myRandom;
17    public NormalDistributedRandom Random {
18      get { return myRandom; }
19      set { myRandom = value; }
20    }
21
22    private double myShakingFactor;
23    public double ShakingFactor {
24      get { return myShakingFactor; }
25      set { myShakingFactor = value; }
26    }*/
27
28    public NormalDoubleManipulator()
29      : base() {
30      AddVariableInfo(new VariableInfo("ShakingFactor", "", typeof(DoubleData), VariableKind.In));
31      /*myRandom = new NormalDistributedRandom(new MersenneTwister(), 0.0, 1.0);
32      myShakingFactor = 1.0;*/
33    }
34
35    /*public override IView CreateView() {
36      return new NormalDoubleManipulatorView(this);
37    }*/
38
39    protected override void Apply(IScope scope, IRandom random, IItem item) {
40      double shakingFactor = GetVariableValue<DoubleData>("ShakingFactor", scope, true).Data;
41      NormalDistributedRandom normal = new NormalDistributedRandom(random, 0.0, shakingFactor);
42      if (item is DoubleData) {
43        ((DoubleData)item).Data += normal.NextDouble();
44        return;
45      } else if (item is ConstrainedDoubleData) {
46        ConstrainedDoubleData data = (item as ConstrainedDoubleData);
47        for (int tries = 100; tries >= 0; tries--) {
48          double newValue = data.Data + normal.NextDouble();
49
50          if (IsIntegerConstrained(data)) {
51            newValue = Math.Round(newValue);
52          }
53          if (data.TrySetData(newValue)) {
54            return;
55          }
56        }
57        throw new InvalidProgramException("Coudn't find a valid value in 100 tries");
58      } else throw new InvalidOperationException("ERROR: NormalDoubleManipulator does not know how to work with " + ((item != null) ? (item.GetType().ToString()) : ("null")) + " data");
59    }
60
61    /*public override void Visit(DoubleData data) {
62      data.Data += (myRandom.NextDouble() dat* myShakingFactor);
63    }
64
65    public override void Visit(ConstrainedDoubleData data) {
66      for (int tries = 100; tries >= 0; tries--) {
67        double newValue = data.Data + myRandom.NextDouble() * myShakingFactor;
68
69        if (IsIntegerConstrained(data)) {
70          newValue = Math.Round(newValue);
71        }
72        if (data.TrySetData(newValue)) {
73          return;
74        }
75      }
76
77      throw new InvalidProgramException("Coudn't find a valid value in 100 tries");
78    }
79
80    #region clone & persistence
81    public override object Clone(IDictionary<Guid, object> clonedObjects) {
82      NormalDoubleManipulator clone = new NormalDoubleManipulator();
83      clonedObjects.Add(Guid, clone);
84      clone.Random = (NormalDistributedRandom)myRandom.Clone(clonedObjects);
85      clone.ShakingFactor = myShakingFactor;
86      return clone;
87    }
88
89    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
90      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
91      XmlNode randomNode = PersistenceManager.Persist("Random", Random, document, persistedObjects);
92      node.AppendChild(randomNode);
93
94      XmlNode shakingFactorNode = document.CreateNode(XmlNodeType.Element, "ShakingFactor", null);
95      shakingFactorNode.InnerText = myShakingFactor.ToString(CultureInfo.InvariantCulture);
96      node.AppendChild(shakingFactorNode);
97
98      return node;
99    }
100
101    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
102      base.Populate(node, restoredObjects);
103      myRandom = (NormalDistributedRandom)PersistenceManager.Restore(node.SelectSingleNode("Random"), restoredObjects);
104      myShakingFactor = double.Parse(node.SelectSingleNode("ShakingFactor").InnerText, CultureInfo.InvariantCulture);
105    }
106    #endregion*/
107  }
108}
Note: See TracBrowser for help on using the repository browser.