【问题标题】:Fixing a variable in JuMP / Julia在 JuMP / Julia 中修复变量
【发布时间】:2020-04-05 20:32:37
【问题描述】:

我有一个这样的 AMPL 代码:

param N;
set R := 1..N;
set V := 1..N;
initializeSendPrepareReq{i in R, v in V}: SendPrepReq[1, i, v] = 0;

我需要使用 JuMP 在 Julia 中编写它。

N = 10
R = 1:N
V = 1:N
?

我知道我可能需要使用 JuMP.fix() 但不知道如何使用。谢谢

【问题讨论】:

    标签: optimization julia modeling ampl julia-jump


    【解决方案1】:
    for i in R, v in V
       fix(SendPrepReq[1, i, v], 0)
    end
    

    如果SendPrepReq有其他界限,则需要

    for i in R, v in V
       fix(SendPrepReq[1, i, v], 0; force = true)
    end
    

    这里是相关文档:https://www.juliaopt.org/JuMP.jl/stable/variables/#JuMP.fix

    【讨论】:

      【解决方案2】:

      只需使用 zeros() 函数

      N=10
      SendPrepReq=zeros(1,N,N) or SendPrepReq=zeros(Int,1,N,N)
      

      或者如果你真的想使用 for 循环:

      N=10
      R = 1:N
      V = 1:N
      for r in R
         for v in V 
            SendPrepReq[1,r,v]=0
         end
      end
      

      如果 SendPrepReq 是一个变量:

      for r in R
         for v in V 
            @constraint(model, SendPrepReq[1,r,v] == 0 )
         end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-26
        • 1970-01-01
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多