ga/src/crossovers/onepoint.cpp

00001 /********************************************************************************
00002  *  FARSA Genetic Algorithm Library                                             *
00003  *  Copyright (C) 2007-2008 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 "crossovers/onepoint.h"
00021 #include "core/genotype.h"
00022 #include "randomgenerator.h"
00023 #include "configurationparameters.h"
00024 
00025 namespace farsa {
00026 
00027 OnePoint::OnePoint() : Crossover() {
00028 }
00029 
00030 OnePoint::~OnePoint() {
00031     //--- nothing to do
00032 }
00033 
00034 void OnePoint::crossover( Genotype* father, const Genotype* mother ) {
00035     int max = qMin( father->size(), mother->size() );
00036     int splitpoint = globalRNG->getInt( 0, max );
00037     for( int i=splitpoint; i<max; i++ ) {
00038         if ( father->bit(i) != mother->bit(i) ) {
00039             father->toggle(i);
00040         }
00041     }
00042 }
00043 
00044 void OnePoint::configure( ConfigurationParameters& params, QString prefix ) {
00045     Q_UNUSED( params );
00046     Q_UNUSED( prefix );
00047     //--- nothing to do
00048 }
00049 
00050 void OnePoint::save( ConfigurationParameters& params, QString prefix ) {
00051     params.createParameter( prefix, QString("type"), "OnePoint" );
00052 }
00053 
00054 void OnePoint::describe( QString type ) {
00055     addTypeDescription( type, "The one point crossover", "It randomly select one point where to split the two Genotypes, and create a new one combining the first part of one Genotype with the second part of the other one" );
00056 }
00057 
00058 } // end namespace farsa