Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2825-NSGA3/HeuristicLab.Algorithms.NSGA3/3.3/SimulatedBinaryCrossover.cs @ 17703

Last change on this file since 17703 was 17657, checked in by dleko, 4 years ago

#2825 Implement recombination.

File size: 3.4 KB
Line 
1using System;
2using HeuristicLab.Core;
3using HeuristicLab.Data;
4using HeuristicLab.Encodings.RealVectorEncoding;
5
6namespace HeuristicLab.Algorithms.NSGA3
7{
8    /*
9     * Implements the simulated binary crossover based on the Tsung Che Chiang's NSGA3 implementation.
10     * http://web.ntnu.edu.tw/~tcchiang/publications/nsga3cpp/nsga3cpp.htm
11     *
12     * The simulated binary crossover in HeuristicLab.Encodings.RealVectorEncoding/Crossovers/SimulatedBinaryCrossover.cs
13     * is insufficient for NSGA3, because the static Apply-method only returns 1 child.
14     */
15
16    public static class SimulatedBinaryCrossover
17    {
18        private const double EPSILON = 10e-6; // a tiny number that is greater than 0
19
20        public static Tuple<RealVector, RealVector> Apply(IRandom random, DoubleMatrix bounds, RealVector parent1, RealVector parent2, double crossoverProbability = 1.0, double eta = 30)
21        {
22            var child1 = new RealVector(parent1);
23            var child2 = new RealVector(parent2);
24
25            if (random.NextDouble() > crossoverProbability) return null;
26
27            for (int i = 0; i < child1.Length; i++)
28            {
29                if (random.NextDouble() > 0.5) continue; // these two variables are not crossovered
30                if (Math.Abs(parent1[i] - parent2[i]) < EPSILON) continue; // two values are the same
31
32                double y1 = Math.Min(parent1[i], parent2[i]);
33                double y2 = Math.Max(parent1[i], parent2[i]);
34
35                // todo: is this correct? test it with Encoding.Length != Number of objectives
36                double lb;
37                double ub;
38                if (bounds.Rows == 1)
39                {
40                    lb = bounds[0, 0];
41                    ub = bounds[0, 1];
42                }
43                else
44                {
45                    lb = bounds[i, 0];
46                    ub = bounds[i, 1];
47                }
48
49                double rand = random.NextDouble();
50
51                // child 1
52                double beta = 1.0 + (2.0 * (y1 - lb) / (y2 - y1));
53                double alpha = 2.0 - Math.Pow(beta, -(eta + 1.0));
54                double betaq = Get_Betaq(rand, alpha, eta);
55
56                child1[i] = 0.5 * ((y1 + y2) - betaq * (y2 - y1));
57
58                // child 2
59                beta = 1.0 + (2.0 * (ub - y2) / (y2 - y1));
60                alpha = 2.0 - Math.Pow(beta, -(eta + 1.0));
61                betaq = Get_Betaq(rand, alpha, eta);
62
63                child2[i] = 0.5 * ((y1 + y2) + betaq * (y2 - y1));
64
65                // boundary checking
66                child1[i] = Math.Min(ub, Math.Max(lb, child1[i]));
67                child2[i] = Math.Min(ub, Math.Max(lb, child2[i]));
68
69                if (random.NextDouble() <= 0.5)
70                {
71                    // swap values
72                    var tmp = child1[i];
73                    child1[i] = child2[i];
74                    child2[i] = tmp;
75                }
76            }
77
78            return Tuple.Create(child1, child2);
79        }
80
81        private static double Get_Betaq(double rand, double alpha, double eta)
82        {
83            double betaq;
84            if (rand <= (1.0 / alpha))
85                betaq = Math.Pow((rand * alpha), (1.0 / (eta + 1.0)));
86            else
87                betaq = Math.Pow((1.0 / (2.0 - rand * alpha)), (1.0 / (eta + 1.0)));
88            return betaq;
89        }
90    }
91}
Note: See TracBrowser for help on using the repository browser.