GeneralDoubleVecFactory.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-7.1.0).
03  * Copyright (c) 2007-2022 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
19  */
20 package io.jenetics.ext.moea;
21 
22 import static java.lang.String.format;
23 import static io.jenetics.ext.moea.Vecs.requireVecLength;
24 import static io.jenetics.ext.moea.Vecs.toFlags;
25 
26 import java.util.Comparator;
27 import java.util.List;
28 
29 import io.jenetics.Optimize;
30 
31 /**
32  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
33  @version 5.2
34  @since 5.2
35  */
36 final class GeneralDoubleVecFactory implements VecFactory<double[]{
37     private final boolean[] _maximisations;
38 
39     private final ElementComparator<double[]> _comparator = this::cmp;
40     private final ElementDistance<double[]> _distance = this::dst;
41     private final Comparator<double[]> _dominance = this::dom;
42 
43     GeneralDoubleVecFactory(final List<Optimize> optimizes) {
44         Vecs.checkVecLength(optimizes.size());
45         _maximisations = toFlags(optimizes);
46     }
47 
48     private int cmp(final double[] u, final double[] v, final int i) {
49         return _maximisations[i]
50             ? Double.compare(u[i], v[i])
51             : Double.compare(v[i], u[i]);
52     }
53 
54     private double dst(final double[] u, final double[] v, final int i) {
55         return _maximisations[i]
56             ? u[i- v[i]
57             : v[i- u[i];
58     }
59 
60     private int dom(final double[] u, final double[] v) {
61         return Pareto.dominance(u, v, _maximisations.length, _comparator);
62     }
63 
64     @Override
65     public Vec<double[]> newVec(final double[] array) {
66         requireVecLength(_maximisations.length, array.length);
67         return new GeneralDoubleVec(array, _comparator, _distance, _dominance);
68     }
69 
70     @Override
71     public String toString() {
72         return format("VecFactory<double[%d]>", _maximisations.length);
73     }
74 
75 }