Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GEKKO Error: "Equation without an equality (=) or inequality (>,<)" when calling functions within constraint and objective

Tags:

python

gekko

I have been getting the below error for my code and I am at a complete loss as to the source of the error:

@error: Equation Definition
Equation without an equality (=) or inequality (>,<)
true
STOPPING...

I am seeking to identify the solution 'x' that minimises the result of the function 'was_constraint' subject to meeting the constraint set by 'warf_moodys_constraint'. The functions return a float value and when i just pass the initial starting vector 'x' to each function separately I don't receive any errors originating from those functions. Can anyone please advise where I might be going wrong?

def was_constraint(sol_g, df, orig):
    sol = gekko_to_numpy(sol_g)
    x1 = orig.loc["Denominator","WAS"]*orig.loc["Current","WAS"]
    x2 = (sol*df["All-In Rate"]).sum()/100
    y1 = orig.loc["Denominator","WAS"]+sum(sol)
    return y1/(x1+x2)

def warf_moodys_constraint(sol_g, df, orig):
    sol = gekko_to_numpy(sol_g)
    x1 = orig.loc["Denominator","Moodys WARF"]*orig.loc["Current","Moodys WARF"]
    x2 = sum(np.where(sol > 0, sol*df["Moody's WARF"], 0))
    y1 = orig.loc["Denominator","Moodys WARF"] +sum(np.where(sol > 0, sol, 0))
    return 3000 - (x1+x2)/y1 

def gekko_to_numpy(sol_g):
    res = np.zeros(len(sol_g))
    for i in range(len(sol_g)):
        res[i] = sol_g[i].value.value
    return res

clo_data = pd.read_excel('CLO.xlsx', sheet_name='CLO')
m = GEKKO()
x = [m.Var() for i in range(len(clo_data["Holdings"]))]

for i in range(len(clo_data["Lower Bound"])):
    x[i].lower = 0
    x[i].upper = 1000000

m.Equation(warf_moodys_constraint(x, clo_data, metrics)>=0)
m.Obj(was_constraint(x, clo_data, metrics))
m.options.IMODE = 3 #steady state optimization
m.solve()
like image 725
Toby-wan Avatar asked Nov 06 '22 05:11

Toby-wan


1 Answers

You need to define the equations in terms of Gekko variables. The approach to translate Gekko variables into a Numpy array won't work to define the equations because Gekko doesn't do call-backs into the Python functions.

def gekko_to_numpy(sol_g):
    res = np.zeros(len(sol_g))
    for i in range(len(sol_g)):
        res[i] = sol_g[i].value.value
    return res

Gekko builds the gk_model0.apm model in the run folder that you can see with m.open_folder(). When you solve with m.solve() Gekko compiles the model into byte-code and solves it with sparse nonlinear solvers such as IPOPT or APOPT. If you can't use Gekko variables then maybe the scipy.opitimize.minimize() function would be a better choice. Here is a tutorial with that optimizer.

like image 123
John Hedengren Avatar answered Nov 14 '22 21:11

John Hedengren