I am trying to find an optimal number of employees to satisfy a scheduling problem, this is my code:
using JuMP
using GLPK
# Constants and parameters
Wjk = [19 16 22 22 22 22 22; 19 16 22 22 22 22 22; 14 11 16 16 16 16 16] # Workload requirement
Ajk = 8 # Some constant value for Ajk
M = 395 # Total number of shifts
# Create a JuMP model
model = Model(GLPK.Optimizer)
# Decision variables for shift allocation
@variable(model, i >= N , Int) # Number of employees
@variable(model, 1 <= j <= 3, Int) # Number of shifts per day
@variable(model, 1 <= k <= 7, Int) # Number of days per week
@variable(model, x[1:N, 1:3, 1:7] >= 0, Int) # Decision variable for shift allocation
# Objective function
@objective(model, Min, sum(x[i, j, k] for i in 1:N, j in 1:3, k in 1:7))
# Objective function
@objective(model, Min, sum(x[i, j, k] for i in 1:N for j in 1:3 for k in 1:7))
# Constraints
for i in 1:N
for k in 1:7
@constraint(model, sum(x[i, j, k] for j in 1:3) <= 2) # Constraints (8) - (10)
@constraint(model, sum(x[i, j, k]) + sum(x[i, 2, mod(k + 1, 7) + 1] for j in 2:3) <= 2) # Constraint (9)
@constraint(model, sum(x[i, 3, k] for j in 1:3) + sum(x[i, 1, mod(k + 1, 7) + 1]) +
sum(x[i, 2, (mod(k + 1, 7)) + 1]) <= 2) # Constraint (10)
end
end
for j in 1:3
for k in 1:7
@constraint(model, sum(x[i, j, k] for i in 1:N) >= Wjk[k, j]) # Constraint (11)
@constraint(model, sum(x[i, j, k] for i in 1:N) <= Wjk[k, j] + Ajk) # Constraint (12)
end
end
for i in 1:N
@constraint(model, sum(x[i, j, ((i - 1) % 7) + 1] + x[i, j, i % 7 + 1] for j in 1:3) == 0) # Constraint (13)
end
for i in 1:N
for k in 1:5
@constraint(model, sum(x[i, j, (i + k) % 7 + 1] for j in 1:3) >= 1) # Constraint (14)
end
end
@constraint(model, sum(x[i, j, k] for i in 1:N for j in 1:3 for k in 1:7) == M)
# Solve the optimization problem
optimize!(model)
# Display the results
println("Optimal Number of Employees: ", value(N))
println("Optimal Shift Allocation:")
for i in 1:value(N)
for j in 1:3
for k in 1:7
if value(x[i, j, k]) > 0.5
println("Employee $i, Shift $j on Day $k")
end
end
end
end
When this runs I keep getting an error:
> ERROR: MethodError: Cannot `convert` an object of type VariableRef to an object of type Float64
> Closest candidates are:
> convert(::Type{T}, ::Base.TwicePrecision) where T<:Number at twiceprecision.jl:250
> convert(::Type{T}, ::AbstractChar) where T<:Number at char.jl:180
> convert(::Type{T}, ::CartesianIndex{1}) where T<:Number at multidimensional.jl:136
> ...
> Stacktrace:
> [1] MathOptInterface.GreaterThan{Float64}(lower::VariableRef)
> @ MathOptInterface C:\Users\doriiido\.julia\packages\MathOptInterface\IiXiU\src\sets.jl:171
> [2] _moi_constrain_variable(moi_backend::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimize er}, MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}}, index::MathOptInterface.VariableIndex, info::VariableInfo{VariableRef, Float64, Float64, Float64}, #unused#::Type{Float64})
> @ JuMP C:\Users\doriiido\.julia\packages\JuMP\ToPd2\src\variables.jl:1754
> [3] _moi_add_variable(moi_backend::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.Bridges.LazyBridgeOptimizer{GLPK.Optimizer}, MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}}, model::Model, v::ScalarVariable{VariableRef, Float64, Float64, Float64}, name::String)
> @ JuMP C:\Users\doriiido\.julia\packages\JuMP\ToPd2\src\variables.jl:1737
> [4] add_variable(model::Model, v::ScalarVariable{VariableRef, Float64, Float64, Float64}, name::String)
> @ JuMP C:\Users\doriiido\.julia\packages\JuMP\ToPd2\src\variables.jl:1726
> [5] macro expansion
> @ C:\Users\doriiido\.julia\packages\JuMP\ToPd2\src\macros.jl:1213 [inlined]
> [6] top-level scope
> @ Untitled-2:13
I am not sure how to correct this