org.jenetics.engine.CompositeCodec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jenetics Show documentation
Show all versions of jenetics Show documentation
Jenetics - Java Genetic Algorithm Library
/*
* Java Genetic Algorithm Library (jenetics-3.8.0).
* Copyright (c) 2007-2017 Franz Wilhelmstötter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author:
* Franz Wilhelmstötter ([email protected])
*/
package org.jenetics.engine;
import static java.util.Objects.requireNonNull;
import java.util.function.Function;
import org.jenetics.Chromosome;
import org.jenetics.Gene;
import org.jenetics.Genotype;
import org.jenetics.util.Factory;
import org.jenetics.util.ISeq;
/**
* Composites a list of codecs into one {@code Codec} class.
*
* @param the gene type
* @param the argument type of the compound codec
*
* @author Franz Wilhelmstötter
* @version 3.3
* @since 3.3
*/
final class CompositeCodec> implements Codec {
private final ISeq extends Codec, G>> _codecs;
private final Function super Object[], ? extends T> _decoder;
private final int[] _lengths;
private final Genotype _encoding;
/**
* Combines the given {@code codecs} into one codec. This lets you divide
* a problem into sub problems and combine them again.
*
* @param codecs the {@code Codec} sequence of the sub-problems
* @param decoder the decoder which combines the argument types from the
* given given codecs, to the argument type of the resulting codec.
* @throws NullPointerException if one of the arguments is {@code null}
*/
CompositeCodec(
final ISeq extends Codec, G>> codecs,
final Function super Object[], ? extends T> decoder
) {
_codecs = requireNonNull(codecs);
_decoder = requireNonNull(decoder);
final ISeq> genotypes = _codecs
.map(c -> c.encoding() instanceof Genotype>
? (Genotype)c.encoding()
: c.encoding().newInstance());
_lengths = genotypes.stream()
.mapToInt(Genotype::length)
.toArray();
_encoding = Genotype.of(
genotypes.stream()
.flatMap(Genotype::stream)
.collect(ISeq.toISeq())
);
}
@Override
public Factory> encoding() {
return _encoding;
}
@Override
public Function, T> decoder() {
return gt -> _decoder.apply(groups(gt));
}
private Object[] groups(final Genotype genotype) {
final Object[] groups = new Object[_codecs.length()];
final ISeq> chromosomes = genotype.toSeq();
int start = 0;
for (int i = 0; i < _codecs.length(); ++i) {
final int end = start + _lengths[i];
final Genotype gt = Genotype.of(chromosomes.subSeq(start, end));
groups[i] = _codecs.get(i).decoder().apply(gt);
start = end;
}
return groups;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy