bbopt / nomad

NOMAD - A blackbox optimization software
https://nomad-4-user-guide.readthedocs.io/
GNU Lesser General Public License v3.0
116 stars 24 forks source link

MATLAB Interface: Specifying "String"-Only Params With Iteration Function #64

Closed Arrowstar closed 8 months ago

Arrowstar commented 2 years ago

In NOMAD 3.9.x, it was possible to specify an "iteration function" that was called each iteration. In this version, the iteration function was a function handle that was called in a particular war. However, when running NOMAD 4.2 using the MATLAB interface, I get the following error when specifying a param structure with a function handle:

Error using nomadOpt

 NOMAD Parameter Error:
Params should be provided as Matlab structure with string value: struct('KEY1','VAL1','KEY2','VAL2')

My params structure has the following relevant entry:

iterfun: @(iter,fval,x)nomadOutput1(iter,fval,x,'iter')

This worked in the previous 3.9.x release of NOMAD but no longer works here. Please advise on how iteration functions should be specified in NOMAD 4.2+ using the MATLAB interface. Thank you!

ctribes commented 2 years ago

The iteration function is not handled in v4.2.0. Let me check how it can be done. Most of the nomadmex.cpp source code is the same but iteration function was not migrated from 3.9 to 4.2.

Arrowstar commented 2 years ago

The iteration function is not handled in v4.2.0. Let me check how it can be done. Most of the nomadmex.cpp source code is the same but iteration function was not migrated from 3.9 to 4.2.

Please do, thank you! The iteration function functionality is important to my application and I can't migrate to NOMAD 4 without it, too many things would break on my end. Thanks!

ctribes commented 2 years ago

The iteration function in Matlab interface for Nomad 3.9 is used for stopping the optimization nicely. The information available to stop or not is the same as what is in the blackbox evaluation. In retrospect, I find it duplicates a very flexible stopping criterion available in Nomad 3.

STAT_SUM_TARGET (advanced) {

        . MADS terminates if STAT_SUM reaches the value of this
            parameter (STAT_SUM is one of the possible outputs
            defined in BB_OUTPUT_TYPE)
        . Argument: one real
        . No default
        . Example: STAT_SUM_TARGET 100.0
    }

An extra output called STAT_SUM can be added to the BB_OUTPUT_TYPE. Nomad will sum this from all evaluations. If the STAT_SUM reaches the STAT_SUM_TARGET, Nomad 3 will stop. If during an evaluation, we reach an ad hoc criterion we can simply set the STAT_SUM output to 1, and 0 otherwise. With the STAT_SUM_TARGET 1, Nomad will stop.

In Nomad 4, passing the matlab iteration function name or handle is not possible in the list of Params. Adding an extra argument in the nomadOpt function is also not very convenient. I would rather implement the STAT_SUM and STAT_SUM_TARGET functionalities. It is not yet available but it can be done quickly.

Arrowstar commented 2 years ago

The iteration function in Matlab interface for Nomad 3.9 is used for stopping the optimization nicely. The information available to stop or not is the same as what is in the blackbox evaluation. In retrospect, I find it duplicates a very flexible stopping criterion available in Nomad 3.

This is one of the use cases for the iterations function but it is not the only use case. I have been using the iteration function to display iteration-level data to users on the progress of the optimization via a GUI. Users can also use that GUI to stop the optimization themselves without having to wait for the optimizer's stop conditions to be met. In other words, I use the iteration function to enable feedback to user and user interaction because it allows me to get inside the optimizer at each step and see what's going on.

There's no way a MATLAB or MEX level workaround could be implemented? Another input argument for the mex file with a function handle for the iteration function or something similar that would avoid having to modify the base NOMAD code?

ctribes commented 2 years ago

Thanks for giving details about this use case for iteration function.

I can implement the iteration function as a patch available when compiling with a flag.

Arrowstar commented 2 years ago

Thanks for giving details about this use case for iteration function.

I can implement the iteration function as a patch available when compiling with a flag.

Thank you! I appreciate your help in implementing this feature.

ctribes commented 2 years ago

A fix is available in the develop branch. It will make its way into a new release later.

There is no need to provide a special flag during the compilation to obtain the updated function nomadOpt(fun,x0,lb,b,param,cbfun). An example of user callback function has been added in the Matlab_MEX directory.