Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fmincon's problems in sharing variables between functions of objective and nonlinear constrains

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: Ken Lai

Date: 9 Jan, 2010 14:39:03

Message: 1 of 14

To solve a constrainted nonlinear programming problem, with both objective (function) and nonlinear constraints (function) using the same result from the same computation on optimized variables, e.g. the nonlinear constraints is the maximum value of objective. In order to avoid performing the same computation on optimized variables twices, it is deisred to share the resultant varialbes between objective and nonlinear constraint.

As described in "Example: Using fminimax with a Simulink Model" in help manual, "the nonlinear constraint function is always called immediately after the objective function in fmincon...with the same values".

Based on the above "fact", one can get used of nested function to share variable between objective and nonlinear constratins. However, it is truth only in "active-set" method, and not all all in "interior-point" and "sqp" methods. For example, in "interior-point" method, the "barrier" function somehow call the nonlinear constraint before calling objective, which leads to problems in sharing variables thus in optimization.

Does anyone come across the same problem?

And is there any good ideas to sharing variable between objective and nonlinear constraints, with and/or without considerations on the order to call objective and constraint?

Moreover, is there anyway I can inform MATLAB that there maybe a possible misleading in the help manual, so that they can correct it, or the other way to prove that I am wrong about it?

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: Matt J

Date: 9 Jan, 2010 15:18:03

Message: 2 of 14

"Ken Lai" <c.k.lai@cranfield.ac.uk> wrote in message <hia4e7$spj$1@fred.mathworks.com>...
> To solve a constrainted nonlinear programming problem, with both objective (function) and nonlinear constraints (function) using the same result from the same computation on optimized variables, e.g. the nonlinear constraints is the maximum value of objective. In order to avoid performing the same computation on optimized variables twices, it is deisred to share the resultant varialbes between objective and nonlinear constraint.
>
> As described in "Example: Using fminimax with a Simulink Model" in help manual, "the nonlinear constraint function is always called immediately after the objective function in fmincon...with the same values".
>
> Based on the above "fact", one can get used of nested function to share variable between objective and nonlinear constratins. However, it is truth only in "active-set" method, and not all all in "interior-point" and "sqp" methods. For example, in "interior-point" method, the "barrier" function somehow call the nonlinear constraint before calling objective, which leads to problems in sharing variables thus in optimization.
>
> Does anyone come across the same problem?
>
> And is there any good ideas to sharing variable between objective and nonlinear constraints, with and/or without considerations on the order to call objective and constraint?
======================

You could build your objective and constraint handles something like the example below. Note, in this example, that LastX and ImportantQuantity are externally scoped variables, so the workspace of both objective() and constraint() share copies of them. In the example, the ImportantQuantity is the recurring quantity you are talking about and only gets updated when a new x is being processed, either by the constraint or the objective.

function [f,c]=ObjectiveAndConstraints

ImportantQuantity=[];
LastX=[];

f=@objective;
c=@constraint;

   function objective(x)
    
        if isequal(x,LastX)
            ImportantQuantity=....;
       else
          LastX=x;
       end

        %other code down here
       
   end

   function constraint(x)
    
        if isequal(x,LastX)
            ImportantQuantity=....;
       else
          LastX=x;
       end

        %other code down here

   end


end



============
> Moreover, is there anyway I can inform MATLAB that there maybe a possible misleading in the help manual, so that they can correct it, or the other way to prove that I am wrong about it?
=========================

Email tech support?

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: Matt J

Date: 9 Jan, 2010 18:01:05

Message: 3 of 14

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hia6nb$n1n$1@fred.mathworks.com>...

> if isequal(x,LastX)
> ImportantQuantity=....;
> else
> LastX=x;
> end

Sorry, this should really be

        if ~isequal(x,LastX) %update if x is new

            ImportantQuantity=....;
            LastX=x;

       end

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: Ken Lai

Date: 9 Jan, 2010 20:00:06

Message: 4 of 14

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hiag90$5gg$1@fred.mathworks.com>...
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hia6nb$n1n$1@fred.mathworks.com>...
>
> > if isequal(x,LastX)
> > ImportantQuantity=....;
> > else
> > LastX=x;
> > end
>
> Sorry, this should really be
>
> if ~isequal(x,LastX) %update if x is new
>
> ImportantQuantity=....;
> LastX=x;
>
> end

Matt, your additional flow control does solve my problem!!! Thank you very much!!! You save not only my time but also my algorithm's computation time. Thanks again!

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: John

Date: 15 Jun, 2011 18:38:04

Message: 5 of 14

