00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _eoOpContainer_H
00027 #define _eoOpContainer_H
00028
00029 #include <eoGenOp.h>
00030
00041 template <class EOT>
00042 class eoOpContainer : public eoGenOp<EOT>
00043 {
00044 public :
00046 eoOpContainer() : max_to_produce(0) {}
00047
00050 virtual ~eoOpContainer(void) {}
00051
00053 virtual unsigned max_production(void)
00054 {
00055 return max_to_produce;
00056 }
00057
00063 void add(eoOp<EOT>& _op, double _rate)
00064 {
00065 ops.push_back(&wrap_op<EOT>(_op, store));
00066 rates.push_back(_rate);
00067 max_to_produce = std::max(max_to_produce,ops.back()->max_production());
00068 }
00069
00070 virtual std::string className() const = 0;
00071
00072 protected :
00073
00074 std::vector<double> rates;
00075 std::vector<eoGenOp<EOT>*> ops;
00076
00077 private :
00078 eoFunctorStore store;
00079 unsigned max_to_produce;
00080 };
00081
00087 template <class EOT>
00088 class eoSequentialOp : public eoOpContainer<EOT>
00089 {
00090 public:
00091
00092 using eoOpContainer<EOT>::ops;
00093 using eoOpContainer<EOT>::rates;
00094
00095 typedef unsigned position_type;
00096
00097
00098 void apply(eoPopulator<EOT>& _pop) {
00099 position_type pos = _pop.tellp();
00100 for (size_t i = 0; i < rates.size(); ++i) {
00101 _pop.seekp(pos);
00102 do {
00103 if (eo::rng.flip(rates[i])) {
00104
00105
00106
00107 (*ops[i])(_pop);
00108
00109
00110
00111
00112
00113
00114
00115 }
00116
00117 if (!_pop.exhausted())
00118 ++_pop;
00119 }
00120 while (!_pop.exhausted());
00121 }
00122 }
00123 virtual std::string className() const {return "SequentialOp";}
00124
00125 private:
00126
00127 std::vector<size_t> to_apply;
00128 std::vector<size_t> production;
00129 };
00130
00131
00132
00134 template <class EOT>
00135 class eoProportionalOp : public eoOpContainer<EOT>
00136 {
00137 public:
00138
00139 using eoOpContainer< EOT >::ops;
00140 using eoOpContainer< EOT >::rates;
00141
00142 void apply(eoPopulator<EOT>& _pop)
00143 {
00144 unsigned i = eo::rng.roulette_wheel(rates);
00145
00146 try
00147 {
00148 (*ops[i])(_pop);
00149 ++_pop;
00150 }
00151 catch( typename eoPopulator<EOT>::OutOfIndividuals&)
00152 {}
00153 }
00154 virtual std::string className() const {return "ProportionalOp";}
00155 };
00156
00157
00158 #endif
00159