001/* 002 * Java Genetic Algorithm Library (jenetics-7.1.0). 003 * Copyright (c) 2007-2022 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@gmail.com) 019 */ 020package io.jenetics; 021 022/** 023 * Numeric chromosome interface. 024 * 025 * @implSpec 026 * Implementations of the {@code NumericChromosome} interface must be 027 * <em>immutable</em> and guarantee an efficient random access ({@code O(1)}) to 028 * the genes. A {@code Chromosome} must contains at least one {@code Gene}. 029 * 030 * @see NumericGene 031 * 032 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> 033 * @since 1.6 034 * @version 5.2 035 */ 036public interface NumericChromosome< 037 N extends Number & Comparable<? super N>, 038 G extends NumericGene<N, G> 039> 040 extends BoundedChromosome<N, G> 041{ 042 043 /** 044 * Return the byte value of this {@code NumericChromosome} at the given 045 * {@code index}. 046 * 047 * @param index the index of the {@link NumericGene}. 048 * @return the byte value of the {@link Gene} with the given {@code index}. 049 * @throws IndexOutOfBoundsException if the index is out of range 050 * (index < 0 || index >= length()). 051 */ 052 default byte byteValue(final int index) { 053 return get(index).allele().byteValue(); 054 } 055 056 /** 057 * Return the byte value of this {@code NumericChromosome} at the 058 * {@code index} 0. 059 * 060 * @return the byte value of the {@link Gene} with {@code index} 0. 061 */ 062 default byte byteValue() { 063 return byteValue(0); 064 } 065 066 /** 067 * Return the short value of this {@code NumericChromosome} at the given 068 * {@code index}. 069 * 070 * @param index the index of the {@link NumericGene}. 071 * @return the short value of the {@link Gene} with the given {@code index}. 072 * @throws IndexOutOfBoundsException if the index is out of range 073 * (index < 0 || index >= length()). 074 */ 075 default short shortValue(final int index) { 076 return get(index).allele().shortValue(); 077 } 078 079 /** 080 * Return the short value of this {@code NumericChromosome} at the 081 * {@code index} 0. 082 * 083 * @return the short value of the {@link Gene} with {@code index} 0. 084 */ 085 default short shortValue() { 086 return shortValue(0); 087 } 088 089 /** 090 * Return the int value of this {@code NumericChromosome} at the given 091 * {@code index}. 092 * 093 * @param index the index of the {@link NumericGene}. 094 * @return the int value of the {@link Gene} with the given {@code index}. 095 * @throws IndexOutOfBoundsException if the index is out of range 096 * (index < 0 || index >= length()). 097 */ 098 default int intValue(final int index) { 099 return get(index).allele().intValue(); 100 } 101 102 /** 103 * Return the int value of this {@code NumericChromosome} at the 104 * {@code index} 0. 105 * 106 * @return the int value of the {@link Gene} with {@code index} 0. 107 */ 108 default int intValue() { 109 return intValue(0); 110 } 111 112 /** 113 * Return the long value of this {@code NumericChromosome} at the given 114 * {@code index}. 115 * 116 * @param index the index of the {@link NumericGene}. 117 * @return the long value of the {@link Gene} with the given {@code index}. 118 * @throws IndexOutOfBoundsException if the index is out of range 119 * (index < 0 || index >= length()). 120 */ 121 default long longValue(final int index) { 122 return get(index).allele().longValue(); 123 } 124 125 /** 126 * Return the long value of this {@code NumericChromosome} at the 127 * {@code index} 0. 128 * 129 * @return the long value of the {@link Gene} with {@code index} 0. 130 */ 131 default long longValue() { 132 return longValue(0); 133 } 134 135 /** 136 * Return the float value of this {@code NumericChromosome} at the given 137 * {@code index}. 138 * 139 * @param index the index of the {@link NumericGene}. 140 * @return the float value of the {@link Gene} with the given {@code index}. 141 * @throws IndexOutOfBoundsException if the index is out of range 142 * (index < 0 || index >= length()). 143 */ 144 default float floatValue(final int index) { 145 return get(index).allele().floatValue(); 146 } 147 148 /** 149 * Return the float value of this {@code NumericChromosome} at the 150 * {@code index} 0. 151 * 152 * @return the float value of the {@link Gene} with {@code index} 0. 153 */ 154 default float floatValue() { 155 return floatValue(0); 156 } 157 158 /** 159 * Return the double value of this {@code NumericChromosome} at the given 160 * {@code index}. 161 * 162 * @param index the index of the {@link NumericGene}. 163 * @return the double value of the {@link Gene} with the given {@code index}. 164 * @throws IndexOutOfBoundsException if the index is out of range 165 * (index < 0 || index >= length()). 166 */ 167 default double doubleValue(final int index) { 168 return get(index).allele().doubleValue(); 169 } 170 171 /** 172 * Return the double value of this {@code NumericChromosome} at the 173 * {@code index} 0. 174 * 175 * @return the double value of the {@link Gene} with {@code index} 0. 176 */ 177 default double doubleValue() { 178 return doubleValue(0); 179 } 180 181}