"Matt J" wrote in message <hia6nb$n1n$1@fred.mathworks.com>...
> "Ken Lai" <c.k.lai@cranfield.ac.uk> wrote in message <hia4e7$spj$1@fred.mathworks.com>...
> > To solve a constrainted nonlinear programming problem, with both objective (function) and nonlinear constraints (function) using the same result from the same computation on optimized variables, e.g. the nonlinear constraints is the maximum value of objective. In order to avoid performing the same computation on optimized variables twices, it is deisred to share the resultant varialbes between objective and nonlinear constraint.
> >
> > As described in "Example: Using fminimax with a Simulink Model" in help manual, "the nonlinear constraint function is always called immediately after the objective function in fmincon...with the same values".
> >
> > Based on the above "fact", one can get used of nested function to share variable between objective and nonlinear constratins. However, it is truth only in "active-set" method, and not all all in "interior-point" and "sqp" methods. For example, in "interior-point" method, the "barrier" function somehow call the nonlinear constraint before calling objective, which leads to problems in sharing variables thus in optimization.
> >
> > Does anyone come across the same problem?
> >
> > And is there any good ideas to sharing variable between objective and nonlinear constraints, with and/or without considerations on the order to call objective and constraint?
> ======================
>
> You could build your objective and constraint handles something like the example below. Note, in this example, that LastX and ImportantQuantity are externally scoped variables, so the workspace of both objective() and constraint() share copies of them. In the example, the ImportantQuantity is the recurring quantity you are talking about and only gets updated when a new x is being processed, either by the constraint or the objective.
>
> function [f,c]=ObjectiveAndConstraints
>
> ImportantQuantity=[];
> LastX=[];
>
> f=@objective;
> c=@constraint;
>
> function objective(x)
>
> if isequal(x,LastX)
> ImportantQuantity=....;
> else
> LastX=x;
> end
>
> %other code down here
>
> end
>
> function constraint(x)
>
> if isequal(x,LastX)
> ImportantQuantity=....;
> else
> LastX=x;
> end
>
> %other code down here
>
> end
>
>
> end
>
>
>
> ============
> > Moreover, is there anyway I can inform MATLAB that there maybe a possible misleading in the help manual, so that they can correct it, or the other way to prove that I am wrong about it?
> =========================
>
> Email tech support?

Is it possible you could be more specific as to how the process works using fmincon? I am facing a similar problem-- my constrain function uses the value calculated by the objective function-- i.e., the value that needs to be minimized. In the above code, how can you use fmincon?-- fmincon calls both the objective function and the constraint.

Your help is much appreciated!

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: Matt J

Date: 15 Jun, 2011 20:18:04

Message: 6 of 14

 
> Is it possible you could be more specific as to how the process works using fmincon? I am facing a similar problem-- my constrain function uses the value calculated by the objective function-- i.e., the value that needs to be minimized. In the above code, how can you use fmincon?-- fmincon calls both the objective function and the constraint.
==========================

If I've understood you (and I'm not sure that I have), you want your constraint functions to have access to the current value of the objective(x) without having to call objective(x) additional times. If that's what you mean, adapting the above to your situation would probably look like the following:



function [f,c]=ObjectiveAndConstraints

ImportantQuantity=[]; %Will always store the current value of objective

LastX=[]; %The current value of x computed by the algorithm

f=@objective;
c=@constraint;

   function Fval=objective(x)
    

        if ~isequal(x,LastX) %update stored quantities if x has changed

             val=...%code to compute objective function value at current x


            ImportantQuantity=val; %store updated objective value

            LastX=x; %store upated x

       end

           Fval=ImportantQuantity; %ImportantQuantity is always the current objective value
      

       
   end

   function Cval=constraint(x)
    
        if ~isequal(x,LastX) %update stored quantities if x has changed

            ImportantQuantity=objective(x);
            LastX=x;
       end

       Cval =... % use ImportantQuantity to determine output Cval

   end


end

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: John

Date: 15 Jun, 2011 20:26:03

Message: 7 of 14

"Matt J" wrote in message <itb41s$oqe$1@newscl01ah.mathworks.com>...
>
> > Is it possible you could be more specific as to how the process works using fmincon? I am facing a similar problem-- my constrain function uses the value calculated by the objective function-- i.e., the value that needs to be minimized. In the above code, how can you use fmincon?-- fmincon calls both the objective function and the constraint.
> ==========================
>
> If I've understood you (and I'm not sure that I have), you want your constraint functions to have access to the current value of the objective(x) without having to call objective(x) additional times. If that's what you mean, adapting the above to your situation would probably look like the following:
>
>
>
> function [f,c]=ObjectiveAndConstraints
>
> ImportantQuantity=[]; %Will always store the current value of objective
>
> LastX=[]; %The current value of x computed by the algorithm
>
> f=@objective;
> c=@constraint;
>
> function Fval=objective(x)
>
>
> if ~isequal(x,LastX) %update stored quantities if x has changed
>
> val=...%code to compute objective function value at current x
>
>
> ImportantQuantity=val; %store updated objective value
>
> LastX=x; %store upated x
>
> end
>
> Fval=ImportantQuantity; %ImportantQuantity is always the current objective value
>
>
>
> end
>
> function Cval=constraint(x)
>
> if ~isequal(x,LastX) %update stored quantities if x has changed
>
> ImportantQuantity=objective(x);
> LastX=x;
> end
>
> Cval =... % use ImportantQuantity to determine output Cval
>
> end
>
>
> end

Thanks very much. It may very well be that you didn't understand me-- I've just finished sophomore year and I'm pretty new to programming. So I may not even understand the question I need to be asking! I will study what you've written and see if I can figure it out-- or maybe phrase my question more intelligently.
Thanks again.

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: John

Date: 15 Jun, 2011 20:40:19

Message: 8 of 14

Ok, if I sound stupid, it's because I'm an amateur. In your above post, you have:
.....
> > function Fval=objective(x)
> >
> >
> > if ~isequal(x,LastX) %update stored quantities if x has changed
> >
> > val=...%code to compute objective function value at current x
> >
> >
> > ImportantQuantity=val; %store updated objective value
> >
> > LastX=x; %store upated x
> >
> > end
.....

Right, so where you write val = %code to compute objective function value at current x, I want to use fmincon. The syntax for fmincon is as follows:

X = FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON)

Is it possible for this to work, or must you write your own code to replace fmincon?

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: Matt J

Date: 16 Jun, 2011 01:43:02

Message: 9 of 14

"John " <johnps@gmail.com> wrote in message <itb5bj$sng$1@newscl01ah.mathworks.com>...
>
> Right, so where you write val = %code to compute objective function value at current x, I want to use fmincon.

No, I'm skeptical that that is what you truly want.

In what I've written above, objective(x) is supposed to represent the function you are trying to minimize (using FMINCON) and constraint(x) is supposed to represent the nonlinear constraints in your problem.

Normally, FMINCON is meant to operate on your desired objective(x) and
constraint(x), and not the other way around. It is therefore peculiar that you've reversed their roles and are now telling us that objective() is supposed to call FMINCON.
It makes me suspect you've confused the roles of the different functions that have been shown.

The intended use of the outer function, what I've called ObjectiveAndConstraints, is to build handles to objective() and constraint() so that that you might then pass those handles to FMINCON, as in,

 [FUN,NONLCON]=ObjectiveAndConstraints

 X = FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON)


Because objective() and constraint() are nested functions inside ObjectiveAndConstraints, they share access to its workspace. That means that ImportantQuantity and LastX are like global variables shared among all these functions. So, if you want some intermediate quantity computed inside objective(x) to be accessible to constraint(x), or vice versa, all you have to do is store that quantity to ImportantQuantity and both functions will have global access to it.

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: John

Date: 16 Jun, 2011 14:20:20

Message: 10 of 14

"Matt J" wrote in message <itbn36$cod$1@newscl01ah.mathworks.com>...
> "John " <johnps@gmail.com> wrote in message <itb5bj$sng$1@newscl01ah.mathworks.com>...
> >
> > Right, so where you write val = %code to compute objective function value at current x, I want to use fmincon.
>
> No, I'm skeptical that that is what you truly want.
>
> In what I've written above, objective(x) is supposed to represent the function you are trying to minimize (using FMINCON) and constraint(x) is supposed to represent the nonlinear constraints in your problem.
>
> Normally, FMINCON is meant to operate on your desired objective(x) and
> constraint(x), and not the other way around. It is therefore peculiar that you've reversed their roles and are now telling us that objective() is supposed to call FMINCON.
> It makes me suspect you've confused the roles of the different functions that have been shown.
>
> The intended use of the outer function, what I've called ObjectiveAndConstraints, is to build handles to objective() and constraint() so that that you might then pass those handles to FMINCON, as in,
>
> [FUN,NONLCON]=ObjectiveAndConstraints
>
> X = FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON)
>
>
> Because objective() and constraint() are nested functions inside ObjectiveAndConstraints, they share access to its workspace. That means that ImportantQuantity and LastX are like global variables shared among all these functions. So, if you want some intermediate quantity computed inside objective(x) to be accessible to constraint(x), or vice versa, all you have to do is store that quantity to ImportantQuantity and both functions will have global access to it.

Alright. Actually, you described what I want-- I want a quantity computed inside objective(x) to be accessible to constraint(x). And I actually do understand the purposes of objective versus constraints functions-- it is their interaction with fmincon that is probably causing me some confusion. Again, computer programming in general is new to me.


I'm going to try to get this to work. Your help is very much appreciated.

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: Sargondjani

Date: 4 Apr, 2012 07:27:18

Message: 11 of 14

Matt, you are a king!! This is ace.... really, just what I needed.

I do have 1 further question though: is there a way to get the value of 'ImportantQuantity' in the final iteration?

It would be nice if I didnt have to calculate it again after the optimization.

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: Matt J

Date: 4 Apr, 2012 13:37:10

Message: 12 of 14

"Sargondjani" wrote in message <jlgt4m$dgt$1@newscl01ah.mathworks.com>...
> Matt, you are a king!! This is ace.... really, just what I needed.
>
> I do have 1 further question though: is there a way to get the value of 'ImportantQuantity' in the final iteration?
>
> It would be nice if I didnt have to calculate it again after the optimization.

Just return another nested function handle that let's you flush the state variables (or post-manipulate them in whatever other way you like). So, that might look like:

function [f,c,flush]=ObjectiveAndConstraints

ImportantQuantity=[];
LastX=[];

f=@objective;
c=@constraint;
flush=@flushstate;

   function [fval,grad]=objective(x)
       ....
   end

   function [c,ceq]=constraint(x)
      ...
   end

   function [state1, state2]=flushstate(x)

      state1=ImportantQuantity;
      state2=LastX;

     ImportantQuantity=[]; LastX=[]; %might be a good idea to clear
    
   end

end

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: hadi

Date: 24 May, 2012 11:37:07

Message: 13 of 14

"Matt J" wrote in message <jlhiq6$ga$1@newscl01ah.mathworks.com>...
> "Sargondjani" wrote in message <jlgt4m$dgt$1@newscl01ah.mathworks.com>...
> > Matt, you are a king!! This is ace.... really, just what I needed.
> >
> > I do have 1 further question though: is there a way to get the value of 'ImportantQuantity' in the final iteration?
> >
> > It would be nice if I didnt have to calculate it again after the optimization.
>
> Just return another nested function handle that let's you flush the state variables (or post-manipulate them in whatever other way you like). So, that might look like:
>
> function [f,c,flush]=ObjectiveAndConstraints
>
> ImportantQuantity=[];
> LastX=[];
>
> f=@objective;
> c=@constraint;
> flush=@flushstate;
>
> function [fval,grad]=objective(x)
> ....
> end
>
> function [c,ceq]=constraint(x)
> ...
> end
>
> function [state1, state2]=flushstate(x)
>
> state1=ImportantQuantity;
> state2=LastX;
>
> ImportantQuantity=[]; LastX=[]; %might be a good idea to clear
>
> end
>
> end


Thanks Matt for comprehensive explanation, however how we could call the fmincon function?!!
when I call the fmincon function as follows:
[x,fval,exitflag,output]=fmincon(@f,x0,A,b,Aeq,beq,lb,ub,@c,options)
and define the f and c function exactly as you suggested on above, I faced to the below error:

??? Error using ==> feval
Undefined function or method 'f' for input arguments of type 'double'.

Error in ==> fmincon at 574
      initVals.f = feval(funfcn{3},X,varargin{:});

Caused by:
    Failure in initial user-supplied objective function evaluation. FMINCON cannot
    continue.
    
*And the second question is will I be able to define the x as a column vector (44x1) and uses it in a four divided vectors(11x1)?!! x(1:11);x(12:22);x(23:33)and x(34:44)?

Subject: fmincon's problems in sharing variables between functions of objective and nonlinear constrains

From: Matt J

Date: 24 May, 2012 13:42:07

Message: 14 of 14

"hadi " <hadisengin@yahoo.co.uk> wrote in message <jpl6h3$j3l$1@newscl01ah.mathworks.com>...
>
> Thanks Matt for comprehensive explanation, however how we could call the fmincon function?!!
> when I call the fmincon function as follows:
> [x,fval,exitflag,output]=fmincon(@f,x0,A,b,Aeq,beq,lb,ub,@c,options)
> and define the f and c function exactly as you suggested on above, I faced to the below error:
>
> ??? Error using ==> feval
> Undefined function or method 'f' for input arguments of type 'double'.
=================

If you've constructed f and c as I've described, then they are already handles and don't need the '@' operator. In other words, you should be calling FMINCON like this

[x,fval,exitflag,output]=fmincon(f,x0,A,b,Aeq,beq,lb,ub,c,options)


> *And the second question is will I be able to define the x as a column vector (44x1) and uses it in a four divided vectors(11x1)?!! x(1:11);x(12:22);x(23:33)and x(34:44)?
=============

Once you've passed x to f and c, you can divide up x into separate parts or do anything else you please (as long as the final value returned by them is differentiable function of x)

However A and Aeq must have 44 columns.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us