Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17958 was 17727, checked in by dleko, 4 years ago

#2825 Refactoring.

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 not usable, because it takes different parameters than stated in the NSGA-III paper. It takes contiguity as a
14     * parameter instead of an eta value.
15     */
16
17    public static class SimulatedBinaryCrossover
18    {
19        private const double EPSILON = 10e-6; // a tiny number that is greater than 0
20
21        public static Tuple<RealVector, RealVector> Apply(IRandom random, DoubleMatrix bounds, RealVector parent1, RealVector parent2, double crossoverProbability = 1.0, double eta = 20)
22        {
23            var child1 = new RealVector(parent1);
24            var child2 = new RealVector(parent2);
25
26            if (random.NextDouble() > crossoverProbability) return null;
27
28            for (int i = 0; i < child1.Length; i++)
29            {
30                if (random.NextDouble() > 0.5) continue; // these two variables are not crossovered
31                if (Math.Abs(parent1[i] - parent2[i]) < EPSILON) continue; // two values are the same
32
33                double y1 = Math.Min(parent1[i], parent2[i]);
34                double y2 = Math.Max(parent1[i], parent2[i]);
35
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.