juliajulia-jump

How does one elegantly initialize start values or parameter values when creating a julia JuMP parameter or variable container?


Was hoping a Julia or JuMP expert could help me on how to do the following (apologies as I am new to the language and tried all resources but cannot find a solution to this problem)

I am trying to initialize the start values of all variables when creating a JuMP variable container.

I have a function defined as follows:

function sam(i, j)
    if i == "BRD" && j == "CAP"
        return 5
    elseif i == "BRD" && j == "LAB"
        return 10
    elseif i == "MLK" && j == "CAP"
        return 20
    elseif i == "MLK" && j == "LAB"
        return 15
    elseif i == "CAP" && j == "HOH"
        return 25
    elseif i == "HOH" && j == "BRD"
        return 15
    elseif i == "HOH" && j == "MLK"
        return 35
    else
        return nothing
    end
end

I want to initialize a couple model variables, let's say household consumption of a product.

@variable(model, 0.001 <= Xᵢ[h, g]) # where h = ["HOH"] and g = ["BRD", "MLK"]

This variable can be initialized using the function above. It should be "HOH" -> "BRD" and "HOH" -> "MLK". So 15 and 35 respectively.

I can do these initializations by doing this:

set_start_value(Xᵢ["HOH", "BRD"], sam("HOH", "BRD"))
set_start_value(Xᵢ["HOH", "MLK"], sam("HOH", "MLK"))

However, I was hoping there would be a better way to do this using the start option. I have tried the following with no success.

@variable(model, 0.001 <= Xᵢ[h, g], start = sam(h, g)) # option 1
@variable(model, 0.001 <= Xᵢ[h, g], start = sam.(h, g)) # option 2
@variable(model, 0.001 <= Xᵢ[h, g], start = sam.(permute(h), g)) # option 3
@variable(model, 0.001 <= Xᵢ[h, g], start = [sam(h,g) for h in h, for g in g]) # option 4

The same question similarly applies to the creation of @NLparameter`. How can one do this for a parameter. Options tried below:

@NLparameter(model, 0.001 <= FFᶠ[f][h] == sam(f, h))
@NLparameter(model, 0.001 <= FFᶠ[f][h] == sam.(f, h))
@NLparameter(model, 0.001 <= FFᶠ[f][h] == sam.(permute(f), h))
@NLparameter(model, 0.001 <= FFᶠ[f][h] == [sam(f,h) for f in f, for h in h])

A simpler example:

I have some function f = x^2 + y^2 and arrays x=[1,2,3,4,5,6] and y=[1,2,3,4,5,6] how can I write:

@variable(model, v[x,y], start=f(x,y))

Such that value(v[1, 2]) start value will be equal to 1^2 + 2^2 therefore 5.


Solution

  • Do something like:

    julia> using JuMP
    
    julia> h, g = ["HOH"], ["BRD", "MLK"]
    (["HOH"], ["BRD", "MLK"])
    
    julia> model = Model()
    A JuMP Model
    Feasibility problem with:
    Variables: 0
    Model mode: AUTOMATIC
    CachingOptimizer state: NO_OPTIMIZER
    Solver name: No optimizer attached.
    
    julia> function sam(i, j)
               if i == "HOH" && j == "BRD"
                   return 15
               else
                   return nothing
               end
           end
    sam (generic function with 1 method)
    
    julia> @variable(model, 0.001 <= X[i=h, j=g], start = sam(i, j))
    2-dimensional DenseAxisArray{VariableRef,2,...} with index sets:
        Dimension 1, ["HOH"]
        Dimension 2, ["BRD", "MLK"]
    And data, a 1×2 Matrix{VariableRef}:
     X[HOH,BRD]  X[HOH,MLK]
    
    julia> start_value.(X)
    2-dimensional DenseAxisArray{Union{Nothing, Float64},2,...} with index sets:
        Dimension 1, ["HOH"]
        Dimension 2, ["BRD", "MLK"]
    And data, a 1×2 Matrix{Union{Nothing, Float64}}:
     15.0  nothing
    

    I guess we could make this more explicit in the documentation. I've opened an issue to get this fixed: https://github.com/jump-dev/JuMP.jl/issues/3147