Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.RealVector/MichalewiczNonUniformAllPositionsManipulator.cs @ 5586

Last change on this file since 5586 was 294, checked in by swagner, 16 years ago

Worked on ticket #161

  • removed bounds checking in MichalewiczNonUniformAllPositionsManipulator and MichalewiczNonUniformOnePositionManipulator
File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27
28namespace HeuristicLab.RealVector {
29  public class MichalewiczNonUniformAllPositionsManipulator : RealVectorManipulatorBase {
30    public override string Description {
31      get { return
32@"Non-uniformly distributed change of all positions of a real vector (Michalewicz 1992)
33Initially, the space will be searched uniformly and very locally at later stages. This increases the probability of generating the new number closer to its successor instead of a random number.
34
35Michalewicz, Z. (1992). Genetic Algorithms + Data Structures = Evolution Programs. Springer Verlag.";
36      }
37    }
38
39    public MichalewiczNonUniformAllPositionsManipulator()
40      : base() {
41      AddVariableInfo(new VariableInfo("Minimum", "Minimum of the sampling range for the vector element (included)", typeof(DoubleData), VariableKind.In));
42      AddVariableInfo(new VariableInfo("Maximum", "Maximum of the sampling range for the vector element (excluded)", typeof(DoubleData), VariableKind.In));
43      AddVariableInfo(new VariableInfo("CurrentGeneration", "Current Generation of the algorithm", typeof(IntData), VariableKind.In));
44      AddVariableInfo(new VariableInfo("MaximumGenerations", "Maximum number of Generations", typeof(IntData), VariableKind.In));
45      VariableInfo genDepInfo = new VariableInfo("GenerationsDependency", "Specifies the degree of dependency on the number of generations", typeof(IntData), VariableKind.In);
46      genDepInfo.Local = true;
47      AddVariableInfo(genDepInfo);
48      AddVariable(new Variable("GenerationsDependency", new IntData(5)));
49    }
50
51    protected override double[] Manipulate(IScope scope, IRandom random, double[] vector) {
52      double min = GetVariableValue<DoubleData>("Minimum", scope, true).Data;
53      double max = GetVariableValue<DoubleData>("Maximum", scope, true).Data;
54      int currentGeneration = GetVariableValue<IntData>("CurrentGeneration", scope, true).Data;
55      int maximumGenerations = GetVariableValue<IntData>("MaximumGenerations", scope, true).Data;
56      int generationsDependency = GetVariableValue<IntData>("GenerationsDependency", scope, true).Data;
57      return Apply(random, vector, min, max, currentGeneration, maximumGenerations, generationsDependency);
58    }
59
60    public static double[] Apply(IRandom random, double[] vector, double min, double max, int currentGeneration, int maximumGenerations, int generationsDependency) {
61      int length = vector.Length;
62      double[] result = new double[length];
63
64      for (int i = 0; i < length; i++) {
65        if (random.NextDouble() < 0.5) {
66          vector[i] = vector[i] + Delta(random, currentGeneration, max - vector[i], maximumGenerations, generationsDependency);
67        } else {
68          vector[i] = vector[i] - Delta(random, currentGeneration, vector[i] - min, maximumGenerations, generationsDependency);
69        }
70      }
71      return vector;
72    }
73
74    // returns a value between 0 and y (both included)
75    private static double Delta(IRandom random, int currentGeneration, double y, int maximumGenerations, int generationsDependency) {
76      return y * (1 - Math.Pow(random.NextDouble(), Math.Pow(1 - currentGeneration / maximumGenerations, generationsDependency)));
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.