summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/optidef/optidef.tex
blob: a5147d46c1a370ce65f2a71e16fd6c3848eea44e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
\documentclass[]{report}
\usepackage{remreset}
\usepackage{optidef}

% Title Page
\title{\textit{\textbf{Optidef}} \\ A Latex library for minimization/maximization problems definition}

\author{Jesus Lago Garcia}

\makeatletter
\renewcommand \thesection {\@arabic\c@section}
\@removefromreset{section}{chapter}
\makeatother


\begin{document}
\maketitle

\section{Introduction and features}

This small library provides a standard set of environments for writing minimization problems. The most important features are:
\begin{enumerate}
	\item It automatically aligns the problems in three points with an optional fourth:
		\begin{enumerate}
			\item Beginning of the words "minimize/argmin" and "subject to"
			\item The objective function and the longest left hand side of the constraints
			\item The $= | > | <$ signs of the constraints.
			\item Optionally, the user can add manually a double align character \&\& to align some common constraints feature. A clear example could be the constraints names, e.g. (boundary constraint) alignment with (dynamic constraint), or the index of the constraints, e.g. in the case of having something like $h(x_k,u_k)\leq 0,\quad k=0,\ldots,N$, align the indexes $k=0,\ldots,N$ across constraint lines.
		\end{enumerate}

\item It provides an easy interface to define optimization problem for three different reference situations:
\begin{enumerate}
	\item Where no equation is referenced/numbered.
	\item Where the problem is referenced with a single number.
	\item Where each equation has an individual reference.
\end{enumerate} 

\item It also allows a definition of any optimization problem without a limitless number of constraints.

\end{enumerate}

\section{Environment types}
There are three basic environments depending on the type of referencing that should be used.
\begin{enumerate}
	\item The \textbf{mini} environment for defining problems with a single reference label:
		\begin{mini}{1}
			{w}{f(w)+ R(w+6x) + \nabla_w R}
			{g(w)}{=0}
			{\label{eq:Ex1}}{}
		\end{mini}
	\item The \textbf{mini*} environment if the problem does not have to be referenced:
			\begin{mini*}{1}
				{w}{f(w)+ R(w+6x) + \nabla_w R}
				{g(w)}{=0}
				{}{}
			\end{mini*}
	\item  The \textbf{mini!} environment if each equation should be referenced:
			\begin{mini!}{1}
				{w}{f(w)+ R(w+6x) + \nabla_w R \label{eq:Ex2}}
				{g(w)}{=0}
				{\label{eq:Ex1}}{}
			\end{mini!}		
\end{enumerate}

\section{Environment Syntaxes}
The three environments use the same syntax with the same number of parameters. In particular, considering that LHS stands for Left-hand-side and RHS for Right-hand-side, the basic structure to define a general optimization problem is:
\begin{verbatim}

