# Is there a package for specifying optimization problems?

Is there a package for specifying optimization problems, such as in linear programming, quadratic programming, convex programming, etc.?

Or do I have to write my own code with align, constraints and so on? I thought that there might be a very elegant way to specify a min/max problem with some constraints below it.

I am talking about something like Example 2.2 in http://people.ucsc.edu/~rgil/Optimization.pdf.

EDIT: Following a request below, I add an illustrating example of a problem I had with align.

\begin{alignat}{3}
\min && n \\
\mathrm{s.t.} && xxxxxxx \\
&& yyyyyyyyyyyyyyyyyyy \\
&& zzzzzzzzzzzzzzzzzzz
\end{alignat}


What happens is that n is aligned to the far left. I want all of the left columns to be centered below each other. This is my way of formulating a constrained optimization problem, if someone has a better idea I will be glad to hear about it.

If I use only two columns, there is almost no space between $\min$ and $n$. I am almost tempted to use eqnarray :-)

• I am not familiar with the desired formatting. Could you please add an image of (or provide a link to) some example of what you want to achieve? – Gonzalo Medina May 30 '11 at 17:57
• Example 2.2 in people.ucsc.edu/~rgil/Optimization.pdf – kloop May 30 '11 at 18:12
• Maybe I am expecting for too much from latex, and it can be just as easily done as using begin{align} end{align}. I thought there might be a more elegant way to do it. Thanks. – kloop May 30 '11 at 18:13
• perhaps you could edit your question to provide the link there and not just in a comment? – Gonzalo Medina May 30 '11 at 19:00

A bit late but it might help other people looking for the same answer. The package optidef was intended as the easy way to define optimization problems.

\usepackage{optidef}
\begin{mini}|l|
{w,u}{f(w)+ R(w+6x)}{}{}
\end{mini}


The package has several more options. Below some examples are provided. To have a completely overview of the features and options check the documentation.

## Using a shorter description of minimize/subject to.

\usepackage[short]{optidef}
\begin{mini}
{w,u}{f(w)+ R(w+6x)}{}{}
\end{mini}


## Breaking the objective accross several lines.

\usepackage[long]{optidef}
\begin{mini}
{w,u}{f(w)+ R(w+6x)+ H(100w-x*w/500)}{}{}
\breakObjective{-g(w^3-x^2*200+10000*w^5)}
\end{mini}


## Multilabeling/no labeling:

\begin{mini*}
{w}{f(w)+ R(w+6x)}
{}{}
\end{mini*}


\begin{mini!}
{w}{f(w)+ R(w+6x)}
{}{}
\end{mini!}


## Alignment position of constraints with respect to the objective:

Alternative 1

\begin{mini}[2]
{w}{f(w)+ R(w+6x)}
{\label{eq:Ex1}}{}
\end{mini}


Alternative 2

\begin{mini}[3]
{w}{f(w)+ R(w+6x)}
{\label{eq:Ex1}}{}
\end{mini}


Alternative 3

\begin{mini}[4]
{w}{f(w)+ R(w+6x)}
{\label{eq:Ex1}}{}
\end{mini}


## Other optimization environments

argmini

\begin{argmini}
{w}{f(w)+ R(w+6x)}
{}{}
\end{argmini}


maxi

\begin{maxi}
{w}{f(w)+ R(w+6x)}
{}{}
\end{maxi}


argmaxi

\begin{argmaxi}
{w}{f(w)+ R(w+6x)}
{}{}
\end{argmaxi}


Full details on the different implementations above (and more details) can be found in the documentation: http://ctan.cs.uu.nl/macros/latex/contrib/optidef/optidef.pdf.

Feature requests and discussion of issues can be done through the github page: https://github.com/jeslago/optidef

• interesting package, are you sure it is suppose to be $4$ and not just [4] in your examples above? – daleif May 24 '17 at 9:15
• Yes indeed. I had copied the code directly from the package documentation, hence the mistake. Thanks a lot for pointing that out. – jesus May 25 '17 at 11:35

The align of alignat environments (from amsmath) seem the way to go here; in some cases you could use \intertext (or \shotintertex from the mathtools package) for interrupting some multiline displays while still maintaining the alignment points.

EDIT: As a side note, the set of equations in Example 2.2 (in the document you linked in a comment) that goes after "Then," was probably typeset using eqnarray; you should avoid using this environment since the spacing surrounding the equal sign is bigger than for the other aligning environments, rendering the document somehow inconsistent. This nice article from Lars Madsen explains (and illustrates with examples) why eqnarray shouln't be used anymore: Avoid eqnarray!

EDIT2: using an align environment you can write:

\documentclass{report}
\usepackage{amsmath}

\begin{document}

\begin{align}
\min  &\hspace{0.5em} n \\
\mathrm{s.t.}   &\hspace{0.5em} xxxxxxx \\
& \hspace{0.5em} yyyyyyyyyyyyyyyyyyy \\
& \hspace{0.5em} zzzzzzzzzzzzzzzzzzz
\end{align}

\end{document}

• when I use align, everything is aligned to the right. Is there a way to make everything centered? I couldn't find anything on google. – kloop May 30 '11 at 19:22
• @klooṕ: I've updated my answer adding an example given the desired alignment; I wasn't sure that this was what you desired; if it is not, please let me know, and explain in some more detail the kind of alignment that you want to achieve. – Gonzalo Medina May 30 '11 at 22:22
• FWIW, when used within a .Rmd file and knitting to .docx, this produces an error message suggesting that \hspace{} is a problem. Removing \hspace{} does the trick. – lowndrul May 7 at 16:03

This is another possibility, where I like the spacing:

\begin{aligned} &\min &&c^\top x + p^\top(b-Ax)\\ &\textrm{s.t.} &&x \geq 0 \label{eq:LagrangeRelax} \end{aligned}