Debian Codegraph @

texmf-dist/doc/latex/optidef/optidef.tex

raw
1 \documentclass[a4paper]{article}
2 \usepackage{remreset}
3 \usepackage{optidef}
4 \usepackage{listings}
5 \usepackage{enumitem}
6 \usepackage[hidelinks]{hyperref}
7
8 \lstset{basicstyle=\ttfamily,breaklines=true}
9 % Title Page
10 \title{\textit{\textbf{Optidef}} \\ A Latex library for optimization problems\\ \textnormal{Version - 3.1}}
11
12 \author{Jesus Lago}
13
14 \makeatletter
15 \renewcommand \thesection {\@arabic\c@section}
16 \@removefromreset{section}{chapter}
17 \makeatother
18
19
20 \begin{document}
21 \maketitle
22
23 \newpage
24
25 \tableofcontents
26
27 \newpage
28
29 \section{Introduction and features}
30
31 This Latex library provides a standard set of environments for writing optimization problems. The most important features are:
32 \begin{enumerate}
33 \item It references optimization problem using three different policies: no equation is referenced, the problem is referenced with a single label, each equation has an individual reference. For more details refer to Sections \ref{sec:syntax} and \ref{sec:environments}.
34 \item It defines two problem size formats: a long format and a short format. For more details refer to Sections \ref{sec:syntax} and \ref{sec:longshort}.
35 \item It allows four different outputs for the location of the constraints. For more details refer to Sections \ref{sec:syntax} and \ref{sec:format}.
36 \item It allows the definition of a limitless number of constraints. For more details refer to Section \ref{subsec:syntax}.
37 \item Four different type of problems: \textit{minimize}, \textit{maximize}, \textit{arg min} and \textit{arg max}. For more details refer to Sections \ref{sec:syntax} and \ref{sec:environments}.
38 \item The optimization problem can be broken in several pages without compromising the alignment or the structure of the problem. For more details refer to Section \ref{sec:breakpages}.
39 \item The objective function can be broken in several lines without compromising the alignment or the structure of the problem. For more details refer to Section \ref{sec:breakObj}.
40 \end{enumerate}
41
42 \section{Using the package}
43 The package can be imported by directly adding
44 \begin{lstlisting}
45 \usepackage{optidef}
46 \end{lstlisting}
47 to the document preamble. When importing the packages three options can be used, \verb|short|, \verb|nocomma|, and either \verb|c1|, \verb|c2|, or \verb|c3|:
48
49 \begin{lstlisting}
50 \usepackage[short,c1|c2|c3,nocomma]{optidef}
51 \end{lstlisting}
52
53 The first option changes the default long format of the optimization problems to a shorter format; for a better explanation (including examples) of the \verb|short| option check Section \ref{sec:longshort}.
54
55 The options \verb|c1|, \verb|c2|, and \verb|c3| change the default format of the constraints; the default format is format 0 (as defined in Section \ref{sec:format}); \verb|c1|, \verb|c2|, and \verb|c3| respectively change the default constraint arrangement to format 1, 2, and 3. For a better explanation of the four formats including examples, we refer to Section \ref{sec:format}.
56
57 For the \verb|nocomma| option check Section \ref{sec:comma}. For a detailed description of how to use the package keep reading the next section.
58
59 \section{Environment Syntax Definition}
60 \label{sec:syntax}
61 Considering that \verb|Const.i| stands for constraint $i$, \verb|LHS.i| stands for the left-hand-side of constraint $i$, and \verb|RHS.i| for the right-hand-side counterpart, the basic structure to define a general optimization problem with $N$ constraints is:
62 \begin{verbatim}
63
64 \begin{mini#}|sizeFormat|[constraintFormat]<break>
65 {optimizationVariable}
66 {objectiveFunction\label{objective}}
67 {\label{optimizationProblem}}
68 {optimizationResult}
69
70 \addConstraint{LHS.1}{RHS.1\label{Const1}}{extraConst1}
71 \addConstraint{LHS.2}{RHS.2\label{Const2}}{extraConst2}
72 .
73 .
74 \addConstraint{LHS.N}{RHS.N\label{ConstN}}{extraConstN}
75 \end{mini#}
76 \end{verbatim}
77
78 \subsection{Definition of Problem parameters}
79
80 \begin{enumerate}[label=(\roman*)]
81 \item \verb|mini#|: defines the type of environment and reference used. There are four environments: \verb|mini|, \verb|maxi|, \verb|argmini|, and \verb|argmaxi|. There are three types of referencing: \verb|mini|, \verb|mini*| and \verb|mini!|. Consult Section \ref{sec:environments} for more details.
82 \item (Optional) \verb|sizeFormat|: optional parameter to define the size format of the problem. The possible values are:
83 \begin{itemize}
84 \item l: for the long format as defined in Section \ref{sec:longshort}.
85 \item s: for the short format as defined in Section \ref{sec:longshort}.
86 \end{itemize}
87 \item (Optional) \verb|constraintFormat|: optional parameter to change the format of the constraints. The parameter \verb|constraintFormat| can take the following values:
88 \begin{itemize}
89 \item 0: for the Standard definition in Section \ref{sec:format}.
90 \item 1: for Alternative 1 in Section \ref{sec:format}.
91 \item 2: for Alternative 2 in Section \ref{sec:format}
92 \item 3: for Alternative 3 in Section \ref{sec:format}
93 \end{itemize}
94 \item (Optional) \verb|break|: optional parameter to allow the optimization problem to break across multiple pages. For details on this feature, check Section \ref{sec:breakpages}.
95 \item \verb|optimizationVariable|: variable to be optimizated in the problem, e.g. $w \in \Re^N$.
96 \item \verb|objectiveFunction\label{objective}|: function to be minimized/maximized as a function of the optimization variable, e.g. $\|w\|_2$. If required, the objective function label should also be included withing this term
97 \item \verb|\label{optimizationProblem}|: it defines the main and general reference for the optimization problem. It is used for the \verb|mini| and \verb|mini!| enviroments. In the \verb|mini*| environment should be left blank, i.e. \{\}, \textbf{not to be ommited}.
98 \item \verb|optimizationResult|: a term expressing the result of the optimization problem, e.g. $J(w^*)~=$. If not needed leave it blank, \textbf{not to be ommited}.
99 \end{enumerate}
100
101 The last two defined problem parameters, \verb|\label{optimizationProblem}| and \verb|optimizationResult|, could be made optional. However, in order to improve the problem readibility, line breaking between the 7 parametes was implemented; unfortunately, linea breaking and optional parameters are not compatible and these two parameters had to be made mandatory.
102
103 \subsection{Adding Constraints}
104 \label{subsec:syntax}
105 After the definition of the problem parameters, the environment accepts the definition of an infinite number of constraints. For this definitions the following command is used:
106 ~\\
107
108 \verb|\addConstraint{LHS.k}{RHS.k\label{Const.k}}{extraConst.k}|
109 ~\\
110
111 The command accepts three different parameters
112 \begin{enumerate}
113 \item \verb|LHS.k|: the left-hand side of the the constraint $k$, e.g. $3w^\top w$.
114 \item (Optional) \verb|RHS.k\label{Const.k}|: the right-hand side of the constraint k if the equations should be aligned in the equality or inequality signs, e.g. $\leq \|w\|_\infty$. If required, the constraint label should also be included in this term.
115 \item (Optional) \verb|extraConst.k|: optional parameter to add extra alignment point for additional constraint information. An example would be the constraint names. Look Example \ref{ex:extra} or the Section \ref{sec:extraAlign}.
116 \end{enumerate}
117
118 \subsubsection{Constraints referencing}
119 Notice that the label for the constraints is always included in the right hand side expression and it only makes sense for the case of using the \verb|mini!| enviroment. The label of the objective function can also be included in a similar way.
120
121
122 \section{Environment Types}
123 \label{sec:environments}
124 There are four basic environments depending on the type of referencing that should be used.
125 \begin{enumerate}
126 \item The \textbf{mini} environment for defining problems with a single reference label:
127 \begin{mini}
128 {w}{f(w)+R(w+6x)}
129 {\label{eq:Ex1}}{}
130 \breakObjective{+L(x)}
131 \addConstraint{g(w)}{=0}
132 \end{mini}
133 \item The \textbf{mini*} environment if the problem does not have to be referenced:
134 \begin{mini*}
135 {w}{f(w)+ R(w+6x)}
136 {}{}
137 \addConstraint{g(w)}{=0}
138 \end{mini*}
139 \item The \textbf{mini!} environment if each equation should be referenced:
140 \begin{mini!}
141 {w}{f(w)+ R(w+6x)\label{eq:Ex2}}
142 {\label{eq:Ex1}}{}
143 \addConstraint{g(w)}{=0}
144 \end{mini!}
145 \item The \textbf{minie} environment: same functionality as the \textbf{mini!} environment and it replaces \textbf{mini!} when using the \texttt{optidef} library with some languages in the babel package. For further details we refer to Section \ref{sec:babel}.
146 %in older versions of the package it was the same as the current \textbf{minie} environment. \textbf{mini?} has been re-branded to \textbf{minie} for compatibility issues with some languages in the babel package. While its use is deprecated, it is kept in the package to ensure back-compatibility with old documents.
147 \end{enumerate}
148
149 \noindent Additionally, there are four basic definitions of optimization problems:
150
151 \begin{enumerate}
152 \item The \textbf{mini} environment:
153 \begin{mini}
154 {w}{f(w)+ R(w+6x)}
155 {}{}
156 \addConstraint{g(w)}{=0}
157 \end{mini}
158 \item The \textbf{maxi} environment:
159 \begin{maxi}
160 {w}{f(w)+ R(w+6x)}
161 {}{}
162 \addConstraint{g(w)}{=0}
163 \end{maxi}
164 \item The \textbf{argmini} environment:
165 \begin{argmini}
166 {w}{f(w)+ R(w+6x)}
167 {}{}
168 \addConstraint{g(w)}{=0}
169 \end{argmini}
170 \item The \textbf{argmaxi} environment:
171 \begin{argmaxi}
172 {w}{f(w)+ R(w+6x)}
173 {}{}
174 \addConstraint{g(w)}{=0}
175 \end{argmaxi}
176 \end{enumerate}
177
178 \section{Long and Short Output Formats}
179 \label{sec:longshort}
180 The library permits the definition of two different problem size: a long format and a short format.
181
182 \subsection{Long Format}
183 Selected by \verb|sizeFormat|=l. It makes use of \textit{subject to} and \textit{minimize/maximize}
184 \begin{mini*}|l|
185 {w}{f(w)+ R(w+6x)}{}{}
186 \addConstraint{g(w)}{=0}
187 \end{mini*}
188 \subsection{Short Format}
189 Selected by \verb|sizeFormat|=s. It uses instead the shorter \textit{s.t.} and \textit{min/max}
190 \begin{mini*}|s|
191 {w}{f(w)+ R(w+6x)}{}{}
192 \addConstraint{g(w)}{=0}
193 \end{mini*}
194
195 \noindent By the default the long format is used. To change the default to the short format the package must be imported with the \verb|short| option:
196
197 \begin{lstlisting}
198 \usepackage[short]{optidef}
199 \end{lstlisting}
200
201 %\section{Alignment of Equations}
202 %\label{sec:alignment}
203 %
204 %\begin{enumerate}
205 %\item Alignment at the beginning of the words \textit{minimize} and \textit{subject to}:
206 %\begin{mini}
207 %{w}{f(w)+ R(w+6x)}{}{}
208 %\addConstraint{g(w)}{=0}
209 %\end{mini}
210 %\item (Optional) Alignment at the $=,~>,~<$ signs of the constraints.
211 %\begin{mini*}[1]
212 %{w}{f(w)+ R(w+6x)}{}{}
213 %\addConstraint{g(w)+h(w)}{=0}
214 %\addConstraint{l(w)}{=5w.}
215 %\end{mini*}
216 %\item (Optional) Alignment of the longest constraint and the objective function:
217 %\begin{mini*}
218 % {w}{f(w)+ R(w+6x)}{}{}
219 % \addConstraint{g(w)+h(w)}{=0}
220 % \addConstraint{l(w)}{=5w.}
221 %\end{mini*}
222 %\item (Optional) Third alignment point on the constraints to set some constraint features. A clear example could be the constraints names:
223 %\begin{mini*}
224 %{w}{f(w)+ R(w+6x)}{}{}
225 %\addConstraint{g(w)+h(w)}{=0,}{\text{(Topological Constraint)}}
226 %\addConstraint{l(w)}{=5w,\quad}{\text{(Boundary Constraint)}}
227 %\end{mini*}
228 %or the index of the constraints:
229 %\begin{mini*}
230 %{w,u}{f(w)+ R(w+6x)}{}{}
231 %\addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1}
232 %\addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
233 %\end{mini*}
234 %\end{enumerate}
235
236 \section{Output Formats for the Constraints}
237 \label{sec:format}
238 There are four basic output formats for the location of the constraints. They are controlled by the environment parameter \verb|constraintFormat|.
239 \subsection{Alternative 0}
240 In this format option, the constraints are located to the right of \textit{subject to} and aligned with the objective function. It also has a second alignment point at the $=,~\leq,~\geq$ signs:
241 \begin{mini}
242 {w}{f(w)+ R(w+6x)}
243 {\label{eq:Ex1}}{}
244 \addConstraint{g(w)+h(w)}{=0}
245 \addConstraint{t(w)}{=0.}
246 \end{mini}
247
248 \noindent It is the default format if no format option is provided. Alternatively, it can also be set by selecting \verb|constraintFormat|=0.
249
250 \subsection{Alternative 1}
251 Selected by \verb|constraintFormat|=1. It locates the constraints below \textit{subject to} and keeps them aligned at the inequality/equality signs:
252 \begin{mini}[1]
253 {w}{f(w)+ R(w+6x)}
254 {\label{eq:Ex1}}{}
255 \addConstraint{g(w)+h(w)}{=0}
256 \addConstraint{t(w)}{=0.}
257 \end{mini}
258 \subsection{Alternative 2}
259 Selected by \verb|constraintFormat|=2. It aligns all the constraints with the objective function.
260 \begin{mini}[2]
261 {w}{f(w)+ R(w+6x)}
262 {\label{eq:Ex1}}{}
263 \addConstraint{g(w)+h(w)}{=0}
264 \addConstraint{t(w)}{=0.}
265 \end{mini}
266 \subsection{Alternative 3}
267 Selected by \verb|constraintFormat|=3. It aligns all the constraints below \textit{subject to}:
268 \begin{mini}[3]
269 {w}{f(w)+ R(w+6x)}
270 {\label{eq:Ex1}}{}
271 \addConstraint{g(w)+h(w)}{=0}
272 \addConstraint{t(w)}{=0.}
273 \end{mini}
274
275 \begin{lstlisting}
276 \usepackage[c1|c2|c3]{optidef}
277 \end{lstlisting}
278
279 \subsection{Extra alignment alternative}
280 \label{sec:extraAlign}
281 By default, the constraints have 2 aligned elements. However, a third alignment point can be used to set some constraint features. A clear example could be the constraints names:
282 \begin{mini*}
283 {w}{f(w)+ R(w+6x)}{}{}
284 \addConstraint{g(w)+h(w)}{=0,}{\text{(Topological Constraint)}}
285 \addConstraint{l(w)}{=5w,\quad}{\text{(Boundary Constraint)}}
286 \end{mini*}
287 or the index of the constraints:
288 \begin{mini*}
289 {w,u}{f(w)+ R(w+6x)}{}{}
290 \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1}
291 \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
292 \end{mini*}
293 This extra alignment point can be added using a third input parameter on the \verb|\addConstraint| parameter. An example using the last constraint of the previous example would be:
294 \begin{lstlisting}
295 \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
296 \end{lstlisting}
297
298
299 \subsection{Default format}
300 The default format is alternative 0. To change the default format across the whole document, the package can be imported using one of the three options: \verb|c1|, \verb|c2|, \verb|c3|, i.e.:
301
302 \section{Breaking the optimization problem across multiple pages}
303 \label{sec:breakpages}
304 In several cases, people encounter the problem of having an optimization problem that is too long to fit in a single page. In those cases, optidef can automatically break the problem across multiple pages by simply using the optional argument \verb|<b>|. For example:
305
306 \begin{lstlisting}
307 \begin{mini*}<b>
308 {w,u}{f(w)+ R(w+6x)+ H(100w-x*w/500)}{}{}
309 \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1}
310 \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
311 \end{mini*}
312 \end{lstlisting}
313
314
315 However, when using this option \verb|<b>|, it is important to note that labeling of equations is no longer automatic. To create the number/label, the command \verb|\labelOP{label}| should be used. In particular, in the equation/constraint of the optimization problem where the label/number should be located, simply add \verb|\labelOP{label}|. For example, the following code:
316
317 \begin{lstlisting}
318 \begin{mini}<b>
319 {w,u}{f(w)+ R(w+6x)+ H(100w-x*w/500)}{}{}
320 \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1 \labelOP{eq:label}}
321 \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
322 \end{mini}
323 \end{lstlisting}
324
325 \noindent would display this:
326
327 \begin{mini}<b>
328 {w,u}{f(w)+ R(w+6x)+ H(100w-x*w/500)}{}{}
329 \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1 \labelOP{eq:label}}
330 \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
331 \end{mini}
332
333 The option \verb|<b>| automatically breaks the optimization problem when the problem is too large to fit in one page (e.g.\ see an example in \ref{ex:break}). However, manual breaks at selected locations are also possible using the \verb|\displaybreak| command. Just add \verb|\displaybreak| between the two constraints that need to be broken, e.g.:
334
335 \begin{lstlisting}
336 \begin{mini}<b>
337 {w,u}{f(w)+ R(w+6x)+ H(100w-x*w/500)}{}{}
338 \breakObjective{-g(w^3-x^2*200+10000*w^5)}
339 \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1 \labelOP{eq:label}}
340 \displaybreak
341 \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
342 \end{mini}
343 \end{lstlisting}
344 \noindent would display:
345 \begin{mini}<b>
346 {w,u}{f(w)+ R(w+6x)+ H(100w-x*w/500)}{}{}
347 \breakObjective{-g(w^3-x^2*200+10000*w^5)}
348 \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1 \labelOP{eq:label}}
349 \displaybreak
350 \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
351 \end{mini}
352
353 \section{Breaking the objective across several lines}
354 \label{sec:breakObj}
355 In several cases, people encounter the problem of having an optimization problem which objective function is too long to be set in a single line. In such cases, a line breaking that respects the rest of the problem syntax would be desirable. To account for that, the command \verb|\breakObjective| can be used. The idea is that, if the objective function shall be split in $n$ different functions, e.g.~$f_1,\ldots,f_n$, the default objective parameter would include just $f_1$ and then, we would include $n-1$ statements \verb|\breakObjective|($f_k$), $\forall k=2,\ldots,n$ right before defining the \verb|\addConstraint| commands.
356
357 Let's illustrate this with an example. We could consider the example from before:
358
359 \begin{mini}
360 {w,u}{f(w)+ R(w+6x)}{}{}
361 \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1}
362 \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
363 \end{mini}
364 If now the cost function were too long, i.e:
365 \[
366 f(w)+ R(w+6x)+ H(100w-x*w/500)-g(w^3-x^2*200+10000*w^5)
367 \]
368 We could split it as:
369
370 \begin{mini}
371 {w,u}{f(w)+ R(w+6x)+ H(100w-x*w/500)}{}{}
372 \breakObjective{-g(w^3-x^2*200+10000*w^5)}
373 \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1}
374 \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
375 \end{mini}
376 by simpling using the following command:
377
378 \begin{lstlisting}
379 \begin{mini*}
380 {w,u}{f(w)+ R(w+6x)+ H(100w-x*w/500)}{}{}
381 \breakObjective{-g(w^3-x^2*200+10000*w^5)}
382 \addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1}
383 \addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
384 \end{mini*}
385 \end{lstlisting}
386
387 It is important to notice the specific location of the \verb|\breakObjective| command. In order to work properly, it has to be defined right before \verb|\addConstraint| and right after the definition of the environment parameters; i.e.~in any case the command should be used right after defining the first part of the objective function and not finishing the definition of the mandatory environment parameters.
388
389 \section{Default comma at the end of the constraint}
390 \label{sec:comma}
391 By default, the algorithms adds a comma at the end of any constraint that is not the last one. This feature was implemented due to correctness of mathematical notation. However, this behavior can be removed by adding the option \verb|nocomma| when importing the package:
392
393 \begin{lstlisting}
394 \usepackage[nocomma]{optidef}
395 \end{lstlisting}
396
397
398
399 \section{Long Optimization Variables}
400 The standard appearance for long optimization variables is as follows:
401
402 \begin{mini!}
403 {x_0,u_0,x_1,\hdots,u_{N-1},x_N}
404 {\sum_{k=0}^{N-1} L(x_k,u_k)\!\!+\!\!E(x_N)\label{OCPobj}}
405 {\label{eq:OCP}}{}
406 \addConstraint{x_{k+1}-f(x_k,u_k)}{= 0, \label{dOCP:modelc}\quad k=0,\dots,N-1}
407 \addConstraint{h(x_k,u_k)}{\leq 0, \quad k=0,\dots,N-1}
408 \addConstraint{r(x_0,x_N)}{= 0. \label{dOCP:boundary}}
409 \end{mini!}
410
411 \noindent A possible way to reduce the large variable spacing is to stack them with the command: \begin{verbatim}
412 \substack{x_0,u_0,x_1,\hdots,\\u_{N-1},x_N}
413 \end{verbatim}
414
415 \begin{mini!}
416 {\substack{x_0,u_0,x_1,\hdots,\\ u_{N-1},x_N}}
417 {\sum_{k=0}^{N-1} L(x_k,u_k)\!\!+\!\!E(x_N)\label{OCPobj}}
418 {\label{eq:OCP}}{}
419 \addConstraint{x_{k+1}-f(x_k,u_k)}{= 0, \label{dOCP:modelc}\quad k=0,\dots,N-1}
420 \addConstraint{h(x_k,u_k)}{\leq 0, \quad k=0,\dots,N-1}
421 \addConstraint{r(x_0,x_N)}{= 0. \label{dOCP:boundary}}
422 \end{mini!}
423
424 \section{Compatibility issues with other packages}
425 Issues with three different packages have been reported: cleveref, babel, and mathabx.
426 \subsection{Cleveref}
427 When using the cleveref package in couple with the optidef package two measures have to taken for the packages to work properly:
428
429 \begin{enumerate}
430 \item As also indicated in the cleveref documentation, the optidef package has to be loaded before the cleveref package.
431 \item To avoid crashes, the \verb|\label| commands in the optidef environments have to be replaced by the protected counterparts \verb|\protect\label|. This is required because of the standard Latex issue of moving arguments and fragile commands\footnote{\url{goo.gl/wmKbNU}}.
432 \end{enumerate}
433
434 \noindent A code example taking into account both measures is the following:
435
436 \begin{verbatim}
437 \documentclass{article}
438 \usepackage{optidef}
439 \usepackage{cleveref}
440
441 \begin{document}
442
443 \begin{mini!}
444 {w}{f(w)+ R(w+6x) \protect\label{eq:ObjectiveExample1}}
445 {\label{eq:Example1}}{}
446 \addConstraint{g(w)}{=0 \protect\label{eq:C1Example3}}
447 \addConstraint{n(w)}{= 6 \protect\label{eq:C2Example1}}
448 \addConstraint{L(w)+r(x)}{=Kw+p \protect\label{eq:C3Example1}}
449 \end{mini!}
450
451 Example labels: \cref{eq:Example1} and \cref{eq:ObjectiveExample1}.
452
453 \end{document}
454 \end{verbatim}
455
456 As an alternative to the second step, i.e.~protecting the \verb|\label| command, the command can be robustify in the document preamble and then \verb|\protect| is not longer needed. To robustify the \verb|\label| command, the following has to be added to the preamble:
457
458
459 \begin{verbatim}
460 \usepackage{etoolbox}
461 \robustify{\label}
462 \end{verbatim}
463
464 \subsection{Babel}
465 \label{sec:babel}
466 When importing the package babel with some specific languages, e.g.~French, the \verb|mini!| environment clashes because of the exclamation mark.
467
468 This issue has been resolved starting from Optidef 2.7, where a working alternative to the \verb|mini!| environment is included: the \verb|minie| enviroment. Both environemnts have the same functionality, but when using the babel package it is recommended to use the \verb|minie| environment to avoid issues.
469
470 \subsection{Mathabx}
471 When using the mathabx package in couple with the optidef package, the optidef package must be loaded first in order to avoid malfunction of the mathabx package. In addition, the amsmath package should also be loaded before both of them. The preamble should look like:
472
473 \begin{verbatim}
474 \usepackage{amsmath}
475 \usepackage{mathabx}
476 \usepackage{optidef}
477 \end{verbatim}
478
479
480 \section{Examples}
481 \subsection{Example 1 - mini environment}
482 The code:
483
484 \begin{verbatim}
485 \begin{mini}
486 {w}{f(w)+ R(w+6x)}
487 {\label{eq:Example1}}{}
488
489 \addConstraint{g(w)}{=0}
490 \addConstraint{n(w)}{= 6}
491 \addConstraint{L(w)+r(x)}{=Kw+p}
492 \addConstraint{h(x)}{=0.}
493 \end{mini}
494 \end{verbatim}
495
496 \noindent outputs:
497
498 \begin{mini}
499 {w}{f(w)+ R(w+6x)}
500 {\label{eq:Ex11}}{}
501 \addConstraint{g(w)}{=0}
502 \addConstraint{n(w)}{= 6}
503 \addConstraint{L(w)+r(x)}{=Kw+p}
504 \addConstraint{h(x)}{=0.}
505 \end{mini}
506
507 \subsection{Example 2 - mini* environment}
508 On the other hand:
509
510 \begin{verbatim}
511 \begin{mini*}
512 {w}{f(w)+ R(w+6x)}
513 {}{}
514
515 \addConstraint{g(w)}{=0}
516 \addConstraint{n(w)}{= 6,}
517 \addConstraint{L(w)+r(x)}{=Kw+p}
518 \addConstraint{h(x)}{=0.}
519 \end{mini*}
520 \end{verbatim}
521
522 \noindent it is almost the same but removing the reference:
523
524 \begin{mini*}
525 {w}{f(w)+ R(w+6x)}
526 {}{}
527 \addConstraint{g(w)}{=0}
528 \addConstraint{n(w)}{= 6}
529 \addConstraint{L(w)+r(x)}{=Kw+p}
530 \addConstraint{h(x)}{=0.}
531 \end{mini*}
532
533 \subsection{Example 3 - mini! environment}
534
535 \noindent Finally, the multireferencing environment outputs:
536
537 \begin{verbatim}
538 \begin{mini!}
539 {w}{f(w)+ R(w+6x) \label{eq:ObjectiveExample1}}
540 {\label{eq:Example1}}{}
541
542 \addConstraint{g(w)}{=0 \label{eq:C1Example3}}
543 \addConstraint{n(w)}{= 6 \label{eq:C2Example1}}
544 \addConstraint{L(w)+r(x)}{=Kw+p \label{eq:C3Example1}}
545 \addConstraint{h(x)}{=0. \label{eq:C4Example1}}
546 \end{mini!}
547 \end{verbatim}
548
549 \begin{mini!}
550 {w}{f(w)+ R(w+6x)\label{eq:ObjectiveExample3}}
551 {\label{eq:Example3}}
552 {}
553 \addConstraint{g(w)}{=0 \label{eq:C1Example3}}
554 \addConstraint{n(w)}{= 6 \label{eq:C2Example3}}
555 \addConstraint{L(w)+r(x)}{=Kw+p \label{eq:C3Example3}}
556 \addConstraint{h(x)}{=0.\label{eq:C4Example3}}
557 \end{mini!}
558
559 \subsection{Example 4 - Problem Result}
560
561 \noindent Adding the problem result:
562
563 \begin{verbatim}
564 \begin{mini}
565 {w}{f(w)+ R(w+6x)}
566 {\label{eq:Example1}}
567 {J(w^*)=}
568
569 \addConstraint{g(w)}{=0}
570 \addConstraint{n(w)}{= 6}
571 \addConstraint{L(w)+r(x)}{=Kw+p}
572 \addConstraint{h(x)}{=0.}
573 \end{mini}
574 \end{verbatim}
575
576 \noindent outputs:
577
578 \begin{mini}
579 {w}{f(w)+ R(w+6x)}
580 {\label{eq:Ex1}}{J(w^*)~=~}
581 \addConstraint{g(w)}{=0}
582 \addConstraint{n(w)}{= 6}
583 \addConstraint{L(w)+r(x)}{=Kw+p}
584 \addConstraint{h(x)}{=0.}
585 \end{mini}
586
587 \subsection{Example 5 - Short Format}
588
589 \noindent Adding the short format parameter:
590
591 \begin{verbatim}
592 \begin{mini}|s|
593 {w}{f(w)+ R(w+6x)}
594 {\label{eq:Example1}}
595 {}
596
597 \addConstraint{g(w)}{=0}
598 \addConstraint{n(w)}{= 6}
599 \addConstraint{L(w)+r(x)}{=Kw+p}
600 \addConstraint{h(x)}{=0.}
601 \end{mini}
602 \end{verbatim}
603
604 \noindent outputs:
605
606 \begin{mini}|s|
607 {w}{f(w)+ R(w+6x)}
608 {\label{eq:Ex1}}{}
609 \addConstraint{g(w)}{=0}
610 \addConstraint{n(w)}{= 6}
611 \addConstraint{L(w)+r(x)}{=Kw+p}
612 \addConstraint{h(x)}{=0.}
613 \end{mini}
614
615 \subsection{Example 6 - Alternative 1 for Constraints}
616
617 \noindent If including a 1 as optional parameter, the first constraint will appear aligned to the left right below \textit{subject to}.
618
619 \begin{verbatim}
620 \begin{mini}[1]
621 {w}{f(w)+ R(w+6x)}
622 {\label{eq:Example1}}
623 {}
624
625 \addConstraint{g(w)}{=0}
626 \addConstraint{n(w)}{= 6}
627 \addConstraint{L(w)+r(x)}{=Kw+p}
628 \addConstraint{h(x)}{=0.}
629 \end{mini}
630 \end{verbatim}
631
632 \noindent outputs:
633
634 \begin{mini}[1]
635 {w}{f(w)+ R(w+6x)}
636 {\label{eq:Ex1}}{}
637 \addConstraint{g(w)}{=0}
638 \addConstraint{n(w)}{= 6}
639 \addConstraint{L(w)+r(x)}{=Kw+p}
640 \addConstraint{h(x)}{=0.}
641 \end{mini}
642
643 \subsection{Example 7 - Alternative 2 for Constraints}
644
645 \noindent If including a 2 as optional parameter, the constraint will appear to the right of \textit{subject to} but a single alignment point.
646
647 \begin{verbatim}
648 \begin{mini}[2]
649 {w}{f(w)+ R(w+6x)}
650 {\label{eq:Example1}}
651 {}
652
653 \addConstraint{g(w)}{=0}
654 \addConstraint{n(w)}{= 6}
655 \addConstraint{L(w)+r(x)}{=Kw+p}
656 \addConstraint{h(x)}{=0.}
657 \end{mini}
658 \end{verbatim}
659
660 \noindent outputs:
661
662 \begin{mini}[2]
663 {w}{f(w)+ R(w+6x)}
664 {\label{eq:Ex1}}{}
665 \addConstraint{g(w)}{=0}
666 \addConstraint{n(w)}{= 6}
667 \addConstraint{L(w)+r(x)}{=Kw+p}
668 \addConstraint{h(x)}{=0.}
669 \end{mini}
670
671 \subsection{Example 8 - Alternative 3 for Constraints}
672
673 \noindent If including a 3 as optional parameter, the first constraint will appear aligned to the left right below \textit{subject to} and with a single alignment point.
674
675 \begin{verbatim}
676 \begin{mini}[3]
677 {w}{f(w)+ R(w+6x)}
678 {\label{eq:Example1}}
679 {}
680
681 \addConstraint{g(w)}{=0}
682 \addConstraint{n(w)}{= 6}
683 \addConstraint{L(w)+r(x)}{=Kw+p}
684 \addConstraint{h(x)}{=0.}
685 \end{mini}
686 \end{verbatim}
687
688 \noindent outputs:
689
690 \begin{mini}[3]
691 {w}{f(w)+ R(w+6x)}
692 {\label{eq:Ex1}}{}
693 \addConstraint{g(w)}{=0}
694 \addConstraint{n(w)}{= 6}
695 \addConstraint{L(w)+r(x)}{=Kw+p}
696 \addConstraint{h(x)}{=0.}
697 \end{mini}
698
699
700
701 \subsection{Example 9 - Breaking a long objective}
702 \begin{lstlisting}
703 \begin{mini*}
704 {w,u}{f(w)+ R(w+6x)+ H(100w-x*w/500)}{}{}
705 \breakObjective{-g(w^3-x^2*200+10000*w^5)}
706 \addConstraint{g(w_k)+h(w_k)}{=0,}
707 \addConstraint{l(w_k)}{=5u,\quad}
708 \end{mini*}
709 \end{lstlisting}
710 outputs:
711 \begin{mini}
712 {w,u}{f(w)+ R(w+6x)+ H(100w-x*w/500)}{}{}
713 \breakObjective{-g(w^3-x^2*200+10000*w^5)}
714 \addConstraint{g(w_k)+h(w_k)}{=0}
715 \addConstraint{l(w_k)}{=5u.}
716 \end{mini}
717
718
719 \subsection{Example 10 - Extra Alignment in the Constraints}
720 \label{ex:extra}
721 Adding optional alignment to add constraint names:
722
723 \begin{verbatim}
724 \begin{mini*}
725 {w}{f(w)+ R(w+6x)}
726 {}{}
727 \addConstraint{g(w)}{=0,}{ \quad \text{(Dynamic constraint)}}
728 \addConstraint{n(w)}{= 6,}{ \quad \text{(Boundary constraint)}}
729 \addConstraint{L(w)+r(x)}{=Kw+p,}{ \quad \text{(Random constraint)}}
730 \addConstraint{h(x)}{=0,}{ \quad \text{(Path constraint).}}
731 \end{mini*}
732 \end{verbatim}
733
734 \subsection{Example 11 - The \textit{argmini} Environment}
735 Similar to the \verb|mini|, \verb|mini*| and \verb|mini!| environments, the environments \verb|argmini|, \verb|argmini*| and \verb|argmini!| are very similar environments that use the same syntax but the output is slightly different:
736
737 \begin{verbatim}
738 \begin{argmini}
739 {w}{f(w)+ R(w+6x)}
740
741 {\label{eq:Example1}}{w^*=}
742
743 \addConstraint{g(w)}{=0}
744 \addConstraint{n(w)}{= 6}
745 \addConstraint{L(w)+r(x)}{=Kw+p}
746 \addConstraint{h(x)}{=0.}
747 \end{argmini}
748 \end{verbatim}
749
750 \noindent outputs:
751
752 \begin{argmini}
753 {w}{f(w)+ R(w+6x)}
754 {\label{eq:Ex1}}{w^*~=~}
755 \addConstraint{g(w)}{=0}
756 \addConstraint{n(w)}{= 6}
757 \addConstraint{L(w)+r(x)}{=Kw+p}
758 \addConstraint{h(x)}{=0.}
759 \end{argmini}
760
761 \subsection{Example 12 - The \textit{maxi} and \textit{argmaxi} Environments}
762 Exactly the same syntax and definition as the previous environments, but now for defining maximization environments. The following code serves for illustration:
763
764 \begin{verbatim}
765 \begin{maxi}
766 {w}{f(w)+ R(w+6x)}
767 {g(w)}{=0}
768
769 {\label{eq:Example1}}{}
770
771 \addConstraint{g(w)}{=0}
772 \addConstraint{n(w)}{= 6}
773 \addConstraint{L(w)+r(x)}{=Kw+p}
774 \addConstraint{h(x)}{=0.}
775 \end{maxi}
776 \end{verbatim}
777
778 \noindent outputs:
779
780 \begin{maxi}
781 {w}{f(w)+ R(w+6x)}
782 {\label{eq:Example1}}{}
783 \addConstraint{g(w)}{=0}
784 \addConstraint{n(w)}{= 6}
785 \addConstraint{L(w)+r(x)}{=Kw+p}
786 \addConstraint{h(x)}{=0.}
787 \end{maxi}
788
789 \subsection{Example 13 - Breaking optimization problem}
790 \label{ex:break}
791 \begin{lstlisting}
792 \begin{mini}<b>
793 {w}{f(w)+ R(w+6x)}
794 {\label{eq:Example1}}{}
795 \addConstraint{g(w)}{=0}
796 \addConstraint{p(w)}{=0}
797 \addConstraint{q(w)}{=0}
798 \addConstraint{r(w)}{=0\labelOP{testLabel}}
799 \addConstraint{n(w)}{= 6}
800 \addConstraint{L(w)+r(x)}{=Kw+p}
801 \addConstraint{h(x)}{=0.}
802 \end{mini}
803
804 \end{lstlisting}
805 outputs:
806 \begin{mini}<b>
807 {w}{f(w)+ R(w+6x)}
808 {\label{eq:Example1}}{}
809 \addConstraint{g(w)}{=0}
810 \addConstraint{p(w)}{=0}
811 \addConstraint{q(w)}{=0}
812 \addConstraint{r(w)}{=0\labelOP{testLabel}}
813 %\addConstraint{s(w)}{=0}
814 %\addConstraint{t(w)}{=0}
815 %\addConstraint{v(w)}{=0}
816 %\addConstraint{z(w)}{=0}
817 \addConstraint{n(w)}{= 6}
818 \addConstraint{L(w)+r(x)}{=Kw+p}
819 \addConstraint{h(x)}{=0.}
820 \end{mini}
821
822
823 \subsection{Example 14 - All Possible Parameters}
824
825 \begin{verbatim}
826 \begin{mini!}|s|[2]<b>
827 {w}{f(w)+ R(w+6x)\label{eq:ObjectiveExample3}}
828 {\label{eq:Example3}}
829 {w^*=}
830 \addConstraint{g(w)}{=0 \label{eq:C1Example3}}
831 \addConstraint{n(w)}{= 6 \label{eq:C2Example3}}
832 \addConstraint{L(w)+r(x)}{=Kw+p \label{eq:C3Example3}}
833 \addConstraint{h(x)}{=0.\label{eq:C4Example3}}
834 \end{mini!}
835 \end{verbatim}
836
837 \begin{mini!}|s|[2]<b>
838 {w}{f(w)+ R(w+6x)\label{eq:ObjectiveExample3}}
839 {\label{eq:Example3}}
840 {w^*=}
841 \addConstraint{g(w)}{=0 \label{eq:C1Example3}}
842 \addConstraint{n(w)}{= 6 \label{eq:C2Example3}}
843 \addConstraint{L(w)+r(x)}{=Kw+p \label{eq:C3Example3}}
844 \addConstraint{h(x)}{=0.\label{eq:C4Example3}}
845 \end{mini!}
846
847 \section{Reporting bugs and feature requests}
848 To report any bug or request some feature please use the issue section in the github repository: \url{https://github.com/jeslago/optidef/issues}.
849
850 %\section{Code definition}
851 %\begin{lstlisting}
852 %\end{lstlisting}
853
854 \end{document}