首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向JuMP添加矢量化约束时抛出错误

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

Stack Overflow用户
提问于 2020-09-09 02:22:44
回答 2查看 40关注 0票数 2

我正在尝试重现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]'

下面是reprex:

代码语言:javascript
复制
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 ) )

有没有人知道为什么抛出这个问题,以及如何修复它?

感谢任何人的帮助!

运行Julia 1.5.1

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-09 05:56:26

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

有了这些约束,你可以做的一种方式就是:

代码语言:javascript
复制
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]))
票数 2
EN

Stack Overflow用户

发布于 2020-09-09 10:47:06

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

代码语言:javascript
复制
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)

以下是constraints https://jump.dev/JuMP.jl/stable/constraints的JuMP文档

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63799612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档