Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.RealVector/AverageCrossover.cs @ 70

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

Worked on ticket #15

  • added crossover and manipulation operators implemented by adoppelb
File size: 1.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6
7namespace HeuristicLab.RealVector {
8  class AverageCrossover : RealVectorCrossoverBase {
9    public override string Description {
10      get { return "Average (Continuous) crossover for real vectors."; }
11    }
12
13    public static double[] Apply(IScope scope, IRandom random, double[] parent1, double[] parent2) {
14      int length = parent1.Length;
15      double[] result = new double[length];
16      double min = scope.GetVariableValue<DoubleData>("Minimum", true).Data;
17      double max = scope.GetVariableValue<DoubleData>("Maximum", true).Data;
18
19      for (int i = 0; i < length; i++) {
20        if (random.NextDouble() < 0.5) {
21          result[i] = (parent1[i] + parent2[i]) / 2;
22        } else {
23          result[i] = parent1[i];
24        }
25
26        // check borders
27        if (result[i] < min) { result[i] = min; }
28        if (result[i] > max) { result[i] = max; }
29      }
30
31      return result;
32    }
33
34    protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {
35      return Apply(scope, random, parent1, parent2);
36    }
37  }
38}
Note: See TracBrowser for help on using the repository browser.