1)    Design the Objective function for the maximum number of constraints
For  simple design, there may be only several variables in my objective  
function, and may be hundreds of variables for complicated design. If I fix the 
mumber of variables to it's maxium number, it may cause a lot of usless 
computation.


2)    Fix the non-variable variables using equal upper and lower bounds?        
  I had  try to fix the variable's upper and lower bounds as you say , but it 
doesnot work.
       
//In Visual Studio 2010:
#include "stdafx.h"
#include <math.h>
#include <nlopt.h>
int count = 0;
typedef struct 
{
    double a, b;
} my_constraint_data;

double myconstraint(unsigned n, const double *x, double *grad, void *data)
{
    my_constraint_data *d = (my_constraint_data *) data;
    double a = d->a, b = d->b;
    if (grad) {
        grad[0] = 3 * a * (a*x[0] + b) * (a*x[0] + b);
        grad[1] = -1.0;
    }
    return ((a*x[0] + b) * (a*x[0] + b) * (a*x[0] + b) - x[1]);
}
double myfunc(unsigned n, const double *x, double *grad, void *my_func_data)
{
    count++;
    printf("eval #%d ",count);
    if (grad) {
        grad[0] = 0.0;
        grad[1] = 0.5 / sqrt(x[1]);
    }
    printf(" :: f(%f , %f) = %f\n\r",x[0],x[1],sqrt(x[1]));
    return sqrt(x[1]);
    
}
int _tmain(int argc, _TCHAR* argv[])
{
    double lb[2] = { -HUGE_VAL, 0.296296};//0 }; /* lower bounds */
    double ub[2] = { +HUGE_VAL, 0.296296};//6};
    nlopt_opt opt;
    opt = nlopt_create(NLOPT_LD_MMA, 2); /* algorithm and dimensionality */
    nlopt_set_lower_bounds(opt, lb);
    nlopt_set_upper_bounds(opt,ub);
    nlopt_set_min_objective(opt, myfunc, NULL);

    my_constraint_data data[2] = { {2,0}, {-1,1} };

    nlopt_add_inequality_constraint(opt, myconstraint, &data[0], 1e-8);
    nlopt_add_inequality_constraint(opt, myconstraint, &data[1], 1e-8);
    nlopt_set_xtol_rel(opt, 1e-4);
    double x[2] = { 1.234, 0.296296};//5.678 };  /* some initial guess */
    double minf; /* the minimum objective value, upon return */

    if (nlopt_optimize(opt, x, &minf) < 0) {
        printf("nlopt failed!\n");
    }
    else {
        printf("found minimum at f(%g,%g) = %0.10g\n", x[0], x[1], minf);
    }
    nlopt_destroy(opt);
    return 0;
}

I fix the second variable as it's best value: x[2]=0.296296,
The results:

eval #1  :: f(1.234000 , 0.296296) = 0.544331
eval #2  :: f(1.234000 , 1.#QNAN0) = 1.#QNAN0
eval #3  :: f(1.234000 , 1.#QNAN0) = 1.#QNAN0
eval #4  :: f(1.234000 , 1.#QNAN0) = 1.#QNAN0
eval #5  :: f(1.234000 , 1.#QNAN0) = 1.#QNAN0
eval #6  :: f(1.234000 , 1.#QNAN0) = 1.#QNAN0
eval #7  :: f(1.234000 , 1.#QNAN0) = 1.#QNAN0
eval #8  :: f(1.234000 , 1.#QNAN0) = 1.#QNAN0
eval #9  :: f(1.234000 , 1.#QNAN0) = 1.#QNAN0






________________________________
From: "Waters, Anthony" <[email protected]>
To: King Simon <[email protected]>; [email protected]
Sent: Mon, January 17, 2011 6:57:16 PM
Subject: RE: [NLopt-discuss] How to design objective function with 
nonconstantnumber of variables?

 
I’m no expert, but couldn’t you:
 
1)    Design the Objective function for the maximum number of constraints
2)    Fix the non-variable variables using equal upper and lower bounds?
 
__________________________________________________________________________________________

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 King Simon 

Sent: 16 January 2011 10:39
To: [email protected]
Subject: [NLopt-discuss] How to design objective function with 
nonconstantnumber 
of variables?
 
Hi, 
My objective function have a lot of variables, but the number of variables is 
not constant, 

for example, the nubmer of varibles is decide by the number of layers of films 
every time before optimization.
And some variables may be set to constant value or not, which is decide by the 
user,
for example, after several optimazitons, some varible was set to a constant 
value. but later, it may be set to varible again.
Is ther anyone meet this kind of problem?
How to design my objective function?
Thanks a lot!
 
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 Webroot 
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

Reply via email to