ga/src/genotypes/doublegenotype.cpp

00001 /********************************************************************************
00002  *  FARSA Genetic Algorithm Library                                             *
00003  *  Copyright (C) 2007-2009 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #include "genotypes/doublegenotype.h"
00021 #include <cmath>
00022 
00023 namespace farsa {
00024 
00025 DoubleGenotype::DoubleGenotype( unsigned int numGenes, double min, double max, unsigned int bitPrecision )
00026     : Genotype( numGenes*bitPrecision ) {
00027     numgenes = numGenes;
00028     bitprec = bitPrecision;
00029     maxv = max;
00030     minv = min;
00031 }
00032 
00033 DoubleGenotype::~DoubleGenotype() {
00034     //--- nothing to do
00035 }
00036 
00037 DoubleGenotype::DoubleGenotype( const DoubleGenotype& genotype )
00038     : Genotype( genotype ) {
00039     this->numgenes = genotype.numgenes;
00040     this->bitprec = genotype.bitprec;
00041     this->maxv = genotype.maxv;
00042     this->minv = genotype.minv;
00043 }
00044 
00045 unsigned int DoubleGenotype::numGenes() {
00046     return numgenes;
00047 }
00048 
00049 double DoubleGenotype::maxValue() {
00050     return maxv;
00051 }
00052 
00053 double DoubleGenotype::minValue() {
00054     return minv;
00055 }
00056 
00057 unsigned int DoubleGenotype::bitPrecision() {
00058     return bitprec;
00059 }
00060 
00061 unsigned int DoubleGenotype::geneToBitIndex( unsigned int gene ) const {
00062     return gene * bitprec;
00063 }
00064 
00065 unsigned int DoubleGenotype::bitToGeneIndex( unsigned int bit ) const {
00066     return bit / bitprec;
00067 }
00068 } // end namespace farsa