15 views (last 30 days)

Hello everybody,

I am dealing with the following problem:

I need to minimize the solution of a system of ordinary differential equations in order to find the optima parameters for my system. So, I give an initial x0 (containing my parameters) and then I write an objective function where I solve the system (with my own scheme) with the parameters and then I compare the solution of the system to the one I would like to get...To do this I use simulated annealing or minsearch and it works.

The point is that I need to impose some relations on the parameters x0, for example I would like that x0(1)>x0(2) etc...

I am trying to write some penalties in my objective function, but it does not work...How should I do?

Thank you in advance

Jeff Miller
on 9 Feb 2020

It depends a little on exactly what relations you want to impose, but often you can do this by mapping MATLAB's real-number parameters to the constrained parameters that you really want, Do something like this inside the objective function:

function err = myObjFn(matlabx0)

myx0(1) = matlabx0(1)^2 + matlabx0(2); % constraint that myx0(1)>myx0(2)

myx0(2) = matlabx0(2);

% Now proceed with your calculations using myx0, which satisfies your constraints

end

Sign in to comment.

Star Strider
on 9 Feb 2020

The siumlannealbnd function only permits range bounds on the parameters. The ga function permits linear and nonlinear constraints as well. See Minimize a Nonsmooth Function with Linear Constraints and others to understand how to do that. I almost always use ga because it allows such constraints, so I have no recent experience with simulannealbnd. It appears that the fitness functions require the same syntax.

See: Is it possible to use Simulated Annealing or Genetic Algorithm for parameter estimation of a parametric System of ODEs? for an example of using ga to estimate the parameters of a system of ordinary differential equations.

Sign in to comment.

John D'Errico
on 9 Feb 2020

Edited: John D'Errico
on 9 Feb 2020

You do not want to solve it using penalties. That tends to cause problems, making the solution more difficult. At best, the optimization will probably take more function evaluations than necessary. It can still work, but if a better solution exists, you want to use it...

The idea is to perform a transformation inside your objective function. For example, to insure that your parameters are decreasing, so x(1) >= x(2) >= x(3) >= ...

function result = objfun(x)

xtrans = x(1) - cumsum(x(2:end).^2);

% now compute your objective function, as a function of the vector xtrans.

(do stuff here, returning result, as a function of xtrans)

end

As you should see, the vector xtrans will always obey the requirement of being decreasing. When the optimizer is done, perform the same transformation on the vector it returns, to give you the unknowns as you wish, thus a decreasing sequence.

The only time that will even result in two consecutive elements as EXACTLY equal will be when the optimizer chose one of the variables in x as identically zero. That will essentually never happen, but if you truly need a strict inequality, then you might do something like...

dxmin = 1.e-15;

xtrans = x(1) - (cumsum(x(2:end).^2 + dxmin);

That will force a tiny increment always in the variables. You can choose dxmin to be whatever you wish as realistic for your problem. Just don't make it too tiny.

There are many other ways you can use simple internal transformations to achieve your goals.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/504509-fminsearch-and-simulated-annealing-with-penalties#comment_795275

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/504509-fminsearch-and-simulated-annealing-with-penalties#comment_795275

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/504509-fminsearch-and-simulated-annealing-with-penalties#comment_795333

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/504509-fminsearch-and-simulated-annealing-with-penalties#comment_795333

Sign in to comment.