简体   繁体   中英

How can I simplify a large scale NLP model in Gekko Python?

I'm still new to Gekko optimization in Python, and I have a problem scaling up my NLP problem. After trying to call several times (And in different ways) the function m.Sum, using m.Intermediate and using list comprehensions for reducing the size of the string representing my model, I still get this error while trying to optimize a model that only has 200 variables. The model works when I optimize for only 80 variables.

This is the error I get:

APM model error: string > 15000  characters
 Consider breaking up the line into multiple equations

 The may also be due to only using newline character CR
   instead of CR LF (for Windows) or LF (for MacOS/Linux)
 To fix this problem, save APM file with appropriate newline characters

 STOPPING...

Here's a snippet of the code that's getting me in trouble:

def function_to_minimize(variables0,m):
    somme = [ m.Intermediate(
                    variables0[0+(4*i)] * df['value1'][i] + \
                    variables0[1+(4*i)] * df['value2'][i] + \
                    variables0[2+(4*i)] * df['value3'][i] + \
                    variables0[3+(4*i)] * df['value4'][i]
     ) for i in range(0,hours)] 

    somme = reduce(lambda a, b: a+b, somme)
    m.Obj(m.Intermediate(somme))

Try calling m.Obj() multiple times instead of creating a summation. Below is a minimal complete problem that demonstrates the issue. If v = m.Array(m.Var,100,lb=0,ub=10) is an array of more variables then the 15,000 equation length limit could be reached.

from gekko import GEKKO
m = GEKKO()
v = m.Array(m.Var,100,lb=0,ub=10)
hours = 24
def function_to_minimize(variables0,m):
    somme = [ m.Intermediate(
                    variables0[0+(4*i)] * 1 + \
                    variables0[1+(4*i)] * 2 + \
                    variables0[2+(4*i)] * 3 + \
                    variables0[3+(4*i)] * 4
     ) for i in range(0,hours)] 
    somme = sum(somme)
    m.Minimize(m.Intermediate(somme))
function_to_minimize(v,m)
m.solve()

Use m.sum() to avoid the limit.

from gekko import GEKKO

m = GEKKO()
v = m.Array(m.Var,100,lb=0,ub=10)
hours = 24

def function_to_minimize(variables0,m):
    somme = [ m.Intermediate(
                    variables0[0+(4*i)] * 1 + \
                    variables0[1+(4*i)] * 2 + \
                    variables0[2+(4*i)] * 3 + \
                    variables0[3+(4*i)] * 4
     ) for i in range(0,hours)] 

    m.Minimize(m.sum(somme))
function_to_minimize(v,m)
m.solve()

or call m.Obj() or m.Minimize() separately for each element with [m.Minimize(s) for s in somme] .

from gekko import GEKKO

m = GEKKO()
v = m.Array(m.Var,100,lb=0,ub=10)
hours = 24

def function_to_minimize(variables0,m):
    somme = [ m.Intermediate(
                    variables0[0+(4*i)] * 1 + \
                    variables0[1+(4*i)] * 2 + \
                    variables0[2+(4*i)] * 3 + \
                    variables0[3+(4*i)] * 4
     ) for i in range(0,hours)] 

    [m.Minimize(s) for s in somme]
function_to_minimize(v,m)
m.solve()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM