25 #include "noboundstrategy.h"
26 #include "pwq_bound_strategy.h"
32 typedef std::function<void (const double*, double*, const int&)> TransFunc;
34 template <
class TBoundStrategy=NoBoundStrategy,
class TScalingStrategy=NoScalingStrategy>
44 GenoPheno(TransFunc &genof, TransFunc &phenof)
45 :_genof(genof),_phenof(phenof),_id(
false)
48 GenoPheno(
const double *lbounds,
const double *ubounds,
const int &dim)
49 :_boundstrategy(lbounds,ubounds,dim),_id(
true),_scalingstrategy(lbounds,ubounds,dim)
51 if (_scalingstrategy._id)
52 _boundstrategy = TBoundStrategy(lbounds,ubounds,dim);
55 std::vector<double> lb(dim,_scalingstrategy._intmin);
56 std::vector<double> ub(dim,_scalingstrategy._intmax);
57 _boundstrategy = TBoundStrategy(&lb.front(),&ub.front(),lbounds,ubounds,dim);
61 GenoPheno(TransFunc &genof, TransFunc &phenof,
62 const double *lbounds,
const double *ubounds,
const int &dim)
63 :_boundstrategy(lbounds,ubounds,dim),_genof(genof),_phenof(phenof),_id(
false),_scalingstrategy(lbounds,ubounds,dim)
65 if (_scalingstrategy._id)
66 _boundstrategy = TBoundStrategy(lbounds,ubounds,dim);
69 std::vector<double> lb(dim,_scalingstrategy._intmin);
70 std::vector<double> ub(dim,_scalingstrategy._intmax);
71 _boundstrategy = TBoundStrategy(&lb.front(),&ub.front(),lbounds,ubounds,dim);
85 const double *lbounds=
nullptr,
86 const double *ubounds=
nullptr)
98 dMat pheno_candidates(
const dMat &candidates)
const
102 dMat ncandidates = dMat(candidates.rows(),candidates.cols());
103 #pragma omp parallel for if (candidates.cols() >= 100)
104 for (
int i=0;i<candidates.cols();i++)
106 dVec ext = dVec(candidates.rows());
107 _phenof(candidates.col(i).data(),ext.data(),candidates.rows());
108 ncandidates.col(i) = ext;
115 dMat geno_candidates(
const dMat &candidates)
const
119 dMat ncandidates = dMat(candidates.rows(),candidates.cols());
120 #pragma omp parallel for if (candidates.cols() >= 100)
121 for (
int i=0;i<candidates.cols();i++)
123 dVec in = dVec(candidates.rows());
124 _genof(candidates.col(i).data(),in.data(),candidates.rows());
125 ncandidates.col(i) = in;
133 dMat pheno(
const dMat &candidates)
const
136 dMat ncandidates = pheno_candidates(candidates);
139 #pragma omp parallel for if (ncandidates.cols() >= 100)
140 for (
int i=0;i<ncandidates.cols();i++)
143 _boundstrategy.to_f_representation(ncandidates.col(i),ycoli);
144 ncandidates.col(i) = ycoli;
148 if (!_scalingstrategy._id)
150 #pragma omp parallel for if (ncandidates.cols() >= 100)
151 for (
int i=0;i<ncandidates.cols();i++)
154 _scalingstrategy.scale_to_f(ncandidates.col(i),ycoli);
155 ncandidates.col(i) = ycoli;
161 dMat geno(
const dMat &candidates)
const
164 dMat ncandidates = candidates;
165 if (!_scalingstrategy._id)
167 #pragma omp parallel for if (ncandidates.cols() >= 100)
168 for (
int i=0;i<ncandidates.cols();i++)
171 _scalingstrategy.scale_to_internal(ycoli,ncandidates.col(i));
172 ncandidates.col(i) = ycoli;
177 #pragma omp parallel for if (ncandidates.cols() >= 100)
178 for (
int i=0;i<ncandidates.cols();i++)
181 _boundstrategy.to_internal_representation(ycoli,ncandidates.col(i));
182 ncandidates.col(i) = ycoli;
186 ncandidates = geno_candidates(ncandidates);
190 dVec pheno(
const dVec &candidate)
const
196 ncandidate = dVec(candidate.rows());
197 _phenof(candidate.data(),ncandidate.data(),candidate.rows());
201 dVec phen = dVec::Zero(candidate.rows());
203 _boundstrategy.to_f_representation(candidate,phen);
204 else _boundstrategy.to_f_representation(ncandidate,phen);
207 if (!_scalingstrategy._id)
209 dVec sphen = dVec::Zero(phen.rows());
210 _scalingstrategy.scale_to_f(phen,sphen);
216 dVec geno(
const dVec &candidate)
const
218 dVec ccandidate = candidate;
219 dVec gen = dVec::Zero(candidate.rows());
222 if (!_scalingstrategy._id)
224 _scalingstrategy.scale_to_internal(gen,candidate);
229 _boundstrategy.to_internal_representation(gen,ccandidate);
234 dVec ncandidate(gen.rows());
235 _genof(gen.data(),ncandidate.data(),gen.rows());
241 TBoundStrategy get_boundstrategy()
const {
return _boundstrategy; }
243 TBoundStrategy& get_boundstrategy_ref() {
return _boundstrategy; }
245 TScalingStrategy get_scalingstrategy()
const {
return _scalingstrategy; }
247 void remove_dimensions(
const std::vector<int> &k)
249 if (!_scalingstrategy.is_id())
250 _scalingstrategy.remove_dimensions(k);
251 if (!_boundstrategy.is_id())
252 _boundstrategy.remove_dimensions(k);
256 TBoundStrategy _boundstrategy;
260 TScalingStrategy _scalingstrategy;
264 template<>
inline dMat GenoPheno<NoBoundStrategy,NoScalingStrategy>::pheno(
const dMat &candidates)
const
268 else return pheno_candidates(candidates);
270 template<>
inline dVec GenoPheno<NoBoundStrategy,NoScalingStrategy>::pheno(
const dVec &candidate)
const
276 dVec ncandidate(candidate.rows());
277 _phenof(candidate.data(),ncandidate.data(),candidate.rows());
281 template<>
inline dVec GenoPheno<NoBoundStrategy,NoScalingStrategy>::geno(
const dVec &candidate)
const
287 dVec ncandidate(candidate.rows());
288 _genof(candidate.data(),ncandidate.data(),candidate.rows());
293 template<>
inline dVec GenoPheno<NoBoundStrategy,linScalingStrategy>::pheno(
const dVec &candidate)
const
295 dVec ncandidate(candidate.rows());
297 _phenof(candidate.data(),ncandidate.data(),candidate.rows());
301 _scalingstrategy.scale_to_f(ncandidate,sphen);
302 else _scalingstrategy.scale_to_f(candidate,sphen);
305 template<>
inline dVec GenoPheno<NoBoundStrategy,linScalingStrategy>::geno(
const dVec &candidate)
const
307 dVec scand = dVec::Zero(candidate.rows());
308 _scalingstrategy.scale_to_internal(scand,candidate);
313 dVec ncandidate(candidate.rows());
314 _genof(scand.data(),scand.data(),candidate.rows());
318 template<>
inline dMat GenoPheno<NoBoundStrategy,linScalingStrategy>::pheno(
const dMat &candidates)
const
322 ncandidates = pheno_candidates(candidates);
323 else ncandidates = candidates;
326 #pragma omp parallel for if (ncandidates.cols() >= 100)
327 for (
int i=0;i<ncandidates.cols();i++)
330 _scalingstrategy.scale_to_f(ncandidates.col(i),ycoli);
331 ncandidates.col(i) = ycoli;
336 template<>
inline GenoPheno<NoBoundStrategy,linScalingStrategy>::GenoPheno(
const dVec &scaling,
338 const double *lbounds,
339 const double *ubounds)
344 _scalingstrategy = linScalingStrategy(scaling,shift);
347 template<>
inline GenoPheno<pwqBoundStrategy,linScalingStrategy>::GenoPheno(
const dVec &scaling,
349 const double *lbounds,
350 const double *ubounds)
353 _scalingstrategy = linScalingStrategy(scaling,shift);
354 if (lbounds ==
nullptr || ubounds ==
nullptr)
356 dVec vlbounds = Eigen::Map<dVec>(
const_cast<double*
>(lbounds),scaling.size());
357 dVec vubounds = Eigen::Map<dVec>(
const_cast<double*
>(ubounds),scaling.size());
358 dVec nlbounds, nubounds;
359 _scalingstrategy.scale_to_internal(nlbounds,vlbounds);
360 _scalingstrategy.scale_to_internal(nubounds,vubounds);
361 _boundstrategy = pwqBoundStrategy(nlbounds.data(),nubounds.data(),scaling.size());
Holder of the set of evolving solutions from running an instance of CMA-ES.
Definition: cmasolutions.h:41
GenoPheno(const dVec &scaling, const dVec &shift, const double *lbounds=nullptr, const double *ubounds=nullptr)
this is a dummy constructor to accomodate an easy to use linear scaling with pwq bounds from a given ...
Definition: genopheno.h:83
linear scaling of the parameter space to achieve similar sensitivity across all components.
Definition: acovarianceupdate.cc:25
Definition: genopheno.h:35