向 JuMP 添加矢量化约束时抛出错误

Error thrown when adding vectorised constraints to JuMP

我正在尝试重现 this 模型 - 本教程中的代码适用于 JuMP/Julia 的旧版本,而不是 运行。

但是,当我尝试添加约束时:

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

我收到错误 Unexpected assignment in expression 'c[i = 1:N]'

这是代表:

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 ) )

有谁知道为什么会抛出这个问题以及如何解决它?

感谢任何帮助!

运行 朱莉娅 1.5.1

使用JuMP中的c[i = 1:N]只能定义变量。

有了这些限制,您可以采取的一种方法是:

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]))

Przemyslaw 的回答很好。如果你想坚持使用矢量化语法,你可以去

N = 1_000
e = rand(N)
w = rand(N)
τ = 0.2
model = Model()
@variable(model, c[i = 1:N] >= 0)
@variable(model, 0 <= l[i = 1:N] <= 1)
@constraint(model, c .== (1 - τ) .* (1 .- l) .* w .+ e)

这是约束的 JuMP 文档https://jump.dev/JuMP.jl/stable/constraints