Click here to Skip to main content
15,743,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to ask about NLopt as follows below. (The NLopt website refers to Tutorial - NLopt Documentation[^])

Question1: If the number of constraints is bigger than the number of variables, how can we set “grad[ ]” in the “myconstraint”? Is there any (automatic) method to solve the problem without introducing Lagrangian multiplier?

Using Lagrangian multiplexer, I know we can solve the problem. However the use of Lagrangian multiplexer we have to obtain ”my_constraint_data’’ manually, which make it difficult to solve large-scale problem.

Example : Minimize f= -((x1)^3)-(2*(x2)^2)+(10*(x1))-6-(2*(x2)^3) subject to 10-((x1)(x2))>=0, ((x1)(x2)^2)-5>=0 and (x2)-(x1)*(x2)^3 >= 0

Based on the problem above, we have three inequality constraints like below.

Constraint 1: c1 = 10-(x1)*(x2) >= 0
Constraint 2: c2 = ((x1)(x2)^2)-5>=0
Constraint 3: c3 = (x2)-(x1)(x2)^3 >= 0

In NLopt tutorial, we know that grad[0] = d(c1)/d(x1) and grad1 = d(c2)/d(x2) as the gradient of constraints. Then, we set grad[ ] as same as follows.
double myconstraint(unsigned n, const double *x, double *grad, void *data) {
	my_constraint_data *d = (my_constraint_data *)data;

	if (grad) {
		grad[0] = -x[1];       //grad[0] = d(c1)/dx[1]
		grad[1] = 2*x[0]+x[1]; //grad[1] = d(c2)/dx[2]
        grad[2] = ???;         //grad[2] = d(c3)/dx[3] but we only have 2 variable (x1)&(x2)
	return (10-x[0]*x[1], x[0]*x[1]*x[1]-5, x[1]-x[0]*x[1]*x[1]*x[1];
The problem is we do not know how to set “grad[ ]” (especially for c3) if the number of constraints are larger than the number of variables.

What I have tried:

Of course we can solve the problem with non-automatic method like below by using Lagrangian multiplexer (l1, l2, l3) where grad[0] = -l1*(d(c1)/d(x1))-l2*(d(c2)/d(x1))-l3*(d(c)/d(x1)) and grad[1] = -l1*(d(c1)/d(x2))-l2*(d(c2)/d(x2))-l3*(d(c)/d(x3)).
double myconstraint(unsigned n, const double *x, double *grad, void *data) {
	my_constraint_data *d = (my_constraint_data *)data;
        //set l1, l2, and l3 as parameter of lagrangian multiplier
	double l1=d->l1,l2=d->l2,l3=d->l3;
	if (grad) {
		grad[0] = l1*x[1]-l2*x[1]*x[1]-l3*x[1]*x[1]*x[1];
		grad[1] = l1*x[0]-2*l2*x[0]*x[1]-l3+3*l3*x[0]*x[1]*x[1];
	return (10-x[0]*x[1], x[0]*x[1]*x[1]-5, x[1]-x[0]*x[1]*x[1]*x[1]);
Meanwhile, it is not easy to apply non-automatic method into large-scale problem because it will be inefficient and complicated in programming.

Question2: Is there any method to solve nonlinear simultaneous equations using NLopt? (When Lagrangian multiplexer is applied in case of the number of constraints are larger than the number of variables, nonlinear simultaneous equations should be solved).

Your answer will be really helpful to us. Thank you.
Updated 11-Mar-19 23:23pm

1 solution

You will most likely get a quicker answer at NLopt-discuss Info Page[^].
Share this answer

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