Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SimOpt/SimOptSelfAdaptiveNumericVectorNormalMutation.cs @ 1507

Last change on this file since 1507 was 1413, checked in by abeham, 16 years ago

added some manipulation operators (#540)

File size: 4.1 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      int tries;
51      ConstrainedItemList temp = null;
52      ICollection<IConstraint> tmp;
53
54      NormalDistributedRandom nd = new NormalDistributedRandom(random, 0.0, 1.0);
55
56      for (tries = 0; tries < 10000; tries++) {
57        temp = (ConstrainedItemList)parameters.Clone();
58
59        temp.BeginCombinedOperation();
60        for (int i = 0; i < temp.Count; i++) {
61          if (random.NextDouble() < shakingFactors.Data[i % shakingFactors.Data.Length]) {
62            if (((Variable)temp[i]).Value is IntData) {
63              ((IntData)((Variable)temp[i]).Value).Data += ((int)(nd.NextDouble() * shakingFactors.Data[i % shakingFactors.Data.Length]));
64            } else if (((Variable)temp[i]).Value is DoubleData) {
65              ((DoubleData)((Variable)temp[i]).Value).Data += nd.NextDouble() * shakingFactors.Data[i % shakingFactors.Data.Length];
66            } else if (((Variable)temp[i]).Value is ConstrainedIntData) {
67              int val = ((ConstrainedIntData)((Variable)temp[i]).Value).Data;
68              ((ConstrainedIntData)((Variable)temp[i]).Value).TrySetData(val + ((int)(nd.NextDouble() * shakingFactors.Data[i % shakingFactors.Data.Length])));
69            } else if (((Variable)temp[i]).Value is ConstrainedDoubleData) {
70              double val = ((ConstrainedDoubleData)((Variable)temp[i]).Value).Data;
71              ((ConstrainedDoubleData)((Variable)temp[i]).Value).TrySetData(val + nd.NextDouble() * shakingFactors.Data[i % shakingFactors.Data.Length]);
72            }
73          }
74        }
75        if (temp.EndCombinedOperation(out tmp)) break;
76      }
77
78      if (tries < 10000) {
79        parameters.BeginCombinedOperation();
80        for (int i = 0; i < temp.Count; i++)
81          parameters.TrySetAt(i, temp[i], out tmp);
82        parameters.EndCombinedOperation(out tmp);
83      } else throw new InvalidOperationException("ERROR in SimOptSelfAdaptiveNumericVectorProbabilityMutation: no feasible result in 10000 tries");
84
85      return null;
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.