13 #include "BitVector.h"
57 template <
typename IntegerType = u
int64_t,
bool use_gray_encoding = true>
59 size_t gene_index, IntegerType min = 0,
60 IntegerType max = std::numeric_limits<IntegerType>::max())
const {
67 assert(
sizeof(IntegerType) * CHAR_BIT >= gene_width);
69 auto value = GetInt<IntegerType>(gene_bit_index, gene_width);
70 if (use_gray_encoding) {
71 value = DecodeGray<IntegerType>(value);
75 return (value % (max - min)) + min;
86 template <
typename IntegerType = u
int64_t,
bool use_gray_encoding = true>
90 assert(
sizeof(IntegerType) * CHAR_BIT >= gene_width);
92 if (use_gray_encoding) {
93 value = EncodeGray<IntegerType>(value);
95 SetInt<IntegerType>(value, gene_bit_index, gene_width);
109 template <
typename FloatType =
double,
bool use_gray_encoding = true>
111 FloatType max)
const {
112 const uint64_t int_value =
113 DecodeIntegerGene<uint64_t, use_gray_encoding>(gene_index);
115 return DecodeFloat<FloatType, uint64_t>(int_value, gene_width, min, max);
127 template <
typename FloatType =
double,
bool use_gray_encoding = true>
131 const uint64_t int_value =
132 EncodeFloat<FloatType, uint64_t>(value, gene_width, min, max);
133 EncodeIntegerGene<uint64_t, use_gray_encoding>(gene_index, int_value);
159 std::byte*
GetRawGene(
size_t gene_index,
size_t* gene_bit_width);
166 template <
typename IntegerType = u
int64_t>
168 IntegerType binary_value = gray_value;
169 while (gray_value >>= 1) {
170 binary_value ^= gray_value;
180 template <
typename IntegerType = u
int64_t>
182 return binary_value ^ (binary_value >> 1);
193 template <
typename FloatType =
double,
typename IntegerType = u
int64_t>
194 static FloatType
DecodeFloat(IntegerType int_value,
size_t bit_width,
195 FloatType min, FloatType max) {
196 constexpr
size_t bits_per_byte = 8;
197 const IntegerType local_max =
198 std::numeric_limits<IntegerType>::max() >>
199 (
sizeof(IntegerType) * bits_per_byte - bit_width);
200 const FloatType factor =
201 static_cast<FloatType
>(int_value) /
static_cast<FloatType
>(local_max);
202 return factor * (max - min) + min;
214 template <
typename FloatType =
double,
typename IntegerType = u
int64_t>
215 static IntegerType
EncodeFloat(FloatType float_value,
size_t bit_width,
216 FloatType min, FloatType max) {
217 constexpr
size_t bits_per_byte = 8;
218 const IntegerType local_max =
219 std::numeric_limits<IntegerType>::max() >>
220 (
sizeof(IntegerType) * bits_per_byte - bit_width);
221 const FloatType factor = (float_value - min) / (max - min);
222 return factor > 1.0 ? local_max
223 :
static_cast<IntegerType
>(factor * local_max);
239 bool ignore_gene_boundaries =
true);
255 bool ignore_gene_boundaries =
true);
271 #endif // CHROMOSOME_H__