PaGMO  1.1.5
golomb_ruler.cpp
1 /*****************************************************************************
2  * Copyright (C) 2004-2015 The PaGMO development team, *
3  * Advanced Concepts Team (ACT), European Space Agency (ESA) *
4  * *
5  * https://github.com/esa/pagmo *
6  * *
7  * act@esa.int *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  *****************************************************************************/
24 
25 #include <algorithm>
26 #include <boost/integer_traits.hpp>
27 #include <boost/numeric/conversion/cast.hpp>
28 #include <climits>
29 #include <cstddef>
30 #include <exception>
31 #include <iterator>
32 #include <typeinfo>
33 
34 #include <string>
35 
36 #include "../exceptions.h"
37 #include "base.h"
38 #include "golomb_ruler.h"
39 
40 namespace pagmo { namespace problem {
41 
42 static inline int check_golomb_order(int n)
43 {
44  if (n < 2) {
45  pagmo_throw(value_error,"Golomb ruler problem must have at least order 2");
46  }
47  return n;
48 }
49 
51 
57 golomb_ruler::golomb_ruler(int n, int m):base(check_golomb_order(n) - 1,n - 1,1,1,0),m_max_length(boost::numeric_cast<std::size_t>(m))
58 {
59  if (!m_max_length || m_max_length > static_cast<std::size_t>(INT_MAX)) {
60  pagmo_throw(value_error,"maximum distance between consecutive marks must be in the ]0,32767] range");
61  }
62  if (get_dimension() == boost::integer_traits<size_type>::const_max) {
63  pagmo_throw(std::overflow_error,"size overflow in Golomb ruler problem");
64  }
65  if (double(m_max_length) * get_dimension() > static_cast<double>(INT_MAX)) {
66  pagmo_throw(std::overflow_error,"fitness value overflow in Golomb ruler problem");
67  }
68  // Lower bound is already set to 0.
69  set_ub(m_max_length);
70 }
71 
74 {
75  return base_ptr(new golomb_ruler(*this));
76 }
77 
79 
83 {
84  pagmo_assert(typeid(*this) == typeid(other));
85  return (m_max_length == dynamic_cast<golomb_ruler const &>(other).m_max_length);
86 }
87 
89 
96 {
97  pagmo_assert(f.size() == 1 && x.size() == get_dimension());
98  compute_marks_and_dist(x);
99  // Fitness is the maximum distance.
100  f[0] = *std::max_element(m_tmp_dist.begin(),m_tmp_dist.end());
101 }
102 
104 
112 {
113  pagmo_assert(c.size() == 1 && x.size() == get_dimension());
114  compute_marks_and_dist(x);
115  // Sort the vector of distances.
116  std::sort(m_tmp_dist.begin(),m_tmp_dist.end());
117  // Now compute how many duplicate distances are there.
118  c[0] = boost::numeric_cast<double>(m_tmp_dist.size()) - std::distance(m_tmp_dist.begin(),std::unique(m_tmp_dist.begin(),m_tmp_dist.end()));
119 }
120 
121 // Compute marks and distances of x and store them internally.
122 void golomb_ruler::compute_marks_and_dist(const decision_vector &x) const
123 {
124  // We already computed distances and marks of this decision vector, do not do anything.
125  if (x == m_tmp_x) {
126  return;
127  }
128  m_tmp_x = x;
129  const size_type size = m_tmp_x.size(), marks_size = size + 1;
130  m_tmp_marks.resize(marks_size);
131  m_tmp_marks[0] = 0;
132  // Write marks into temporary vector.
133  for (size_type i = 0; i < size; ++i) {
134  m_tmp_marks[i + 1] = m_tmp_marks[i] + m_tmp_x[i];
135  }
136  // Clear distances vector.
137  m_tmp_dist.clear();
138  for (size_type i = 0; i < marks_size - 1; ++i) {
139  for (size_type j = i + 1; j < marks_size; ++j) {
140  const double tmp = m_tmp_marks[j] - m_tmp_marks[i];
141  pagmo_assert(tmp >= 0);
142  m_tmp_dist.push_back(tmp);
143  }
144  }
145 }
146 
147 std::string golomb_ruler::get_name() const
148 {
149  return "Golomb ruler";
150 }
151 
152 }}
153 
154 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::golomb_ruler)
Golomb ruler problem.
Definition: golomb_ruler.h:56
Root PaGMO namespace.
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base problem.
Definition: problem/base.h:62
void compute_constraints_impl(constraint_vector &, const decision_vector &) const
Implementation of constraint calculation.
std::vector< double > decision_vector
Decision vector type.
Definition: types.h:40
base_ptr clone() const
Clone method.
golomb_ruler(int=5, int=10)
Constructor from order and maximum distance between consecutive marks.
STL namespace.
Base problem class.
Definition: problem/base.h:148
size_type get_dimension() const
Return global dimension.
bool equality_operator_extra(const base &) const
Additional requirements for equality.
void set_ub(const decision_vector &)
Set upper bounds from pagmo::decision_vector.
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
std::vector< double > constraint_vector
Constraint vector type.
Definition: types.h:44
void objfun_impl(fitness_vector &, const decision_vector &) const
Implementation of the objective function.
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.
Definition: problem/base.h:160
std::string get_name() const
Get problem's name.