Issue with using amoeba...


wolfwolf
11-12-2008, 03:30 AM
Hi there,

I am using amoeba in visual studio 9 and am having a few problems.

Here is the relevant piece of code:
#include "amoeba.h"
#include "function.h"

main(){
Amoeba am(0.001);
vector<double> point;
for (int i=0; i<3; i++) point.push_back(0);
vector<double> dels(3);
vector<double> pmin;
func sinusoid;

pmin = am.minimize(point,dels,sinusoid);
}

I have declared my function as a structure in function.h (see code below):

struct func {
vector<double> operator() (const double x){
vector <double> ans(3);
ans[0] = 1.;
ans[1] = x ;
ans[2] = sin(x) ;
return ans;
}
};

When I try to compile with "cl - EHsc myProgram.cpp", I get the following error message:

myProgram.cpp
myProgram.cpp(687) : error C2780: 'std::vector<_Ty> Amoeba::minimize(MatDoub_I &,std::vector<_Ty> &)' : expects 2 arguments - 3 provided
with
[
_Ty=double
]
C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\amoeba.h(30) : see declaration of 'Amoeba::minimize'
myProgram.cpp(687) : error C2783: 'std::vector<_Ty> Amoeba::minimize(std::vector<_Ty> &,std::vector<_Ty> &,std::vector<_Ty> &)' : could not deduc
e template argument for 'T'
with
[
_Ty=double
]
C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\amoeba.h(18) : see declaration of 'Amoeba::minimize'
myProgram.cpp(687) : error C2783: 'std::vector<_Ty> Amoeba::minimize(std::vector<_Ty> &,double,std::vector<_Ty> &)' : could not deduce template a
rgument for 'T'
with
[
_Ty=double
]
C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\amoeba.h(12) : see declaration of 'Amoeba::minimize'

Could anyone please suggest why the appropriate template is not being recognised? There are three distinct templates, yet the compiler does not seem to recognise that. Any help would be very much appreciated.

Many thanks.
Wolf

kutta
11-23-2008, 08:31 AM
Hi there,

I am using amoeba in visual studio 9 and am having a few problems.

Here is the relevant piece of code:
#include "amoeba.h"
#include "function.h"

main(){
Amoeba am(0.001);
vector<double> point;
for (int i=0; i<3; i++) point.push_back(0);
vector<double> dels(3);
vector<double> pmin;
func sinusoid;

pmin = am.minimize(point,dels,sinusoid);
}

I have declared my function as a structure in function.h (see code below):

struct func {
vector<double> operator() (const double x){
vector <double> ans(3);
ans[0] = 1.;
ans[1] = x ;
ans[2] = sin(x) ;
return ans;
}
};

When I try to compile with "cl - EHsc myProgram.cpp", I get the following error message:

myProgram.cpp
myProgram.cpp(687) : error C2780: 'std::vector<_Ty> Amoeba::minimize(MatDoub_I &,std::vector<_Ty> &)' : expects 2 arguments - 3 provided
with
[
_Ty=double
]
C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\amoeba.h(30) : see declaration of 'Amoeba::minimize'
myProgram.cpp(687) : error C2783: 'std::vector<_Ty> Amoeba::minimize(std::vector<_Ty> &,std::vector<_Ty> &,std::vector<_Ty> &)' : could not deduc
e template argument for 'T'
with
[
_Ty=double
]
C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\amoeba.h(18) : see declaration of 'Amoeba::minimize'
myProgram.cpp(687) : error C2783: 'std::vector<_Ty> Amoeba::minimize(std::vector<_Ty> &,double,std::vector<_Ty> &)' : could not deduce template a
rgument for 'T'
with
[
_Ty=double
]
C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\amoeba.h(12) : see declaration of 'Amoeba::minimize'

Could anyone please suggest why the appropriate template is not being recognised? There are three distinct templates, yet the compiler does not seem to recognise that. Any help would be very much appreciated.

Many thanks.
Wolf

