Help with GAMS.jl "unrecognized operation" error

Hi,

I am trying to use GAMS to solve a constrained optimization problem. My own problem is quite complicated, and I must use user-defined functions for both the objective and constraints. Below is a reproducible, simple version.

utility(x…) is the objective function, while budget(x…) is the constraint

const p1 = 10.0
const p2 =10.0
const income = 100.0

function utility(x…)
x0 = collect(x)
out = log(x0[1])+log(x0[2])
return out
end

function budget(x…)
x0 = collect(x)
surplus = income - p1x0[1] - p2x0[2]
return surplus
end

Now I use JuMP and GAMS to solve the problem:

set_optimizer_attribute(model_gams, GAMS.SysDir(), “/Library/Frameworks/GAMS.framework/Versions/43/Resources/”)

model_gams = Model(GAMS.Optimizer)

@variable(model_gams, x[1:2] >= 0)

JuMP.register(model_gams, :utility, 2, utility, autodiff=true)

JuMP.register(model_gams, :budget, 2, budget, autodiff=true)

@constraint(model_gams, budget(x…) >= 0)

@NLobjective(model_gams, Max, utility(x…))

JuMP.optimize!(model_gams)

Then an error message appears:

Error: Unrecognized operation (utility)

I am really confused why this utility(x…) function can’t be recognized. Because Is it a user-defined function? I started using JuMP very recently, and I am not a mature programmer. So any suggestions or comments will be highly valuable to me!

Hi @Huan_Deng, welcome to the forum.

This is an error with GAMS.jl, because it does not support user-defined functions. Unfortunately, the error isn’t very descriptive.

cc @renke.kuhlmann should the error be improved?

and I must use user-defined functions for both the objective and constraints.

If this is true, you won’t be able to use GAMS.jl. But in most cases you can write the model without needing user-defined functions:

using JuMP
import GAMS
const p = [10.0, 10.0]
const income = 100.0
model = Model(GAMS.Optimizer)
set_optimizer_attribute(model, GAMS.SysDir(), "/Library/Frameworks/GAMS.framework/Versions/43/Resources/")
@variable(model, x[1:2] >= 0)
@constraint(model, income >= sum(p[i] * x[i] for i in 1:2))
@NLobjective(model, Max, sum(log(x[i]) for i in 1:2))
optimize!(model)
1 Like

Thank you @odow for your explanation! Unfortunately, my original problem is quite complicated; it is a problem that maximizes a likelihood function with 200 constraints and millions of observations. Both the likelihood function and the constraints are much more complicated than the example that I used here. I have written around 100 functions in the middle to really get to the objective function and constraints. I have tried Ipopt, which takes user-defined functions, but it is really slow. So I tried GAMS to see whether I could get a faster speed…

Hope user-defined functions can be better supported in the future! For me and many researchers in economics, I feel it will be hard to avoid user-defined functions when we do optimization in the end. Maybe there is some method that we can use to express our problems directly that I don’t know.

Thank you! At least I know GAMS.jl is not the way to go at this moment:)

This is a limitation of using GAMS.jl.

GAMS takes the model you write in JuMP and writes it out to a file and passes it to the external GAMS library. The GAMS library cannot evaluate Julia code, so it cannot callback into Julia to evaluate your functions. This limitation is unlikely to change.

I have written around 100 functions in the middle to really get to the objective function and constraints. I have tried Ipopt, which takes user-defined functions, but it is really slow.

This is likely because of how you’ve written the model. JuMP will perform poorly if you have lots of nested user-defined functions. Where possible, try writing out the expressions algebraically.

If you can simplify things, make a new post on this forum and we might be able to steer you in a better direction.

1 Like

@odow Thank you! I will see whether I can simplify my model. But I am a little bit confused about how I can write the model in an algebraic way since I have data and need to do integration in the middle. If there is any example that I can take a look at, that would be great! I have very limited resources to improve my modeling in Julia because Julia has a small user base in economics…Thank you very much for kindly answering my questions:)