How can I use Genetic Algorithm toolbox to calibrate a model?

Hi, this is my first time I use Optimize Live Editor Task.
I have a function, which calculates some outputs throughot the time,
[zz,Az,Vz,tt,Qst,Kzt,Tzt,Czt,Szt,Pzt,Chlzt,PPzt,DOPzt,DOCzt,Qzt_sed,lambdazt,...
P3zt_sed,P3zt_sed_sc,His,DoF,DoM,MixStat,Wt]...
= solvemodel_v12(m_start,m_stop,initfile,'lake',inputfile,'timeseries', parafile,'lake');
Each output is calculated throughout the time step (if it is a year, we would have 365 values for every output).
I have the observation of an output Tzt, and now I want to calibrate my model to minimize RMSQ, and during the calibration process, I would like parameters in the parafile excelsheet to be changed, which its maximum and minimum values are also indicated in that excelsheet. I don't know how I can use FA in live editor task to calibrate this model.

Answers (1)

It is entirely possible that I have misunderstood your question in some way. Feel freee to ask again with more explanation if my answer is not relevant.
First, it is not at all clear to me that ga is the best tool for this task. Why not use lsqnonlin, which is generally faster and more accurate?
But if you have a good reason for using ga then you can write the objective as something like
function Tzt = ...
myfun(m_start,m_stop,initfile,'lake',inputfile,'timeseries', parafile,'lake');
[~,~,~,~,~,~,Tzt,~,~,~,~,~,~,~,~,~,~,~,~,~,~,~,~] = ...
solvemodel_v12(m_start,m_stop,initfile,'lake',inputfile,'timeseries', parafile,'lake');
end
Call ga like this:
fun = @(parafile)sum((myfun(m_start,m_stop,initfile,'lake',inputfile,'timeseries', parafile,'lake') ...
- (theExpectedValue)).^2);
[sol,fval,efag,output] = ga(fun,nvar,A,b,Aeq,beq,lb,ub,nlcon)
Put in any necessary constraints in the ga call as well.
The solution sol has the new values for the parafile variables. You can write this out to the Excel file after ga runs.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

6 Comments

Actually I need that some parameters be tuned in the ga from parafile excelsheet (for example B10, B14,B15). How can I specify so? And also Tzt is an output, that I would like its RMS error of this values from the observation be minimized.
Sorry, I do not understand what you are asking, nor what you didn't like in my previous answer. I might not be able to help you.
Alan Weiss
MATLAB mathematical toolbox documentation
In the parafile there are many parameters, from those I just need 3 parameters to be tuned in the genetic algorithm. That is the only thing I'd like to be changed in your script.
Thank you so much.
Since you only want three parameters changed out of many, you can modify the script just a bit.
You need to extract the parafile values and edit them within your function. Suppose that you have paraval as the MATLAB variable containing data. You use this as an unchanging data value that you pass to your objective function. I also suppose that you have the expected values that you are trying to match as the vector theExpectedValue.
function val = sumsq(params,paraval,theExpectedValue)
% Put the params values into paraval, using the appropriate mapping
paraval(10) = params(1);
paraval(14) = params(2);
paraval(15) = params(3);
% Evaluate your objective at paraval
values = solvemodel_v12(m_start,m_stop,initfile,'lake',inputfile,'timeseries', paraval,'lake');
% Compute the sum of squares differences
val = sum((values - theExpectedValue).^2);
end
Call the minimization like this:
fun = @(params)sumsq(params,paraval,theExpectedValue);
[sol,fval] = ga(fun,3,A,b,Aeq,beq,lb,ub);
% Here lb and ub apply only to the 3-D vector params
% You may well have A, b, Aeq, and beq all = []
Again, I might have misunderstood something, but this is the solution as I understand it.
Alan Weiss
MATLAB mathematical toolbox documentation
Thank you so much. In the function, paraval I think is a MATLAB variable, while in order to run the function solvemodel_v12 I need to put this as an excel file, in the 'lake' sheet. So, I have to use xlsread and xlswrite?
Yes, you have to get the data into MATLAB.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

Categories

Products

Release

R2021a

Asked:

on 28 Feb 2022

Commented:

on 3 Mar 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!