\begin{mini#}{Number of constraints}
   {Optimization variable}
   {Objective function \label{Objective function referece}}
   {LHS Constraint 1}{RHS Constraint 1 \label{Reference Constraint 1}} 
   {\label{Global referece of Optimization Problem}}  
   {Result of the optimization problem or any expression on 
   the left of the minimize word}

   \addConstraint{LHS Constraint 2}{RHS Constraint 2 \label{Reference Constraint 2}}
   \addConstraint{LHS Constraint 3}{RHS Constraint 3 \label{Reference Constraint 3}}
   .
   .
   \finalConstraint{LHS N}  {RHSConstraint N \label{Reference Constraint N}}
\end{mini#}
\end{verbatim}

\noindent where \# $\in \{*, !, ~ \}$.
\subsection{Parameters definition}
The number of parameters that the environment uses is 7:

\begin{enumerate}
	\item \verb|Number of constraints|, e.g. 0,1,2,3,4...
	\item \verb|Optimization variable|, e.g. $w \in \Re^N$.
	\item \verb|Objective function|, e.g. $\|w\|_2$.
	\item \verb|LHS Constraint 1| (optional): the left-hand side of the first constraint, e.g. $3w^\top w$. For unconstrained problems leave it blank, i.e. \{\}
	\item \verb|RHS Constraint 1| (optional): the right-hand side of the first constraints including the equal, less and greater signs, e.g. $\leq \|w\|_\infty$. For unconstrained problems leave it blank, i.e. \{\}
	\item \verb|\label{Global referece of Optimization Problem}|: 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 leave it blank, i.e. \{\}.   
	\item \verb|Result of the optimization problem|: optionally a term expressing the result of the optimization problem, e.g. $J(w^*)~=$.
\end{enumerate}

\noindent Notice that only the first three will be really necessary in every definition, nevertheless and for the sake of having homogeneous definitions, we opted for requiring the 7 parameters in every definition and expecting empty parameters definitions, i.e. \{\}, when they are not needed.

After the definition of this parameters, the environment accepts the definition of an infinite number of constraints. For this definitions the command:

\verb|\addConstraint{LHS Constraint k}|

\verb|{RHS Constraint k \label{Reference Constraint k}}| 

\noindent has to be used, where the left hand side and right hand side syntax is the same as used for the mandatory third and fourth environment parameters.

Notice that the last constraint has to be defined using the \verb|\finalConstraint| command, instead of the \verb|\addConstraint|.

\subsubsection{Constraints referencing}
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.

\subsubsection{Example 1 - mini environment}
The code:

\begin{verbatim}
\begin{mini}{4}
   {w}{f(w)+ R(w+6x) + \nabla_w R}
   {g(w)}{=0}
   
   {\label{eq:Example1}}
   {}
   
   \addConstraint{n(w)}{= 6}
   \addConstraint{L(w)+r(x)}{=Kw+p}
   \finalConstraint{h(x)}{=0.}
\end{mini}
\end{verbatim}

\noindent outputs:

\begin{mini}{4}
	{w}{f(w)+ R(w+6x) + \nabla_w R}
	{g(w)}{=0}
	{\label{eq:Ex1}}{}
	\addConstraint{n(w)}{= 6}
	\addConstraint{L(w)+r(x)}{=Kw+p}
	\finalConstraint{h(x)}{=0.}
\end{mini}

\subsubsection{Example 2 - mini* environment}

On the other hand:

\begin{verbatim}
\begin{mini*}{4}
   {w}{f(w)+ R(w+6x) + \nabla_w R}
   {g(w)}{=0,}
   
   {}{}
   
   \addConstraint{n(w)}{= 6,}
   \addConstraint{L(w)+r(x)}{=Kw+p}
   \finalConstraint{h(x)}{=0.}  
\end{mini*}
\end{verbatim}

\noindent it is almost the same but removing the reference:

\begin{mini*}{3}
	{w}{f(w)+ R(w+6x) + \nabla_w R}
	{g(w)}{=0,}
	{}{}
	\addConstraint{n(w)}{= 6, }
	\addConstraint{L(w)+r(x)}{=Kw+p, }
	\finalConstraint{h(x)}{=0.}
\end{mini*}

\subsubsection{Example 3 - mini! environment}

\noindent Finally, the multireferencing environment outputs:

\begin{verbatim}
\begin{mini!}{4}
   {w}{f(w)+ R(w+6x) + \nabla_w R \label{eq:ObjectiveExample1}}
   {g(w)}{=0 \label{eq:C1Example1}}
   
   {\label{eq:Example1}}
   {}

   \addConstraint{n(w)}{= 6 \label{eq:C2Example1}}
   \addConstraint{L(w)+r(x)}{=Kw+p \label{eq:C3Example1}}
   \finalConstraint{h(x)}{=0. \label{eq:C4Example1}}
\end{mini!}
\end{verbatim}

\begin{mini!}{4}
	{w}{f(w)+ R(w+6x) + \nabla_w R\label{eq:ObjectiveExample3}}
	{g(w)}{=0 \label{eq:C1Example3}}
	{\label{eq:Example3}}
	{}
	\addConstraint{n(w)}{= 6 \label{eq:C2Example3}}
	\addConstraint{L(w)+r(x)}{=Kw+p \label{eq:C3Example3}}
	\finalConstraint{h(x)}{=0. \label{eq:C4Example3}}
\end{mini!}

\subsubsection{Example 4 - mini + problem result}

\noindent Adding the problem result:

\begin{verbatim}
\begin{mini}{4}
{w}{f(w)+ R(w+6x) + \nabla_w R}
{g(w)}{=0}

{\label{eq:Example1}}
{J(w^*)=}

\addConstraint{n(w)}{= 6}
\addConstraint{L(w)+r(x)}{=Kw+p}
\finalConstraint{h(x)}{=0.}
\end{mini}
\end{verbatim}

\noindent outputs:

\begin{mini}{4}
	{w}{f(w)+ R(w+6x) + \nabla_w R}
	{g(w)}{=0}
	{\label{eq:Ex1}}{J(w^*)~=~}
	\addConstraint{n(w)}{= 6}
	\addConstraint{L(w)+r(x)}{=Kw+p}
	\finalConstraint{h(x)}{=0.}
\end{mini}

\subsubsection{Example 5 - mini* + extra constraint alignment}
Adding the fourth optional alignment to add constraint names:

\begin{verbatim}
\begin{mini*}{4}
{w}{f(w)+ R(w+6x) + \nabla_w R}
{g(w)}{=0, \quad && \text{(Dynamic constraint)}}

{}{}

\addConstraint{n(w)}{= 6, \quad && \text{(Boundary constraint)}}
\addConstraint{L(w)+r(x)}{=Kw+p, \quad && \text{(Random constraint)}}
\finalConstraint{h(x)}{=0.\quad && \text{(Path constraint)}}  
\end{mini*}
\end{verbatim}


\begin{mini*}{3}
	{w}{f(w)+ R(w+6x) + \nabla_w R}
	{g(w)}{=0, \quad && \text{(Dynamic constraint)}}
	{}{}
	\addConstraint{n(w)}{= 6, \quad && \text{(Boundary constraint)}}
	\addConstraint{L(w)+r(x)}{=Kw+p, \quad && \text{(Random constraint)}}
	\finalConstraint{h(x)}{=0. \quad && \text{(Path constraint).}}
\end{mini*}


\subsubsection{Example 6 - mini environment on the unconstrained case}

\begin{verbatim}

\begin{mini}{0}
   {w}{f(w)+ R(w+6x) + \nabla_w R}  
   {}{}
   {}{}
   {\label{eq:Ex4}}
   {}   
\end{mini}
\end{verbatim}

\noindent outputs:

\begin{mini}{0}
	{w}{f(w)+ R(w+6x) + \nabla_w R}
	{}{}
	{\label{eq:Ex4}}{}
\end{mini}

\section{The \textit{argmini} environment}
 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. The following code serves for illustration:
 
\begin{verbatim}
\begin{argmini}{4}
   {w}{f(w)+ R(w+6x) + \nabla_w R}
   {g(w)}{=0}
   
   {\label{eq:Example1}}{w^*=}
   
   \addConstraint{n(w)}{= 6}
   \addConstraint{L(w)+r(x)}{=Kw+p}
   \finalConstraint{h(x)}{=0.}
\end{argmini}
\end{verbatim}

\noindent outputs:

\begin{argmini}{3}
	{w}{f(w)+ R(w+6x) + \nabla_w R}
	{g(w)}{=0}
	{\label{eq:Ex1}}{w^*~=~}
	\addConstraint{n(w)}{= 6}
	\addConstraint{L(w)+r(x)}{=Kw+p}
	\finalConstraint{h(x)}{=0.}
\end{argmini}

\section{The \textit{maxi} and \textit{argmaxi} environments}
Exactly the same syntax and definition as the previous environments, but now for defining maximization environments. The following code serves for illustration:

\begin{verbatim}
\begin{maxi}{4}
   {w}{f(w)+ R(w+6x) + \nabla_w R}
   {g(w)}{=0}

   {\label{eq:Example1}}{}

   \addConstraint{n(w)}{= 6}
   \addConstraint{L(w)+r(x)}{=Kw+p}
   \finalConstraint{h(x)}{=0.}
\end{maxi}
\end{verbatim}

\noindent outputs:

\begin{maxi}{4}
	{w}{f(w)+ R(w+6x) + \nabla_w R}
	{g(w)}{=0}
	{\label{eq:Example1}}{}
	\addConstraint{n(w)}{= 6}
	\addConstraint{L(w)+r(x)}{=Kw+p}
	\finalConstraint{h(x)}{=0.}
\end{maxi}


\section{Problems and limitations}
One current limitation is that the size of the optimization variables must be smaller than the word minimize, otherwise, the output is not properly aligned. Example:

\begin{mini!}{3}
	{x_0,u_0,x_1,\hdots,u_{N-1},x_N}
	{\sum_{k=0}^{N-1} L(x_k,u_k)\!\!+\!\!E(x_N)\label{OCPobj}}
	{x_{k+1}-f(x_k,u_k)}{=  0, \label{dOCP:modelc}\quad k=0,\dots,N-1}
	{\label{eq:OCP}}{}
	\addConstraint{h(x_k,u_k)}{\leq 0,  \quad k=0,\dots,N-1}
	\finalConstraint{r(x_0,x_N)}{= 0.  \label{dOCP:boundary}}
\end{mini!}

\noindent A possible way to avoid this is to stack them with the command: \begin{verbatim}
\substack{x_0,u_0,x_1,\hdots,\\u_{N-1},x_N}
\end{verbatim}

\begin{mini!}{3}
	{\substack{x_0,u_0,x_1,\hdots,\\ u_{N-1},x_N}}
	{\sum_{k=0}^{N-1} L(x_k,u_k)\!\!+\!\!E(x_N)\label{OCPobj}}
	{x_{k+1}-f(x_k,u_k)}{=  0, \label{dOCP:modelc}\quad k=0,\dots,N-1}
	{\label{eq:OCP}}{}
	\addConstraint{h(x_k,u_k)}{\leq 0,  \quad k=0,\dots,N-1}
	\finalConstraint{r(x_0,x_N)}{= 0.  \label{dOCP:boundary}}
\end{mini!}

\noindent If you want to increase the size of the optimization variables:
\noindent A possible way to avoid this is to stack them with the command: \begin{verbatim}
\substack{\displaystyle x_0,u_0,x_1,\hdots,\\ \displaystyle u_{N-1},x_N}
\end{verbatim}

\begin{mini!}{3}
	{\substack{\displaystyle x_0,u_0,x_1,\hdots,\\ \displaystyle u_{N-1},x_N}}
	{\sum_{k=0}^{N-1} L(x_k,u_k)\!\!+\!\!E(x_N)\label{OCPobj}}
	{x_{k+1}-f(x_k,u_k)}{=  0, \label{dOCP:modelc}\quad k=0,\dots,N-1}
	{\label{eq:OCP}}{}
	\addConstraint{h(x_k,u_k)}{\leq 0,  \quad k=0,\dots,N-1}
	\finalConstraint{r(x_0,x_N)}{= 0.  \label{dOCP:boundary}}
\end{mini!}

The other option is to define the set of variables before the problem definition as a single variable, i.e.  $w = [x_0,u_0,x_1,\hdots,u_{N-1},x_N]$ and:

%\begin{mini3!}
%	{w}
%	{\sum_{k=0}^{N-1} L(x_k,u_k)\!\!+\!\!E(x_N)}{\label{OCPobj}}
%	{x_{k+1}-f(x_k,u_k)}{=  0, \label{dOCP:modelc}\quad k=0,\dots,N-1}
%	{h(x_k,u_k)} {\leq 0,  \quad k=0,\dots,N-1}
%	{r(x_0,x_N)}{= 0.  \label{dOCP:boundary}}
%	\label{eq:OCP}
%\end{mini3!}

\section{Code definition}
\begin{verbatim}
% optidef - Version 08/06/2016
%
%Copyright 2016 J. Lago Garcia
%
%This work may be distributed and/or modified under the conditions of the LaTeX Project Public License, either version 1.3 of this license or (at your option) any later version.
%The latest version of this license is in http://www.latex-project.org/lppl.txt and version 1.3 or later is part of all distributions of LaTeX version 2005/12/01 or later.
%
%This work has the LPPL maintenance status 'maintained'. The Current Maintainer of this work is J. Lago Garcia, under the supervision of Prof. Dr. Moritz Diehl and Prof. Dr. Sebastien Gross.
%
%E-mail: jesus.lago.garcia@venus.uni-freiburg.de
%
%This work consists of the file optidef.sty.
  
  \NeedsTeXFormat{LaTeX2e}
  \ProvidesPackage{optidef}[2016/06/06 Package for defining optimization problems]
  
  \RequirePackage{environ}
  \RequirePackage{ifthen}	
  \RequirePackage{mathtools}	
  \RequirePackage{xifthen}	
  
  % Input minimization evironments
  
  % Macros for objective definition, constraint definition and extra constraint definition
  \newcommand{\bodyobj}[4]
  {
  \ifthenelse{\isempty{#4}}
  {
  &\underset{\displaystyle #1}{\mbox{#3}} \quad #2\span\span\span\span
  }
  {
  #4 ~~ &\underset{\displaystyle #1}{\mbox{#3}} \quad #2\span\span\span\span
  }
  }
  
  
  \newcommand{\bodyconst}[2]
  {&\mbox{subject to} \quad &#1 & #2\span\span}
  
  \newcommand{\addConstraint}[2]{&\quad &#1 & #2, \\}
  \newcommand{\finalConstraint}[2]{&\quad &#1 & #2}
  
  
  %\newcommand(\breakObjectiveUnconstraint)[1]{\\%
  %\bodyobjUn{}{#1}{\phantom{minimize}}{}}
  \newcommand{\breakObjectiveOneConstraint}[1]{&&&#1\\}
  %\newcommand(\breakObjective)[1]{\bodyobj{}{#4}{\phantom{minimize}}{}\nonumber\\}	}
  
  \newcommand{\bodyconstOne}[2]
  {&\text{subject to} \quad &&#1 #2}
  
  % This body of the objective is only valid for 
  % one constraint problem since there is no need to
  % use mathllap to achieve second alignment level
  \newcommand{\bodyobjOne}[4]
  {
  \ifthenelse{\isempty{#4}}
  {
  &\underset{\displaystyle #1}{\text{#3}} \quad &&#2
  }
  {
  #4 ~~ &\underset{\displaystyle #1}{\text{#3}} \quad &&#2}
  }
  
  
  % This body of the objective is only valid for 
  % no constraint problem since there is no need to
  % align
  \newcommand{\bodyobjUn}[4]
  {	
  \ifthenelse{\isempty{#4}}
  {
  }
  {
  #4~
  }
  \underset{\displaystyle #1}{\text{#3}} \quad #2
  }
  
  %MINIMIZATION ENVIRONMENTS
  
  \NewEnviron{mini}[7]{%
  \ifthenelse{\equal{#1}{0}}{%
  \begin{equation}
  #6
  \bodyobjUn{#2}{#3}{minimize}{#7}
  \end{equation}
  }%
  {%
  \ifthenelse{\equal{#1}{1}}{%
  \begin{equation}
  #6
  \begin{alignedat}{3}
  \bodyobjOne{#2}{#3}{minimize}{#7}\\
  \bodyconstOne{#4}{#5}
  \end{alignedat}
  \end{equation}			
  }%
  {%
  \begin{equation}
  #6
  \begin{alignedat}{4}
  \bodyobj{#2}{#3}{minimize}{#7}\\		
  \bodyconst{#4}{#5,} \\
  \BODY
  \end{alignedat}
  \end{equation}				
  }%
  }%
  }
  
  \NewEnviron{argmini}[7]{%
  \ifthenelse{\equal{#1}{0}}{%
  \begin{equation}
  #6
  \bodyobjUn{#2}{#3}{arg min}{#7}
  \end{equation}
  }%
  {%
  \ifthenelse{\equal{#1}{1}}{%
  \begin{equation}
  #6
  \begin{alignedat}{3}
  \bodyobjOne{#2}{#3}{arg min}{#7}\\
  \bodyconstOne{#4}{#5}
  \end{alignedat}
  \end{equation}			
  }%
  {%
  \begin{equation}
  #6
  \begin{alignedat}{4}
  \bodyobj{#2}{#3}{arg min}{#7}\\		
  \bodyconst{#4}{#5,} \\
  \BODY
  \end{alignedat}
  \end{equation}				
  }%
  }%
  }
  
  \NewEnviron{mini*}[7]{%
  \ifthenelse{\equal{#1}{0}}{%
  \begin{equation*}
  \bodyobjUn{#2}{#3}{minimize}{#7}
  \end{equation*}
  }%
  {%
  \ifthenelse{\equal{#1}{1}}{%
  \begin{alignat*}{3}
  \bodyobjOne{#2}{#3}{minimize}{#7}\\
  \bodyconstOne{#4}{#5}
  \end{alignat*}
  }%
  {%
  \begin{alignat*}{5}
  \bodyobj{#2}{#3}{minimize}{#7}\\		
  \bodyconst{#4}{#5,} \\
  \BODY
  \end{alignat*}
  }%
  }%
  }
  
  \NewEnviron{argmini*}[7]{%
  \ifthenelse{\equal{#1}{0}}{%
  \begin{equation*}
  \bodyobjUn{#2}{#3}{arg min}{#7}
  \end{equation*}
  }%
  {%
  \ifthenelse{\equal{#1}{1}}{%
  \begin{alignat*}{3}
  \bodyobjOne{#2}{#3}{arg min}{#7}\\
  \bodyconstOne{#4}{#5}
  \end{alignat*}
  }%
  {%
  \begin{alignat*}{4}
  \bodyobj{#2}{#3}{arg min}{#7}\\		
  \bodyconst{#4}{#5,} \\
  \BODY
  \end{alignat*}
  }%
  }%
  }
  
  
  \NewEnviron{mini!}[7]{%
  \ifthenelse{\equal{#1}{1}}{%
  \begin{subequations}
  #6
  \begin{alignat}{3}
  \bodyobjOne{#2}{#3}{minimize}{#7}\\
  \bodyconstOne{#4}{#5}
  \end{alignat}
  \end{subequations}		
  }%
  {%
  \begin{subequations}
  #6
  \begin{alignat}{4}
  \bodyobj{#2}{#3}{minimize}{#7}\\		
  \bodyconst{#4}{#5,} \\
  \BODY
  \end{alignat}
  \end{subequations}			
  }%
  }
  
  \NewEnviron{argmini!}[7]{%
  \ifthenelse{\equal{#1}{1}}{%
  \begin{subequations}
  #6
  \begin{alignat}{3}
  \bodyobjOne{#2}{#3}{arg min}{#7}\\
  \bodyconstOne{#4}{#5}
  \end{alignat}
  \end{subequations}		
  }%
  {%
  \begin{subequations}
  #6
  \begin{alignat}{4}
  \bodyobj{#2}{#3}{arg min}{#7}\\		
  \bodyconst{#4}{#5,} \\
  \BODY
  \end{alignat}
  \end{subequations}			
  }%
  }
  
  %MAXIMIZATION ENVIRONMENTS
  
  \NewEnviron{maxi}[7]{%
  \ifthenelse{\equal{#1}{0}}{%
  \begin{equation}
  #6
  \bodyobjUn{#2}{#3}{maximize}{#7}
  \end{equation}
  }%
  {%
  \ifthenelse{\equal{#1}{1}}{%
  \begin{equation}
  #6
  \begin{alignedat}{3}
  \bodyobjOne{#2}{#3}{maximize}{#7}\\
  \bodyconstOne{#4}{#5}
  \end{alignedat}
  \end{equation}			
  }%
  {%
  \begin{equation}
  #6
  \begin{alignedat}{4}
  \bodyobj{#2}{#3}{maximize}{#7}\\		
  \bodyconst{#4}{#5,} \\
  \BODY
  \end{alignedat}
  \end{equation}				
  }%
  }%
  }
  
  \NewEnviron{argmaxi}[7]{%
  \ifthenelse{\equal{#1}{0}}{%
  \begin{equation}
  #6
  \bodyobjUn{#2}{#3}{arg max}{#7}
  \end{equation}
  }%
  {%
  \ifthenelse{\equal{#1}{1}}{%
  \begin{equation}
  #6
  \begin{alignedat}{3}
  \bodyobjOne{#2}{#3}{arg max}{#7}\\
  \bodyconstOne{#4}{#5}
  \end{alignedat}
  \end{equation}			
  }%
  {%
  \begin{equation}
  #6
  \begin{alignedat}{4}
  \bodyobj{#2}{#3}{arg max}{#7}\\		
  \bodyconst{#4}{#5,} \\
  \BODY
  \end{alignedat}
  \end{equation}				
  }%
  }%
  }
  
  \NewEnviron{maxi*}[7]{%
  \ifthenelse{\equal{#1}{0}}{%
  \begin{equation*}
  \bodyobjUn{#2}{#3}{maximize}{#7}
  \end{equation*}
  }%
  {%
  \ifthenelse{\equal{#1}{1}}{%
  \begin{alignat*}{3}
  \bodyobjOne{#2}{#3}{maximize}{#7}\\
  \bodyconstOne{#4}{#5}
  \end{alignat*}
  }%
  {%
  \begin{alignat*}{5}
  \bodyobj{#2}{#3}{maximize}{#7}\\		
  \bodyconst{#4}{#5,} \\
  \BODY
  \end{alignat*}
  }%
  }%
  }
  
  \NewEnviron{argmaxi*}[7]{%
  \ifthenelse{\equal{#1}{0}}{%
  \begin{equation*}
  \bodyobjUn{#2}{#3}{arg max}{#7}
  \end{equation*}
  }%
  {%
  \ifthenelse{\equal{#1}{1}}{%
  \begin{alignat*}{3}
  \bodyobjOne{#2}{#3}{arg max}{#7}\\
  \bodyconstOne{#4}{#5}
  \end{alignat*}
  }%
  {%
  \begin{alignat*}{4}
  \bodyobj{#2}{#3}{arg max}{#7}\\		
  \bodyconst{#4}{#5,} \\
  \BODY
  \end{alignat*}
  }%
  }%
  }
  
  
  \NewEnviron{maxi!}[7]{%
  \ifthenelse{\equal{#1}{1}}{%
  \begin{subequations}
  #6
  \begin{alignat}{3}
  \bodyobjOne{#2}{#3}{maximize}{#7}\\
  \bodyconstOne{#4}{#5}
  \end{alignat}
  \end{subequations}		
  }%
  {%
  \begin{subequations}
  #6
  \begin{alignat}{4}
  \bodyobj{#2}{#3}{maximize}{#7}\\		
  \bodyconst{#4}{#5,} \\
  \BODY
  \end{alignat}
  \end{subequations}			
  }%
  }
  
  \NewEnviron{argmaxi!}[7]{%
  \ifthenelse{\equal{#1}{1}}{%
  \begin{subequations}
  #6
  \begin{alignat}{3}
  \bodyobjOne{#2}{#3}{arg max}{#7}\\
  \bodyconstOne{#4}{#5}
  \end{alignat}
  \end{subequations}		
  }%
  {%
  \begin{subequations}
  #6
  \begin{alignat}{4}
  \bodyobj{#2}{#3}{arg max}{#7}\\		
  \bodyconst{#4}{#5,} \\
  \BODY
  \end{alignat}
  \end{subequations}			
  }%
  }
\end{verbatim}

\end{document}