Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.SimOpt/NormalDoubleManipulator.cs @ 5618

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