LaTeX forum ⇒ Text FormattingWriting a code for optimization problem

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

Writing a code for optimization problem

Postby omkardpd » Fri Nov 06, 2009 8:38 am

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

Tags:

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

Postby Juanjo » Sun Nov 08, 2009 3:05 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: 82
Joined: Sun Feb 24, 2008 7:23 am

Postby omkardpd » Sun Nov 08, 2009 6:32 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
image.JPG (22.46 KiB) Viewed 30523 times

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

Postby Juanjo » Sun Nov 08, 2009 4:36 pm

The following code may serve as a starting point for writing your own optimization problems:
  1. \documentclass[a4paper,12pt]{article}
  2. \usepackage{amsmath}
  3. \DeclareMathOperator*{\Max}{Max}
  4.  
  5. \begin{document}
  6. Let us consider the following maximization problem:
  7. \begin{align}
  8. &\Max_{P_1,P_h,q_1,q_h,q_m}
  9. \begin{aligned}[t]
  10. &n_1\bigl(P_1-cq_1^2+\alpha cf(q_m)\big) \\
  11. &\qquad+n_h \bigl(P_h-cq_h^2+\alpha cf(q_m)\big)
  12. \end{aligned} \notag \\
  13. &\text{subject to} \notag \\
  14. & v_1 (q_1+\beta_pq_m)-P_1\geq v_1(q_h-\beta_dq_m)-P_h, \\
  15. & v_h (q_h+\beta_pq_m)-P_h\geq v_h(q_1+\beta_dq_m)-P_1, \\
  16. & v_1 (q_1+\beta_pq_m)\geq P_1, \\
  17. & v_h (q_h-\beta_dq_m)\geq P_h, \\
  18. & q_h\geq0,\quad q_1\geq0,\quad q_h\geq q_m,\quad q_1\geq q_m,
  19. \quad q_m\geq0.\notag
  20. \end{align}
  21.  
  22. \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

Postby jesus » Sun Jan 22, 2017 6:47 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.

  1. \usepackage{optidef}
  2. \begin{mini}|l|
  3. {w,u}{f(w)+ R(w+6x)}{}{}
  4. \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1}
  5. \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
  6. \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/contrib/optidef/optidef.pdf.


Using a shorter description of minimize/subject to.

  1. \usepackage[short]{optidef}
  2. \begin{mini}
  3. {w,u}{f(w)+ R(w+6x)}{}{}
  4. \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1}
  5. \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
  6. \end{mini}


Image



Breaking the objective accross several lines.

  1. \usepackage[long]{optidef}
  2. \begin{mini}
  3. {w,u}{f(w)+ R(w+6x)+ H(100w-x*w/500)}{}{}
  4. \breakObjective{-g(w^3-x^2*200+10000*w^5)}
  5. \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1}
  6. \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
  7. \end{mini}


Image




Multilabeling/no labeling:

  1. \begin{mini*}
  2. {w}{f(w)+ R(w+6x)}
  3. {}{}
  4. \addConstraint{g(w)}{=0}
  5. \end{mini*}


Image

  1. \begin{mini!}
  2. {w}{f(w)+ R(w+6x)}
  3. {}{}
  4. \addConstraint{g(w)}{=0}
  5. \end{mini!}


Image



Alignment position of constraints with respect to the objective:

Alternative 1

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


Image

Alternative 2

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


Image

Alternative 3

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


Image



Other optimization environments

argmini

  1. \begin{argmini}
  2. {w}{f(w)+ R(w+6x)}
  3. {}{}
  4. \addConstraint{g(w)}{=0}
  5. \end{argmini}


Image

maxi

  1. \begin{maxi}
  2. {w}{f(w)+ R(w+6x)}
  3. {}{}
  4. \addConstraint{g(w)}{=0}
  5. \end{maxi}


Image

argmaxi

  1. \begin{argmaxi}
  2. {w}{f(w)+ R(w+6x)}
  3. {}{}
  4. \addConstraint{g(w)}{=0}
  5. \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/contrib/optidef/optidef.pdf.


Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 2 guests