|
| 1 | +#= |
| 2 | +attributes.jl |
| 3 | +
|
| 4 | +Specifies the attributes supported by the optimizer. |
| 5 | +Handles, setting and retrieving those attributes. |
| 6 | +=# |
| 7 | + |
| 8 | +#= |
| 9 | +Custom attributes specific to the optimizer |
| 10 | +=# |
| 11 | + |
| 12 | +"""Root type for all AOC attributes""" |
| 13 | +abstract type AOCAttribute <: MOI.AbstractOptimizerAttribute end |
| 14 | + |
| 15 | +"""Specifies the random seed to use""" |
| 16 | +struct Seed <: AOCAttribute end |
| 17 | + |
| 18 | +"""Specifies the working directory where |
| 19 | +the optimizer should store temporary files and updates (where applicable)""" |
| 20 | +struct WorkDir <: AOCAttribute end |
| 21 | + |
| 22 | +"""Specifies the numeric type to use (Float64, Float32, Float16, BFloat16), |
| 23 | +when running locally (if applicable); |
| 24 | +Float64 may not be available when running on the GPU""" |
| 25 | +struct NumericType <: AOCAttribute end |
| 26 | + |
| 27 | +"""Specifies the numeric type to use (Float64, Float32, Float16, BFloat16), |
| 28 | +when running remotely; |
| 29 | +Float64 may not be available when running on the GPU""" |
| 30 | +struct Precision <: AOCAttribute end |
| 31 | + |
| 32 | +"""Specifies the backend to use. |
| 33 | +By default it is the online service (Service); |
| 34 | +other backends are available (e.g., [`RandomAssignment`](@ref) for quick testing)""" |
| 35 | +struct Backend <: AOCAttribute end |
| 36 | + |
| 37 | + |
| 38 | +const AOC_RAW_ATTRIBUTES = Dict{String,Any}( |
| 39 | + "seed" => Seed(), |
| 40 | + "work_dir" => WorkDir(), |
| 41 | + "numeric_type" => NumericType(), |
| 42 | + "backend" => Backend(), |
| 43 | +) |
| 44 | + |
| 45 | +MOI.supports(::Optimizer, ::AOCAttribute) = true |
| 46 | + |
| 47 | +MOI.get(optimizer::Optimizer, ::Seed) = get(optimizer.aim_attributes, :seed, nothing) |
| 48 | +function MOI.set(optimizer::Optimizer, ::Seed, value::Integer) |
| 49 | + optimizer.aim_attributes[:seed] = value |
| 50 | + return nothing |
| 51 | +end |
| 52 | +function MOI.set(optimizer::Optimizer, ::Seed, ::Nothing) |
| 53 | + delete!(optimizer.aim_attributes, :seed) |
| 54 | + return nothing |
| 55 | +end |
| 56 | + |
| 57 | +MOI.get(optimizer::Optimizer, ::WorkDir) = get(optimizer.aim_attributes, :work_dir, nothing) |
| 58 | +function MOI.set(optimizer::Optimizer, ::WorkDir, value::AbstractString) |
| 59 | + optimizer.aim_attributes[:work_dir] = String(value) |
| 60 | + return nothing |
| 61 | +end |
| 62 | +function MOI.set(optimizer::Optimizer, ::WorkDir, ::Nothing) |
| 63 | + delete!(optimizer.aim_attributes, :work_dir) |
| 64 | + return nothing |
| 65 | +end |
| 66 | + |
| 67 | +MOI.get(optimizer::Optimizer{T}, ::NumericType) where {T<:Real} = |
| 68 | + get(optimizer.aim_attributes, :numeric_type, T) |
| 69 | + |
| 70 | +function MOI.set(optimizer::Optimizer, ::NumericType, ::Type{T}) where {T<:Real} |
| 71 | + optimizer.aim_attributes[:numeric_type] = T |
| 72 | + return nothing |
| 73 | +end |
| 74 | + |
| 75 | +MOI.get(optimizer::Optimizer, ::Precision) = get(optimizer.aim_attributes, :precision, "Float32") |
| 76 | +function MOI.set(optimizer::Optimizer, ::Precision, value::String) |
| 77 | + @assert value ∈ ("BFloat16", "Float16", "Float32", "Float64") |
| 78 | + optimizer.aim_attributes[:precision] = value |
| 79 | + return nothing |
| 80 | +end |
| 81 | + |
| 82 | +MOI.get(optimizer::Optimizer, ::Backend) = get(optimizer.aim_attributes, :backend, AOC.Solver.best_engine()) |
| 83 | +function MOI.set(optimizer::Optimizer, ::Backend, value::B) where {B<:Engine} |
| 84 | + optimizer.aim_attributes[:backend] = value |
| 85 | + return nothing |
| 86 | +end |
| 87 | + |
| 88 | + |
| 89 | +#= |
| 90 | +Attributes to interface with the MOI backend |
| 91 | +=# |
| 92 | + |
| 93 | +MOI.get(::Optimizer, ::MOI.SolverName) = "AOC Optimizer" |
| 94 | + |
| 95 | +# TODO: This should be AOC's version instead! |
| 96 | +MOI.get(::Optimizer, ::MOI.SolverVersion) = AOC.__VERSION__ |
| 97 | + |
| 98 | +MOI.get(optimizer::Optimizer, ::MOI.RawSolver) = optimizer |
| 99 | + |
| 100 | +MOI.supports(::Optimizer, attr::MOI.RawOptimizerAttribute) = true # haskey(AOC_RAW_ATTRIBUTES, attr.name) |
| 101 | +function MOI.get(optimizer::Optimizer, attr::MOI.RawOptimizerAttribute) |
| 102 | + if haskey(AOC_RAW_ATTRIBUTES, attr.name) |
| 103 | + return MOI.get(optimizer, AOC_RAW_ATTRIBUTES[attr.name]) |
| 104 | + else |
| 105 | + return optimizer.raw_attributes[attr.name] |
| 106 | + end |
| 107 | +end |
| 108 | +function MOI.set(optimizer::Optimizer, attr::MOI.RawOptimizerAttribute, value::Any) |
| 109 | + if haskey(AOC_RAW_ATTRIBUTES, attr.name) |
| 110 | + MOI.set(optimizer, AOC_RAW_ATTRIBUTES[attr.name], value) |
| 111 | + else |
| 112 | + optimizer.raw_attributes[attr.name] = value |
| 113 | + end |
| 114 | + |
| 115 | + return nothing |
| 116 | +end |
| 117 | + |
| 118 | +MOI.supports(::Optimizer, ::MOI.Name) = true |
| 119 | +MOI.get(optimizer::Optimizer, ::MOI.Name) = get(optimizer.moi_attributes, :name, "") |
| 120 | +function MOI.set(optimizer::Optimizer, ::MOI.Name, value::AbstractString) |
| 121 | + optimizer.moi_attributes[:name] = String(value) |
| 122 | + return nothing |
| 123 | +end |
| 124 | + |
| 125 | +MOI.supports(::Optimizer, ::MOI.Silent) = true |
| 126 | +MOI.get(optimizer::Optimizer, ::MOI.Silent) = get(optimizer.moi_attributes, :silent, false) |
| 127 | +function MOI.set(optimizer::Optimizer, ::MOI.Silent, value::Bool) |
| 128 | + optimizer.moi_attributes[:silent] = value |
| 129 | + return nothing |
| 130 | +end |
| 131 | + |
| 132 | +MOI.supports(::Optimizer, ::MOI.TimeLimitSec) = true |
| 133 | +MOI.get(optimizer::Optimizer, ::MOI.TimeLimitSec) = get(optimizer.moi_attributes, :time_limit_sec, nothing) |
| 134 | +function MOI.set(optimizer::Optimizer, ::MOI.TimeLimitSec, value::Real) |
| 135 | + @assert value >= 0 |
| 136 | + optimizer.moi_attributes[:time_limit_sec] = Float64(value) |
| 137 | + return nothing |
| 138 | +end |
| 139 | +function MOI.set(optimizer::Optimizer, ::MOI.TimeLimitSec, ::Nothing) |
| 140 | + delete!(optimizer.moi_attributes, :time_limit_sec) |
| 141 | + return nothing |
| 142 | +end |
| 143 | + |
| 144 | +MOI.supports(::Optimizer, ::MOI.NumberOfThreads) = true |
| 145 | +MOI.get(optimizer::Optimizer, ::MOI.NumberOfThreads) = get(optimizer.moi_attributes, :number_of_threads, 2) |
| 146 | + |
| 147 | +function MOI.set(optimizer::Optimizer, ::MOI.NumberOfThreads, value::Integer) |
| 148 | + @assert value >= 1 |
| 149 | + |
| 150 | + optimizer.moi_attributes[:number_of_threads] = value |
| 151 | + return nothing |
| 152 | +end |
| 153 | + |
| 154 | + |
| 155 | +#= |
| 156 | +Unsupported attributes |
| 157 | +=# |
| 158 | + |
| 159 | +MOI.supports(::Optimizer, ::MOI.ObjectiveLimit) = false |
| 160 | +MOI.supports(::Optimizer, ::MOI.SolutionLimit) = false |
| 161 | +MOI.supports(::Optimizer, ::MOI.AbsoluteGapTolerance) = false |
| 162 | +MOI.supports(::Optimizer, ::MOI.RelativeGapTolerance) = false |
| 163 | +MOI.supports(::Optimizer, ::MOI.AbstractModelAttribute) = false |
| 164 | +MOI.supports(::Optimizer, ::MOI.AbstractOptimizerAttribute) = false |
0 commit comments