BYU-PRISM / GEKKO

GEKKO Python for Machine Learning and Dynamic Optimization
https://machinelearning.byu.edu
Other
569 stars 102 forks source link

*** Error in syntax of function string: Mismatched parenthesis #159

Closed espringe closed 1 year ago

espringe commented 1 year ago

It is this line: q = m.if3(tlX <= 0, 1, 0)

That causes an error:

 @error: Model Expression
 *** Error in syntax of function string: Mismatched parenthesis

Position: 4                   
 (0)))-((((1-int_v7))*(v1)-slk_3
    ?

reproduced in example

from gekko import GEKKO
import random

m = GEKKO(remote=True) 

t = m.Const(1)
f = m.Const(0)

radius = 31

def newVar(lb, ub):
  return m.Var(value=random.uniform(lb, ub) , lb=lb, ub=ub)

tlX = newVar(-radius, radius)
tlY = newVar(-radius, radius)
m.Equation(m.sqrt(tlX*tlX + tlY*tlY)  <= radius )

brX = newVar(-radius, radius)
brY = newVar(-radius, radius)

m.Equation(m.sqrt(brX*brX + brY*brY)  <= radius )

xDiff = m.Var()
yDiff = m.Var()

q = m.if3(tlX <= 0, 1, 0)

m.Equation(xDiff == brX - tlX)
m.Equation(yDiff == brY - tlY)

area = m.Var()
m.Equation(area == xDiff * yDiff)

m.Maximize(area)

m.solve() 

print("tlX: ", tlX.value)
print("tlY: ", tlY.value)

print("brX", brX.value)
print("brY", brY.value)

print("xDiff: ", xDiff.value)
print("yDiff: ", yDiff.value)

print("Area: ", area.value)
# print("q: ", q)
APMonitor commented 1 year ago

The m.if3() statement does not have an inequality or equality expression. Use tlX instead of tlX <= 0. Here is an example from the documentation:

import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO
m = GEKKO(remote=False)
p = m.Param()
y = m.if3(p-4,p**2,p+1)

# solve with condition<0
p.value = 3
m.solve(disp=False)
print(y.value)

# solve with condition>=0
p.value = 5
m.solve(disp=False)
print(y.value)

Please post future application questions to StackOverflow: https://stackoverflow.com/questions/tagged/gekko