1

I'm practicing with Julia but got stuck trying to make a constraint.

# 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)

The problem is that when I print the model I get this:

enter image description here

When I am expecting only 4 resctrictions on demand, one for each city, like:

x11+x21+x31 == 100

x12+x22+x32 == 120

x13+x23+x33 == 80

x14+x24+x34 == 200

How can I edit the constrain to make it right?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Yuri
  • 73
  • 6

2 Answers2

2

Got it, but not the way I was thinking: I just changed the @constrain to loop through the right side of the constriction.

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
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Yuri
  • 73
  • 6
1

You want:

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

This will add exactly num_cidades constraints

Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • Thanks for your answer, but I'm still having problems. The code you made gives me an error "MethodError: no method matching zero(::Type{Vector{Int64}})" and if I change to `@constraint(model_s,[j=1:num_cidades], sum(x[:,j]) .>= df_demanda)` I have the same error. I found a solution, but not as i expected.... thanks again! – Yuri Aug 29 '21 at 19:26
  • It should be on RHS: ` >= df_demanda[j]` that is why I asked you to correct fonts - it was hard to sort out which variable is what – Przemyslaw Szufel Aug 29 '21 at 19:56