How can I pass additional parameters to the constraint and objective functions in the Optimization Toolbox functions in versions prior to MATLAB 7.0 (R14) ?

I would like to pass additional parameters to the nonlinear constraint function as well as objective function in my Optimization problem. I am using a MATLAB version prior to MATLAB 7.0 (R14) that did not support anonymous functions.

 Risposta accettata

If you are using a version of MATLAB prior to MATLAB 7.0 (R14), which did not have the ability to create anonymous functions, you will need to pass the additional parameters into the objective and constraint functions as demonstrated in the example below. We are using FMINCON for demonstration purposes.
[x,fval] = fmincon(@fun,x0,A,b,Aeq,beq,lb,ub,@nonlcon,options,P1,P2,...)
FMINCON passes the problem-dependent parameters P1, P2, etc., directly to the functions "fun" , "nonlcon" and any function in the options structure.
In this case a nonlinear constraint function could be of this form:
function [c, ceq] = nonlcon(x,P1,P2)
c = x(1)*P1 - x(2)*P2
...
...
ceq = [];
Note that P1 and P2 are also available to "fun" whether you use it or not. You can try this simple example:
The objective function "fun" is :
function f = fun(x,p1,p2)
f = -x(1) * x(2) * x(3)*p1;
The nonlinear constraint function "nonlcon" is :
function [c, ceq] = nonlcon(x,p1,p2)
% Define two inequality constraints which use p1 and p2
c(1) = x(1)*p1 + 2*x(2)*x(1)*p2 + 2*x(3) - p2;
c(2) = x(1)*x(2)-100;
% Define the equality constraints
ceq(1) = x(2) -x(1)*x(2);
ceq(2) = x(2) - x(1)*x(3);
The call to FMINCON used to minimize this expression is:
x0 = [10; 10; 10];
p1 = 1;
p2 = 72;
lb = [0 0 0];
ub = [ 50 50 50];
options = optimset('Largescale','off','Display','iter');
[x, fval] = fmincon(@fun, x0, [], [], [], [], lb, ub, @nonlcon, options, p1, p2)
The ODE functions use the same convention for handling additional parameters as is described here. Additional information about the ODE solvers can be found in the related solution below.

4 Commenti

You are using fmincon(). It will call the function passing in one parameter, that is the vector of values that are being optimized over.
fun = @(x0,StoreInitialization,ttransij1,texeij1,theta,D_Cloud,F_Cloud,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3)FCVEC06January2022V2;
That defines a function handle for a function that expects about 36 inputs, and which then ignores all of the inputs and invokes FCVEC06January2022V2 with no inputs at all.
x = fmincon(fun(x0,StoreInitialization,ttransij1,texeij1,theta,D_Cloud,F_Cloud,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3),x0,A,b,Aeq,beq,lb,ub,nonlcon(Numtk,constr,ttransij1,texeij1,cdk),options);
The first parameter you have there is
fun(x0,StoreInitialization,ttransij1,texeij1,theta,D_Cloud,F_Cloud,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3)
This asks that fun() be invoked with all of those parameters, and that the output is expected to be the handle of a function that should then be passed into fmincon .
The part
nonlcon(Numtk,constr,ttransij1,texeij1,cdk)
says that nonlcon is to be invoked with those 5 parameters, and that the output should be a function handle that will be passed in to fmincon to be invoked to determine the nonlinear inequalities.
I suspect you need something more like
fun = @(x) FCVEC06January2022V2(x,StoreInitialization,ttransij1,texeij1,theta,D_Cloud,F_Cloud,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3);
ncon = @(x) nonlcon(x, Numtk,constr,ttransij1,texeij1,cdk);
x = fmincon(fun, x0, A, b, Aeq, beq, lb, ub, ncon, options);
Notice that x had to be added to the parameters for nonlcon
We have not seen your code for FCVEC06January2022V2 . The error message would be consistent with the possibility that your function does not define any outputs, such as if you had
function FCVEC06January2022V2(a bunch of parameters here)
I do not see anything obvious, but I do not have the full code to test with.
Put in a breakpoint at the line
x = fmincon(fun, x0, A, b, Aeq, beq, lb, ub, ncon, options);
and when you get there, execute
fun(x0)
and show us what the result is.

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R13SP1

Tag

Non è stata ancora inserito alcun tag.

Community Treasure Hunt

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

Start Hunting!

Translated by