在我们的程序中应用nlopt时如何调用成员函数? [英] How to call a member function when we applied nlopt in our program ?

查看:239
本文介绍了在我们的程序中应用nlopt时如何调用成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用CNonlinearOptimization类在我的程序中实现nlopt库。问题是通过使用指针调用成员函数myfunction,我得到了错误,因为该函数与nlopt_func类型的参数不匹配。



这是成员函数:

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));
}





错误是myfunction和myconstraint1在主函数中如下



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 ?

推荐答案

在处理函数指针时我喜欢做的就是为它们创建一个typedef。以下是与Windows中的BeginThread调用一起使用的函数:
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 :
typedef UINT( __stdcall * ThreadFunc )( PVOID );

看来该库有一个名为nlopt_func的文件。在文档中,它显示函数具有原型:

It appears that library has one too called nlopt_func. In the documentation it shows the function has the prototype :

double f(unsigned n, const double* x, double* grad, void* f_data)

问题是,这不是您班级方法的原型。这是一个独立的功能。这意味着您必须通过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 :

double MyFunction( unsigned n, const double *x, double *grad, void *pdata )
{
   CNonlinearOptimization * pnlo = (CNonlinearOptimization *)pdata;
   pnlo->myfunction( n, x, grad );
}

你应该删除my_constraint_data作为你的方法的参数,如本片段所示。



这同样的策略应该与mycontstraint1一起使用。

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.


这篇关于在我们的程序中应用nlopt时如何调用成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