## Mpec Examples

## Apps.MpecExamples History

Hide minor edits - Show changes to markup

**GEKKO MIN function (min2)**

See GEKKO Documentation for additional examples.

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

(:source lang=python:)

(:sourceend:)

**GEKKO MAX function (min2)**

See 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 SIGNUM function (sign2)**

See GEKKO Documentation for additional examples.

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

(:source lang=python:)

- solve

m.solve()

m.solve() # solve

Model signum

Model

Model signum

Model

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

(:source lang=python:)

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

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

(:sourceend:)

(:source lang=python:)

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

(:sourceend:)

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

(:source lang=python:)

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

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

(:sourceend:)

(:source lang=python:)

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

(:sourceend:)

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

(:source lang=python:)

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

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

(:sourceend:)

(:source lang=python:)

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

(:sourceend:)

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

(:source lang=python:)

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

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

(:sourceend:)

(:source lang=python:)

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

(:sourceend:)

**GEKKO ABS function (abs2)**

See 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. Article

#### 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. 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. Article

# SIGN function as an Object

# SIGN function MPEC as an Object

# ABS function as an Object

# ABS function MPEC as an Object

# MIN function MPEC as an Object

# MAX function MPEC as an Object

# SIGN function with Object

# SIGN function as an Object

# MPEC formulation for ABS function using an object

# ABS function as an Object

(: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:)

# 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 > than zero

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

# 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'

# MPEC formulation for ABS function using an object

End Objects

End Connections

End Parameters

End Variables

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

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

(: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>

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

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

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

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

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

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

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

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.

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.

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.

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.

Model signum

Model sign

Model signum

Model abs

y = x1 + s_a

y = x1 - s_a

# 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

(: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

(: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

### SIGN Operator

### Absolute Value (ABS) Operator

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

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

# 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:)

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

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

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