【问题标题】:Julia JuMP Constraint from Vector来自向量的 Julia JuMP 约束
【发布时间】:2021-08-29 17:39:13
【问题描述】:

我正在和 Julia 一起练习,但在试图做出限制时卡住了。

# Server Costs

> df_custos = DataFrame(C1=[2,4,3],C2=[1,2,1],C3=[3,3,2],C4=[6,2,3],)

# City num

> num_cidades=ncol(df_custos)

# Server num 

> num_servidores = nrow(df_custos)

# Server demand

> df_demanda = [100, 120, 80, 200]

# Model -----------------------------------------------

> model_s = Model(Cbc.Optimizer)

# X[i,j] where i = Server and j=City

> @variable(model_s,
> x[i=1:num_servidores,j=1:num_cidades],Int,lower_bound=0)

# Objetive as costs[i.j]*x[i,j]

> @objective(model_s, Min, sum(df_custos[i,j]*x[i,j] for
> i=1:num_servidores,j=1:num_cidades))

# Sum(x[i,j])==df_demanda

> @constraint(model_s,[j=1:num_cidades], sum(x[i,j] for
> i=1:num_servidores) .>= df_demanda

)

> print(model_s)

问题是当我打印模型时,我得到了这个:

当我预计只有 4 个按需限制时,每个城市一个,例如:

x11+x21+x31 == 100

x12+x22+x32 == 120

x13+x23+x33 == 80

x14+x24+x34 == 200

如何编辑约束以使其正确?

【问题讨论】:

    标签: julia linear-programming julia-jump linear-optimization


    【解决方案1】:

    知道了,但不是我想的那样:我只是将@constrain 更改为循环通过收缩的右侧。

    for i_r=1:length(df_demanda)
        
    print(df_demanda[i_r])
        
    @constraint(model_s,[j=i_r],sum(x[i,j] for i=1:num_servidores) >=df_demanda[i_r])
    
    end
    

    【讨论】:

      【解决方案2】:

      你想要:

      @constraint(model_s,[j=1:num_cidades], sum(x[:,j]) >= df_demanda[j])
      

      这将完全添加num_cidades 约束

      【讨论】:

      • 感谢您的回答,但我仍然遇到问题。您编写的代码给了我一个错误“MethodError:没有方法匹配零(::Type{Vector{Int64}})”,如果我更改为@constraint(model_s,[j=1:num_cidades], sum(x[:,j]) .>= df_demanda),我也会遇到同样的错误。我找到了解决方案,但不是我预期的那样......再次感谢!
      • 它应该在 RHS 上:` >= df_demanda[j]` 这就是为什么我要求你更正字体 - 很难弄清楚哪个变量是什么
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多