ga/src/evaluations/multitrials.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 "evaluations/multitrials.h"
00021 #include "configurationparameters.h"
00022 
00023 namespace farsa {
00024 
00025 MultiTrials::MultiTrials( int steps, int trials )
00026     : Evaluation() {
00027     numTrials = steps;
00028     numSteps = trials;
00029     incrementStep = true;
00030 }
00031 
00032 MultiTrials::~MultiTrials() {
00033     //--- nothing to do
00034 }
00035 
00036 void MultiTrials::setTrials( int t ) {
00037     numTrials = t;
00038 }
00039 
00040 int MultiTrials::trials() const {
00041     return numTrials;
00042 }
00043 
00044 int MultiTrials::currentTrial() const {
00045     return currTrial;
00046 }
00047 
00048 void MultiTrials::setSteps( int s ) {
00049     numSteps = s;
00050 }
00051 
00052 int MultiTrials::steps() const {
00053     return numSteps;
00054 }
00055 
00056 int MultiTrials::currentStep() const {
00057     return currStep;
00058 }
00059 
00060 void MultiTrials::trialDone() {
00061     currStep = numSteps+1;
00062 }
00063 
00064 void MultiTrials::configure( ConfigurationParameters& params, QString prefix ) {
00065     setSteps( params.getValue( prefix + QString( "nsteps" ) ).toInt() );
00066     setTrials( params.getValue( prefix + QString( "ntrials" ) ).toInt() );
00067     if ( steps() == 0 ) {
00068         qWarning( "Setting the number of Steps to ZERO!! Check you config file" );
00069     }
00070     if ( trials() == 0 ) {
00071         qWarning( "Setting the number of Trials to ZERO!! Check you config file" );
00072     }
00073 }
00074 
00075 void MultiTrials::save( ConfigurationParameters& params, QString prefix ) {
00076     params.createParameter( prefix, QString("type"), "MultiTrials" );
00077     params.createParameter( prefix, QString("nsteps"), QString("%1").arg(steps()) );
00078     params.createParameter( prefix, QString("ntrials"), QString("%1").arg(trials()) );
00079 }
00080 
00081 void MultiTrials::describe( QString type ) {
00082     Descriptor d = addTypeDescription( type, "Evaluation of the fitness based on more trials", "This evaluation of the fitness suppose that each individual will be tested on ntrials trials on which each lasts nsteps steps" );
00083     d.describeInt( "nsteps" ).limits( 1, INT_MAX ).help( "The number of steps for each trial" );
00084     d.describeInt( "ntrials" ).limits( 1, INT_MAX).help( "The number of trials to do" );
00085 }
00086 
00087 void MultiTrials::remainInCurrentStep(bool remain)
00088 {
00089     incrementStep = !remain;
00090 }
00091 
00092 bool MultiTrials::remainingInCurrentStep() const
00093 {
00094     return !incrementStep;
00095 }
00096 
00097 void MultiTrials::init() {
00098     currStep = 0;
00099     currTrial = 0;
00100     mainInit();
00101     trialInited = false;
00102 }
00103 
00104 void MultiTrials::step() {
00105     if ( !trialInited ) {
00106         //--- initialize the Trial
00107         if ( !trialInit( currTrial ) ) return;
00108         trialInited = true;
00109     }
00110     //--- do a step
00111     trialStep( currStep, currTrial );
00112     if (incrementStep) {
00113         currStep++;
00114         if ( currStep >= numSteps ) {
00115             //--- reached the maximum number of steps,
00116             //--- pass to next trial
00117             trialFini( currTrial );
00118             currTrial++;
00119             currStep = 0;
00120             trialInited = false;
00121             if ( currTrial >= numTrials ) {
00122                 //--- Evaluation ended
00123                 evaluationDone();
00124             }
00125         }
00126     }
00127 }
00128 
00129 void MultiTrials::fini() {
00130     mainFini();
00131 }
00132 
00133 } // end namespace farsa