PaGMO  1.1.5
shifted.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 <cmath>
26 
27 #include "../exceptions.h"
28 #include "../types.h"
29 #include "../population.h"
30 #include "base.h"
31 #include "shifted.h"
32 
33 namespace pagmo { namespace problem {
34 
45  const decision_vector & translation):
46  base_meta(
47  p,
48  p.get_dimension(),
49  p.get_i_dimension(),
50  p.get_f_dimension(),
51  p.get_c_dimension(),
52  p.get_ic_dimension(),
53  p.get_c_tol()),
54  m_translation(translation)
55 {
56  if (translation.size() != p.get_dimension()) {
57  pagmo_throw(value_error,"The size of the shifting vector must be equal to the problem dimension");
58  }
59  configure_shifted_bounds(m_translation);
60 }
61 
62 
73  const double t):
74  base_meta(
75  p,
76  p.get_dimension(),
77  p.get_i_dimension(),
78  p.get_f_dimension(),
79  p.get_c_dimension(),
80  p.get_ic_dimension(),
81  p.get_c_tol()),
82  m_translation(decision_vector(p.get_dimension(), t))
83 {
84  configure_shifted_bounds(m_translation);
85 }
86 
96  base_meta(
97  p,
98  p.get_dimension(),
99  p.get_i_dimension(),
100  p.get_f_dimension(),
101  p.get_c_dimension(),
102  p.get_ic_dimension(),
103  p.get_c_tol()),
104  m_translation(decision_vector(p.get_dimension(),0))
105 {
106  for (size_t i=0; i< m_translation.size();++i) {
107  m_translation[i] = (2*((double) rand() / (RAND_MAX))-1) * (p.get_ub()[i]-p.get_lb()[i]);
108  }
109  configure_shifted_bounds(m_translation);
110 }
111 
114 {
115  return base_ptr(new shifted(*this));
116 }
117 
119 void shifted::configure_shifted_bounds(const decision_vector & translation)
120 {
121  decision_vector shifted_lb = m_original_problem->get_lb();
122  decision_vector shifted_ub = m_original_problem->get_ub();
123 
124  for(problem::base::size_type i = 0; i < shifted_lb.size(); ++i) {
125  shifted_lb[i] += translation[i];
126  shifted_ub[i] += translation[i];
127  }
128  set_bounds(shifted_lb, shifted_ub);
129 }
130 
137 {
138  //TODO Need some assertion here?
139  decision_vector x_translated(x.size(), 0);
140  for(problem::base::size_type i = 0; i < x.size(); ++i) {
141  x_translated[i] = x[i] - m_translation[i];
142  }
143  return x_translated;
144 }
145 
149 {
150  decision_vector x_translated = deshift(x);
151  m_original_problem->objfun(f, x_translated);
152 }
153 
157 {
158  decision_vector x_translated = deshift(x);
159  m_original_problem->compute_constraints(c, x_translated);
160 }
161 
169  return m_translation;
170 }
171 
172 std::string shifted::get_name() const
173 {
174  return m_original_problem->get_name() + " [Shifted]";
175 }
176 
178 
181 std::string shifted::human_readable_extra() const
182 {
183  std::ostringstream oss;
184  oss << m_original_problem->human_readable_extra() << std::endl;
185  oss << "\n\tShift vector: " << m_translation << std::endl;
186  return oss.str();
187 }
188 }}
189 
190 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::shifted)
const decision_vector & get_shift_vector() const
Definition: shifted.cpp:168
Root PaGMO namespace.
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base problem.
Definition: problem/base.h:62
std::vector< double > decision_vector
Decision vector type.
Definition: types.h:40
Shifted meta-problem.
Definition: shifted.h:45
void compute_constraints_impl(constraint_vector &, const decision_vector &) const
Definition: shifted.cpp:156
Base problem class.
Definition: problem/base.h:148
size_type get_dimension() const
Return global dimension.
Meta=problems base class.
Definition: base_meta.h:50
shifted(const base &, const decision_vector &)
Definition: shifted.cpp:44
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
std::vector< double > constraint_vector
Constraint vector type.
Definition: types.h:44
const decision_vector & get_ub() const
Upper bounds getter.
void objfun_impl(fitness_vector &, const decision_vector &) const
Definition: shifted.cpp:148
base_ptr clone() const
Clone method.
Definition: shifted.cpp:113
const decision_vector & get_lb() const
Lower bounds getter.
std::string human_readable_extra() const
Extra human readable info for the problem.
Definition: shifted.cpp:181
void set_bounds(const decision_vector &, const decision_vector &)
Bounds setter from pagmo::decision_vector.
std::string get_name() const
Get problem's name.
Definition: shifted.cpp:172
decision_vector deshift(const decision_vector &) const
Definition: shifted.cpp:136
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.
Definition: problem/base.h:160
base_ptr m_original_problem
Smart pointer to the original problem instance.
Definition: base_meta.h:80