1

我正在和 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

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

4

2 回答 2

2

明白了,但不是我想的那样:我只是将 更改@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
于 2021-08-29T19:30:24.070 回答
1

你要:

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

这将准确添加num_cidades约束

于 2021-08-29T17:44:48.610 回答