Hello Comrade
The feeding in the data in FunctionTemplate
like the one in Amoeba
is to be slightly different because of its unique features .
On the solution front for codedstuffof urs please try the relevent codes in the main as follws .
Though on execution it still requires function to be defined.So please define in and you would get the output.
{Also try with the other function given here removing your function for the okness of this example for testing in C++)
Thanking You
As
Kutta(C.R.Muthukumar)
:)



#include "function.h"
#include <stdio.h>
#include <math.h>
#include "nr3.h"

#define MP 22
#define NP 21 //Maximum value for NDIM=20

typedef double MAT[MP][NP];

MAT P;
double Y[MP], PT[MP];
int I,ITER,J,NDIM;
double FTOL;

/**user defined function to minimize
double FUNC(double *P) {
double R;
R=sqrt(P[1]*P[1]+P[2]*P[2]);
if (fabs(R) < 1e-12)
return 1.0;
else
return sin(R)/R;
}
*/
double FUNC {
vector<double> operator() (const double x){
vector <double> ans(3);
ans[0] = 1.;
ans[1] = x ;
ans[2] = sin(x) ;
return ans;
}
}

[edited by webmaster to remove copyrighted NR code]

int main() {


NDIM=2; // 2 variables
FTOL=1e-8; // User given tolerance

//define NDIM+1 initial vertices (one by row)
P[1][1]= 1.0; P[1][2]=2.0;
P[2][1]=-2.0; P[2][2]=-3.0;
P[3][1]= 4.0; P[3][2]=2.0;

//Initialize Y to the values of FUNC evaluated
//at the NDIM+1 vertices (rows] of P
for (I=1; I<=NDIM+1; I++) {
PT[1]=P[I][1]; PT[2]=P[I][2];
Y[I]=FUNC(PT);printf("%lf\n ",Y[I]);
}

//call main function
AMOEBA(P,Y,NDIM,FTOL,&ITER);


//print results
printf("\n Number of iterations: %d\n\n", ITER);
printf(" Best NDIM+1 points:\n");
for (I=1 ; I<=NDIM+1; I++) {
for (J=1; J<=NDIM; J++) printf(" %f", P[I][J]);
printf("\n");
}
printf("\n Best NDIM+1 mimimum values:\n");
for (I=1; I<=NDIM+1; I++) printf(" %14.10f\n", Y[I]);
printf("\n");
return 0;
/**
Amoeba am(0.001);
vector<double> point;
for (int i=0; i<3; i++) point.push_back(0);
vector<double> dels(3);
vector<double> pmin;

pmin = am.minimize(point,dels,sinusoid);*/
}

gjm
07-14-2009, 10:46 AM
Wolf: You're trying to pass a vector<double> where Amoeba::minimize expects a VecDoub. Declare your variables points and dels as VecDoub(3) instead of vector<double>(3).

Kutta: Eh?

kutta
07-16-2009, 04:56 AM
Wolf: You're trying to pass a vector<double> where Amoeba::minimize expects a VecDoub. Declare your variables points and dels as VecDoub(3) instead of vector<double>(3).

Kutta: Eh?

Hello NR Comrade
On Execution under C++
The OutPut is as under

0.351845
-0.124112
-0.217184

Number of iterations: 22

Best NDIM+1 points:
4.122686 1.786915
4.166477 1.682698
4.142454 1.741176

Best NDIM+1 mimimum values:
-0.2172336265
-0.2172336281
-0.2172336271

Thanking You
As
Kutta(C.R.Muthukumar)

gjm
07-16-2009, 07:52 AM
Kutta, I said "Eh?" not because I wanted to know what your program outputs but because I don't see that it has much to do with what the original poster was asking. Wolf wanted to know how to use the Nelder-Mead code from NR3; your code (so far as I can guess, since it seems that you copied a load of NR code and modified it but that got deleted by the forum moderators) instead seems to involve rewriting the NR code to have a different interface.