Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.SimOpt/NormalIntManipulator.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.6 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 NormalIntManipulator : SimOptManipulationOperatorBase {
13    public override string Description {
14      get { return @"Perturbs an IntData or ConstrainedIntData by a value normally distributed around 0"; }
15    }
16
17    public NormalIntManipulator()
18      : base() {
19      AddVariableInfo(new VariableInfo("ShakingFactor", "Strength of the perturbation", typeof(DoubleData), VariableKind.In));
20    }
21
22    protected override void Apply(IScope scope, IRandom random, IItem item) {
23      double shakingFactor = GetVariableValue<DoubleData>("ShakingFactor", scope, true).Data;
24      NormalDistributedRandom normal = new NormalDistributedRandom(random, 0.0, shakingFactor);
25      if (item is IntData) {
26        ((IntData)item).Data += (int)normal.NextDouble();
27        return;
28      } else if (item is ConstrainedIntData) {
29        ConstrainedIntData data = (item as ConstrainedIntData);
30        for (int tries = 100; tries >= 0; tries--) {
31          int newValue = data.Data + (int)normal.NextDouble();
32          if (data.TrySetData(newValue)) return;
33        }
34        throw new InvalidProgramException("Coudn't find a valid value in 100 tries");
35      } else throw new InvalidOperationException("ERROR: UniformIntManipulator does not know how to work with " + ((item != null) ? (item.GetType().ToString()) : ("null")) + " data");
36    }
37  }
38}
Note: See TracBrowser for help on using the repository browser.