Constrained optimization problems are almost everywhere in engineering research. A mathematical description of those problems with a single objective is to minimize or maximize an objective function over a set of decision variables under a set of constraints. There are different ways to format optimization problems; personally, I follow the format used in the book “Convex Optimization” by Stephen Boyd and Lieven Vandenberghe. For example, a general optimization problem has the form

This is generated by the following code:

\begin{equation*}
\begin{aligned}
& \underset{x}{\text{minimize}}
& & f_0(x) \\
& \text{subject to}
& & f_i(x) \leq b_i, \; i = 1, \ldots, m.
\end{aligned}
\end{equation*}

As seen in the code, the formatting is done by the aligned environment, which is defined in the amsmath package, so you need to include the following line in the preamble:

\usepackage{amsmath}

Unlike the tabular environment, in which you can specify the alignment of each column, in the aligned environment, each column (separated by &) has a default alignment, which alternates between right and left-aligned. Therefore, all the odd columns are right-aligned and all the even columns are left-aligned.

We conclude with a real example:

The above problem is formulated for completing low-rank positive semidefinite matrices. It is convex, or more precisely, it is a semidefinite program. The corresponding code is

\begin{equation*}
\begin{aligned}
& \underset{X}{\text{minimize}}
& & \mathrm{trace}(X) \\
& \text{subject to}
& & X_{ij} = M_{ij}, \; (i,j) \in \Omega, \\
&&& X \succeq 0.
\end{aligned}
\end{equation*}

Recently, Mr. Garcia has brought to my attention a very neat package that he has contributed: optidef. The package documentation has all the details, and he has also provided some quick examples. I’d definitely recommend this package since its syntax aligns with how you define instead of how you format an optimization problem, which I assume that most people would prefer.

### Like this:

Like Loading...

*Related*

Good reference! Also it leads me to read some paper in your website! Thoughtful man!

Nice piece of code, I had this in Masters thesis too, but can’t find my LaTex scripts.

Cheers

Thanks for sharing this code.

It turns out that book is pretty much my text book

Thank you so much for the code.

It helps a lot.

Thank you. It works like a charm.

Really nice, looks much better than what I was doing. Just need to think of whether I prefer multiple inputs to have parenthesis or not.

Hi,

Thanks a lot for the code, really helpful. I was also wondering how do you include number for each equation?

In each line, I would like an equation number (like \label{}), is that possible and how?

Thanks a lot!

To insert the equation number, just remove the star (*) sign from \equation*

As Mohamed suggested, you can remove the asterisk (*) from \begin{equation*} & \end{equation*} to assign a single number to the entire problem. If you’d like to assign a number to each line, then the following example should help:

\begin{alignat}{2}

& \underset{X}{\text{minimize}}

& & \mathrm{trace}(X) \\

& \text{subject to} \quad

& & X_{ij} = M_{ij}, \; (i,j) \in \Omega, \\

&&& X \succeq 0.

\end{alignat}

I tried the “align” environment, but it left too much space between columns. Notice the \quad I added after \text{subject to}, without which there will be too little space between columns. You can adjust the space as you like.

Great! This is exactly what I was looking for

Thanks a lot!

tnx man u just saved me

works great. thanks.

Thank you a lot !

Thanks a lot. Very helpful.

Thank you so much. Saved me a lot of time.

[…] is a template I found at JC Notes. It produces left-aligned blocks so you need to use constraints without […]

Instead of using the underset, you can use the following (needs standard AMS packages):

\DeclareMathOperator*{\minimize}{minimize}

then use \minimize_{x} and it will make a nice subscript for you

Reblogged this on Kay Fan E and commented:

I hope that there will be a LaTex tool to help write optimization formulations.

I have recently developed a small library to define optimization problems. The source and documentation are in https://www.ctan.org/pkg/optidef, and I have written a small explanation in here http://tex.stackexchange.com/questions/19465/is-there-a-package-for-specifying-optimization-problems

If you find it useful, it would be great if you could also share it.

THANK YOU SO MUCH. Surprisingly complicated, this solution works great, with two different formatting options.