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:
How to generate input files for fmincon using regular mathematical program syntax

Subject: How to generate input files for fmincon using regular mathematical program syntax

From: Angie

Date: 18 Jan, 2010 22:25:04

Message: 1 of 7

Hi:

I was wondering if there exists code to (auto) generate the required input files for the constraints and the objective function to use with fmincon. Specifically, I want to enter the following in a text file:

minimize f(x)
subject to
g(x) <= 0
h(x) = 0

where f(x), g(x) and h(x) are (possibly nonlinear) functions of several variables denoted by x(1), x(2), ....

and I want the corresponding objective function and constraint files including the gradients and the hessians to be formed automatically.

Thank you,

Angie

Subject: How to generate input files for fmincon using regular mathematical program syntax

From: Matt J

Date: 18 Jan, 2010 23:15:09

Message: 2 of 7

"Angie" <angie11tr@yahoo.com> wrote in message <hj2n40$fg4$1@fred.mathworks.com>...
> Hi:
>
> I was wondering if there exists code to (auto) generate the required input files for the constraints and the objective function to use with fmincon. Specifically, I want to enter the following in a text file:
>
> minimize f(x)
> subject to
> g(x) <= 0
> h(x) = 0

What you've shown in this example "text file" gives no information about the definitions of f(), g(), or h(). Where would that info come from?

Subject: How to generate input files for fmincon using regular mathematical program syntax

From: Angie

Date: 18 Jan, 2010 23:48:03

Message: 3 of 7

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hj2q1t$l1j$1@fred.mathworks.com>...
> "Angie" <angie11tr@yahoo.com> wrote in message <hj2n40$fg4$1@fred.mathworks.com>...
> > Hi:
> >
> > I was wondering if there exists code to (auto) generate the required input files for the constraints and the objective function to use with fmincon. Specifically, I want to enter the following in a text file:
> >
> > minimize f(x)
> > subject to
> > g(x) <= 0
> > h(x) = 0
>
> What you've shown in this example "text file" gives no information about the definitions of f(), g(), or h(). Where would that info come from?

Sorry for the confusion, I simply simplified the expressions into f(x), g(x), h(x). As an example consider the following mathematical program:

minimize a*x(1)^2 + b*x(2)^2 + c*x(1)*x(2)
subject to
- d*x(1)^2 + 1 <= 0
- e*x(1)*x(2) + 2 <= 0
- x(1) <= 0
- x(2) <= 0

in which I don't have any equalities for simplicity. a, b, c, d, e will be symbolic parameters. I want my text file to contain the above mathematical model as it is. Does this answer your question?

Subject: How to generate input files for fmincon using regular mathematical program syntax

From: Matt J

Date: 19 Jan, 2010 03:51:03

Message: 4 of 7

"Angie" <angie11tr@yahoo.com> wrote in message <hj2rvj$liu$1@fred.mathworks.com>...

> Sorry for the confusion, I simply simplified the expressions into f(x), g(x), h(x). As an example consider the following mathematical program:
>
> minimize a*x(1)^2 + b*x(2)^2 + c*x(1)*x(2)
> subject to
> - d*x(1)^2 + 1 <= 0
> - e*x(1)*x(2) + 2 <= 0
> - x(1) <= 0
> - x(2) <= 0
>
> in which I don't have any equalities for simplicity. a, b, c, d, e will be symbolic parameters. I want my text file to contain the above mathematical model as it is. Does this answer your question?
=========================

I see. Well, I don't think code already exists, although you could check the File Exchange.

If you wanted to code it yourself, you could fairly easily use fgetl() and regexp() to parse the file and find the expressions for f,g, and h as strings. However, you would then probably need the symbolic toolbox to do the symbolic differentiation to get the expression for the gradient/Hessian.

In any case, this is not normally the way you would want to do things. It is not typically efficient to implement the objective function, constraints, and gradients as directly given by their symbolic expressions.

For example, if your objective function is, like in your example, a quadratic
f(x)=x'*(Q/2)*x and you know you also want to use the analytic gradient
grad(x)=Q*x, then you would not want a file that evaluates these two symbolic expressions separately. You would want a file that is smart enough to first compute the gradient grad(x) and then derive f(x) from the gradient according to
f(x)=x'*grad(x), which is equivalent to x'*Q*x but doesn't require you to evaluate Q*x a second time.

Were you hoping to find a piece of code that recognizes these efficient strategies automatically? Such code would probably border on artifical intelligence, unless it new very specific things ahead of time about the structure of your problem.

Subject: How to generate input files for fmincon using regular mathematical program syntax

From: Angie

Date: 19 Jan, 2010 05:56:02

