optimization using package

Viewed 327

I'm using gekko for the first time and I want to know if there is any solution to solve max min optimization problem with gekko max( min (function1, function2)). I found only Maximize and Minimize function. Otherwise is there any other python solver or solution using scipy or other for optimization problem in python?

2 Answers

This can be done without a special function:

maximize z
z <= f1
z <= f2 

(this is math notation, not Python/Gekko).

So you just need an additional variable z and two inequalities. This is a standard formulation, so it is good to know about this.

A disadvantage of this formulation is that we now deal with additional non-linear functions in the constraints as opposed to in the objective. This can have an impact on performance and reliability. An advantage is that this can prevent a non-differentiability issue.

Here is a simple example where the sum of the variables must equal 15. The objective is to maximize the minimum of the variables.

max min(x1,x2,x3)
s.t. x1 + x2 + x3 = 15

A standard method for maximin or minimax problems is to reformulate the problem with the additional variable Z.

 max Z
 s.t. x1 + x2 + x3 = 15
      Z <= x1
      Z <= x2
      Z <= x3

Here is the reformulated maximin problem in Gekko.

from gekko import GEKKO
m = GEKKO(remote=False)
m.options.SOLVER = 1
x1,x2,x3,Z = m.Array(m.Var,4)
m.Maximize(Z)
m.Equation(x1+x2+x3==15)
m.Equations([Z<=x1,Z<=x2,Z<=x3])
m.solve()
print('x1: ',x1.value[0])
print('x2: ',x2.value[0])
print('x3: ',x3.value[0])
print('Z:  ',Z.value[0])

That gives the solution:

x1:  5.0
x2:  5.0
x3:  5.0
Z:   5.0

I've added a few of the missing constants (defaults=1) to your original problem to get a successful solution.

from gekko import GEKKO
m = GEKKO(remote=True)

alpha = m.Var(0,lb=0,ub=1)
Ps    = m.Var(5,lb=0,ub=10)
Pr    = m.Var(0,lb=0,ub=10)
PP    = m.Var(5,lb=0,ub=10)

PD=4; to=0.1; NP=20; grp=1; gpp=1; Nr = 1; gsr=1; gpr=1; gss=1; grs=1; Ns=1
m.Equation(grp*Pr>=(gpp*PP/((1+(gpp*PP/NP))**(1-to)-1))-NP)

def FDF1(alpha,Ps):
    return (gsr*(1-alpha)*Ps)/(gpr*Pp+Nr)

def FDF2(alpha,Ps,Pr):
    return (gss*Ps+grs*Pr+2*(np.sqrt(grs*gss*alpha*PP*Pr)))/(Ns)
Func_FDF1 = 1/2*m.log((gsr*(1-alpha)*Ps)/gpr*PP+Nr)
Func_FDF2 = 1/2*m.log(1+(gss*Ps+grs*Pr+2*(m.sqrt(grs*gss*alpha*PP*Pr)))/(Ns))
m.Maximize(Func_FDF2) #===> min(Func_FDF1, Func_FDF2)
m.solve()

print('')
print('Results')
print('Ps: ' + str(Ps.value))
print('Pr: ' + str(Pr.value))
print('alpha: ' + str(alpha.value))

This gives the solution:

Results
Ps: [10.0]
Pr: [10.0]
alpha: [1.0]

To solve the maximin problem, you can add the Z variable

from gekko import GEKKO
m = GEKKO(remote=True)

alpha = m.Var(0,lb=0,ub=1)
Ps    = m.Var(5,lb=0,ub=10)
Pr    = m.Var(0,lb=0,ub=10)
PP    = m.Var(5,lb=0,ub=10)

PD=4; to=0.1; NP=20; grp=1; gpp=1; Nr = 1; gsr=1; gpr=1; gss=1; grs=1; Ns=1
m.Equation(grp*Pr>=(gpp*PP/((1+(gpp*PP/NP))**(1-to)-1))-NP)

def FDF1(alpha,Ps):
    return (gsr*(1-alpha)*Ps)/(gpr*Pp+Nr)

def FDF2(alpha,Ps,Pr):
    return (gss*Ps+grs*Pr+2*(np.sqrt(grs*gss*alpha*PP*Pr)))/(Ns)
Func_FDF1 = 1/2*m.log((gsr*(1-alpha)*Ps)/gpr*PP+Nr)
Func_FDF2 = 1/2*m.log(1+(gss*Ps+grs*Pr+2*(m.sqrt(grs*gss*alpha*PP*Pr)))/(Ns))

# max min(Func_FDF1, Func_FDF2)
Z = m.Var()
m.Maximize(Z)
m.Equation(Z<=Func_FDF1)
m.Equation(Z<=Func_FDF2)
m.solve()

print('')
print('Results')
print('Ps: ' + str(Ps.value))
print('Pr: ' + str(Pr.value))
print('alpha: ' + str(alpha.value))

This now gives the solution that maximizes the minimum of Func_FDF1 and Func_FDF2.

Results
Ps: [10.0]
Pr: [10.0]
alpha: [0.63999999961]
Related