Minor cosmetics.
[PaGMO.git] / Functions / objfuns / classicobjfuns.cpp
blobc5e78f8585d59a6ffe6c03b4330fd5eb9600b4c6
1 /*
2 * GOProblems.cpp
3 * SeGMO, a Sequential Global Multiobjective Optimiser
5 * Created by Dario Izzo on 5/17/08.
6 * Copyright 2008 ¿dvanced Concepts Team (European Space Agency). All rights reserved.
8 */
10 #include <cmath>
12 #include "classicobjfuns.h"
14 using namespace std;
16 double testfunction (const vector<double>& x){
17 double finalvalue=0;
18 for (unsigned int i=0; i<x.size(); i++){
19 finalvalue += x[i];
21 return finalvalue;
24 double rastrigin (const vector<double>& x){
25 double omega = 2.0 * M_PI;
26 double value=0;
27 int n = x.size();
29 for (int i=0; i<n; i++){
30 value += x[i]*x[i] - 10.0 * cos(omega*x[i]);
32 return (10.0*n + value);
35 double schwefel (const vector<double>& x){
36 int n = x.size();
37 double value=0;
39 for (int i=0; i<n; i++){
40 value += x[i] * sin(sqrt(fabs(x[i])));
42 return (418.9829 * n - value);
45 double ackley (const vector<double>& x){
46 int n = x.size();
47 double omega = 2.0 * M_PI;
48 double s1=0.0, s2=0.0;
49 double nepero=exp(1.0);
51 for (int i=0; i<n; i++){
52 s1 += x[i]*x[i];
53 s2 += cos(omega*x[i]);
55 return -20*exp(-0.2 * sqrt(1.0/n * s1))-exp(1.0/n*s2)+ 20 + nepero;
58 double lennardjones(const vector <double>& x){
59 int n = x.size();
60 int atoms = (n + 6) / 3;
61 vector<double> dummy(3);
62 vector< vector<double> > r(atoms,dummy); //double dimensional vector containing the atom positions
63 double V = 0; //LJ potential
64 double sixth,dist;
66 //we transform the decision vector x in atoms positions r
67 r[0][0] = r[0][1] = r[0][2] = 0; //x1,y1,z1 fixed
68 r[1][0] = r[1][1] = 0; //x2,y2 fixed
69 r[1][2] = x[0]; //z2 is a variable
70 r[2][0] = 0; //x3 fixed
71 r[2][1] = x[1]; //y3 is a variable
72 r[2][2] = x[2]; //z3 is a variable
74 for (int i=3; i<atoms; i++){
75 r[i][0] = x[3*(i-2)];
76 r[i][1] = x[3*(i-2)+1];
77 r[i][2] = x[3*(i-2)+2];
80 //We evaluate the potential
81 for ( int i=0; i<(atoms-1); i++ ) {
82 for ( int j=(i+1); j<atoms; j++ ) {
83 dist = pow(r[i][0]-r[j][0],2) + pow(r[i][1]-r[j][1],2) + pow(r[i][2]-r[j][2],2); //rij^2
84 if ( dist == 0.0 ) {
85 return 1e+20; //penalty
87 else {
88 sixth = pow(dist,-3); //rij^-6
89 V += ( pow(sixth,2) - sixth );
93 return 4 * V;
96 double rosenbrock (const vector<double>& x){
97 int n = x.size();
98 double value=0.0;
100 for (int i=0; i<n-1; i++){
101 value += 100 * (x[i]*x[i] -x[i+1])*(x[i]*x[i] -x[i+1]) + (x[i]-1)*(x[i]-1);
103 return value;
106 double levy(const vector<double>& x){
107 int n = x.size();
108 double isum = 0.0;
109 double jsum = 0.0;
110 double ret;
111 int i, j;
113 for ( j=0; j<n; j+=2 ) {
114 for ( i=1; i<=5; i++ ) {
115 isum += (double)(i) * cos((double)(i-1)*x[j] + (double)(i));
116 jsum += (double)(i) * cos((double)(i+1)*x[j+1] + (double)(i));
120 ret = isum*jsum;
121 for ( j=0; j<n; j+=2 )
122 ret += pow(x[j]+1.42513,2) + pow(x[j+1]+0.80032,2);
124 return ret;