Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.SimOpt/3.2/SimOptSelfAdaptiveNumericVectorNormalMutation.cs @ 4483

Last change on this file since 4483 was 1791, checked in by abeham, 16 years ago

removed annoying quit after x tries

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2009 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;
26
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Random;
30
31namespace HeuristicLab.SimOpt {
32  public class SimOptSelfAdaptiveNumericVectorNormalMutation : OperatorBase {
33
34    public override string Description {
35      get { return @"This operator modifies all elements in the parameter vector using a normal distributed variable with mean 0 and variable sigma"; }
36    }
37
38    public SimOptSelfAdaptiveNumericVectorNormalMutation()
39      : base() {
40      AddVariableInfo(new VariableInfo("Random", "The random number generator", typeof(IRandom), VariableKind.In));
41      AddVariableInfo(new VariableInfo("ShakingFactors", "The mutation strength vector", typeof(DoubleArrayData), VariableKind.In));
42      AddVariableInfo(new VariableInfo("Items", "The parameter vector", typeof(ConstrainedItemList), VariableKind.In | VariableKind.Out));
43    }
44
45    public override IOperation Apply(IScope scope) {
46      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
47      DoubleArrayData shakingFactors = GetVariableValue<DoubleArrayData>("ShakingFactors", scope, false);
48
49      ConstrainedItemList parameters = GetVariableValue<ConstrainedItemList>("Items", scope, false);
50      ConstrainedItemList temp = null;
51      ICollection<IConstraint> tmp;
52
53      NormalDistributedRandom nd = new NormalDistributedRandom(random, 0.0, 1.0);
54
55      do {
56        temp = (ConstrainedItemList)parameters.Clone();
57
58        temp.BeginCombinedOperation();
59        for (int i = 0; i < temp.Count; i++) {
60          if (random.NextDouble() < shakingFactors.Data[i % shakingFactors.Data.Length]) {
61            if (((Variable)temp[i]).Value is IntData) {
62              ((IntData)((Variable)temp[i]).Value).Data += ((int)(nd.NextDouble() * shakingFactors.Data[i % shakingFactors.Data.Length]));
63            } else if (((Variable)temp[i]).Value is DoubleData) {
64              ((DoubleData)((Variable)temp[i]).Value).Data += nd.NextDouble() * shakingFactors.Data[i % shakingFactors.Data.Length];
65            } else if (((Variable)temp[i]).Value is ConstrainedIntData) {
66              int val = ((ConstrainedIntData)((Variable)temp[i]).Value).Data;
67              ((ConstrainedIntData)((Variable)temp[i]).Value).TrySetData(val + ((int)(nd.NextDouble() * shakingFactors.Data[i % shakingFactors.Data.Length])));
68            } else if (((Variable)temp[i]).Value is ConstrainedDoubleData) {
69              double val = ((ConstrainedDoubleData)((Variable)temp[i]).Value).Data;
70              ((ConstrainedDoubleData)((Variable)temp[i]).Value).TrySetData(val + nd.NextDouble() * shakingFactors.Data[i % shakingFactors.Data.Length]);
71            }
72          }
73        }
74      } while (!temp.EndCombinedOperation(out tmp));
75
76      parameters.BeginCombinedOperation();
77      for (int i = 0; i < temp.Count; i++)
78        parameters.TrySetAt(i, temp[i], out tmp);
79      parameters.EndCombinedOperation(out tmp);
80
81      return null;
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.