Search code examples
juliaeconomicsjulia-jump

Error thrown when adding vectorised constraints to JuMP


I am trying to reproduce this model - the code in the tutorial is for an old version of JuMP/Julia and does not run.

However, when I try to add the constraint:

@constraint(model, con, c[i = 1:N] .== ( ((1 - τ) * (1 - l[i]) .* w[i]) + e[i]))

I get the error Unexpected assignment in expression 'c[i = 1:N]'.

Here is the reprex:

using Random
using Distributions
using JuMP
using Ipopt

Random.seed!(123)
N = 1000
γ = 0.5
τ = 0.2

ϵ = rand(Normal(0, 1), N)
wage = rand(Normal(10, 1), N)
consumption = (γ * (1 - τ) * wage) + (γ * ϵ)
leisure = (1 - γ) .+ (( 1 - γ) * ϵ) ./ (( 1 - τ ) * wage)


model = Model(Ipopt.Optimizer)
@variable(model, c[i = 1:N] >= 0)
@variable(model, 0 <= l[i = 1:N] <= 1)
@constraint(model, con, c[i = 1:N] .== ( ((1 - τ) * (1 - l[i]) .* w[i]) + e[i]))
@NLobjective(model, Max, sum(γ *log(c[i]) + (1-γ)*log(l[i]) for i in 1:N ) )

Does anyone know why this is being thrown and how to fix it?

Any and all help appreciated!

Running Julia 1.5.1


Solution

  • With the c[i = 1:N] in JuMP yo can only define variables.

    With the constraints one way you could do is just:

    w = wage # not in your code
    e = ϵ  # not in your code
    @constraint(model, con[i = 1:N], c[i] == ( ((1 - τ) * (1 - l[i]) .* w[i]) + e[i]))