I'm an infrequent MatLab user, so when I need it I tend to find an example and tailor it to my problem. I followed that approach recently in building a constrained optimization model.
I wrote several functions for a) calculating values b) evaluating constraints and c) the objective function. My final script looked like this
x0 = [0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0]; lb = [0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0]; %at work version -release returns 2014b, and then the options are
%MaxFunEvals, MaxIter
%or on some computers, its 2017a, and then the options are
%MaxFunctionEvaluations, MaxIterations
version = evalc('version -release'); if strcmp(version,'2017a') options = optimoptions(@fmincon,'Algorithm','active-set','MaxFunctionEvaluations',100000,'MaxIterations',100000); else options = optimoptions(@fmincon,'Algorithm','active-set','MaxFunEvals',100000,'MaxIter',100000); end p = gcp('nocreate'); % If no pool, do not create new one.
if isempty(p) parpool; end ms = MultiStart('UseParallel', true); problem = createOptimProblem('fmincon','objective',@carry_forward_objective,'nonlcon',@turbulence_constraints,'x0',x0,'lb',lb,'options',options); [x,fval] = run(ms,problem,100)
This has worked well, but note that the problem is hard-coded…it's the same with the referenced functions @carry_forward_objective and @turbulence_constraints. In this case there are 23 rows and 5 columns of decision variables.
My challenge now is to generalize the process so that I can read in a variable number of rows (there will always be 5 columns) from a file that defines a problem and set up the optimization. I can use xlsread to get the data in, but am unsure how to proceed programmatically creating the functions/handles required to set up the problem. Can anyone point me in the right direction?
Thanks!
Best Answer