Message: 5 of 7

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hj3a77$lte$1@fred.mathworks.com>...
> "Angie" <angie11tr@yahoo.com> wrote in message <hj2rvj$liu$1@fred.mathworks.com>...
>
> > Sorry for the confusion, I simply simplified the expressions into f(x), g(x), h(x). As an example consider the following mathematical program:
> >
> > minimize a*x(1)^2 + b*x(2)^2 + c*x(1)*x(2)
> > subject to
> > - d*x(1)^2 + 1 <= 0
> > - e*x(1)*x(2) + 2 <= 0
> > - x(1) <= 0
> > - x(2) <= 0
> >
> > in which I don't have any equalities for simplicity. a, b, c, d, e will be symbolic parameters. I want my text file to contain the above mathematical model as it is. Does this answer your question?
> =========================
>
> I see. Well, I don't think code already exists, although you could check the File Exchange.
>
> If you wanted to code it yourself, you could fairly easily use fgetl() and regexp() to parse the file and find the expressions for f,g, and h as strings. However, you would then probably need the symbolic toolbox to do the symbolic differentiation to get the expression for the gradient/Hessian.
>
> In any case, this is not normally the way you would want to do things. It is not typically efficient to implement the objective function, constraints, and gradients as directly given by their symbolic expressions.
>
> For example, if your objective function is, like in your example, a quadratic
> f(x)=x'*(Q/2)*x and you know you also want to use the analytic gradient
> grad(x)=Q*x, then you would not want a file that evaluates these two symbolic expressions separately. You would want a file that is smart enough to first compute the gradient grad(x) and then derive f(x) from the gradient according to
> f(x)=x'*grad(x), which is equivalent to x'*Q*x but doesn't require you to evaluate Q*x a second time.
>
> Were you hoping to find a piece of code that recognizes these efficient strategies automatically? Such code would probably border on artifical intelligence, unless it new very specific things ahead of time about the structure of your problem.

Thank you for your answer. I have several models and each time I use fmincon I have to update everything from scratch. It is easy to make mistakes with copy and paste and this was the main reason for my post. I tried to come up with my own piece of code to do what I want but it gets messy. I was hoping maybe that such code exists. Maybe I will look more into what I did and if I come up with something I will post again.

Thanks again...

Subject: How to generate input files for fmincon using regular mathematical

From: Alan Weiss

Date: 19 Jan, 2010 12:37:33

Message: 6 of 7

Take a look at the following doc example, which shows how to do what you
are asking:
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1

This example requires Symbolic Math Toolbox(tm).

You might also want to look the a demo showing how to do the same sort
of things using function handles instead of files:
http://www.mathworks.com/products/optimization/demos.html?file=/products/demos/shipping/optim/symbolic_optim_demo.html

Alan Weiss
MATLAB mathematical toolbox documentation

Angie wrote:
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
> <hj2q1t$l1j$1@fred.mathworks.com>...
>> "Angie" <angie11tr@yahoo.com> wrote in message
>> <hj2n40$fg4$1@fred.mathworks.com>...
>> > Hi:
>> > > I was wondering if there exists code to (auto) generate the
>> required input files for the constraints and the objective function to
>> use with fmincon. Specifically, I want to enter the following in a
>> text file:
>> > > minimize f(x)
>> > subject to
>> > g(x) <= 0
>> > h(x) = 0
>>
>> What you've shown in this example "text file" gives no information
>> about the definitions of f(), g(), or h(). Where would that info come
>> from?
>
> Sorry for the confusion, I simply simplified the expressions into f(x),
> g(x), h(x). As an example consider the following mathematical program:
>
> minimize a*x(1)^2 + b*x(2)^2 + c*x(1)*x(2)
> subject to
> - d*x(1)^2 + 1 <= 0
> - e*x(1)*x(2) + 2 <= 0
> - x(1) <= 0
> - x(2) <= 0
>
> in which I don't have any equalities for simplicity. a, b, c, d, e will
> be symbolic parameters. I want my text file to contain the above
> mathematical model as it is. Does this answer your question?

Subject: How to generate input files for fmincon using regular mathematical

From: Angie

Date: 19 Jan, 2010 22:02:04

Message: 7 of 7

Alan Weiss <aweiss@mathworks.com> wrote in message <hj492e$e0i$1@fred.mathworks.com>...
> Take a look at the following doc example, which shows how to do what you
> are asking:
> http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1
>
> This example requires Symbolic Math Toolbox(tm).
>
> You might also want to look the a demo showing how to do the same sort
> of things using function handles instead of files:
> http://www.mathworks.com/products/optimization/demos.html?file=/products/demos/shipping/optim/symbolic_optim_demo.html
>
> Alan Weiss
> MATLAB mathematical toolbox documentation
>
> Angie wrote:
> > "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
> > <hj2q1t$l1j$1@fred.mathworks.com>...
> >> "Angie" wrote in message
> >> <hj2n40$fg4$1@fred.mathworks.com>...
> >> > Hi:
> >> > > I was wondering if there exists code to (auto) generate the
> >> required input files for the constraints and the objective function to
> >> use with fmincon. Specifically, I want to enter the following in a
> >> text file:
> >> > > minimize f(x)
> >> > subject to
> >> > g(x) <= 0
> >> > h(x) = 0
> >>
> >> What you've shown in this example "text file" gives no information
> >> about the definitions of f(), g(), or h(). Where would that info come
> >> from?
> >
> > Sorry for the confusion, I simply simplified the expressions into f(x),
> > g(x), h(x). As an example consider the following mathematical program:
> >
> > minimize a*x(1)^2 + b*x(2)^2 + c*x(1)*x(2)
> > subject to
> > - d*x(1)^2 + 1 <= 0
> > - e*x(1)*x(2) + 2 <= 0
> > - x(1) <= 0
> > - x(2) <= 0
> >
> > in which I don't have any equalities for simplicity. a, b, c, d, e will
> > be symbolic parameters. I want my text file to contain the above
> > mathematical model as it is. Does this answer your question?

==============================
Thank you both.

@Alan Weiss: the link you sent:

http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1

perfectly worked for my case and I could come up with a customized version.

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