Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.RealVector/BlendAlphaCrossover.cs @ 4384

Last change on this file since 4384 was 104, checked in by swagner, 16 years ago

Closed ticket #81

  • added additional operators for real vectors implemented by adoppelb
File size: 2.6 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 BlendAlphaCrossover : RealVectorCrossoverBase {
30    public override string Description {
31      get { return
32@"Blend alpha crossover for real vectors. Creates a new offspring by selecting a random value from the interval between the two alleles of the parent solutions. The interval is increased in both directions by the factor alpha.
33Please use the operator BoundsChecker if necessary.";
34      }
35    }
36
37    public BlendAlphaCrossover()
38      : base() {
39      VariableInfo alphaVarInfo = new VariableInfo("Alpha", "Value for alpha", typeof(DoubleData), VariableKind.In);
40      alphaVarInfo.Local = true;
41      AddVariableInfo(alphaVarInfo);
42      AddVariable(new Variable("Alpha", new DoubleData(0.5)));
43    }
44
45    public static double[] Apply(IRandom random, double[] parent1, double[] parent2, double alpha) {
46      int length = parent1.Length;
47      double[] result = new double[length];
48      double cMax = 0, cMin = 0, interval = 0, resMin = 0, resMax = 0;
49
50      for (int i = 0; i < length; i++) {
51        cMax = Math.Max(parent1[i], parent2[i]);
52        cMin = Math.Min(parent1[i], parent2[i]);
53        interval = Math.Abs(cMax - cMin);
54        resMin = cMin - interval * alpha;
55        resMax = cMax + interval * alpha;
56
57        result[i] = resMin + random.NextDouble() * Math.Abs(resMax - resMin);
58      }
59      return result;
60    }
61
62    protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {
63      double alpha = GetVariableValue<DoubleData>("Alpha", scope, true).Data;
64      return Apply(random, parent1, parent2, alpha);
65    }
66  }
67}
Note: See TracBrowser for help on using the repository browser.