Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SimOpt/3.2/SimOptSelfAdaptiveNumericVectorProbabilityMutation.cs @ 1793

Last change on this file since 1793 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 4.3 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;
29
30namespace HeuristicLab.SimOpt {
31  public class SimOptSelfAdaptiveNumericVectorProbabilityMutation : OperatorBase {
32
33    public override string Description {
34      get { return @"Takes the values of a strategy vectors as possibilities to manipulate a certain dimension in the parameter vector"; }
35    }
36
37    public SimOptSelfAdaptiveNumericVectorProbabilityMutation()
38      : base() {
39      AddVariableInfo(new VariableInfo("Random", "The random number generator", typeof(IRandom), VariableKind.In));
40      AddVariableInfo(new VariableInfo("Probabilities", "The probability vector", typeof(DoubleArrayData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("Items", "The parameter vector", typeof(ConstrainedItemList), VariableKind.In | VariableKind.Out));
42      AddVariableInfo(new VariableInfo("MaxVector", "Vector containing the maximum values", typeof(DoubleArrayData), VariableKind.In));
43      AddVariableInfo(new VariableInfo("MinVector", "Vector containing the minimum values", typeof(DoubleArrayData), VariableKind.In));
44    }
45
46    public override IOperation Apply(IScope scope) {
47      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
48      DoubleArrayData max = GetVariableValue<DoubleArrayData>("MaxVector", scope, true);
49      DoubleArrayData min = GetVariableValue<DoubleArrayData>("MinVector", scope, true);
50      DoubleArrayData probs = GetVariableValue<DoubleArrayData>("Probabilities", scope, false);
51
52      ConstrainedItemList parameters = GetVariableValue<ConstrainedItemList>("Items", scope, false);
53      int tries;
54      ConstrainedItemList temp = null;
55      ICollection<IConstraint> tmp;
56
57      for (tries = 0; tries < 100; tries++) {
58        temp = (ConstrainedItemList)parameters.Clone();
59
60        temp.BeginCombinedOperation();
61        for (int i = 0; i < temp.Count; i++) {
62          if (random.NextDouble() < probs.Data[i % probs.Data.Length]) {
63            if (((Variable)temp[i]).Value is IntData) {
64              ((IntData)((Variable)temp[i]).Value).Data = random.Next((int)Math.Floor(min.Data[i % min.Data.Length]), (int)Math.Ceiling(max.Data[i % max.Data.Length]));
65            } else if (((Variable)temp[i]).Value is DoubleData) {
66              ((DoubleData)((Variable)temp[i]).Value).Data = min.Data[i] + (max.Data[i % max.Data.Length] - min.Data[i % min.Data.Length]) * random.NextDouble();
67            } else if (((Variable)temp[i]).Value is ConstrainedIntData) {
68              ((ConstrainedIntData)((Variable)temp[i]).Value).TrySetData(random.Next((int)Math.Floor(min.Data[i % min.Data.Length]), (int)Math.Ceiling(max.Data[i % max.Data.Length])));
69            } else if (((Variable)temp[i]).Value is ConstrainedDoubleData) {
70              ((ConstrainedDoubleData)((Variable)temp[i]).Value).TrySetData(min.Data[i % min.Data.Length] + (max.Data[i % max.Data.Length] - min.Data[i % min.Data.Length]) * random.NextDouble());
71            }
72          }
73        }
74        if (temp.EndCombinedOperation(out tmp)) break;
75      }
76
77      if (tries < 100) {
78        parameters.BeginCombinedOperation();
79        for (int i = 0; i < temp.Count; i++)
80          parameters.TrySetAt(i, temp[i], out tmp);
81        parameters.EndCombinedOperation(out tmp);
82      } else throw new InvalidOperationException("ERROR in SimOptSelfAdaptiveNumericVectorProbabilityMutation: no feasible result in 100 tries");
83
84      return null;
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.