2

I'm new to Julia and JuMP, a library I'm going to use.

Trying to define the following constraint, after having defined the variables, I receive an error:

for r = 1:11
    for d = 1:7
        for s = 1:12
            @constraint(model, mod(ris_day_ora[r,d,s],0.5)==0)
        end
    end
end

Here the error:

ERROR: LoadError: MethodError: no method matching mod(::VariableRef, ::Float64)

Could you please help me?

Thanks a lot in advance!

Ken White
  • 123,280
  • 14
  • 225
  • 444
DDGG82
  • 21
  • 1

1 Answers1

2

You cannot have a mod in a JuMP constraint.

You need to reformulate the model and there are many ways to do that. In your case the easiest thing would be to declare ris_day_ora as Int and then divide it everywhere by 2.

@variable(model, ris_day_ora[1:11, 1:7, 1:12] >=0, Int)

And now everywhere in the code use ris_day_ora[r,d,s]/2.0 instead of ris_day_ora[r,d,s].

Edit:

if your variable ris_day_ora takes three values 0, 0.5, 1 you just model it as:

@variable(model, 0 <= ris_day_ora[1:11, 1:7, 1:12] <= 2, Int)

And in each place in model use it as 0.5 * ris_day_ora[r,d,s]

Edit 2

Perhaps you are looking for a more general solution. Consider x that can only be either 0.1, 0.3, 0.7 this could be written as:

@variable(model, x)
@variable(model, helper[1:3], Bin)
@contraint(model, x == 0.1*helper[1] + 0.3*helper[2] + 0.7*helper[3])
@contraint(model, sum(helper) == 1)
Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • Thanks Przemyslaw! The variable ris_day_ora, in my case, should be one of these values: 0, 0.5, 1. How can I model the constraint? – DDGG82 Aug 05 '21 at 20:11