PaGMO  1.1.5
string_match.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/numeric/conversion/cast.hpp>
26 #include <cmath>
27 #include <string>
28 
29 #include "../types.h"
30 #include "base.h"
31 #include "string_match.h"
32 
33 namespace pagmo { namespace problem {
34 
36 string_match::string_match(const std::string &str):base(boost::numeric_cast<int>(str.size()),boost::numeric_cast<int>(str.size()),1,0,0),m_str(str)
37 {
38  set_bounds(0,255);
39 }
40 
42 string_match::string_match(const char *str):base(boost::numeric_cast<int>(std::string(str).size()),boost::numeric_cast<int>(std::string(str).size()),1,0,0),m_str(str)
43 {
44  set_bounds(0,255);
45 }
46 
48 {
49  return base_ptr(new string_match(*this));
50 }
51 
53 {
54  double retval = 0;
55  for (std::string::size_type i = 0; i < m_str.size(); ++i) {
56  retval += std::abs(static_cast<char>(x[boost::numeric_cast<size_type>(i)]) - m_str[i]);
57  }
58  f[0] = retval;
59 }
60 
61 
63 
66 std::string string_match::pretty(const decision_vector &x) const {
67  std::string retval;
68  for (decision_vector::size_type i = 0; i < x.size(); ++i) {
69  retval += char(x[i]);
70  }
71  return retval;
72 }
73 
75 {
76  return std::string("\tString: \"") + m_str + "\"\n";
77 }
78 
79 std::string string_match::get_name() const
80 {
81  return "String match";
82 }
83 
84 } }
85 
86 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::string_match)
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
string_match(const std::string &)
Constructor from C++ string.
STL namespace.
Base problem class.
Definition: problem/base.h:148
std::string pretty(const decision_vector &x) const
Returns the encoded string.
String matching problem.
Definition: string_match.h:47
void objfun_impl(fitness_vector &, const decision_vector &) const
Objective function implementation.
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
std::string get_name() const
Get problem's name.
base_ptr clone() const
Clone method.
std::string human_readable_extra() const
Extra information in human readable format.
void set_bounds(const decision_vector &, const decision_vector &)
Bounds setter from pagmo::decision_vector.