multitrials.cpp
1 /********************************************************************************
2  * FARSA Genetic Algorithm Library *
3  * Copyright (C) 2007-2008 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #include "evaluations/multitrials.h"
21 #include "configurationparameters.h"
22 
23 namespace farsa {
24 
25 MultiTrials::MultiTrials( int steps, int trials )
26  : Evaluation() {
27  numTrials = steps;
28  numSteps = trials;
29  incrementStep = true;
30 }
31 
33  //--- nothing to do
34 }
35 
36 void MultiTrials::setTrials( int t ) {
37  numTrials = t;
38 }
39 
40 int MultiTrials::trials() const {
41  return numTrials;
42 }
43 
45  return currTrial;
46 }
47 
48 void MultiTrials::setSteps( int s ) {
49  numSteps = s;
50 }
51 
52 int MultiTrials::steps() const {
53  return numSteps;
54 }
55 
57  return currStep;
58 }
59 
61  currStep = numSteps+1;
62 }
63 
64 void MultiTrials::configure( ConfigurationParameters& params, QString prefix ) {
65  setSteps( params.getValue( prefix + QString( "nsteps" ) ).toInt() );
66  setTrials( params.getValue( prefix + QString( "ntrials" ) ).toInt() );
67  if ( steps() == 0 ) {
68  qWarning( "Setting the number of Steps to ZERO!! Check you config file" );
69  }
70  if ( trials() == 0 ) {
71  qWarning( "Setting the number of Trials to ZERO!! Check you config file" );
72  }
73 }
74 
75 void MultiTrials::save( ConfigurationParameters& params, QString prefix ) {
76  params.createParameter( prefix, QString("type"), "MultiTrials" );
77  params.createParameter( prefix, QString("nsteps"), QString("%1").arg(steps()) );
78  params.createParameter( prefix, QString("ntrials"), QString("%1").arg(trials()) );
79 }
80 
81 void MultiTrials::describe( QString type ) {
82  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" );
83  d.describeInt( "nsteps" ).limits( 1, INT_MAX ).help( "The number of steps for each trial" );
84  d.describeInt( "ntrials" ).limits( 1, INT_MAX).help( "The number of trials to do" );
85 }
86 
88 {
89  incrementStep = !remain;
90 }
91 
93 {
94  return !incrementStep;
95 }
96 
97 void MultiTrials::init() {
98  currStep = 0;
99  currTrial = 0;
100  mainInit();
101  trialInited = false;
102 }
103 
104 void MultiTrials::step() {
105  if ( !trialInited ) {
106  //--- initialize the Trial
107  if ( !trialInit( currTrial ) ) return;
108  trialInited = true;
109  }
110  //--- do a step
111  trialStep( currStep, currTrial );
112  if (incrementStep) {
113  currStep++;
114  if ( currStep >= numSteps ) {
115  //--- reached the maximum number of steps,
116  //--- pass to next trial
117  trialFini( currTrial );
118  currTrial++;
119  currStep = 0;
120  trialInited = false;
121  if ( currTrial >= numTrials ) {
122  //--- Evaluation ended
123  evaluationDone();
124  }
125  }
126  }
127 }
128 
129 void MultiTrials::fini() {
130  mainFini();
131 }
132 
133 } // end namespace farsa