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:
Question regarding fmincon and matrix input

Subject: Question regarding fmincon and matrix input

From: toms Seidel

Date: 12 Sep, 2012 15:33:08

Message: 1 of 4

Hello Matlab Fellows!

I have to solve a minimization problem with nonlinear constraints in the most general case. There are about 150 decision variables and almost the same number of constraints. I think fmincon should be able to tackle such a problem right?

In fact there a three different types of decision variables, one modelling the number of items produced, inventory and backlog. For all of them, they also depend on the time, so I thought of modelling it as matrix x of size Time Periods x 3. By doing so, I hope to gain additional clearity and avoiding of having to remember x(153) is the backlog variable of time period 12.

Does such an approach work? I searched the newsgroup and found out that matrix input would be changed to a serialized vector. But this would ruin my idea then, wouldn't it. For instance, I thought of the following:

[sol, fval] = fmincon(obj_fun, x0, ... whereby

function eq = obj_fun(x)
 % x(:,1) procued quantity of all periods
  eq = sum( x(:,1)*2) + sum(x(:, 2) * 3
end

plus an additional nonlinear constraint function which would address the decision variable matrix in the same way.

Any hints appreciated!

Subject: Question regarding fmincon and matrix input

From: Alan_Weiss

Date: 12 Sep, 2012 15:50:12

Message: 2 of 4

On 9/12/2012 11:33 AM, toms Seidel wrote:
> Hello Matlab Fellows!
>
> I have to solve a minimization problem with nonlinear constraints in
> the most general case. There are about 150 decision variables and
> almost the same number of constraints. I think fmincon should be able
> to tackle such a problem right?
>
> In fact there a three different types of decision variables, one
> modelling the number of items produced, inventory and backlog. For all
> of them, they also depend on the time, so I thought of modelling it as
> matrix x of size Time Periods x 3. By doing so, I hope to gain
> additional clearity and avoiding of having to remember x(153) is the
> backlog variable of time period 12.
>
> Does such an approach work? I searched the newsgroup and found out
> that matrix input would be changed to a serialized vector. But this
> would ruin my idea then, wouldn't it. For instance, I thought of the
> following:
>
> [sol, fval] = fmincon(obj_fun, x0, ... whereby
>
> function eq = obj_fun(x)
> % x(:,1) procued quantity of all periods
> eq = sum( x(:,1)*2) + sum(x(:, 2) * 3
> end
>
> plus an additional nonlinear constraint function which would address
> the decision variable matrix in the same way.
>
> Any hints appreciated!

Here is a sneak preview of some relevant upcoming documentation:

Nonlinear solvers accept matrix initial point x0. They also accept
matrix bounds lb and ub. Here's how solvers handle matrix arguments.

  *

    Internally, solvers convert matrix arguments into vectors before
    processing. For example, x0 becomes x0(:). For an explanation of
    this syntax, see the A(:) entry in colon <../../matlab/ref/colon.html>.

  *

    For output, solvers reshape the solution x to the same size as the
    input x0.

  *

    When x0 is a matrix, solvers pass x as a matrix of the same size as
    x0 to both the objective function and to any nonlinear constraint
    function.

  *

    Linear constraints <writing-constraints.html#brhkghv-14>, though,
    take x in vector form, x(:). In other words, a linear constraint of
    the form

    A*x ≤ b or Aeq*x = beq

    takes x as a vector, not a matrix. Ensure that your matrix A or Aeq
    has the same number of columns as x0 has elements, or the solver
    will error.


Alan Weiss
MATLAB mathematical toolbox documentation

Subject: Question regarding fmincon and matrix input

From: toms Seidel

Date: 12 Sep, 2012 16:15:08

Message: 3 of 4

Alan, thanks a lot for your helpful piece of advice. So as long as I pass x0 as a matrix as well, I can follow this approach. Am I correct?

Thanks!

Alan_Weiss <aweiss@mathworks.com> wrote in message <k2qavk$d0k$1@newscl01ah.mathworks.com>...
> On 9/12/2012 11:33 AM, toms Seidel wrote:
> > Hello Matlab Fellows!
> >
> > I have to solve a minimization problem with nonlinear constraints in
> > the most general case. There are about 150 decision variables and
> > almost the same number of constraints. I think fmincon should be able
> > to tackle such a problem right?
> >
> > In fact there a three different types of decision variables, one
> > modelling the number of items produced, inventory and backlog. For all
> > of them, they also depend on the time, so I thought of modelling it as
> > matrix x of size Time Periods x 3. By doing so, I hope to gain
> > additional clearity and avoiding of having to remember x(153) is the
> > backlog variable of time period 12.
> >
> > Does such an approach work? I searched the newsgroup and found out
> > that matrix input would be changed to a serialized vector. But this
> > would ruin my idea then, wouldn't it. For instance, I thought of the
> > following:
> >
> > [sol, fval] = fmincon(obj_fun, x0, ... whereby
> >
> > function eq = obj_fun(x)
> > % x(:,1) procued quantity of all periods
> > eq = sum( x(:,1)*2) + sum(x(:, 2) * 3
> > end
> >
> > plus an additional nonlinear constraint function which would address
> > the decision variable matrix in the same way.
> >
> > Any hints appreciated!
>
> Here is a sneak preview of some relevant upcoming documentation:
>
> Nonlinear solvers accept matrix initial point x0. They also accept
> matrix bounds lb and ub. Here's how solvers handle matrix arguments.
>
> *
>
> Internally, solvers convert matrix arguments into vectors before
> processing. For example, x0 becomes x0(:). For an explanation of
> this syntax, see the A(:) entry in colon <../../matlab/ref/colon.html>.
>
> *
>
> For output, solvers reshape the solution x to the same size as the
> input x0.
>
> *
>
> When x0 is a matrix, solvers pass x as a matrix of the same size as
> x0 to both the objective function and to any nonlinear constraint
> function.
>
> *
>
> Linear constraints <writing-constraints.html#brhkghv-14>, though,
> take x in vector form, x(:). In other words, a linear constraint of
> the form
>
> A*x ? b or Aeq*x = beq
>
> takes x as a vector, not a matrix. Ensure that your matrix A or Aeq
> has the same number of columns as x0 has elements, or the solver
> will error.
>
>
> Alan Weiss
> MATLAB mathematical toolbox documentation
>

Subject: Question regarding fmincon and matrix input

From: Matt J

Date: 12 Sep, 2012 20:56:08

Message: 4 of 4

"toms Seidel" wrote in message <k2q9vk$947$1@newscl01ah.mathworks.com>...
>
> [sol, fval] = fmincon(obj_fun, x0, ... whereby
>
> function eq = obj_fun(x)
> % x(:,1) procued quantity of all periods
> eq = sum( x(:,1)*2) + sum(x(:, 2) * 3
> end
==============

Incidentally, it would probably be cheaper to do this using 1 call to SUM,

 eq=sum(x(:,1:2))*[2;3]

or

 eq=sum( x(:,1:2) * [2;3] )

Tags for this Thread

No tags are associated with 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