最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

julia - How to use variable references to evaluate expressions at a fixed point? - Stack Overflow

programmeradmin0浏览0评论

I am pretty new to julia and JuMP and I am stuck how to efficiently evaluate the objective or the expressions in a model at a fixed point.

Googling brought me to this solution in the julia discourse, where a dictionary is used to hold the fixed variable values:

using JuMP
model = Model()
@variables(model, begin
    -1 <= x[1:5] <= 1
    -5 <= y[1:3] <= 8
    -30 <= z <= 5
end)
ex = @NLexpression(model, sum(x[i] for i=1:4) - y[1] * y[2] + z)
@NLconstraint(model, ex <= 10)


inp = Dict(x[1] => 1, x[2] => 2, x[3] => 3, x[4] => 4, y[1] => 5, y[2] => 6, z => 7)

value(i -> get(inp, i, 0.0), ex) 

However I have my fixed values saved in several arrays, one for each variable:

last_x = Containers.DenseAxisArray(vcat(1:4), vcat(1:4))
last_y = Containers.DenseAxisArray([5,6], vcat(1:2))
last_z = Containers.DenseAxisArray([7], 1)

How can I evaluate the expression ex of the model such that each variable reference of the model is pointed to the corresponding value in the arrays? I was thinking about taking the indices from the variables and then looping through the arrays might be a way, but I can only get the variable reference as a whole:

model[:x] 
for key in eachindex(model[:x]) # will give only the variable reference
    println((model[:x][key]))
end

I could construct a dictionary as in the linked solution but was searching for a way to avoid this intermediate step. Thanks for any suggestions!

I am pretty new to julia and JuMP and I am stuck how to efficiently evaluate the objective or the expressions in a model at a fixed point.

Googling brought me to this solution in the julia discourse, where a dictionary is used to hold the fixed variable values:

using JuMP
model = Model()
@variables(model, begin
    -1 <= x[1:5] <= 1
    -5 <= y[1:3] <= 8
    -30 <= z <= 5
end)
ex = @NLexpression(model, sum(x[i] for i=1:4) - y[1] * y[2] + z)
@NLconstraint(model, ex <= 10)


inp = Dict(x[1] => 1, x[2] => 2, x[3] => 3, x[4] => 4, y[1] => 5, y[2] => 6, z => 7)

value(i -> get(inp, i, 0.0), ex) 

However I have my fixed values saved in several arrays, one for each variable:

last_x = Containers.DenseAxisArray(vcat(1:4), vcat(1:4))
last_y = Containers.DenseAxisArray([5,6], vcat(1:2))
last_z = Containers.DenseAxisArray([7], 1)

How can I evaluate the expression ex of the model such that each variable reference of the model is pointed to the corresponding value in the arrays? I was thinking about taking the indices from the variables and then looping through the arrays might be a way, but I can only get the variable reference as a whole:

model[:x] 
for key in eachindex(model[:x]) # will give only the variable reference
    println((model[:x][key]))
end

I could construct a dictionary as in the linked solution but was searching for a way to avoid this intermediate step. Thanks for any suggestions!

Share Improve this question asked Nov 28, 2024 at 10:23 HugoHugo 793 bronze badges 1
  • Next time, come post questions like this on our community forum: jump.dev/forum – Oscar Dowson Commented Dec 2, 2024 at 19:32
Add a comment  | 

1 Answer 1

Reset to default 0

Just make the dictionary.

Note that the @NL syntax is now legacy. You can remove in favor of @expression and @constraint.

发布评论

评论列表(0)

  1. 暂无评论