MATLAB: Dynamically generating an optimization problem

optimizationprogramming

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

I assume the quantity that you are reading in is R and maybe also
sumBounds= [ 108.869; 111.567; 108.526; 99.412; 97.558];
the bounds on sum(X). If so, then a dimension-independent way to run what you have shown so far (i.e., neglecting the objective function) is as follows,
[m,n]=size(R);
x0=zeros(m,n);
lb=zeros(m,n);
ub=R;
A=kron(eye(n),ones(1,m));
b=sumBounds(:);
problem = createOptimProblem('fmincon','objective',?,...
'x0',x0,'lb',lb,'ub',ub,'A',A,'b',b,'options',options);
As Walter said, you can get rid of the nonlinear constraint function. The constraints you had there were not really nonlinear.