Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to implement nlopt library in my program with CNonlinearOptimization class. The problem is by using a pointer to call the member function "myfunction", I got the errors because the function doesn't match with parameter of type "nlopt_func".

Here is the member function:
double CNonlinearOptimization::myfunction(unsigned n, const double *x, double *grad, void *my_constraint_data) {
	if (grad) {
		grad[0] = 2*k1*(x[0]-1);
		grad[1] = 2*k2*(x[1]-3);
		grad[2] = 2*k3*(x[2]-5);
	}

	return (k1*(x[0]-1)*(x[0]-1)+k2*(x[1]-3)*(x[1]-3)+k3*(x[2]-5)*(x[2]-5));
}

double CNonlinearOptimization::myconstraint1(unsigned n, const double *x, double *grad, void *data){
	++count;
	if (grad) {
		grad[0] = -1;		//grad[0] = d(c1)/dx1
		grad[1] = 0;		//grad[1] = d(c1)/dx2
		grad[2] = 0;		//grad[2] = d(c1)/dx3
	}
	return (0.9-(x[0]-2));
}


And the error is "myfunction" and "myconstraint1" in main function like below
int CNonlinearOptimization::main() {

	CNonlinearOptimization nonlinopt;

	double lb[2] = { -HUGE_VAL, 0 }; // lower bounds 

	double x[3] = {0,0,1};  // `*`some` `initial` `guess`*` 
	double minf; // `*`the` `minimum` `objective` `value,` `upon` `return`*`

	opt = nlopt_create(NLOPT_LD_MMA, 3); // algorithm and dimensionality 
	nlopt_set_lower_bounds(opt, lb);

	nlopt_set_min_objective(opt,myfunction, NULL);

	nlopt_add_inequality_constraint(opt, myconstraint1, NULL, 1e-8);
	
	nlopt_set_xtol_rel(opt, 1e-4);

	if (nlopt_optimize(opt, x, &minf) < 0) {
		printf("nlopt failed!\n");
	}
	else {
		printf("found minimum after %d evaluation\n", nonlinopt.count);
		printf("found minimum at f(%0.10g,%0.10g,%0.10g) = %0.10g\n", x[0], x[1], x[2], minf);
	}

	nlopt_destroy(opt);
	system("pause");
	return 0;
}


What I have tried:

I tried to make pointer
nonlinopt.myfunction
and another pointer but it doesn't look good. How can I solve it ?
Posted
Updated 28-Mar-19 7:26am
Comments
Richard MacCutchan 28-Mar-19 13:05pm    
What are the actual error messages?
Member 13922009 28-Mar-19 13:10pm    
Here this error:


nonlinearoptimization.cpp(274): error C3867: 'CNonlinearOptimization::myfunction': function call missing argument list; use '&CNonlinearOptimization::myfunction' to create a pointer to member


nonlinearoptimization.cpp(276): error C3867: 'CNonlinearOptimization::myconstraint1': function call missing argument list; use '&CNonlinearOptimization::myconstraint1' to create a pointer to member


Richard MacCutchan 28-Mar-19 13:12pm    
Well that seems clear enough.
Member 13922009 28-Mar-19 13:15pm    
but when I tried it, still error
Richard MacCutchan 28-Mar-19 13:23pm    
What error? Please do not assume that we can see what is displayed on your screen.

1 solution

What I like to do when dealing with pointers to functions is to make a typedef for them. Here is one for a function used with the BeginThread call in Windows :
C++
typedef UINT( __stdcall * ThreadFunc )( PVOID );
It appears that library has one too called nlopt_func. In the documentation it shows the function has the prototype :
C++
double f(unsigned n, const double* x, double* grad, void* f_data)
The problem is that is not a prototype for a method of your class. It is a standalone function. This means you have to pass a pointer to your object through the f_data parameter. You should implement it like this :
C++
double MyFunction( unsigned n, const double *x, double *grad, void *pdata )
{
   CNonlinearOptimization * pnlo = (CNonlinearOptimization *)pdata;
   pnlo->myfunction( n, x, grad );
}
and you should remove my_constraint_data as an argument to your method, as shown in this snippet.

This same tactic should be used with mycontstraint1.
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900