01 /*
02 * Java Genetic Algorithm Library (jenetics-5.2.0).
03 * Copyright (c) 2007-2020 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 GeneralLongVecFactory implements VecFactory<long[]> {
37 private final boolean[] _maximisations;
38
39 private final ElementComparator<long[]> _comparator = this::cmp;
40 private final ElementDistance<long[]> _distance = this::dst;
41 private final Comparator<long[]> _dominance = this::dom;
42
43 GeneralLongVecFactory(final List<Optimize> optimizes) {
44 Vecs.checkVecLength(optimizes.size());
45 _maximisations = toFlags(optimizes);
46 }
47
48 private int cmp(final long[] u, final long[] v, final int i) {
49 return _maximisations[i]
50 ? Long.compare(u[i], v[i])
51 : Long.compare(v[i], u[i]);
52 }
53
54 private double dst(final long[] u, final long[] 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 long[] u, final long[] v) {
61 return Pareto.dominance(u, v, _maximisations.length, this::cmp);
62 }
63
64 @Override
65 public Vec<long[]> newVec(final long[] array) {
66 requireVecLength(_maximisations.length, array.length);
67 return new GeneralLongVec(array, _comparator, _distance, _dominance);
68 }
69
70 @Override
71 public String toString() {
72 return format("VecFactory<long[%d]>", _maximisations.length);
73 }
74
75 }
|