Thanks for your answer.(I added the constraint twice, it's a mistake, but even by correcting this, the output is the same: "inf")
Indeed, I exposed this example just to see if the code will run or not. Because similarly to that example, my real problem has the same shape, I mean one objective function with some squared unknowns (10 and more),and an equality constraint of the same type. What I want to understand is, why this code is not working, even if a trivial solution can be found with the replacement you pointed.What is wrong in my code? the formulation of the problem or some missing parameters? Thank you again. From: [email protected] To: [email protected] Date: Tue, 10 Apr 2012 14:41:49 +0100 Subject: RE: [NLopt-discuss] Nonlinear derivative-free constrained minimization using COBYLA I doubt that it copes well with equality constraints, though I’m no expert.In your example, you would do better to replace the X2^2 in the objective with (22-X1^2), then you don’t need the constraint. In any case, your two constraints are identical in the code below: nlopt_add_inequality_constraint(opt, myconstraint, NULL, 1e-8); nlopt_add_inequality_constraint(opt, myconstraint, NULL, 1e-8); presumably the second one should be different (e.g. opposite sign?). __________________________________________________________________________________________ Anthony Waters • Senior Staff Consultant • KBC Process Technology Ltd T 44 (0)1606 815100 • Direct +44 (0)1606 815182 • F +44 (0)1606 815151 [email protected] From: [email protected] [mailto:[email protected]] On Behalf Of Abdoulaye Diakité Sent: 09 April 2012 02:02 To: [email protected] Subject: [NLopt-discuss] Nonlinear derivative-free constrained minimization using COBYLA Hello everybody. I'm using NLopt as an C++ alternative to the "fmincon" function of Matlab or the "sqp" function of Octave. I have a nonlinear equality constraint and a nonlinear function that I want to minimize without using its the derivatives. It seems the best algorithm to use, with NLopt, is the COBYLA one. I was able to run the tutorial example without any problem, but when I try my own function it's not working. For example (a problem I created just to test): min f(x1,x2) = 100*(x1² - 1) + x2² - 39*x1; subject to x1² + x2² = 22; I tried a C++ and a C version of the code, the first one returns me a roundoff_limited error, and the second one an "inf". Here is the C version (all in a single cpp file): #include <iostream> #include <math.h> #include <nlopt.h> double myfunc(unsigned n, const double *x, double *grad, void *my_func_data); double myconstraint(unsigned n, const double *x, double *grad, void *data); double myfunc(unsigned n, const double *x, double *grad, void *my_func_data) { return 100.0 * (x[0]*x[0] - 1.0) + x[1]*x[1] - 39.0 * x[0]; } double myconstraint(unsigned n, const double *x, double *grad, void *data) { return (x[0]*x[0] + x[1]*x[1] - 22.0); } int main() { nlopt_opt opt; opt = nlopt_create(NLOPT_LN_COBYLA, 2); /* algorithm and dimensionality */ nlopt_set_min_objective(opt, myfunc, NULL); nlopt_add_inequality_constraint(opt, myconstraint, NULL, 1e-8); nlopt_add_inequality_constraint(opt, myconstraint, NULL, 1e-8); nlopt_set_xtol_rel (opt, 1.0e-5); double x[2] = { -1.0, 1.0 }; /* some initial guess */ double minf; /* the minimum objective value, upon return */ nlopt_optimize(opt, x, &minf); std::cout << "found minimum at f(" << x[0] << ", " << x[1] << ") = " << minf << "\n" << std::endl; return 0; } Since Octave was able to give me suiting results, I don't think the problem comes from my function, so maybe I forgot something in the design of the solver. Can somebody please tell me where I'm wrong? Thank you. Mangaf This e-mail is confidential and intended only for the individual(s) to whom it is addressed. If you or your organisation is not an intended recipient of this e-mail, please notify the sender by replying and do not read or disseminate its information. Please delete all copies from your system. KBC is liable neither for the proper or complete transmission of the information contained in this communication nor for any delay in its receipt. Opinions, conclusions and other information in this message and attachments that do not relate to the official business of KBC are neither given nor endorsed by it. Even though the Mimecast Virus Centre has checked this message for all known viruses, you should carry out your own virus checks before opening any attachments. Thank you for your co-operation. www.kbcat.com
_______________________________________________ NLopt-discuss mailing list [email protected] http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss
