Text FormattingWriting a code for optimization problem

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

Writing a code for optimization problem

Post by omkardpd »

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?

Thank you in advance.

Omkar

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

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

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Re: Writing a code for optimization problem

Post by Juanjo »

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

Re: Writing a code for optimization problem

Post by omkardpd »

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
image.JPG (22.46 KiB) Viewed 70091 times
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Writing a code for optimization problem

Post by Juanjo »

The following code may serve as a starting point for writing your own optimization problems:

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}
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

Writing a code for optimization problem

Post by jesus »

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.

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}
Image

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}
Image



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}
Image




Multilabeling/no labeling:

Code: Select all

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

Code: Select all

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



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}
Image

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}
Image

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}
Image



Other optimization environments

argmini

Code: Select all

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

maxi

Code: Select all

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

argmaxi

Code: Select all

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


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.
Post Reply