Text Formatting ⇒ Writing a code for optimization problem
Writing a code for optimization problem
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?
Thank you in advance.
Omkar
NEW: TikZ book now 40% off at Amazon.com for a short time.
And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p
Re: Writing a code for optimization problem
Re: Writing a code for optimization problem
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 70093 times
Writing a code for optimization problem
Code: Select all
\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}
Writing a code for optimization problem
Code: Select all
\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/contr ... tidef.pdf.
Using a shorter description of minimize/subject to.
Code: Select all
\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.
Code: Select all
\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:
Code: Select all
\begin{mini*}
{w}{f(w)+ R(w+6x)}
{}{}
\addConstraint{g(w)}{=0}
\end{mini*}

Code: Select all
\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
Code: Select all
\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
Code: Select all
\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
Code: Select all
\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
Code: Select all
\begin{argmini}
{w}{f(w)+ R(w+6x)}
{}{}
\addConstraint{g(w)}{=0}
\end{argmini}

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

argmaxi
Code: Select all
\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/contr ... tidef.pdf.