Yuri
Yuri

Reputation: 73

Julia JuMP Constraint from Vector

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?

Upvotes: 1

Views: 494

Answers (2)

Yuri
Yuri

Reputation: 73

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

Upvotes: 2

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42244

You want:

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

This will add exactly num_cidades constraints

Upvotes: 1

Related Questions