Dynamically generating an optimization problem
Show older comments
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!
13 Comments
Walter Roberson
on 22 Aug 2017
By the way, you do not need the evalc(): you can use verLessThan()
Walter Roberson
on 22 Aug 2017
It would be trivial to modify the above to read x0 and lb from a file, but I am not clear how the data you would read in would define the problem or change the functions ?
Jeremy Hendrix
on 22 Aug 2017
Walter Roberson
on 22 Aug 2017
It appears to me that you should be able to program those constraints as linear constraints, the A and b matrix inputs for fmincon. If I am correct, then those too could be read in.
Jeremy Hendrix
on 23 Aug 2017
(Just to complicate matters, there is also one project that has its own custom step function)
That does complicate matters as step functions are non-differentiable and so not within the scope of what fmincon can handle. You should probably just use ga().
So my current objective function is
It is still not clear how you want this to function to look for different numbers of rows.
Jeremy Hendrix
on 23 Aug 2017
Walter Roberson
on 23 Aug 2017
You have a limited number of different types of operations. Each different type can be described by matrices of indices and corresponding coefficients. Therefore each complete scenario can be encoded as a series of data matrices. For efficiency you might want to have a routine that takes input data and builds a series of control matrices, so that you do not have to decode the matrices on each iteration; for any given scenario you would parse the input once and use the control matrices repeatedly. To get the control matrices into the objective function, use the techniques described at http://www.mathworks.com/help/matlab/math/parameterizing-functions.html
Jeremy Hendrix
on 23 Aug 2017
Walter Roberson
on 23 Aug 2017
You do not need to define a function within a function, unless you are referring to anonymous functions.
In pseudo-code:
control_inputs = .... %load the various matrices that are in a user-friendly form
[pcm, X0] = build_control_matrices(control_inputs); %transform from user-friendly form to something usable internally
obj = @(x) compute_scenario_value(x, pcm.internal_matrices);
options = ....
fmincon(obj, X0, pcm.A, pcm.b, pcm.Aeq, pcm.beq, pcm.lb, pcm.ub, pcm.nlcon, options)
Jeremy Hendrix
on 30 Aug 2017
Walter Roberson
on 30 Aug 2017
I suggest that you could return a structure
function [pcm, X0] = build_control_matrices(control_inputs)
...
pcm.A = ...
pcm.b = ...
pcm.Aeq = ...
Jeremy Hendrix
on 30 Aug 2017
Accepted Answer
More Answers (0)
Categories
Find more on Choose a Solver in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!