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:
1.6 KB
|
Line | |
---|
1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Globalization;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.Xml;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Data;
|
---|
9 | using HeuristicLab.Random;
|
---|
10 |
|
---|
11 | namespace 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.