Search code examples
matlabparticle-swarm

How to define the objective fucntion's variables to be optimized in MATLAB's global optimization toolbox?


I have an objective function fun where the input parameter x should be optimized by particleswarm(): https://de.mathworks.com/help/gads/particleswarm.html

I want to find the optimal values for x for defined scenarios. Because of this, besides x the function fun also accepts scenario parameters s1 and s2:

function f = objective(x, s1, s2)

Now, how do I tell the solver to just optimize x and ignore the input parameters for s1 and s2?


Solution

  • I think it should be something like

    out = particleswarm(@(x)fun(x,s1,s2))
    

    That is how you do it for in-built optimizers like fminunc

    Edit: Then, if I understand correctly that particleswarm is an optimizer that you wrote yourself, the function definition of particleswarm would be

    function [ out ] = particleswarm( func )
    

    Alternatively, if you are doing something like BFGS where you need to have an initial guess, x0, that changes in the optimization function, you would call the optimizer with

    out = particleswarm(x0, @(x)fun(x,s1,s2))
    

    and the function definition would be

    function [ out ] = particleswarm(x0, func )