File: eoReduceSplit.h

package info (click to toggle)
gamera 1%3A3.4.2%2Bgit20160808.1725654-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,312 kB
  • ctags: 24,991
  • sloc: xml: 122,324; ansic: 52,869; cpp: 50,664; python: 35,034; makefile: 118; sh: 101
file content (328 lines) | stat: -rw-r--r-- 10,323 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-

//-----------------------------------------------------------------------------
// eoReduceSplit.h
//   Base class for population-reducing classes - retaining the poor losers
// (c) GeNeura Team, 1998, Marc Schoenauer, 2002
/*
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   Contact: Marc.Schoenauer@inria.fr
 */
//-----------------------------------------------------------------------------

#ifndef eoReduceSplit_h
#define eoReduceSplit_h

//-----------------------------------------------------------------------------

#include <iostream>

// EO includes
#include <eoPop.h>     // eoPop
#include <eoFunctor.h>  // eoReduce
#include <utils/selectors.h>

/** @addtogroup Replacors
 * @{
 */

/**
 * eoReduceSplit: reduce the pop to the specified size
 *                  AND eventually returns the eliminated guys
*/
template<class EOT> class eoReduceSplit: public eoBF<eoPop<EOT>&, eoPop<EOT> &, void >
{};

/** deterministic truncation method using sort */
template <class EOT>
class eoTruncateSplit : public eoReduceSplit<EOT>
{
public:
  /** Ctor: must provide amount of reduction,
      and whether or not you need to return the eliminated guys
  */
  eoTruncateSplit(eoHowMany _howMany, bool _returnEliminated = false):
    howMany(_howMany), returnEliminated(_returnEliminated) {}

  /** do the jonb */
  void operator()(eoPop<EOT>& _newgen, eoPop<EOT> & _eliminated)
  {
    unsigned popSize = _newgen.size();
    unsigned eliminated = howMany(popSize);
    if (!eliminated)   // nothing to do
      return ;
    unsigned newsize = popSize - eliminated;
    if (newsize < 0)
      throw std::logic_error("eoTruncateSplit: Cannot truncate to a larger size!\n");

    _newgen.nth_element(newsize);

    // save poor losers if necessary
    if (returnEliminated)
      for (unsigned i=0; i<eliminated; i++)
        _eliminated.push_back(_newgen[newsize+i]);
    // truncate
    _newgen.resize(newsize);
    return ;
  }

private:
  eoHowMany howMany;
  bool returnEliminated;
};

/** a ReduceSplit class that does not sort, but repeatidely kills the worse.
To be used in SSGA-like replacements (e.g. see eoSSGAWorseReplacement)
*/
template <class EOT>
class eoLinearTruncateSplit : public eoReduceSplit<EOT>
{
public:
  /** Ctor: must provide amount of reduction,
      and whether or not you need to return the eliminated guys
  */
  eoLinearTruncateSplit(eoHowMany _howMany, bool _returnEliminated = false):
    howMany(_howMany), returnEliminated(_returnEliminated) {}

  /** do the job */
  void operator()(eoPop<EOT>& _newgen, eoPop<EOT> & _eliminated)
  {
    unsigned popSize = _newgen.size();
    unsigned eliminated = howMany(popSize);
    if (!eliminated)   // nothing to do
      return ;
    long newsize = static_cast<long>(popSize) - static_cast<long>(eliminated);
    if (newsize < 0)
      throw std::logic_error("eoLinearTruncateSplit: Cannot truncate to a larger size!\n");

    _eliminated.reserve(_eliminated.size()+eliminated); //in case not empty?
    for (unsigned i=0; i<eliminated; i++)
      {
        typename eoPop<EOT>::iterator it = _newgen.it_worse_element();
        if (returnEliminated)
          _eliminated.push_back(*it);
        _newgen.erase(it);
      }
  }

private:
  eoHowMany howMany;
  bool returnEliminated;
};

/** random truncation - batch version */
template <class EOT>
class eoRandomSplit : public eoReduceSplit<EOT>
{
public:
  /** Ctor: must provide amount of reduction,
      and whether or not you need to return the eliminated guys
  */
  eoRandomSplit(eoHowMany _howMany, bool _returnEliminated = false):
    howMany(_howMany), returnEliminated(_returnEliminated) {}

  /** do the job */
  void operator()(eoPop<EOT>& _newgen, eoPop<EOT> & _eliminated)
  {
    unsigned popSize = _newgen.size();
    unsigned eliminated = howMany(popSize);
    if (!eliminated)   // nothing to do
      return ;
    unsigned newsize = popSize - eliminated;
    if (newsize < 0)
      throw std::logic_error("eoRandomSplit: Cannot truncate to a larger size!\n");

    _newgen.shuffle();

    // save poor losers if necessary
    if (returnEliminated)
      for (unsigned i=0; i<eliminated; i++)
        _eliminated.push_back(_newgen[newsize+i]);
    // truncate
    _newgen.resize(newsize);
    return ;
  }

private:
  eoHowMany howMany;
  bool returnEliminated;
};


/** random truncation - linear version */
template <class EOT>
class eoLinearRandomSplit : public eoReduceSplit<EOT>
{
public:
  /** Ctor: must provide amount of reduction,
      and whether or not you need to return the eliminated guys
  */
  eoLinearRandomSplit(eoHowMany _howMany, bool _returnEliminated = false):
    howMany(_howMany), returnEliminated(_returnEliminated) {}

