26 #include <boost/random/uniform_int.hpp>
27 #include <boost/random/uniform_real.hpp>
30 namespace pagmo {
namespace algorithm {
44 cs::cs(
const int& max_eval,
const double &stop_range,
const double &start_range,
const double &reduction_coeff)
45 :
base(),m_stop_range(stop_range),m_start_range(start_range),m_reduction_coeff(reduction_coeff),m_max_eval(max_eval)
47 if (reduction_coeff >= 1 || reduction_coeff <=0) {
48 pagmo_throw(value_error,
"the reduction coefficient must be smaller than one and positive, You Fool!!");
50 if (start_range > 1 || start_range <= 0) {
51 pagmo_throw(value_error,
"the starting range must be smaller than one and positive, You Fool!!");
53 if (stop_range > 1 || stop_range <= 0 || stop_range>start_range) {
54 pagmo_throw(value_error,
"the minimum range must be smaller than one, positive and smaller than the starting range, (o44portebat studuisse)!!");
57 pagmo_throw(value_error,
"Maximum number of function evaluations needs to be positive");
88 pagmo_throw(value_error,
"There is no continuous part in the problem decision vector for compass search to optimise");
91 if ( prob_c_dimension != 0 ) {
92 pagmo_throw(value_error,
"The problem is not box constrained and compass search is not suitable to solve it");
95 if ( prob_f_dimension != 1 ) {
96 pagmo_throw(value_error,
"The problem is not single objective and compass search is not suitable to solve it");
100 if (NP == 0 || m_max_eval == 0) {
105 const int bestidx = pop.get_best_idx();
114 double newrange=m_start_range;
116 while (newrange > m_stop_range && eval <= m_max_eval) {
118 for (
unsigned int i=0; i<Dc; i++) {
122 newx[i] = x[i] + newrange * (ub[i]-lb[i]);
124 if (newx[i] > ub [i]) newx[i]=ub[i];
126 prob.
objfun(newf,newx); eval++;
135 newx[i] = x[i] - newrange * (ub[i]-lb[i]);
137 if (newx[i] < lb [i]) newx[i]=lb[i];
139 prob.
objfun(newf,newx); eval++;
148 newrange *= m_reduction_coeff;
151 std::transform(x.begin(), x.end(), pop.
get_individual(bestidx).
cur_x.begin(), newx.begin(),std::minus<double>());
152 pop.set_x(bestidx,x);
153 pop.set_v(bestidx,newx);
159 return "Compass Search";
169 std::ostringstream s;
170 s <<
"max_eval:" << m_max_eval <<
' ';
171 s <<
"stop_range:" << m_stop_range <<
' ';
172 s <<
"start_range:" << m_start_range <<
' ';
173 s <<
"reduction_coeff:" << m_reduction_coeff;
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
std::vector< double > decision_vector
Decision vector type.
fitness_vector cur_f
Current fitness vector.
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
base_ptr clone() const
Clone method.
cs(const int &max_eval=1, const double &stop_range=0.01, const double &start_range=0.1, const double &reduction_coeff=0.5)
Constructor.
size_type get_dimension() const
Return global dimension.
bool compare_fitness(const fitness_vector &, const fitness_vector &) const
Compare fitness vectors.
fitness_vector objfun(const decision_vector &) const
Return fitness of pagmo::decision_vector.
std::string get_name() const
Algorithm name.
size_type get_i_dimension() const
Return integer dimension.
std::string human_readable_extra() const
Extra human readable algorithm info.
std::vector< double > fitness_vector
Fitness vector type.
c_size_type get_c_dimension() const
Return global constraints dimension.
const decision_vector & get_ub() const
Upper bounds getter.
container_type::size_type size_type
Population size type.
decision_vector cur_x
Current decision vector.
void evolve(population &) const
Evolve implementation.
f_size_type get_f_dimension() const
Return fitness dimension.
const decision_vector & get_lb() const
Lower bounds getter.
The Compass Search Solver (CS)
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.