PaGMO  1.1.5
util/hv_algorithm/base.h
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 #ifndef PAGMO_UTIL_HV_ALGORITHM_BASE_H
26 #define PAGMO_UTIL_HV_ALGORITHM_BASE_H
27 
28 #include <iostream>
29 #include <string>
30 #include <typeinfo>
31 #include <boost/lexical_cast.hpp>
32 
33 #include "../../config.h"
34 #include "../../exceptions.h"
35 #include "../../serialization.h"
36 #include "../../types.h"
37 
38 namespace pagmo { namespace util {
40 
43 namespace hv_algorithm {
44 
46 class base;
47 
49 typedef boost::shared_ptr<base> base_ptr;
50 
52 
90 class __PAGMO_VISIBLE base
91 {
92 public:
94 
102  virtual double compute(std::vector<fitness_vector> &points, const fitness_vector &r_point) const = 0;
103  virtual double exclusive(const unsigned int, std::vector<fitness_vector> &, const fitness_vector &) const;
104  virtual unsigned int least_contributor(std::vector<fitness_vector> &, const fitness_vector &) const;
105  virtual unsigned int greatest_contributor(std::vector<fitness_vector> &, const fitness_vector &) const;
106  virtual std::vector<double> contributions(std::vector<fitness_vector> &, const fitness_vector &) const;
107 
109 
116  virtual void verify_before_compute(const std::vector<fitness_vector> &points, const fitness_vector &r_point) const = 0;
117 
119 
122  virtual base_ptr clone() const = 0;
123 
124  virtual std::string get_name() const;
125  virtual ~base();
126 
127 protected:
128  void assert_minimisation(const std::vector<fitness_vector> &, const fitness_vector &) const;
129 
130  virtual unsigned int extreme_contributor(std::vector<fitness_vector> &, const fitness_vector &, bool (*)(double, double)) const;
131 
132  // comparison functions for the least and the greatest contributor methods
133  static bool cmp_least(const double, const double);
134  static bool cmp_greatest(const double, const double);
135 
136 public:
137  static double volume_between(const fitness_vector &, const fitness_vector &, unsigned int = 0);
138 
139 protected:
140  // Domination results of the 'dom_cmp' methods
141  enum {
142  DOM_CMP_B_DOMINATES_A = 1, // second argument dominates the first one
143  DOM_CMP_A_DOMINATES_B = 2, // first argument dominates the second one
144  DOM_CMP_A_B_EQUAL = 3, // both points are equal
145  DOM_CMP_INCOMPARABLE = 4 // points are incomparable
146  };
147 
148  static double volume_between(double*, double*, unsigned int);
149  static int dom_cmp(double*, double*, unsigned int);
150  static int dom_cmp(const fitness_vector &, const fitness_vector &, unsigned int = 0);
151 
152 private:
153  friend class boost::serialization::access;
154  template <class Archive>
155  void serialize(Archive &ar, const unsigned int) {
156  (void)ar;
157  }
158 };
159 
161 
168 {
169 public:
170  fitness_vector_cmp(int dim, char cmp_type);
171 
173 
181  inline bool operator()(const fitness_vector &lhs, const fitness_vector &rhs)
182  {
183  return (*m_cmp_obj)(lhs,rhs);
184  }
185 private:
186  struct cmp_fun
187  {
188  int m_dim;
189  cmp_fun(int dim) : m_dim(dim) { }
190  virtual ~cmp_fun() { };
192  virtual inline bool operator()(const fitness_vector &lhs, const fitness_vector &rhs)
193  {
194  return lhs[0] < rhs[0];
195  }
196  };
197 
198  struct cmp_le : cmp_fun
199  {
200  cmp_le(int dim) : cmp_fun(dim) { }
201  inline bool operator()(const fitness_vector &lhs, const fitness_vector &rhs)
202  {
203  return lhs[m_dim] < rhs[m_dim];
204  }
205  };
206 
207  struct cmp_ge : cmp_fun
208  {
209  cmp_ge(int dim) : cmp_fun(dim) { }
210  inline bool operator()(const fitness_vector &lhs, const fitness_vector &rhs)
211  {
212  return lhs[m_dim] > rhs[m_dim];
213  }
214  };
215 
216  boost::shared_ptr<cmp_fun> m_cmp_obj;
217 };
218 
219 } } }
220 
221 BOOST_SERIALIZATION_ASSUME_ABSTRACT(pagmo::util::hv_algorithm::base)
222 
223 #endif
Root PaGMO namespace.
bool operator()(const fitness_vector &lhs, const fitness_vector &rhs)
Overloaded operator()
fitness_vector_cmp(int dim, char cmp_type)
Constructor of the comparator object.
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
boost::shared_ptr< base > base_ptr
Base hypervolume algorithm class.