Skip to content

Commit

Permalink
[FileFormats.MOF] add [email protected] support (#2293)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Sep 27, 2023
1 parent 44e271c commit b07f71e
Show file tree
Hide file tree
Showing 9 changed files with 475 additions and 445 deletions.
6 changes: 3 additions & 3 deletions docs/src/developer/checklists.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ Use this checklist when updating the version of MathOptFormat.
```
## Basic
- [ ] The file at `src/FileFormats/MOF/mof.X.Y.schema.json` is updated
- [ ] The constants `SCHEMA_PATH`, `VERSION`, and `SUPPORTED_VERSIONS` are
updated in `src/FileFormats/MOF/MOF.jl`
- [ ] The file at `src/FileFormats/MOF/mof.schema.json` is updated
- [ ] The constant `_SUPPORTED_VERSIONS` is updated in
`src/FileFormats/MOF/MOF.jl`
## New sets
Expand Down
2 changes: 1 addition & 1 deletion docs/src/submodules/FileFormats/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ julia> print(read("file.mof.json", String))
"name": "MathOptFormat Model",
"version": {
"major": 1,
"minor": 5
"minor": 6
},
"variables": [
{
Expand Down
41 changes: 30 additions & 11 deletions src/FileFormats/MOF/MOF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,26 @@ import OrderedCollections: OrderedDict
import JSON
import MathOptInterface as MOI

const SCHEMA_PATH = joinpath(@__DIR__, "mof.1.5.schema.json")
const VERSION = v"1.5"
const SUPPORTED_VERSIONS =
(v"1.5", v"1.4", v"1.3", v"1.2", v"1.1", v"1.0", v"0.6", v"0.5", v"0.4")
"""
SCHEMA_PATH::String
The path to the latest version of the MathOptFormat schema supported by
MathOptInterface.
"""
const SCHEMA_PATH = joinpath(@__DIR__, "mof.schema.json")

const _SUPPORTED_VERSIONS = (
v"1.6",
v"1.5",
v"1.4",
v"1.3",
v"1.2",
v"1.1",
v"1.0",
v"0.6",
v"0.5",
v"0.4",
)

const OrderedObject = OrderedDict{String,Any}
const UnorderedObject = Dict{String,Any}
Expand Down Expand Up @@ -84,29 +100,30 @@ MOI.Utilities.@model(
MOI.BinPacking,
MOI.Table,
),
(Nonlinear,),
(Nonlinear, MOI.ScalarNonlinearFunction),
(MOI.ScalarAffineFunction, MOI.ScalarQuadraticFunction),
(MOI.VectorOfVariables,),
(MOI.VectorOfVariables, MOI.VectorNonlinearFunction),
(MOI.VectorAffineFunction, MOI.VectorQuadraticFunction)
)

# Indicator is handled by UniversalFallback.
# Reified is handled by UniversalFallback.
# Scaled is handled bby UniversalFallback.
# Scaled is handled by UniversalFallback.

const Model = MOI.Utilities.UniversalFallback{InnerModel{Float64}}

struct Options
print_compact::Bool
warn::Bool
differentiation_backend::MOI.Nonlinear.AbstractAutomaticDifferentiation
parse_as_nlpblock::Bool
end

function get_options(m::Model)
return get(
m.model.ext,
:MOF_OPTIONS,
Options(false, false, MOI.Nonlinear.SparseReverseMode()),
Options(false, false, MOI.Nonlinear.SparseReverseMode(), true),
)
end

Expand All @@ -123,15 +140,19 @@ Keyword arguments are:
- `differentiation_backend::MOI.Nonlinear.AbstractAutomaticDifferentiation = MOI.Nonlinear.SparseReverseMode()`:
automatic differentiation backend to use when reading models with nonlinear
constraints and objectives.
- `parse_as_nlpblock::Bool=true`: if `true` parse `"ScalarNonlinearFunction"`
into an `MOI.NLPBlock`. If `false`, `"ScalarNonlinearFunction"` are parsed as
`MOI.ScalarNonlinearFunction` functions.
"""
function Model(;
print_compact::Bool = false,
warn::Bool = false,
differentiation_backend::MOI.Nonlinear.AbstractAutomaticDifferentiation = MOI.Nonlinear.SparseReverseMode(),
parse_as_nlpblock::Bool = true,
)
model = MOI.Utilities.UniversalFallback(InnerModel{Float64}())
model.model.ext[:MOF_OPTIONS] =
Options(print_compact, warn, differentiation_backend)
Options(print_compact, warn, differentiation_backend, parse_as_nlpblock)
return model
end

Expand All @@ -140,8 +161,6 @@ function Base.show(io::IO, ::Model)
return
end

include("nonlinear.jl")

include("read.jl")
include("write.jl")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json-schema.org/schema#",
"$id": "https://jump.dev/MathOptFormat/schemas/mof.1.5.schema.json",
"$id": "https://jump.dev/MathOptFormat/schemas/mof.1.6.schema.json",
"title": "The schema for MathOptFormat",
"type": "object",
"required": ["version", "variables", "objective", "constraints"],
Expand All @@ -11,7 +11,7 @@
"required": ["minor", "major"],
"properties": {
"minor": {
"enum": [0, 1, 2, 3, 4, 5]
"enum": [0, 1, 2, 3, 4, 5, 6]
},
"major": {
"const": 1
Expand Down Expand Up @@ -199,9 +199,78 @@
"properties": {
"type": {
"enum": [
"log", "log10", "exp", "sqrt", "floor", "ceil",
"abs", "cos", "sin", "tan", "acos", "asin", "atan",
"cosh", "sinh", "tanh", "acosh", "asinh", "atanh"
"abs",
"sqrt",
"cbrt",
"abs2",
"inv",
"log",
"log10",
"log2",
"log1p",
"exp",
"exp2",
"expm1",
"sin",
"cos",
"tan",
"sec",
"csc",
"cot",
"sind",
"cosd",
"tand",
"secd",
"cscd",
"cotd",
"asin",
"acos",
"atan",
"asec",
"acsc",
"acot",
"asind",
"acosd",
"atand",
"asecd",
"acscd",
"acotd",
"sinh",
"cosh",
"tanh",
"sech",
"csch",
"coth",
"asinh",
"acosh",
"atanh",
"asech",
"acsch",
"acoth",
"deg2rad",
"rad2deg",
"erf",
"erfinv",
"erfc",
"erfcinv",
"erfi",
"gamma",
"lgamma",
"digamma",
"invdigamma",
"trigamma",
"airyai",
"airybi",
"airyaiprime",
"airybiprime",
"besselj0",
"besselj1",
"bessely0",
"bessely1",
"erfcx",
"dawson",
"floor",
"ceil"
]
},
"args": {
Expand All @@ -218,7 +287,18 @@
"required": ["args"],
"properties": {
"type": {
"enum": ["/", "^"]
"enum": [
"/",
"^",
"atan",
"&&",
"||",
"<=",
"<",
">=",
">",
"=="
]
},
"args": {
"type": "array",
Expand All @@ -234,7 +314,7 @@
"required": ["args"],
"properties": {
"type": {
"enum": ["+", "-", "*", "min", "max"]
"enum": ["+", "-", "*", "ifelse", "min", "max"]
},
"args": {
"type": "array",
Expand Down Expand Up @@ -441,6 +521,26 @@
}
}
}
}, {
"description": "The vector-valued nonlinear function `f(x)`, comprised of a vector of `ScalarNonlinearFunction`.",
"required": ["rows", "node_list"],
"properties": {
"type": {
"const": "VectorNonlinearFunction"
},
"rows": {
"type": "array",
"items": {
"$ref": "#/definitions/NonlinearTerm"
}
},
"node_list": {
"type": "array",
"items": {
"$ref": "#/definitions/NonlinearTerm"
}
}
}
}]
},
"scalar_sets": {
Expand Down
Loading

0 comments on commit b07f71e

Please sign in to comment.