libfgen
0.1.15
Library for optimization using a genetic algorithm or particle swarm optimization
|
00001 /* 00002 decode.c -- bitstring decoding helper functions. 00003 00004 fgen -- Library for optimization using a genetic algorithm or particle swarm optimization. 00005 Copyright 2012, Harm Hanemaaijer 00006 00007 This file is part of fgen. 00008 00009 fgen is free software: you can redistribute it and/or modify it 00010 under the terms of the GNU Lesser General Public License as published 00011 by the Free Software Foundation, either version 3 of the License, or 00012 (at your option) any later version. 00013 00014 fgen is distributed in the hope that it will be useful, but WITHOUT 00015 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00016 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00017 License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with fgen. If not, see <http://www.gnu.org/licenses/>. 00021 00022 */ 00023 00024 #include <stdlib.h> 00025 #include <stdint.h> 00026 #include <math.h> 00027 #include "fgen.h" 00028 #include "parameters.h" 00029 00030 00035 double fgen_bitstring_uint16_to_double(const unsigned char *bitstring, double domain_min, double domain_max) { 00036 unsigned int x_int; 00037 double x; 00038 /* Construct the 16-bit integer that the bitstring represents. */ 00039 x_int = *(unsigned short int *)&bitstring[0]; 00040 00041 /* Map to the real domain [domain_min, domain_max[. */ 00042 x = domain_min + (domain_max - domain_min) * x_int / (double)65536; 00043 return x; 00044 } 00045 00050 double fgen_bitstring_uint32_to_double(const unsigned char *bitstring, double domain_min, double domain_max) { 00051 unsigned int x_int; 00052 double x; 00053 /* Construct the 32-bit integer that the bitstring represents. */ 00054 #if 1 00055 x_int = *(unsigned int *)&bitstring[0]; 00056 #else 00057 x_int = bitstring[0]; 00058 x_int += (unsigned int)bitstring[1] << 8; 00059 x_int += (unsigned int)bitstring[2] << 16; 00060 x_int += (unsigned int)bitstring[3] << 24; 00061 #endif 00062 00063 /* Map to the real domain [domain_min, domain_max[. */ 00064 x = domain_min + (domain_max - domain_min) * x_int / ((double)65536 * 65536); 00065 return x; 00066 } 00067 00072 double fgen_bitstring_uint64_to_double(const unsigned char *bitstring, double domain_min, double domain_max) { 00073 uint64_t x_int; 00074 double x; 00075 /* Construct the 64-bit integer that the bitstring represents. */ 00076 #if 1 00077 x_int = *(uint64_t *)&bitstring[0]; 00078 #else 00079 x_int = bitstring[0]; 00080 x_int += (uint64_t)bitstring[1] << 8; 00081 x_int += (uint64_t)bitstring[2] << 16; 00082 x_int += (uint64_t)bitstring[3] << 24; 00083 x_int += (uint64_t)bitstring[4] << 32; 00084 x_int += (uint64_t)bitstring[5] << 40; 00085 x_int += (uint64_t)bitstring[6] << 48; 00086 x_int += (uint64_t)bitstring[7] << 56; 00087 #endif 00088 00089 /* Map to the real domain [domain_min, domain_max[. */ 00090 x = domain_min + (domain_max - domain_min) * x_int / (double)pow((double)2, 64); 00091 return x; 00092 } 00093 00094