PaGMO  1.1.5
noisy.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 #include <iostream>
27 #include <boost/functional/hash.hpp>
28 
29 #include "../exceptions.h"
30 #include "../types.h"
31 #include "../population.h"
32 #include "base.h"
33 #include "noisy.h"
34 
35 using namespace std;
36 
37 namespace pagmo { namespace problem {
38 
54 noisy::noisy(const base & p, unsigned int trials, const double param_first, const double param_second, noise_type distribution, unsigned int seed):
55  base_stochastic((int)p.get_dimension(),
56  p.get_i_dimension(),
57  p.get_f_dimension(),
58  p.get_c_dimension(),
59  p.get_ic_dimension(),
60  p.get_c_tol(), seed),
61  m_original_problem(p.clone()),
62  m_trials(trials),
63  m_normal_dist(0.0,1.0),
64  m_uniform_dist(0.0,1.0),
65  m_decision_vector_hash(),
66  m_param_first(param_first),
67  m_param_second(param_second),
68  m_noise_type(distribution)
69 {
70  if(distribution == UNIFORM && param_first > param_second){
71  pagmo_throw(value_error, "Bounds specified for the uniform noise are not valid.");
72  }
73  set_bounds(p.get_lb(),p.get_ub());
74 }
75 
77 noisy::noisy(const noisy &prob):
78  base_stochastic(prob),
79  m_original_problem(prob.m_original_problem->clone()),
80  m_trials(prob.m_trials),
81  m_normal_dist(0.0,1.0),
82  m_uniform_dist(0.0,1.0),
83  m_decision_vector_hash(),
84  m_param_first(prob.m_param_first),
85  m_param_second(prob.m_param_second),
86  m_noise_type(prob.m_noise_type) {}
87 
90 {
91  return base_ptr(new noisy(*this));
92 }
93 
101 void noisy::set_noise_param(double param_first, double param_second)
102 {
103  if(m_noise_type == UNIFORM && param_first > param_second){
104  pagmo_throw(value_error, "Bounds specified for the uniform noise are not valid.");
105  }
106  m_param_first = param_first;
107  m_param_second = param_second;
108 }
109 
115 {
116  return m_param_first;
117 }
118 
124 {
125  return m_param_second;
126 }
127 
131 {
132  //1 - Initialize a temporary fitness vector storing one trial result
133  //and we use it also to init the return value
134  fitness_vector tmp(f.size(),0.0);
135  f=tmp;
136  //2 - We set the seed
137  m_drng.seed(m_seed+m_decision_vector_hash(x));
138  //3 - We average upon multiple runs
139  for (unsigned int j=0; j< m_trials; ++j) {
140  m_original_problem->objfun(tmp, x);
141  inject_noise_f(tmp);
142  for (fitness_vector::size_type i=0; i<f.size();++i) {
143  f[i] = f[i] + tmp[i] / (double)m_trials;
144  }
145  }
146 }
147 
151 {
152  //1 - Initialize a temporary constraint vector storing one trial result
153  //and we use it also to init the return value
154  constraint_vector tmp(c.size(),0.0);
155  c=tmp;
156  //2 - We set the seed
157  m_drng.seed(m_seed+m_decision_vector_hash(x));
158  //3 - We average upon multiple runs
159  for (unsigned int j=0; j< m_trials; ++j) {
160  m_original_problem->compute_constraints(tmp, x);
161  inject_noise_c(tmp);
162  for (constraint_vector::size_type i=0; i<c.size();++i) {
163  c[i] = c[i] + tmp[i] / (double)m_trials;
164  }
165  }
166 }
167 
169 void noisy::inject_noise_f(fitness_vector& f) const
170 {
171  for(f_size_type i = 0; i < f.size(); i++){
172  if(m_noise_type == NORMAL){
173  f[i] += m_normal_dist(m_drng)*m_param_second+m_param_first;
174  }
175  else if(m_noise_type == UNIFORM){
176  f[i] += m_uniform_dist(m_drng)*(m_param_second-m_param_first)+m_param_first;
177  }
178  }
179 }
180 
182 void noisy::inject_noise_c(constraint_vector& c) const
183 {
184  for(c_size_type i = 0; i < c.size(); i++){
185  if(m_noise_type == NORMAL){
186  c[i] += m_normal_dist(m_drng)*m_param_second+m_param_first;
187  }
188  else if(m_noise_type == UNIFORM){
189  c[i] += m_uniform_dist(m_drng)*(m_param_second-m_param_first)+m_param_first;
190  }
191  }
192 }
193 
194 std::string noisy::get_name() const
195 {
196  return m_original_problem->get_name() + " [Noisy]";
197 }
198 
200 
203 std::string noisy::human_readable_extra() const
204 {
205  std::ostringstream oss;
206  oss << m_original_problem->human_readable_extra() << std::endl;
207  oss << "\tNoise type: ";
208  if(m_noise_type == NORMAL){
209  oss << "Normal distribution of ("<<m_param_first<<","<<m_param_second<<")";
210  }
211  else if(m_noise_type == UNIFORM){
212  oss << "Uniform distribution over ("<<m_param_first<<","<<m_param_second<<")";
213  }
214  else{
215  oss << "\n\t Unknown????";
216  }
217  oss << "\n\ttrials: "<<m_trials;
218  oss << "\n\tseed: "<<m_seed << std::endl;
219  //oss << "\n\tDistribution state: "<<m_normal_dist;
220  return oss.str();
221 }
222 }}
223 
224 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::noisy)
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
Normal distribution.
Definition: noisy.h:60
fitness_vector::size_type f_size_type
Fitness' size type: the same as pagmo::fitness_vector's size type.
Definition: problem/base.h:162
Uniform distribution.
Definition: noisy.h:61
STL namespace.
Base problem class.
Definition: problem/base.h:148
rng_double m_drng
Random number generator for double-precision floating point values.
base_ptr clone() const
Clone method.
Definition: noisy.cpp:89
unsigned int m_seed
Seed of the random number generator.
noise_type
Distribution type of the noise.
Definition: noisy.h:59
std::string human_readable_extra() const
Extra human readable info for the problem.
Definition: noisy.cpp:203
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
void objfun_impl(fitness_vector &, const decision_vector &) const
Definition: noisy.cpp:130
std::vector< double > constraint_vector
Constraint vector type.
Definition: types.h:44
const decision_vector & get_ub() const
Upper bounds getter.
Base Stochastic Optimization Problem.
Noisy meta-problem.
Definition: noisy.h:55
constraint_vector::size_type c_size_type
Constraints' size type: the same as pagmo::constraint_vector's size type.
Definition: problem/base.h:164
const decision_vector & get_lb() const
Lower bounds getter.
void set_bounds(const decision_vector &, const decision_vector &)
Bounds setter from pagmo::decision_vector.
void compute_constraints_impl(constraint_vector &, const decision_vector &) const
Definition: noisy.cpp:150
void set_noise_param(double, double)
Definition: noisy.cpp:101
noisy(const base &=ackley(1), unsigned int trials=1, const double param_first=0.0, const double param_second=0.1, noise_type=NORMAL, unsigned int seed=0u)
Definition: noisy.cpp:54
double get_param_first() const
Definition: noisy.cpp:114
double get_param_second() const
Definition: noisy.cpp:123
std::string get_name() const
Get problem's name.
Definition: noisy.cpp:194