📄 eorealop.h
字号:
* @param _alphaMin the amount of exploration OUTSIDE the parents * as in BLX-alpha notation (Eshelman and Schaffer) * 0 == contractive application * Must be positive */ eoSegmentCrossover(const double& _alpha = 0.0) : bounds(eoDummyVectorNoBounds), alpha(_alpha), range(1+2*_alpha) {} /** * Constructor with bounds * @param _bounds an eoRealVectorBounds that contains the bounds * @param _alphaMin the amount of exploration OUTSIDE the parents * as in BLX-alpha notation (Eshelman and Schaffer) * 0 == contractive application * Must be positive */ eoSegmentCrossover(eoRealVectorBounds & _bounds, const double& _alpha = 0.0) : bounds(_bounds), alpha(_alpha), range(1+2*_alpha) {} /// The class name. virtual std::string className() const { return "eoSegmentCrossover"; } /** * segment crossover - modifies both parents * @param _eo1 The first parent * @param _eo2 The first parent */ bool operator()(EOT& _eo1, EOT& _eo2) { unsigned i; double r1, r2, fact; double alphaMin = -alpha; double alphaMax = 1+alpha; if (alpha == 0.0) // no check to perform fact = -alpha + rng.uniform(range); // in [-alpha,1+alpha) else // look for the bounds for fact { for (i=0; i<_eo1.size(); i++) { r1=_eo1[i]; r2=_eo2[i]; if (r1 != r2) { // otherwise you'll get NAN's double rmin = std::min(r1, r2); double rmax = std::max(r1, r2); double length = rmax - rmin; if (bounds.isMinBounded(i)) { alphaMin = std::max(alphaMin, (bounds.minimum(i)-rmin)/length); alphaMax = std::min(alphaMax, (rmax-bounds.minimum(i))/length); } if (bounds.isMaxBounded(i)) { alphaMax = std::min(alphaMax, (bounds.maximum(i)-rmin)/length); alphaMin = std::max(alphaMin, (rmax-bounds.maximum(i))/length); } } } fact = alphaMin + (alphaMax-alphaMin)*rng.uniform(); } for (i=0; i<_eo1.size(); i++) { r1=_eo1[i]; r2=_eo2[i]; _eo1[i] = fact * r1 + (1-fact) * r2; _eo2[i] = (1-fact) * r1 + fact * r2; } return true; // shoudl test if fact was 0 or 1 :-))) }protected: eoRealVectorBounds & bounds; double alpha; double range; // == 1+2*alpha};/** eoHypercubeCrossover --> uniform choice in hypercube == arithmetical with different values for each coordinate\class eoArithmeticCrossover eoRealOp.h Tutorial/eoRealOp.h\ingroup parameteric*/template<class EOT> class eoHypercubeCrossover: public eoQuadOp<EOT>{ public: /** * (Default) Constructor. * The bounds are initialized with the global object that says: no bounds. * * @param _alphaMin the amount of exploration OUTSIDE the parents * as in BLX-alpha notation (Eshelman and Schaffer) * 0 == contractive application * Must be positive */ eoHypercubeCrossover(const double& _alpha = 0.0): bounds(eoDummyVectorNoBounds), alpha(_alpha), range(1+2*_alpha) { if (_alpha < 0) throw std::runtime_error("BLX coefficient should be positive"); } /** * Constructor with bounds * @param _bounds an eoRealVectorBounds that contains the bounds * @param _alphaMin the amount of exploration OUTSIDE the parents * as in BLX-alpha notation (Eshelman and Schaffer) * 0 == contractive application * Must be positive */ eoHypercubeCrossover(eoRealVectorBounds & _bounds, const double& _alpha = 0.0): bounds(_bounds), alpha(_alpha), range(1+2*_alpha) { if (_alpha < 0) throw std::runtime_error("BLX coefficient should be positive"); } /// The class name. virtual std::string className() const { return "eoHypercubeCrossover"; } /** * hypercube crossover - modifies both parents * @param _eo1 The first parent * @param _eo2 The first parent */ bool operator()(EOT& _eo1, EOT& _eo2) { bool hasChanged = false; unsigned i; double r1, r2, fact; if (alpha == 0.0) // no check to perform for (i=0; i<_eo1.size(); i++) { r1=_eo1[i]; r2=_eo2[i]; if (r1 != r2) { // otherwise do nothing fact = rng.uniform(range); // in [0,1) _eo1[i] = fact * r1 + (1-fact) * r2; _eo2[i] = (1-fact) * r1 + fact * r2; hasChanged = true; // forget (im)possible alpha=0 } } else // check the bounds // do not try to get a bound on the linear factor, but rather // on the object variables themselves for (i=0; i<_eo1.size(); i++) { r1=_eo1[i]; r2=_eo2[i]; if (r1 != r2) { // otherwise do nothing double rmin = std::min(r1, r2); double rmax = std::max(r1, r2); // compute min and max for object variables double objMin = -alpha * rmax + (1+alpha) * rmin; double objMax = -alpha * rmin + (1+alpha) * rmax; // first find the limits on the alpha's if (bounds.isMinBounded(i)) { objMin = std::max(objMin, bounds.minimum(i)); } if (bounds.isMaxBounded(i)) { objMax = std::min(objMax, bounds.maximum(i)); } // then draw variables double median = (objMin+objMax)/2.0; // uniform within bounds // double median = (rmin+rmax)/2.0; // Bounce on bounds double valMin = objMin + (median-objMin)*rng.uniform(); double valMax = median + (objMax-median)*rng.uniform(); // don't always put large value in _eo1 - or what? if (rng.flip(0.5)) { _eo1[i] = valMin; _eo2[i] = valMax; } else { _eo1[i] = valMax; _eo2[i] = valMin; } // seomthing has changed hasChanged = true; // forget (im)possible alpha=0 } } return hasChanged; }protected: eoRealVectorBounds & bounds; double alpha; double range; // == 1+2*alphaMin};/** eoRealUxOver --> Uniform crossover, also termed intermediate crossover\class eoRealUxOver eoRealOp.h Tutorial/eoRealOp.h\ingroup parameteric*/template<class EOT> class eoRealUXover: public eoQuadOp<EOT>{ public: /** * (Default) Constructor. * @param _preference bias in the choice (usually, no bias == 0.5) */ eoRealUXover(const float& _preference = 0.5): preference(_preference) { if ( (_preference <= 0.0) || (_preference >= 1.0) ) std::runtime_error("UxOver --> invalid preference"); } /// The class name. virtual std::string className() const { return "eoRealUXover"; } /** * Uniform crossover for real std::vectors * @param _eo1 The first parent * @param _eo2 The second parent * @std::runtime_error if sizes don't match */ bool operator()(EOT& _eo1, EOT& _eo2) { if ( _eo1.size() != _eo2.size()) std::runtime_error("UxOver --> chromosomes sizes don't match" ); bool changed = false; for (unsigned int i=0; i<_eo1.size(); i++) { if (rng.flip(preference)) if (_eo1[i] != _eo2[i]) { double tmp = _eo1[i]; _eo1[i]=_eo2[i]; _eo2[i] = tmp; changed = true; } } return changed; } private: float preference;};//-----------------------------------------------------------------------------//@}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -