Thread Subject: How to use the same data in both objective and constraints function, fmincon

Subject: How to use the same data in both objective and constraints function, fmincon

From: Arne Vagren

Date: 16 Aug, 2009 10:34:03

Message: 1 of 7

Hello!

I am planning on using fmincon for solving constrainted nonlinear optimization problem. The problem involves solving a rather computationally heavy state problem in order to compute the objective and constraint functions and gradients. Now as I understand it, evaluation of objective and constraints are put in separate functions when using fmincon, so it seems as if I would need to solve the state problem twice for each iteration? My first idea to get around this problem was to solve the state problem in the objective function, save the state variables to file, then open that file in the constraints function. I haven't tested the idea yet, but I thought I should ask if there is any better way to do this?

Regards
Arne

Subject: How to use the same data in both objective and constraints function, fmincon

From: Matt

Date: 16 Aug, 2009 16:46:02

Message: 2 of 7

You might be able to use nested functions to manage the state variables, as follows


function Output=MyOptimizer(data)

%State variables
ConstraintState=[];
FunctionValState=[];
Xstate=[];
Updated=1;

Output= fmincon(....) %call to fmincon

  function ProcessStateEquations(x,data)


    [ConstraintState,FunctionValState]=*whatever you use to compute state*
    Updated=1;
    Xstate=x;

  end

  function c=ComputeConstraints(x, data)

     c=ConstraintState;
     if isequal(x,CurrentX);
      Updated=0;
      return;
   else
      ProcessStateEquations(x, data);
    end

  end

   function f=ComputeFunctionVal(x, data)

    f=FunctionValState;
      if isequal(x,CurrentX);
      Updated=0;
      return;
   else
      ProcessStateEquations(x, data);
    end

  end


end

Subject: How to use the same data in both objective and constraints function, fmincon

From: Arne Vagren

Date: 16 Aug, 2009 19:02:04

Message: 3 of 7

Thanks for the reply! Interesting idea, I will look into it. By
the way, is there a particular reason why the developers have
chosen to have the user split objective and constraint calculations
in two functions?

best regards
arne

Subject: How to use the same data in both objective and constraints function, fmincon

From: Matt

Date: 17 Aug, 2009 07:14:02

Message: 4 of 7

"Arne Vagren" <arne.vagren@gmail.com> wrote in message <h69l3c$jet$1@fred.mathworks.com>...
> Thanks for the reply! Interesting idea, I will look into it. By
> the way, is there a particular reason why the developers have
> chosen to have the user split objective and constraint calculations
> in two functions?


I can't speak for the developers, but your case does seem unusual in the way that the constraints and function value emerge from a coupled calculation. In most nonlinear programming problems that I've encountered, the function and constraints are each explicit functions of the design variables.

Subject: How to use the same data in both objective and constraints function, fmincon

From: Arne Vagren

Date: 17 Aug, 2009 07:39:02

Message: 5 of 7

"Matt " <xys@whatever.com> wrote in message <h6avvq$83v$1@fred.mathworks.com>...
> "Arne Vagren" <arne.vagren@gmail.com> wrote in message <h69l3c$jet$1@fred.mathworks.com>...
> > Thanks for the reply! Interesting idea, I will look into it. By
> > the way, is there a particular reason why the developers have
> > chosen to have the user split objective and constraint calculations
> > in two functions?
>
>
> I can't speak for the developers, but your case does seem unusual in the way that the constraints and function value emerge from a coupled calculation. In most nonlinear programming problems that I've encountered, the function and constraints are each explicit functions of the design variables.

Actually this is the standard approach in structural optimization (S0), which is what I'm considering. The displacements, or any other state variables, are eliminated from the problem by solving a state problem which is parameterized by the design variables. In SO litterature this is usually referred to as the nested approach, and the other method is called the simultaneous approach.
/Arne

Subject: How to use the same data in both objective and constraints function, fmincon

From: Steven Lord

Date: 17 Aug, 2009 14:14:02

Message: 6 of 7


"Arne Vagren" <arne.vagren@gmail.com> wrote in message
news:h69l3c$jet$1@fred.mathworks.com...
> Thanks for the reply! Interesting idea, I will look into it. By
> the way, is there a particular reason why the developers have
> chosen to have the user split objective and constraint calculations
> in two functions?

I'm not certain why that would be the case, but one possibility is that the
objective function is a _required_ input argument for FMINCON, while the
nonlinear constraint function is an _optional_ input argument. [I'm also
not certain if FMINCON has always accepted a nonlinear constraint function;
its origins predate my time at The MathWorks.]

--
Steve Lord
slord@mathworks.com

Subject: How to use the same data in both objective and constraints function, fmincon

From: Arne Vagren

Date: 29 Aug, 2009 18:48:04

Message: 7 of 7

Thanks for your reply! If anyone's interested, here's another solution based on using persistent variables (the problem is just a dummy problem for illustration):

clear func % to clear persistent variables
x = fmincon(@(x) func(x),x0,A,B,Aeq,Beq,lb,ub,@(x) func(x,1),options);


function varargout = func(x,varargin)

persistent v; % state variables
persistent xold;
if isempty(xold)
   xold = ~x;
end

if any(xold~=x)
    % Solve state problem
   v = rand;
   xold = x;
end

if nargin<2
    % Objective function
    varargout{1} = sum(x);
    varargout{2} = ones(size(x));
else
    % Nonlinear constraint functions
    varargout{1} = sum(x.^2)-1;
    varargout{2} = [];
    varargout{3} = 2*x;
    varargout{4} = [];
end

/Arne

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
fmincon Arne Vagren 16 Aug, 2009 06:39:03
rssFeed for this Thread

Contact us at files@mathworks.com