Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.RealVector/BlendAlphaBetaCrossover.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: 4.9 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.Evolutionary;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.RealVector {
30  public class BlendAlphaBetaCrossover : CrossoverBase {
31    public override string Description {
32      get { return
33@"Blend alpha-beta 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 as follows: Into the direction of the 'better' solution by the factor alpha, into the direction of the 'worse' solution by the factor beta.
34Please use the operator BoundsChecker if necessary.";
35      }
36    }
37
38    public BlendAlphaBetaCrossover()
39      : base() {
40      AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
42      AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
43      VariableInfo alphaVarInfo = new VariableInfo("Alpha", "Value for alpha", typeof(DoubleData), VariableKind.In);
44      alphaVarInfo.Local = true;
45      AddVariableInfo(alphaVarInfo);
46      AddVariable(new Variable("Alpha", new DoubleData(0.75)));
47      VariableInfo betaVarInfo = new VariableInfo("Beta", "Value for beta", typeof(DoubleData), VariableKind.In);
48      betaVarInfo.Local = true;
49      AddVariableInfo(betaVarInfo);
50      AddVariable(new Variable("Beta", new DoubleData(0.25)));
51    }
52
53    public static double[] Apply(IRandom random, bool maximization, double[] parent1, double quality1, double[] parent2, double quality2, double alpha, double beta) {
54      int length = parent1.Length;
55      double[] result = new double[length];
56
57      for (int i = 0; i < length; i++) {
58        double interval = Math.Abs(parent1[i] - parent2[i]);
59
60        if ((maximization && (quality1 > quality2)) || ((!maximization) && (quality1 < quality2))) {
61          result[i] = SelectFromInterval(random, interval, parent1[i], parent2[i], alpha, beta);
62        } else {
63          result[i] = SelectFromInterval(random, interval, parent2[i], parent1[i], alpha, beta);
64        }
65      }
66      return result;
67    }
68
69    private static double SelectFromInterval(IRandom random, double interval, double val1, double val2, double alpha, double beta) {
70      double resMin = 0;
71      double resMax = 0;
72
73      if (val1 <= val2) {
74        resMin = val1 - interval * alpha;
75        resMax = val2 + interval * beta;
76      } else {
77        resMin = val2 - interval * beta;
78        resMax = val1 + interval * alpha;
79      }
80
81      return SelectRandomFromInterval(random, resMin, resMax);
82    }
83
84    private static double SelectRandomFromInterval(IRandom random, double resMin, double resMax) {
85      return resMin + random.NextDouble() * Math.Abs(resMax - resMin);
86    }
87
88    protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {
89      bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data;
90      DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>("RealVector", false);
91      DoubleData quality1 = parent1.GetVariableValue<DoubleData>("Quality", false);
92      DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>("RealVector", false);
93      DoubleData quality2 = parent2.GetVariableValue<DoubleData>("Quality", false);
94      double alpha = GetVariableValue<DoubleData>("Alpha", scope, true).Data;
95      double beta = GetVariableValue<DoubleData>("Beta", scope, true).Data;
96
97      if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length.");
98
99      double[] result = Apply(random, maximization, vector1.Data, quality1.Data, vector2.Data, quality2.Data, alpha, beta);
100      child.AddVariable(new Variable(child.TranslateName("RealVector"), new DoubleArrayData(result)));
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.