## LaTeX forum ⇒ Text Formatting ⇒ Writing a code for optimization problem

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
omkardpd
Posts: 86
Joined: Sun Feb 24, 2008 7:23 am

### Writing a code for optimization problem

Hi,

I was wondering if there is a standard code format available in LaTex for writing an optimization problem such as

Maximize f(x)
Subject to
Constraint 1 = 0
Constraint 2 = 0
...
...

I see a number of documents which have these problems which specify 'x' under the word 'Maximize' in the objective function. I was unable to find how to arrange these things. May I get some help in this regard?

Omkar

Tags:

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am
Attach or link to an image showing what you would like to obtain.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.

omkardpd
Posts: 86
Joined: Sun Feb 24, 2008 7:23 am
Hi,

Thank you for the reply. I have attached a sample image. I am looking at the following two things specifically:
1. The decision variables written under "Max" and arrangement of the objective function next to "Max".
2. Alignment of the objective function and the constraints.

I tried "align" option, but did not get satisfactory results. I thought there might be a standard format available.

Regards,

Omkar
Attachments
image.JPG (22.46 KiB) Viewed 33082 times

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am
The following code may serve as a starting point for writing your own optimization problems:
\documentclass[a4paper,12pt]{article}\usepackage{amsmath}\DeclareMathOperator*{\Max}{Max} \begin{document}Let us consider the following maximization problem:\begin{align}    &\Max_{P_1,P_h,q_1,q_h,q_m}    \begin{aligned}[t]       &n_1\bigl(P_1-cq_1^2+\alpha cf(q_m)\big) \\       &\qquad+n_h \bigl(P_h-cq_h^2+\alpha cf(q_m)\big)    \end{aligned} \notag \\    &\text{subject to} \notag \\    & v_1 (q_1+\beta_pq_m)-P_1\geq v_1(q_h-\beta_dq_m)-P_h, \\    & v_h (q_h+\beta_pq_m)-P_h\geq v_h(q_1+\beta_dq_m)-P_1, \\    & v_1 (q_1+\beta_pq_m)\geq P_1, \\    & v_h (q_h-\beta_dq_m)\geq P_h, \\    & q_h\geq0,\quad q_1\geq0,\quad q_h\geq q_m,\quad q_1\geq q_m,       \quad q_m\geq0.\notag\end{align} \end{document}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.

jesus
Posts: 1
Joined: Sun Jan 22, 2017 6:31 pm
A bit late but it might help other people looking for the same answer. The package optidef (https://www.ctan.org/pkg/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 http://ctan.cs.uu.nl/macros/latex/contrib/optidef/optidef.pdf.

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://ctan.cs.uu.nl/macros/latex/contrib/optidef/optidef.pdf.