PaGMO  1.1.5
con2mo.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 "con2mo.h"
31 
32 static int __mo_dimension__(const pagmo::problem::base &original_problem,
34 {
35  if( method > 2 || method < 0) {
36  pagmo_throw(value_error, "The constrained to multi-objective method must be set to OBJ_CSTRS for Coello type constrained to multi-objective, to OBJ_CSTRSVIO for COMOGO type constrained to nobj+1 objectives problem or to OBJ_EQVIO_INEQVIO for COMOGO type constrained to nobj+2 objectives problem.");
37  }
38 
39  if(original_problem.get_c_dimension() <= 0){
40  pagmo_throw(value_error,"The original problem has no constraints.");
41  }
42 
43  switch(method)
44  {
46  {
47  return original_problem.get_f_dimension() + original_problem.get_c_dimension();
48  break;
49  }
51  {
52  return original_problem.get_f_dimension() + 1;
53  break;
54  }
56  {
57  return original_problem.get_f_dimension() + 2;
58  break;
59  }
60  default:
61  {
62  return 0.;
63  break;
64  }
65  }
66 }
67 
68 namespace pagmo { namespace problem {
69 
79 con2mo::con2mo(const base &problem, const method_type method):
80  base_meta(
81  problem,
82  problem.get_dimension(),
83  problem.get_i_dimension(),
84  __mo_dimension__(problem, method),
85  0,
86  0,
87  std::vector<double>()),
88  m_method(method)
89 {}
90 
93 {
94  return base_ptr(new con2mo(*this));
95 }
96 
100 {
101  constraint_vector c(m_original_problem->get_c_dimension(),0.);
102  m_original_problem->compute_constraints(c,x);
103 
104  decision_vector original_f(m_original_problem->get_f_dimension(),0.);
105  m_original_problem->objfun(original_f,x);
106 
107  f_size_type original_nbr_obj = original_f.size();
108  c_size_type number_of_constraints = c.size();
109  c_size_type number_of_eq_constraints = number_of_constraints - m_original_problem->get_ic_dimension();
110  c_size_type number_of_violated_constraints = 0;
111 
112  // computes the number of satisfied constraints
113  if(m_method==OBJ_CSTRS){
114  for(c_size_type i=0; i<number_of_constraints; i++){
115  if(!m_original_problem->test_constraint(c,i))
116  number_of_violated_constraints += 1;
117  }
118  }
119 
120  // modify equality constraints to behave as inequality constraints:
121 
122  const std::vector<double> &c_tol = m_original_problem->get_c_tol();
123 
124  for(c_size_type i=0; i<number_of_constraints; i++) {
125  if(i<number_of_eq_constraints){
126  c[i] = std::abs(c[i]) - c_tol.at(i);
127  }
128  else{
129  c[i] = c[i] - c_tol.at(i);
130  }
131  }
132 
133  // clean the fitness vector
134  for(f_size_type i=0; i<f.size(); i++) {
135  f[i] = 0.;
136  }
137 
138  // in all cases, the first objectives holds the initial objectives
139  for(f_size_type i=0; i<original_nbr_obj; i++) {
140  f[i] = original_f.at(i);
141  }
142 
143  switch(m_method)
144  {
145  case OBJ_CSTRS:
146  {
147 
148  for(c_size_type i=0; i<number_of_constraints; i++) {
149  if(c.at(i) > 0.) {
150  f[original_nbr_obj+i] = c.at(i);
151  } else if(number_of_violated_constraints != 0) {
152  f[original_nbr_obj+i] = number_of_violated_constraints;
153  } else {
154  f[original_nbr_obj+i] = 0.;
155  for(f_size_type j=0; j<original_nbr_obj; j++) {
156  f[original_nbr_obj+i] += original_f.at(j);
157  }
158  }
159  }
160  break;
161  }
162  case OBJ_CSTRSVIO:
163  {
164  for(c_size_type i=0; i<number_of_constraints; i++) {
165  if(c.at(i) > 0.) {
166  f[original_nbr_obj] += c.at(i);
167  }
168  }
169  break;
170  }
171  case OBJ_EQVIO_INEQVIO:
172  {
173  // treating equality constraints
174  for(c_size_type i=0; i<number_of_eq_constraints; i++) {
175  if(c.at(i) > 0.) {
176  f[original_nbr_obj] += c.at(i);
177  }
178  }
179 
180  for(c_size_type i=number_of_eq_constraints; i<number_of_constraints; i++) {
181  if(c.at(i) > 0.) {
182  f[original_nbr_obj+1] += c.at(i);
183  }
184  }
185  break;
186  }
187  default:
188  pagmo_throw(value_error, "Error: There are only 3 methods for the constrained to multi-objective!");
189  break;
190  }
191 }
192 
194 
197 std::string con2mo::human_readable_extra() const
198 {
199  std::ostringstream oss;
200  oss << m_original_problem->human_readable_extra() << std::endl;
201  oss << "\n\tConstraints handled with constrained to multi-objective, method ";
202  switch(m_method){
203  case OBJ_CSTRS: {
204  oss << "OBJ_CSTRS ";
205  break;
206  }
207  case OBJ_CSTRSVIO: {
208  oss << "OBJ_CSTRSVIO ";
209  break;
210  }
211  case OBJ_EQVIO_INEQVIO: {
212  oss << "OBJ_EQVIO_INEQVIO ";
213  break;
214  }
215  }
216  oss << std::endl;
217  return oss.str();
218 }
219 
220 std::string con2mo::get_name() const
221 {
222  std::string method;
223 
224  switch(m_method){
225  case OBJ_CSTRS: {
226  method = "OBJ_CSTRS ";
227  break;
228  }
229  case OBJ_CSTRSVIO: {
230  method = "OBJ_CSTRSVIO ";
231  break;
232  }
233  case OBJ_EQVIO_INEQVIO: {
234  method = "OBJ_EQVIO_INEQVIO ";
235  break;
236  }
237  }
238  return m_original_problem->get_name() + " [con2mo, method_" + method + "]";
239 }
240 
241 }}
242 
243 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::con2mo)
244 
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
base_ptr clone() const
Clone method.
Definition: con2mo.cpp:92
method_type
Mechanism used to deal with constraints in the objectives.
Definition: con2mo.h:75
fitness_vector::size_type f_size_type
Fitness' size type: the same as pagmo::fitness_vector's size type.
Definition: problem/base.h:162
void objfun_impl(fitness_vector &, const decision_vector &) const
Definition: con2mo.cpp:99
STL namespace.
Base problem class.
Definition: problem/base.h:148
Each constraint violation is transformed into one objective.
Definition: con2mo.h:76
The total constraint violation is addd as two objectives (equalities + inequalities) ...
Definition: con2mo.h:78
Meta=problems base class.
Definition: base_meta.h:50
con2mo(const base &=cec2006(4), const method_type=OBJ_CSTRS)
Definition: con2mo.cpp:79
The total constraint violation is addd as one objective.
Definition: con2mo.h:77
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
c_size_type get_c_dimension() const
Return global constraints dimension.
std::vector< double > constraint_vector
Constraint vector type.
Definition: types.h:44
std::string get_name() const
Get problem's name.
Definition: con2mo.cpp:220
std::string human_readable_extra() const
Extra human readable info for the problem.
Definition: con2mo.cpp:197
Constrainted to Multi-Objective meta-problem.
Definition: con2mo.h:60
f_size_type get_f_dimension() const
Return fitness dimension.
constraint_vector::size_type c_size_type
Constraints' size type: the same as pagmo::constraint_vector's size type.
Definition: problem/base.h:164
base_ptr m_original_problem
Smart pointer to the original problem instance.
Definition: base_meta.h:80