I am trying to implement NLOPT in a class in my program. The errors are :
Warning 7 warning C4717: 'NonlinearOpt::nlopt_add_inequality_constraint' : recursive on all control paths, function will cause runtime stack overflow d:\program\robotsimu190902_salah\robotsimu\nonlinearopt.cpp 142
Warning 6 warning C4717: 'NonlinearOpt::nlopt_create' : recursive on all control paths, function will cause runtime stack overflow d:\program\robotsimu190902_salah\robotsimu\nonlinearopt.cpp 149
Warning 10 warning C4717: 'NonlinearOpt::nlopt_optimize' : recursive on all control paths, function will cause runtime stack overflow d:\program\robotsimu190902_salah\robotsimu\nonlinearopt.cpp 169
Warning 9 warning C4717: 'NonlinearOpt::nlopt_set_lower_bounds' : recursive on all control paths, function will cause runtime stack overflow d:\program\robotsimu190902_salah\robotsimu\nonlinearopt.cpp 156
Warning 5 warning C4717: 'NonlinearOpt::nlopt_set_min_objective' : recursive on all control paths, function will cause runtime stack overflow d:\program\robotsimu190902_salah\robotsimu\nonlinearopt.cpp 177
Warning 8 warning C4717: 'NonlinearOpt::nlopt_set_xtol_rel' : recursive on all control paths, function will cause runtime stack overflow d:\program\robotsimu190902_salah\robotsimu\nonlinearopt.cpp 161
What I have tried:
Here is the code I mad :
<nonlinearopt.h>
#ifndef __NONLINEAROPT_H__
#define __NONLINEAROPT__
#pragma once
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#include <iostream>
#include "nlopt.h"
#pragma comment(lib, "libnlopt-0.lib")
class NonlinearOpt
{
private:
public:
double ankle_angle;
double knee_angle;
double pelvis_angle;
double k1;
double k2;
double k3;
double max_ankle;
double max_knee;
double max_pelvis;
double min_ankle;
double min_knee;
double min_pelvis;
double new_ankle_angle;
double new_knee_angle;
double new_pelvis_angle;
public:
NonlinearOpt(void){
k1 = 1.0;
k2 = 1.0;
k3 = 1.0;
};
~NonlinearOpt(void){
};
public:
double myfunction(unsigned int n, const double *theta, double *grad);
double myconstraint1(unsigned int n, const double *x, double *grad);
double myconstraint2(unsigned n, const double *x, double *grad);
double myconstraint6(unsigned n, const double *x, double *grad);
int main();
nlopt_result nlopt_add_inequality_constraint(nlopt_opt opt, nlopt_func fc, void* fc_data, double tol);
nlopt_result nlopt_add_equality_constraint(nlopt_opt opt, nlopt_func h, void* h_data, double tol);
nlopt_opt nlopt_create(nlopt_algorithm algorithm, unsigned n);
nlopt_result nlopt_set_lower_bounds(nlopt_opt opt, const double* lb);
nlopt_result nlopt_set_xtol_rel(nlopt_opt opt, double tol);
nlopt_result nlopt_optimize(nlopt_opt opt, double *x, double *opt_f);
nlopt_result nlopt_set_min_objective(nlopt_opt opt, nlopt_func f, void* f_data);
void nlopt_destroy(nlopt_opt opt);
};
#endif
<nonlinearopt.cpp>
#include "stdafx.h"
#include "NonlinearOpt.h"
typedef UINT( __stdcall * ThreadFunc )( PVOID );
double NonlinearOpt::myfunction(unsigned int n, const double *theta, double *grad) {
if (grad) {
grad[0] = 2*k1*(theta[0]-ankle_angle);
grad[1] = 2*k2*(theta[1]-knee_angle);
grad[2] = 2*k3*(theta[2]-pelvis_angle);
}
return (k1*(theta[0]-ankle_angle)*(theta[0]-ankle_angle) + k2*(theta[1]-knee_angle)*(theta[1]-knee_angle) + k3*(theta[2]-pelvis_angle)*(theta[2]-pelvis_angle));
}
double MyFunction( unsigned n, const double *theta, double *grad, void *pdata )
{
NonlinearOpt * pnlo = (NonlinearOpt *)pdata;
return pnlo->myfunction( n, theta, grad );
}
double NonlinearOpt::myconstraint1(unsigned n, const double *theta, double *grad){
if (grad) {
grad[0] = -1;
grad[1] = 0;
grad[2] = 0;
}
return (min_ankle - theta[0]);
}
double MyConstraint1( unsigned n, const double *theta, double *grad, void *pdata )
{
NonlinearOpt * pnlo = (NonlinearOpt *)pdata;
return pnlo->myconstraint1( n, theta, grad );
}
double NonlinearOpt::myconstraint2(unsigned n, const double *theta, double *grad){
if (grad) {
grad[0] = 1;
grad[1] = 0;
grad[2] = 0;
}
return (theta[0] - max_ankle);
}
double MyConstraint2( unsigned n, const double *theta, double *grad, void *pdata )
{
NonlinearOpt * pnlo = (NonlinearOpt *)pdata;
return pnlo->myconstraint2( n, theta, grad );
}
nlopt_result NonlinearOpt::nlopt_add_inequality_constraint(nlopt_opt opt, nlopt_func fc, void *fc_data, double tol){
opt = opt;
fc = fc;
fc_data = fc_data;
tol = tol;
return nlopt_add_inequality_constraint(opt, fc, fc_data, tol);
}
nlopt_opt NonlinearOpt::nlopt_create(nlopt_algorithm algorithm, unsigned int n){
algorithm = algorithm;
n=n;
return nlopt_create(algorithm, n);
}
nlopt_result NonlinearOpt::nlopt_set_lower_bounds(nlopt_opt opt, const double *lb){
opt = opt;
lb = lb;
return nlopt_set_lower_bounds(opt, lb);
}
nlopt_result NonlinearOpt::nlopt_set_xtol_rel(nlopt_opt opt, double tol){
return nlopt_set_xtol_rel(opt, tol);
}
nlopt_result NonlinearOpt::nlopt_optimize(nlopt_opt opt, double *x, double *opt_f){
opt = opt;
x = x;
opt_f = opt_f;
return nlopt_optimize(opt, x, opt_f);
}
nlopt_result NonlinearOpt::nlopt_set_min_objective(nlopt_opt opt, nlopt_func f, void *f_data){
opt = opt;
f = f;
f_data = f_data;
return nlopt_set_min_objective(opt, f, f_data);
}
void NonlinearOpt::nlopt_destroy(nlopt_opt opt){
opt = opt;
}
int NonlinearOpt::main() {
nlopt_opt opt;
double lb[2] = { -HUGE_VAL, 0 };
double theta[3] = {0, 0, 1};
double minf;
opt = nlopt_create(NLOPT_LD_MMA, 3);
nlopt_set_lower_bounds(opt, lb);
nlopt_set_min_objective(opt,MyFunction, NULL);
nlopt_add_inequality_constraint(opt, MyConstraint1, NULL, 1e-8);
nlopt_add_inequality_constraint(opt, MyConstraint2, NULL, 1e-8);
nlopt_set_xtol_rel(opt, 1e-4);
if (nlopt_optimize(opt, theta, &minf) < 0) {
printf("nlopt failed!\n");
}
else {
printf("found minimum at f(%0.10g,%0.10g,%0.10g) = %0.10g\n", theta[0], theta[1], theta[2], minf);
new_ankle_angle = theta[0];
new_knee_angle = theta[1];
new_pelvis_angle = theta[2];
}
nlopt_destroy(opt);
system("pause");
return 0;
}
When I was debugging my program, the error refers to the wrapper function of constraints (MyConstraint1, MyConstraint2, etc.). Do you know how to solve this problem ? specially for my wrapper function to call NLOPT. If you know so well about NLOPT, please let me know. Thank you