PaGMO  1.1.5
lennard_jones.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/math/constants/constants.hpp>
26 #include <cmath>
27 #include <string>
28 #include <vector>
29 
30 #include "../exceptions.h"
31 #include "../types.h"
32 #include "base.h"
33 #include "lennard_jones.h"
34 
35 namespace pagmo { namespace problem {
36 
38 
45 lennard_jones::lennard_jones(int atoms):base(3*atoms-6)
46 {
47  if (atoms <= 0 || atoms < 3) {
48  pagmo_throw(value_error,"number of atoms for lennard-jones problem must be positive and greater than 2");
49  }
50  for (int i = 0; i < 3*atoms-6; i++) {
51  if ( (i != 0) && (i % 3) == 0 ) {
52  set_lb(i,0.0);
53  set_ub(i,6.0);
54  } else {
55  set_lb(i,-3.0);
56  set_ub(i,3.0);
57  }
58  }
59 }
60 
63 {
64  return base_ptr(new lennard_jones(*this));
65 }
66 
68 double lennard_jones::r(const int& atom, const int& coord, const std::vector<double>& x) {
69  if(atom == 0) { //x1,y1,z1 fixed
70  return 0.0;
71  } else if(atom == 1) {
72  if(coord < 2) { //x2,y2 fixed
73  return 0.0;
74  } else { //z2 is a variable
75  return x[0];
76  }
77  } else if(atom == 2) {
78  if(coord == 0) { //x3 fixed
79  return 0.0;
80  } else { //y3 and x3 are variables
81  return x[coord];
82  }
83  } else {
84  return x[3 * (atom - 2) + coord];
85  }
86 }
87 
90 {
91  pagmo_assert(f.size() == 1);
92  std::vector<double>::size_type n = x.size();
93  int atoms = (n + 6) / 3;
94  double sixth, dist;
95 
96  f[0] = 0;
97  //We evaluate the potential
98  for ( int i=0; i<(atoms-1); i++ ) {
99  for ( int j=(i+1); j<atoms; j++ ) {
100  dist = pow(r(i, 0, x) - r(j, 0, x), 2) + pow(r(i, 1, x) - r(j, 1, x), 2) + pow(r(i, 2, x) - r(j, 2, x), 2); //rij^2
101  if ( dist == 0.0 ) {
102  f[0] = 1e+20; //penalty
103  }
104  else {
105  sixth = pow(dist, -3); //rij^-6
106  f[0] += (pow(sixth, 2) - sixth);
107  }
108  }
109  }
110  f[0] = 4 * f[0];
111 }
112 
113 std::string lennard_jones::get_name() const
114 {
115  return "Lennard-Jones";
116 }
117 
118 }}//namespaces
119 
120 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::lennard_jones)
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
The Lennard-Jones problem.
Definition: lennard_jones.h:50
Base problem class.
Definition: problem/base.h:148
void set_lb(const decision_vector &)
Set lower bounds from pagmo::decision_vector.
std::string get_name() const
Get problem's name.
void objfun_impl(fitness_vector &, const decision_vector &) const
Implementation of the objective function.
void set_ub(const decision_vector &)
Set upper bounds from pagmo::decision_vector.
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
lennard_jones(int=3)
Constructor from dimension.
base_ptr clone() const
Clone method.