How to merge two `JuMP` models?

You could write your example as:

function fun(M::Model)
    x, y = M[:x], M[:y]
    @objective(M, Max, x + y)
    optimize!(M)
    return value(x)
end

But do you think it would be the developer’s duty to make sure the auxiliary variables from the input model (M2 ) don’t conflict with the internal main model (M1 )?

Yes. JuMP supports anonymous variables for this use-case.

function fun(M::Model)
    x = M[:x]
    y = @variable(M, upper_bound = 3)
    # This y is not the other y
    @objective(M, Max, x + y)
    optimize!(M)
    return value(x)
end

You should read the tutorial:

4 Likes