diff --git a/PyGMO/problem/__init__.py b/PyGMO/problem/__init__.py index 1d836df5..a74bdb7c 100644 --- a/PyGMO/problem/__init__.py +++ b/PyGMO/problem/__init__.py @@ -678,6 +678,18 @@ def _shifted_ctor(self, problem=None, shift=None): shifted.__init__ = _shifted_ctor +def _relaxed_ctor(self, problem=None): + # We construct the arg list for the original constructor exposed by + # boost_python + arg_list = [] + if problem is None: + problem = ackley(1) + arg_list.append(problem) + self._orig_init(*arg_list) +relaxed._orig_init = relaxed.__init__ +relaxed.__init__ = _relaxed_ctor + + def _rotated_ctor(self, problem=None, rotation=None): """ Rotates a problem. (also reflections are possible) diff --git a/PyGMO/problem/problem_meta.cpp b/PyGMO/problem/problem_meta.cpp index 1dc03dbf..ef782d92 100644 --- a/PyGMO/problem/problem_meta.cpp +++ b/PyGMO/problem/problem_meta.cpp @@ -145,20 +145,24 @@ BOOST_PYTHON_MODULE(_problem_meta) { .def(init()) .add_property("shift_vector",make_function(&problem::shifted::get_shift_vector,return_value_policy())) .def("deshift",&problem::shifted::deshift); - + + // Relaxed meta-problem + meta_problem_wrapper("relaxed","Relaxed problem") + .def(init()); + // Scaled meta-problem meta_problem_wrapper("scaled","Scaled problem") .def(init()) .add_property("units",make_function(&problem::scaled::get_units,return_value_policy())) .def("descale",&problem::scaled::descale); - + // Rotated meta-problem meta_problem_wrapper("rotated","Rotated problem") .def(init()) .def(init()) .add_property("rotation_matrix",&get_rotation_matrix_from_eigen) .def("derotate",&problem::rotated::derotate); - + // Normalized meta-problem meta_problem_wrapper("normalized","Normalized problem") .def(init()) @@ -172,7 +176,7 @@ BOOST_PYTHON_MODULE(_problem_meta) { // Decomposition meta-problem meta_problem_wrapper("decompose","Decomposed problem") .def(init &, const std::vector &, const bool> >()) - .def("compute_decomposed_fitness", &compute_decomposed_fitness_wrapper, + .def("compute_decomposed_fitness", &compute_decomposed_fitness_wrapper, "Computes the fitness of the decomposed problem\n\n" " USAGE:: w = prob.compute_decomposed_fitness(fit,weight)\n" " - fit: multi-dimensional fitness\n" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0a7727dd..e348a361 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -96,8 +96,9 @@ SET(PAGMO_LIB_SRC_LIST ${CMAKE_CURRENT_SOURCE_DIR}/problem/death_penalty.cpp ${CMAKE_CURRENT_SOURCE_DIR}/problem/cstrs_self_adaptive.cpp ${CMAKE_CURRENT_SOURCE_DIR}/problem/antibodies_problem.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/problem/shifted.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/problem/scaled.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/problem/relaxed.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/problem/shifted.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/problem/scaled.cpp ${CMAKE_CURRENT_SOURCE_DIR}/problem/rotated.cpp ${CMAKE_CURRENT_SOURCE_DIR}/problem/normalized.cpp ${CMAKE_CURRENT_SOURCE_DIR}/problem/decompose.cpp diff --git a/src/problem/relaxed.cpp b/src/problem/relaxed.cpp new file mode 100644 index 00000000..000bd634 --- /dev/null +++ b/src/problem/relaxed.cpp @@ -0,0 +1,88 @@ +/***************************************************************************** + * Copyright (C) 2015 The PaGMO development team, * + * Advanced Concepts Team (ACT), European Space Agency (ESA) * + * http://apps.sourceforge.net/mediawiki/pagmo * + * http://apps.sourceforge.net/mediawiki/pagmo/index.php?title=Developers * + * http://apps.sourceforge.net/mediawiki/pagmo/index.php?title=Credits * + * act@esa.int * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + *****************************************************************************/ + +#include "../exceptions.h" +#include "../types.h" +#include "../population.h" +#include "base.h" +#include "relaxed.h" + +namespace pagmo { namespace problem { + +/** + * Construct the continuous relaxation of a (mixed-)integer problem + * + * @param[in] p base::problem to be relaxed + * + * @see problem::base constructors. + */ + +relaxed::relaxed(const base & p): + base_meta( + p, + p.get_dimension(), + 0, + p.get_f_dimension(), + p.get_c_dimension(), + p.get_ic_dimension(), + p.get_c_tol()) +{ +} + +/// Clone method. +base_ptr relaxed::clone() const +{ + return base_ptr(new relaxed(*this)); +} + +/// Implementation of the objective function. +/// (Wraps over the original implementation) +void relaxed::objfun_impl(fitness_vector &f, const decision_vector &x) const +{ + m_original_problem->objfun(f, x); +} + +/// Implementation of the constraints computation. +/// (Wraps over the original implementation) +void relaxed::compute_constraints_impl(constraint_vector &c, const decision_vector &x) const +{ + m_original_problem->compute_constraints(c, x); +} + +std::string relaxed::get_name() const +{ + return m_original_problem->get_name() + " [Relaxed]"; +} + +/// Extra human readable info for the problem. +/** + * Will return a formatted string containing the translation vector + */ +std::string relaxed::human_readable_extra() const +{ + return m_original_problem->human_readable_extra(); +} +}} + +BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::relaxed) diff --git a/src/problem/relaxed.h b/src/problem/relaxed.h new file mode 100644 index 00000000..0b75af14 --- /dev/null +++ b/src/problem/relaxed.h @@ -0,0 +1,74 @@ +/***************************************************************************** + * Copyright (C) 2015 The PaGMO development team, * + * Advanced Concepts Team (ACT), European Space Agency (ESA) * + * http://apps.sourceforge.net/mediawiki/pagmo * + * http://apps.sourceforge.net/mediawiki/pagmo/index.php?title=Developers * + * http://apps.sourceforge.net/mediawiki/pagmo/index.php?title=Credits * + * act@esa.int * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + *****************************************************************************/ + +#ifndef PAGMO_PROBLEM_RELAXED_H +#define PAGMO_PROBLEM_RELAXED_H + +#include + +#include "../serialization.h" +#include "ackley.h" +#include "../types.h" +#include "base_meta.h" + +namespace pagmo{ namespace problem { + +/// Relaxed meta-problem +/** + * Implements a meta-problem class that wraps some other problem and acts as + * its continous relaxation. This basically means that the integer part of the + * underlying problem will be treated the same way as if it was continous. See + * http://docs.mosek.com/7.0/capi/The_optimizers_for_mixed-integer_problem.html + * for more details. + */ + +class __PAGMO_VISIBLE relaxed : public base_meta +{ + public: + //constructor + relaxed(const base & = ackley(1)); + + base_ptr clone() const; + std::string get_name() const; + + protected: + std::string human_readable_extra() const; + void objfun_impl(fitness_vector &, const decision_vector &) const; + void compute_constraints_impl(constraint_vector &, const decision_vector &) const; + private: + void configure_shifted_bounds(const decision_vector &); + + friend class boost::serialization::access; + template + void serialize(Archive &ar, const unsigned int) + { + ar & boost::serialization::base_object(*this); + } +}; + +}} //namespaces + +BOOST_CLASS_EXPORT_KEY(pagmo::problem::relaxed) + +#endif // PAGMO_PROBLEM_RELAXED_H diff --git a/src/problems.h b/src/problems.h index ff6c2ec2..c7e04376 100644 --- a/src/problems.h +++ b/src/problems.h @@ -71,6 +71,7 @@ #include "problem/cstrs_co_evolution.h" #include "problem/cstrs_self_adaptive.h" #include "problem/antibodies_problem.h" +#include "problem/relaxed.h" #include "problem/shifted.h" #include "problem/scaled.h" #include "problem/rotated.h"