Apps
~~----~~

!!! Maximum Selector (~~MAX~~) ~~Operator~~

* %list list-page%[[~~Attach~~:~~max~~.~~apm | MAX Operator Example]]~~

~~----~~

!!! Absolute Value (~~ABS~~) ~~Operator~~

* %list list-page%[[~~Attach~~:~~abs~~.~~apm | ABS Operator Example]]~~

# solve

m.solve()

~~Model signum~~
~~Model signum~~

%width=50px%Attach:apm.png [[Main/Objects|APMonitor Objects]]
~~</pre></font>~~(:~~htmlend~~:)

(:~~html~~:)~~<font size=2><pre>~~
~~</pre></font>~~(:~~htmlend~~:)

~~</pre></font>~~(:~~htmlend~~:)

(:~~html~~:)~~<font size=2><pre>~~
~~</pre></font>~~(:~~htmlend~~:)

~~</pre></font>~~(:~~htmlend~~:)

(:~~html~~:)~~<font size=2><pre>~~
~~</pre></font>~~(:~~htmlend~~:)

~~</pre></font>~~(:~~htmlend~~:)

(:~~html~~:)~~<font size=2><pre>~~
~~</pre></font>~~(:~~htmlend~~:)

'''GEKKO ABS function (abs2)'''

See [[https://gekko.readthedocs.io/en/latest/examples.html|GEKKO Documentation]] for additional examples.

(:source lang=python:)

from gekko import GEKKO

m = GEKKO()

x = m.Param(-2)

# use abs2 to define a new variable

y = m.abs2(x)

# use abs2 in an equation

z = m.Var()

m.Equation(z==m.abs2(x)+1)

# solve

m.solve()

print('x: ' + str(x.value))

print('y: ' + str(y.value))

print('z: ' + str(z.value))

(:sourceend:)
~~*~~ Mojica, J.L., Petersen, D.J., Hansen, B., Powell, K.M., Hedengren, J.D., Optimal Combined Long-Term Facility Design and Short-Term Operational Strategy for CHP Capacity Investments, Energy, Vol 118, 1 January 2017, pp. 97–115. [[https://www.sciencedirect.com/science/article/pii/S036054421631814X|Article]]

(:html:)<font size=2><pre>

Objects

f = max

End Objects

Connections

f.x[1] = x1

f.x[2] = x2

f.y = y

End Connections

Parameters

x1 = -2

x2 = 4

End Parameters

Variables

y

End Variables

</pre></font>(:htmlend:)

(:html:)<font size=2><pre>

Objects

f = min

End Objects

Connections

f.x[1] = x1

f.x[2] = x2

f.y = y

End Connections

Parameters

x1 = -2

x2 = -1

End Parameters

Variables

y

End Variables

</pre></font>(:htmlend:)

(:html:)<font size=2><pre>

! SIGN function with Object

Objects

f = sign

End Objects

Connections

f.x = x

f.y = y

End Connections

Parameters

x = -2

End Parameters

Variables

y

End Variables

</pre></font>(:htmlend:)

~~Model abs~~

Parameters

~~x = -2~~

~~End Parameters~~

Variables

~~y~~

s_a >= ~~0~~

s_b ~~>= 0~~

~~End Variables~~

Equations

~~ ! test abs operator, y = abs(x)~~

x = s_b - s_a

y = s_a + s_b

minimize s_a*s_b

End Equations

End Model

(:html:)<font size=2><pre>

! MPEC formulation for ABS function

! y = ABS(x) returns a value y, where:

! y = x if the corresponding element of X is greater than zero

! y = -x if the corresponding element of X is less than zero

! this uses the APMonitor object 'abs'

Objects

f = abs

Connections

f.x = x

f.y = y

Parameters

x = -2

Variables

y

</pre></font>(:htmlend:)

(:html:)<font size=1><pre>

! MPEC formulation for ABS function

! y = ABS(x) returns a value y, where:

! y = x if the corresponding element of X is greater than zero

! y = -x if the corresponding element of X is less than zero

Model signum

Parameters

x = -2

End Parameters

Variables

y

s_a >= 0

s_b >= 0

End Variables

Equations

! test abs operator, y = abs(x)

x = s_b - s_a

y = s_a + s_b

minimize s_a*s_b

End Equations

End Model

</pre></font>(:htmlend:)

----

!!! Minimum Selector (MIN) Operator

* %list list-page% [[Attach:min.apm | MIN Operator Example]]
~~* %list list~~-~~page% [[Attach:sign.apm | SIGN ~~Operator]]
~~----~~

</pre></font>(:htmlend:)

## Mpec Examples

## Apps.MpecExamples History

Show minor edits - Show changes to output

Changed lines 201-206 from:

!!! Maximum Selector

* %list list-page%

to:

'''GEKKO MIN function (min2)'''

See [[https://gekko.readthedocs.io/en/latest/examples.html|GEKKO Documentation]] for additional examples.

See [[https://gekko.readthedocs.io/en/latest/examples.html|GEKKO Documentation]] for additional examples.

Added lines 206-224:

from gekko import GEKKO

m = GEKKO()

x1 = m.Param(-2)

x2 = m.Param(-1)

# use min2

y = m.min2(x1,x2)

m.solve() # solve

print('x1: ' + str(x1.value))

print('x2: ' + str(x2.value))

print('y: ' + str(y.value))

(:sourceend:)

----

!!! Maximum Selector (MAX) Operator

* %list list-page% [[Attach:max.apm | MAX Operator Example]]

(:source lang=python:)

m = GEKKO()

x1 = m.Param(-2)

x2 = m.Param(-1)

# use min2

y = m.min2(x1,x2)

m.solve() # solve

print('x1: ' + str(x1.value))

print('x2: ' + str(x2.value))

print('y: ' + str(y.value))

(:sourceend:)

----

!!! Maximum Selector (MAX) Operator

* %list list-page% [[Attach:max.apm | MAX Operator Example]]

(:source lang=python:)

Added lines 273-289:

(:sourceend:)

'''GEKKO MAX function (min2)'''

See [[https://gekko.readthedocs.io/en/latest/examples.html|GEKKO Documentation]] for additional examples.

(:source lang=python:)

from gekko import GEKKO

m = GEKKO()

x1 = m.Param(-2)

x2 = m.Param(-1)

# use max2

y = m.max2(x1,x2)

m.solve() # solve

print('x1: ' + str(x1.value))

print('x2: ' + str(x2.value))

print('y: ' + str(y.value))

'''GEKKO MAX function (min2)'''

See [[https://gekko.readthedocs.io/en/latest/examples.html|GEKKO Documentation]] for additional examples.

(:source lang=python:)

from gekko import GEKKO

m = GEKKO()

x1 = m.Param(-2)

x2 = m.Param(-1)

# use max2

y = m.max2(x1,x2)

m.solve() # solve

print('x1: ' + str(x1.value))

print('x2: ' + str(x2.value))

print('y: ' + str(y.value))

Changed lines 59-64 from:

!!! Absolute Value

* %list list-page%

to:

'''GEKKO SIGNUM function (sign2)'''

See [[https://gekko.readthedocs.io/en/latest/examples.html|GEKKO Documentation]] for additional examples.

See [[https://gekko.readthedocs.io/en/latest/examples.html|GEKKO Documentation]] for additional examples.

Added lines 64-80:

from gekko import GEKKO

m = GEKKO()

x = m.Param(-2)

# use sign2 to define a new variable

y = m.sign2(x)

m.solve() # solve

print('x: ' + str(x.value))

print('y: ' + str(y.value))

(:sourceend:)

----

!!! Absolute Value (ABS) Operator

* %list list-page% [[Attach:abs.apm | ABS Operator Example]]

(:source lang=python:)

m = GEKKO()

x = m.Param(-2)

# use sign2 to define a new variable

y = m.sign2(x)

m.solve() # solve

print('x: ' + str(x.value))

print('y: ' + str(y.value))

(:sourceend:)

----

!!! Absolute Value (ABS) Operator

* %list list-page% [[Attach:abs.apm | ABS Operator Example]]

(:source lang=python:)

Deleted line 130:

Changed lines 138-141 from:

# solve

m.solve()

to:

m.solve() # solve

Changed line 144 from:

to:

Model

Changed line 201 from:

to:

Model

Added lines 2-3:

%width=50px%Attach:apm.png [[Main/Objects|APMonitor Objects]]

Changed line 11 from:

(:~~html~~:)~~<font size=2><pre>~~

to:

(:source lang=python:)

Changed lines 35-38 from:

to:

(:sourceend:)

(:source lang=python:)

(:source lang=python:)

Changed lines 55-56 from:

to:

(:sourceend:)

Changed line 63 from:

(:~~html~~:)~~<font size=2><pre>~~

to:

(:source lang=python:)

Changed lines 86-88 from:

(:

to:

(:sourceend:)

(:source lang=python:)

(:source lang=python:)

Changed lines 106-107 from:

to:

(:sourceend:)

Changed line 137 from:

(:~~html~~:)~~<font size=2><pre>~~

to:

(:source lang=python:)

Changed lines 164-166 from:

(:

to:

(:sourceend:)

(:source lang=python:)

(:source lang=python:)

Changed lines 186-187 from:

to:

(:sourceend:)

Changed line 194 from:

(:~~html~~:)~~<font size=2><pre>~~

to:

(:source lang=python:)

Changed lines 221-223 from:

(:

to:

(:sourceend:)

(:source lang=python:)

(:source lang=python:)

Changed line 243 from:

to:

(:sourceend:)

Added lines 108-130:

'''GEKKO ABS function (abs2)'''

See [[https://gekko.readthedocs.io/en/latest/examples.html|GEKKO Documentation]] for additional examples.

(:source lang=python:)

from gekko import GEKKO

m = GEKKO()

x = m.Param(-2)

# use abs2 to define a new variable

y = m.abs2(x)

# use abs2 in an equation

z = m.Var()

m.Equation(z==m.abs2(x)+1)

# solve

m.solve()

print('x: ' + str(x.value))

print('y: ' + str(y.value))

print('z: ' + str(z.value))

(:sourceend:)

Changed lines 225-227 from:

to:

!!!! Reference

Mojica, J.L., Petersen, D.J., Hansen, B., Powell, K.M., Hedengren, J.D., Optimal Combined Long-Term Facility Design and Short-Term Operational Strategy for CHP Capacity Investments, Energy, Vol 118, 1 January 2017, pp. 97–115. [[https://www.sciencedirect.com/science/article/pii/S036054421631814X|Article]]

Mojica, J.L., Petersen, D.J., Hansen, B., Powell, K.M., Hedengren, J.D., Optimal Combined Long-Term Facility Design and Short-Term Operational Strategy for CHP Capacity Investments, Energy, Vol 118, 1 January 2017, pp. 97–115. [[https://www.sciencedirect.com/science/article/pii/S036054421631814X|Article]]

Changed lines 223-225 from:

----

to:

----

* Mojica, J.L., Petersen, D.J., Hansen, B., Powell, K.M., Hedengren, J.D., Optimal Combined Long-Term Facility Design and Short-Term Operational Strategy for CHP Capacity Investments, Energy, Vol 118, 1 January 2017, pp. 97–115. [[https://www.sciencedirect.com/science/article/pii/S036054421631814X|Article]]

* Mojica, J.L., Petersen, D.J., Hansen, B., Powell, K.M., Hedengren, J.D., Optimal Combined Long-Term Facility Design and Short-Term Operational Strategy for CHP Capacity Investments, Energy, Vol 118, 1 January 2017, pp. 97–115. [[https://www.sciencedirect.com/science/article/pii/S036054421631814X|Article]]

Changed line 39 from:

! SIGN function as an Object

to:

! SIGN function MPEC as an Object

Changed line 90 from:

! ABS function as an Object

to:

! ABS function MPEC as an Object

Added line 145:

! MIN function MPEC as an Object

Added line 202:

! MAX function MPEC as an Object

Changed line 39 from:

! SIGN function ~~with~~ Object

to:

! SIGN function as an Object

Changed line 90 from:

! ~~MPEC formulation for ABS function using an object~~

to:

! ABS function as an Object

Added lines 199-220:

(:html:)<font size=2><pre>

Objects

f = max

End Objects

Connections

f.x[1] = x1

f.x[2] = x2

f.y = y

End Connections

Parameters

x1 = -2

x2 = 4

End Parameters

Variables

y

End Variables

</pre></font>(:htmlend:)

Added lines 143-164:

(:html:)<font size=2><pre>

Objects

f = min

End Objects

Connections

f.x[1] = x1

f.x[2] = x2

f.y = y

End Connections

Parameters

x1 = -2

x2 = -1

End Parameters

Variables

y

End Variables

</pre></font>(:htmlend:)

Added lines 36-57:

(:html:)<font size=2><pre>

! SIGN function with Object

Objects

f = sign

End Objects

Connections

f.x = x

f.y = y

End Connections

Parameters

x = -2

End Parameters

Variables

y

End Variables

</pre></font>(:htmlend:)

Changed lines 45-46 from:

! y = x if the corresponding element of X is ~~greater~~ than zero

! y = -x if the corresponding element of X is~~less~~ than zero

! y = -x if the corresponding element of X is

to:

! y = x if the corresponding element of X is > than zero

! y = -x if the corresponding element of X is < than zero

! y = -x if the corresponding element of X is < than zero

Changed lines 68-72 from:

! MPEC formulation for ABS function

~~! y = ABS(x) returns a value y, where:~~

! y = x if the corresponding element of X is greater than zero

! y = -x if the corresponding element of X is less than zero

! this uses the APMonitor object 'abs'

! y = x if the corresponding element of X is greater than zero

! y = -x if the corresponding element of X is less than zero

! this uses the APMonitor object 'abs'

to:

! MPEC formulation for ABS function using an object

Changed lines 71-72 from:

to:

End Objects

Changed lines 76-77 from:

to:

End Connections

Changed lines 80-81 from:

to:

End Parameters

Added line 84:

End Variables

Deleted line 85:

Changed lines 47-66 from:

Parameters

Variables

s_a >

Equations

x = s_b - s_a

y = s_a + s_b

minimize s_a*s_b

End Equations

End Model

to:

Parameters

x = -2

End Parameters

Variables

y

s_a >= 0

s_b >= 0

End Variables

Equations

! test abs operator, y = abs(x)

x = s_b - s_a

y = s_a + s_b

minimize s_a*s_b

End Equations

x = -2

End Parameters

Variables

y

s_a >= 0

s_b >= 0

End Variables

Equations

! test abs operator, y = abs(x)

x = s_b - s_a

y = s_a + s_b

minimize s_a*s_b

End Equations

Added lines 66-87:

(:html:)<font size=2><pre>

! MPEC formulation for ABS function

! y = ABS(x) returns a value y, where:

! y = x if the corresponding element of X is greater than zero

! y = -x if the corresponding element of X is less than zero

! this uses the APMonitor object 'abs'

Objects

f = abs

Connections

f.x = x

f.y = y

Parameters

x = -2

Variables

y

</pre></font>(:htmlend:)

Changed line 11 from:

(:html:)<font size=~~1~~><pre>

to:

(:html:)<font size=2><pre>

Changed line 42 from:

(:html:)<font size=~~1~~><pre>

to:

(:html:)<font size=2><pre>

Changed line 74 from:

(:html:)<font size=~~1~~><pre>

to:

(:html:)<font size=2><pre>

Changed line 108 from:

(:html:)<font size=~~1~~><pre>

to:

(:html:)<font size=2><pre>

Changed line 136 from:

----

to:

----

Changed line 3 from:

Mathematical Programs with Equilibrium Constraints (MPECs) are formulations that can be used to model certain classes of discrete events. MPECs can be more efficient than solving mixed integer formulations of the optimization problems.

to:

Mathematical Programs with Equilibrium Constraints (MPECs) are formulations that can be used to model certain classes of discrete events. MPECs can be more efficient than solving mixed integer formulations of the optimization problems because it avoids the combinatorial difficulties of searching for optimal discrete variables.

Changed line 3 from:

Mathematical Programs with Equilibrium Constraints (MPECs) are formulations that can be used to model certain classes of discrete events. MPECs can be more efficient than solving mixed integer problems.

to:

Mathematical Programs with Equilibrium Constraints (MPECs) are formulations that can be used to model certain classes of discrete events. MPECs can be more efficient than solving mixed integer formulations of the optimization problems.

Changed line 16 from:

Model ~~signum~~

to:

Model sign

Changed line 47 from:

Model ~~signum~~

to:

Model abs

Changed line 96 from:

y = x1 ~~+~~ s_a

to:

y = x1 - s_a

Added lines 75-134:

! MPEC formulation for MIN function

! y = MIN(x1,x2) returns a value y, where:

! y = x1 if x1 < x2

! y = x2 if x2 < x1

Model signum

Parameters

x1 = -2

x2 = -1

End Parameters

Variables

y

! slack variables

s_a >= 0

s_b >= 0

End Variables

Equations

! test min operator, y = min(x1,x2)

x2 - x1 = s_b - s_a

y = x1 + s_a

minimize s_a*s_b

End Equations

End Model

</pre></font>(:htmlend:)

----

!!! Maximum Selector (MAX) Operator

* %list list-page% [[Attach:max.apm | MAX Operator Example]]

(:html:)<font size=1><pre>

! MPEC formulation for MAX function

! y = MAX(x1,x2) returns a value y, where:

! y = x1 if x1 > x2

! y = x2 if x2 > x1

Model signum

Parameters

x1 = -2

x2 = 4

End Parameters

Variables

y

! slack variables

s_a >= 0

s_b >= 0

End Variables

Equations

! test max operator, y = max(x1,x2)

x2 - x1 = s_a - s_b

y = x1 + s_a

minimize s_a*s_b

End Equations

End Model

! y = MIN(x1,x2) returns a value y, where:

! y = x1 if x1 < x2

! y = x2 if x2 < x1

Model signum

Parameters

x1 = -2

x2 = -1

End Parameters

Variables

y

! slack variables

s_a >= 0

s_b >= 0

End Variables

Equations

! test min operator, y = min(x1,x2)

x2 - x1 = s_b - s_a

y = x1 + s_a

minimize s_a*s_b

End Equations

End Model

</pre></font>(:htmlend:)

----

!!! Maximum Selector (MAX) Operator

* %list list-page% [[Attach:max.apm | MAX Operator Example]]

(:html:)<font size=1><pre>

! MPEC formulation for MAX function

! y = MAX(x1,x2) returns a value y, where:

! y = x1 if x1 > x2

! y = x2 if x2 > x1

Model signum

Parameters

x1 = -2

x2 = 4

End Parameters

Variables

y

! slack variables

s_a >= 0

s_b >= 0

End Variables

Equations

! test max operator, y = max(x1,x2)

x2 - x1 = s_a - s_b

y = x1 + s_a

minimize s_a*s_b

End Equations

End Model

Added lines 41-72:

(:html:)<font size=1><pre>

! MPEC formulation for ABS function

! y = ABS(x) returns a value y, where:

! y = x if the corresponding element of X is greater than zero

! y = -x if the corresponding element of X is less than zero

Model signum

Parameters

x = -2

End Parameters

Variables

y

s_a >= 0

s_b >= 0

End Variables

Equations

! test abs operator, y = abs(x)

x = s_b - s_a

y = s_a + s_b

minimize s_a*s_b

End Equations

End Model

</pre></font>(:htmlend:)

----

!!! Minimum Selector (MIN) Operator

* %list list-page% [[Attach:min.apm | MIN Operator Example]]

Changed lines 7-14 from:

!!! ~~MPEC examples~~

-~~---~~

!!!! SIGN Operator

* %list list-page% [[Attach:sign.apm | Download SIGN Operator Example]]

!!!! SIGN Operator

* %list list-page% [[Attach:sign.apm | Download

to:

!!! SIGN Operator

* %list list-page% [[Attach:sign.apm | SIGN Operator Example]]

* %list list-page% [[Attach:sign.apm | SIGN Operator Example]]

Deleted line 35:

Added lines 38-44:

!!! Absolute Value (ABS) Operator

* %list list-page% [[Attach:abs.apm | ABS Operator Example]]

(:html:)<font size=1><pre>

</pre></font>(:htmlend:)

----

* %list list-page% [[Attach:abs.apm | ABS Operator Example]]

(:html:)<font size=1><pre>

</pre></font>(:htmlend:)

----

Changed lines 9-13 from:

to:

----

!!!! SIGN Operator

* %list list-page% [[Attach:sign.apm | Download SIGN Operator Example]]

!!!! SIGN Operator

* %list list-page% [[Attach:sign.apm | Download SIGN Operator Example]]

Deleted lines 10-11:

Changed lines 12-13 from:

</pre></font>(:htmlend:)

to:

! MPEC formulation for SIGN function

! y = SIGN(x) returns a value y, where:

! 1 if the corresponding element of X is greater than zero

! -1 if the corresponding element of X is less than zero

Model signum

Parameters

x = -2

End Parameters

Variables

y >= -1, <= 1

s_a >= 0

s_b >= 0

End Variables

Equations

! test sign operator, y = sign(x)

x = s_b - s_a

minimize s_a*(1+y) + s_b*(1-y)

End Equations

End Model

</pre></font>(:htmlend:)

----

! y = SIGN(x) returns a value y, where:

! 1 if the corresponding element of X is greater than zero

! -1 if the corresponding element of X is less than zero

Model signum

Parameters

x = -2

End Parameters

Variables

y >= -1, <= 1

s_a >= 0

s_b >= 0

End Variables

Equations

! test sign operator, y = sign(x)

x = s_b - s_a

minimize s_a*(1+y) + s_b*(1-y)

End Equations

End Model

</pre></font>(:htmlend:)

----

Added lines 1-15:

!! MPEC: Mathematical Programs with Equilibrium Constraints

Mathematical Programs with Equilibrium Constraints (MPECs) are formulations that can be used to model certain classes of discrete events. MPECs can be more efficient than solving mixed integer problems.

----

!!! MPEC examples

* %list list-page% [[Attach:sign.apm | SIGN Operator]]

----

(:html:)<font size=1><pre>

</pre></font>(:htmlend:)

Mathematical Programs with Equilibrium Constraints (MPECs) are formulations that can be used to model certain classes of discrete events. MPECs can be more efficient than solving mixed integer problems.

----

!!! MPEC examples

* %list list-page% [[Attach:sign.apm | SIGN Operator]]

----

(:html:)<font size=1><pre>

</pre></font>(:htmlend:)