001 /*
002 * Java Genetic Algorithm Library (jenetics-3.5.0).
003 * Copyright (c) 2007-2016 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019 */
020 package org.jenetics;
021
022 import java.util.stream.Stream;
023
024 import org.jenetics.util.Factory;
025 import org.jenetics.util.ISeq;
026 import org.jenetics.util.Verifiable;
027
028 /**
029 * A chromosome consists of one or more genes. It also provides a factory
030 * method for creating new, random chromosome instances of the same type and the
031 * same constraint.
032 * <p>
033 * <span class="simpleTagLabel">API Note: </span>
034 * Implementations of the {@code Chromosome} interface must be <em>immutable</em>
035 * and guarantee an efficient random access ({@code O(1)}) to the genes.
036 *
037 * @see <a href="http://en.wikipedia.org/wiki/Chromosome">Wikipdida: Chromosome</a>
038 *
039 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
040 * @since 1.0
041 * @version 3.1
042 */
043 public interface Chromosome<G extends Gene<?, G>>
044 extends
045 Verifiable,
046 Iterable<G>,
047 Factory<Chromosome<G>>
048 {
049 /**
050 * A factory method which creates a new {@link Chromosome} of specific type
051 * and the given {@code genes}.
052 *
053 * @param genes the genes of the new chromosome. The given genes array is
054 * not copied.
055 * @return A new {@link Chromosome} of the same type with the given genes.
056 * @throws NullPointerException if the given {@code gene}s are {@code null}.
057 * @throws IllegalArgumentException if the length of the given gene sequence
058 * is smaller than one.
059 */
060 public Chromosome<G> newInstance(final ISeq<G> genes);
061
062 /**
063 * Return the first gene of this chromosome. Each chromosome must contain
064 * at least one gene.
065 *
066 * @return the first gene of this chromosome.
067 */
068 public default G getGene() {
069 return getGene(0);
070 }
071
072 /**
073 * Return the gene on the specified index.
074 *
075 * @param index The gene index.
076 * @return the wanted gene.
077 * @throws IndexOutOfBoundsException if the index is out of range
078 * (index < 1 || index >= length()).
079 */
080 public G getGene(final int index);
081
082 /**
083 * Returns the length of the Chromosome. The minimal length of a
084 * chromosome is one.
085 *
086 * @return Length of the Chromosome
087 */
088 public int length();
089
090 /**
091 * Return an unmodifiable sequence of the genes of this chromosome.
092 *
093 * @return an immutable gene sequence.
094 */
095 public ISeq<G> toSeq();
096
097 /**
098 * Returns a sequential {@code Stream} of genes with this chromosome as
099 * its source.
100 *
101 * @since 3.3
102 *
103 * @return a sequential {@code Stream} of genes
104 */
105 public default Stream<G> stream() {
106 return toSeq().stream();
107 }
108
109 }
|