  /** do the job */
  void operator()(eoPop<EOT>& _newgen, eoPop<EOT> & _eliminated)
  {
    unsigned popSize = _newgen.size();
    unsigned eliminated = howMany(popSize);
    if (!eliminated)   // nothing to do
      return ;
    unsigned newsize = popSize - eliminated;
    if (newsize < 0)
      throw std::logic_error("eoLinearRandomSplit: Cannot truncate to a larger size!\n");

    _eliminated.reserve(_eliminated.size()+eliminated); //in case not empty?
    for (unsigned i=0; i<eliminated; i++)
      {
        unsigned loser=random(_newgen.size());
        typename eoPop<EOT>::iterator it = _newgen.begin()+loser;
        if (returnEliminated)
          _eliminated.push_back(*it);
        _newgen.erase(it);
      }
    return ;
  }

private:
  eoHowMany howMany;
  bool returnEliminated;
};


/** a ReduceSplit class based on a repeated deterministic (reverse!) tournament
To be used in SSGA-like replacements (e.g. see eoSSGADetTournamentReplacement)
*/
template <class EOT>
class eoDetTournamentTruncateSplit : public eoReduceSplit<EOT>
{
public:
  /** Ctor: must provide amount of reduction,
      and whether or not you need to return the eliminated guys
  */
  eoDetTournamentTruncateSplit(unsigned _t_size, eoHowMany _howMany,
                      bool _returnEliminated = false):
    t_size(_t_size), howMany(_howMany),
    returnEliminated(_returnEliminated)
  {
    if (t_size < 2)
      {
          eo::log << eo::warnings << "Warning, Size for eoDetTournamentTruncateSplit adjusted to 2" << std::endl;
        t_size = 2;
      }
  }

  /** Performs repeated inverse_deterministic_tournament on the pop */
  void operator()(eoPop<EOT>& _newgen, eoPop<EOT> & _eliminated)
  // BUG???  void operator()(eoPop<EOT>& _newgen, unsigned _newsize)
  {
    unsigned popSize = _newgen.size();
    unsigned eliminated = howMany(popSize);
    if (!eliminated)   // nothing to do
      return ;
    unsigned newsize = popSize - eliminated;
    if (newsize < 0)
      throw std::logic_error("eoDetTournamentTruncateSplit: Cannot truncate to a larger size!\n");


    _eliminated.reserve(_eliminated.size()+eliminated); //in case not empty?
    for (unsigned i=0; i<eliminated; i++)
      {
        typename eoPop<EOT>::iterator it = inverse_deterministic_tournament(_newgen.begin(), _newgen.end(), t_size);
        if (returnEliminated)
          _eliminated.push_back(*it);
        _newgen.erase(it);
      }
  }

private:
  unsigned t_size;
  eoHowMany howMany;
  bool returnEliminated;
};

/** a ReduceSplit class based on a repeated deterministic (reverse!) tournament
To be used in SSGA-like replacements (e.g. see eoSSGAStochTournamentReplacement)
*/
template <class EOT>
class eoStochTournamentTruncateSplit : public eoReduce<EOT>
{
public:
  /** Ctor: must provide amount of reduction,
      and whether or not you need to return the eliminated guys
  */
  eoStochTournamentTruncateSplit(double _t_rate, eoHowMany _howMany,
                      bool _returnEliminated = false):
    t_rate(_t_rate), howMany(_howMany),
    returnEliminated(_returnEliminated)
  {
    if (t_rate <= 0.5)
      {
          eo::log << eo::warnings << "Warning, Rate for eoStochTournamentTruncateSplit adjusted to 0.51" << std::endl;
        t_rate = 0.51;
      }
    if (t_rate > 1)
      {
          eo::log << eo::warnings << "Warning, Rate for eoStochTournamentTruncateSplit adjusted to 1" << std::endl;
        t_rate = 1;
      }
  }

  /** Performs repeated inverse_stochastic_tournament on the pop */
  void operator()(eoPop<EOT>& _newgen, eoPop<EOT> & _eliminated)
  //BUG???  void operator()(eoPop<EOT>& _newgen, unsigned _newsize)
  {
    /* old version
    if (!_eliminated.size())       // nothing to do
      return;
    unsigned oldSize = _newgen.size();
    unsigned newSize = oldSize - _eliminated.size();
    unsigned eliminated = howMany(popSize);
    if (newSize < 0)
      throw std::logic_error("eoStochTournamentTruncateSplit: Cannot truncate to a larger size!\n");

end of old version    */

    unsigned popSize = _newgen.size();
    unsigned eliminated = howMany(popSize);
    if (!eliminated)   // nothing to do
      return ;
    unsigned newsize = popSize - eliminated;
    if (newsize < 0)
      throw std::logic_error("eoDetTournamentTruncateSplit: Cannot truncate to a larger size!\n");



    _eliminated.reserve(_eliminated.size()+eliminated); //in case not empty?
    for (unsigned i=0; i<_eliminated.size(); i++)
      {
        typename eoPop<EOT>::iterator it = inverse_stochastic_tournament(_newgen.begin(), _newgen.end(), t_rate);
        if (returnEliminated)
          _eliminated.push_back(*it);
        _newgen.erase(it);
      }
  }


private:
  double t_rate;
  eoHowMany howMany;
  bool returnEliminated;
};

//-----------------------------------------------------------------------------

/** @} */
#endif