Hi Carlos,
The C++ Reference page for NLopt details how to use a pointer to carry
around whatever additional data you may need for your function
("f_data") or constraint ("fc_data") evaluations. Quoted:
> The f_data argument is the same as the one passed to> nlopt_set_min_objective
> or nlopt_set_max_objective, and may be used> to pass any additional data
> through to the function. (That is, it may> be a pointer to some
> caller-defined data structure/type containing> information your function
> needs, which you convert from void* by a> typecast.)
Whatever data structure you choose to pass as your "f_data" could simply
contain these intermediate values, replacing them on each computation.
However, I don't think that NLopt provides any guarantees about:
1) That your constraints will always be evaluated immediately after your
main function, and be at the same X.
2) That the final evaluation of your main function is necessarily at the
best X.
(If there in fact are any guarantees on these, I'd appreciate if someone
would let me know. But I would suspect there aren't.)
A consequence of 1) is that sharing the same pointer for "f_data" and
"fc_data" and storing intermediates within it will probably necessitate
additional logic from you to determine whether you need to recompute.
Handling this could get messy so I would recommend spending some time
playing around with this, and maybe even reading the
documentation/source for whatever algorithm you're using.
A consequence of 2) is of course that it wouldn't be sufficient so
simply cache the "previous" intermediates. You might instead cache the
"previous" and "best" simultaneously, though again you will need to be
careful with whatever logic you implement to recognise that you can
re-use one of these.
I think 2) is more important to you than 1) but I bring it up as a
potential "gotcha".
I hope this was useful, and again, if anyone has any further comments or
corrections to what I've said, I'd definitely appreciate it.
Best regards,
Adam Hirst
On 22/03/17 12:14, Carlos Tampier wrote:
> Dear All:
>
> I am currently using the COBYLA algorithm to perform an optimization.
> Within the cost function call for the nlopt object (C++), several
> variables are calculated which I would very much like to obtain as a
> result of the optimization process. Although it's true that knowing the
> optimal values allows me to re-compute these variables, it would
> probably be more efficient to just do this computation once. Is this
> possible to somehow encapsulate them in an object or pointer of some sort?
>
> Thank you very much and have a nice day.
>
> Carlos Tampier
> Researcher
> Advanced Mining Technology Center (AMTC)
> Universidad de Chile
> Av. Tupper 2007, 837-0451 Santiago, Chile
>
> Tel: +56-2-2977 1019
> E-mail: [email protected] <mailto:[email protected]>
>
>
> _______________________________________________
> NLopt-discuss mailing list
> [email protected]
> http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss
>
--
Using GPG? Add my key, and respond to me with it enabled!
Also, feel free to verify my key with me online or in person!
Not using GPG? You should, it's easy!
https://www.enigmail.net/documentation/quickstart.php
_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss