PaGMO  1.1.5
con2uncon.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 <algorithm>
27 
28 #include "../exceptions.h"
29 #include "../types.h"
30 #include "con2uncon.h"
31 
32 namespace pagmo { namespace problem {
33 
41 con2uncon::con2uncon(const base &problem, const method_type method):
42  base_meta(problem,
43  problem.get_dimension(),
44  problem.get_i_dimension(),
45  problem.get_f_dimension(),
46  0,
47  0,
48  std::vector<double>()),
49  m_method(method)
50 {
51  if((m_method < 0) || (m_method > 1)) {
52  pagmo_throw(value_error,"the problem method must be either OPTIMALITY or FEASIBILITY.");
53  }
54 }
55 
58 {
59  return base_ptr(new con2uncon(*this));
60 }
61 
65 {
66  switch(m_method) {
67  case(OPTIMALITY): {
68  m_original_problem->objfun(f,x);
69  break;
70  }
71  case(FEASIBILITY): {
72  // get the constraints dimension
73  problem::base::c_size_type prob_c_dimension = m_original_problem->get_c_dimension();
74  problem::base::c_size_type number_of_eq_constraints =
75  m_original_problem->get_c_dimension() -
76  m_original_problem->get_ic_dimension();
77 
78  constraint_vector c(m_original_problem->get_c_dimension(),0);
79  m_original_problem->compute_constraints(c,x);
80 
81  const std::vector<double> &c_tol = m_original_problem->get_c_tol();
82 
83  double c_func = 0.;
84 
85  // update equality constraints
86  for(problem::base::c_size_type j=0; j<number_of_eq_constraints; j++) {
87  if(!m_original_problem->test_constraint(c,j)) {
88  c_func += ( std::abs(c.at(j)) - c_tol.at(j) ) * ( std::abs(c.at(j)) - c_tol.at(j) );
89  }
90  }
91 
92  // update inequality constraints
93  for(problem::base::c_size_type j=number_of_eq_constraints; j<prob_c_dimension; j++) {
94  if(!m_original_problem->test_constraint(c,j)) {
95  c_func += c.at(j) - c_tol.at(j);
96  }
97  }
98 
99  std::fill(f.begin(),f.end(), 0.);
100 
101  //checking overflow
102  if(c_func > boost::numeric::bounds<double>::highest()){
103  f[0] = boost::numeric::bounds<double>::highest();
104  }
105  else{
106  f[0] = c_func;
107  }
108 
109  break;
110  }
111  }
112 }
113 
115 
119 {
120  std::ostringstream oss;
121  oss << m_original_problem->human_readable_extra() << std::endl;
122  oss << "\n\tcon2unconed with method ";
123  switch(m_method){
124  case OPTIMALITY: {
125  oss << "OPTIMALITY ";
126  break;
127  }
128  case FEASIBILITY: {
129  oss << "FEASIBILITY ";
130  break;
131  }
132  }
133  oss << std::endl;
134  return oss.str();
135 }
136 
137 std::string con2uncon::get_name() const
138 {
139  std::string method;
140 
141  switch(m_method){
142  case OPTIMALITY: {
143  method = "OPTIMALITY ";
144  break;
145  }
146  case FEASIBILITY: {
147  method = "FEASIBILITY ";
148  break;
149  }
150  }
151  return m_original_problem->get_name() + " [con2uncon, method_" + method + "]";
152 }
153 
154 }}
155 
156 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::con2uncon)
157 
Root PaGMO namespace.
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base problem.
Definition: problem/base.h:62
con2uncon(const base &=cec2006(4), const method_type=OPTIMALITY)
Definition: con2uncon.cpp:41
std::vector< double > decision_vector
Decision vector type.
Definition: types.h:40
The sum of the constraint violation is used as objective function of the transformed problem...
Definition: con2uncon.h:54
std::string get_name() const
Get problem's name.
Definition: con2uncon.cpp:137
The objective function of the original problem is used as objective function of the transformed probl...
Definition: con2uncon.h:53
STL namespace.
Base problem class.
Definition: problem/base.h:148
Constrained to unconstrained meta-problem.
Definition: con2uncon.h:48
Meta=problems base class.
Definition: base_meta.h:50
method_type
Mechanism used to transform the input problem.
Definition: con2uncon.h:52
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
std::vector< double > constraint_vector
Constraint vector type.
Definition: types.h:44
std::string human_readable_extra() const
Extra human readable info for the problem.
Definition: con2uncon.cpp:118
constraint_vector::size_type c_size_type
Constraints' size type: the same as pagmo::constraint_vector's size type.
Definition: problem/base.h:164
void objfun_impl(fitness_vector &, const decision_vector &) const
Definition: con2uncon.cpp:64
base_ptr clone() const
Clone method.
Definition: con2uncon.cpp:57
base_ptr m_original_problem
Smart pointer to the original problem instance.
Definition: base_meta.h:80