18

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 :-)

4
  • 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? May 30, 2011 at 17:57
  • Example 2.2 in people.ucsc.edu/~rgil/Optimization.pdf
    – kloop
    May 30, 2011 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, 2011 at 18:13
  • perhaps you could edit your question to provide the link there and not just in a comment? May 30, 2011 at 19:00

3 Answers 3

30

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)}{}{}
  \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1}
  \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
 \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)}{}{}
  \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1}
  \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
\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)}
  \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1}
  \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
\end{mini}


Multilabeling/no labeling:

\begin{mini*}
    {w}{f(w)+ R(w+6x)}
    {}{}
    \addConstraint{g(w)}{=0}
\end{mini*}

\begin{mini!}
    {w}{f(w)+ R(w+6x)}
    {}{}
    \addConstraint{g(w)}{=0}
\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}}{}
  \addConstraint{g(w)+h(w)}{=0}
  \addConstraint{t(w)}{=0.}
\end{mini}

Alternative 2

\begin{mini}[3]
    {w}{f(w)+ R(w+6x)}
    {\label{eq:Ex1}}{}
    \addConstraint{g(w)+h(w)}{=0}
    \addConstraint{t(w)}{=0.}
\end{mini}

Alternative 3

\begin{mini}[4]
    {w}{f(w)+ R(w+6x)}
    {\label{eq:Ex1}}{}
    \addConstraint{g(w)+h(w)}{=0}
    \addConstraint{t(w)}{=0.}
\end{mini}


Other optimization environments

argmini

\begin{argmini}
    {w}{f(w)+ R(w+6x)}
    {}{}
    \addConstraint{g(w)}{=0}
\end{argmini}

maxi

\begin{maxi}
    {w}{f(w)+ R(w+6x)}
    {}{}
    \addConstraint{g(w)}{=0}
\end{maxi}

argmaxi

\begin{argmaxi}
    {w}{f(w)+ R(w+6x)}
    {}{}
    \addConstraint{g(w)}{=0}
\end{argmaxi}


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

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

7
  • interesting package, are you sure it is suppose to be \[4\] and not just [4] in your examples above?
    – daleif
    May 24, 2017 at 9:15
  • 1
    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, 2017 at 11:35
  • I am really wondering why has nobody come across the problem with too long constraints. I have carefully read the manual but I think there is no solution for the case when the constraints are too long. While there is an option for breaking objective, it is not possible with constraints, or is it?
    – G.T.
    May 17, 2020 at 8:09
  • For example, I am trying to write this probme but constraint goes beyond the margin to the right. \begin{maxi!}|l|[0] {P_{i,t}^*, B_{t}(i)^{uc}, W(i)}{ E_t \sum_{k=0}^{\infty} \beta^t \bigg\{ \psi_t ln(C_t(i)^{uc} - hC_{t-1}^{uc})-\theta_t \psi \frac{l_t(i)^{1+\zeta}}{1+\zeta}\bigg\} } {\label{calvo1}} {}{} \addConstraint {(1+\tau^c)P_t^c C_t(i)^{uc}+B_{t+1}^{uc}(i)+\int_s q_{t+1}(i,s) a_{t+1}(i,s)=}{(1-\tau^w)W_t(i)l_t(i)+R_{t-1}B_t(i)^{uc}+a_t(i)+T_t^{uc}+(1-\tau^p)D_t(i)\label{const343}} \end{maxi!}
    – G.T.
    May 17, 2020 at 8:09
  • 1
    @G.T. The constraint can be split in multiple lines to avoid going beyond the margin. See github.com/jeslago/optidef/issues/26
    – jesus
    May 18, 2020 at 9:28
4

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}
3
  • 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, 2011 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. May 30, 2011 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, 2019 at 16:03
0

This is another possibility, where I like the spacing:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .