PaGMO  1.1.5
base_gsl.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 <boost/thread/locks.hpp>
26 #include <boost/thread/mutex.hpp>
27 #include <gsl/gsl_vector.h>
28 #include <stdexcept>
29 
30 #include "../exceptions.h"
31 #include "../gsl_init.h"
32 #include "../problem/base.h"
33 #include "base_gsl.h"
34 #include "base.h"
35 
36 namespace pagmo { namespace algorithm {
37 
38 // Static mutex.
39 boost::mutex base_gsl::m_mutex;
40 
41 const gsl_init &base_gsl::get_init()
42 {
43  // Protect from simultaneous access.
44  boost::lock_guard<boost::mutex> lock(m_mutex);
45  static const gsl_init retval;
46  return retval;
47 }
48 
50 
54 {
55  if (!get_init().m_init) {
56  pagmo_throw(std::runtime_error,"GSL support could not be initialised");
57  }
58 }
59 
61 
67 double base_gsl::objfun_wrapper(const gsl_vector *v, void *params)
68 {
70  // Size of the continuous part of the problem.
71  const problem::base::size_type cont_size = par->p->get_dimension() - par->p->get_i_dimension();
72  // Fill up the continuous part of temporary storage with the contents of v.
73  for (problem::base::size_type i = 0; i < cont_size; ++i) {
74  par->x[i] = gsl_vector_get(v,i);
75  }
76  // Compute the objective function.
77  par->p->objfun(par->f,par->x);
78  return par->f[0];
79 }
80 
81 }}
Root PaGMO namespace.
Base algorithm class.
base_gsl()
Default constructor.
Definition: base_gsl.cpp:53
size_type get_dimension() const
Return global dimension.
fitness_vector objfun(const decision_vector &) const
Return fitness of pagmo::decision_vector.
fitness_vector f
Fitness vector.
Definition: base_gsl.h:69
size_type get_i_dimension() const
Return integer dimension.
static double objfun_wrapper(const gsl_vector *, void *)
Objective function wrapper.
Definition: base_gsl.cpp:67
Structure to feed parameters to the wrappers for the objective function and its derivative.
Definition: base_gsl.h:62
decision_vector x
Decision vector.
Definition: base_gsl.h:67
problem::base const * p
Pointer to the problem.
Definition: base_gsl.h:65
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.
Definition: problem/base.h:160