-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathMOF.jl
More file actions
199 lines (175 loc) · 5.27 KB
/
MOF.jl
File metadata and controls
199 lines (175 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Copyright (c) 2017: Miles Lubin and contributors
# Copyright (c) 2017: Google Inc.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
module MOF
import ..FileFormats
import JSON
import MathOptInterface as MOI
"""
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.7",
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",
)
function _parse_mof_version(version)
return VersionNumber(version["major"], version["minor"])
end
struct Nonlinear <: MOI.AbstractScalarFunction
expr::Expr
end
Base.copy(nonlinear::Nonlinear) = Nonlinear(copy(nonlinear.expr))
MOI.Utilities.canonicalize!(nonlinear::Nonlinear) = nonlinear
MOI.Utilities.@model(
InnerModel,
(MOI.ZeroOne, MOI.Integer),
(
MOI.EqualTo,
MOI.GreaterThan,
MOI.LessThan,
MOI.Interval,
MOI.Semicontinuous,
MOI.Semiinteger,
MOI.Parameter,
),
(
MOI.Reals,
MOI.Zeros,
MOI.Nonnegatives,
MOI.Nonpositives,
MOI.HyperRectangle,
MOI.SecondOrderCone,
MOI.RotatedSecondOrderCone,
MOI.GeometricMeanCone,
MOI.ExponentialCone,
MOI.DualExponentialCone,
MOI.NormOneCone,
MOI.NormInfinityCone,
MOI.NormCone,
MOI.NormSpectralCone,
MOI.NormNuclearCone,
MOI.RelativeEntropyCone,
MOI.RootDetConeTriangle,
MOI.RootDetConeSquare,
MOI.LogDetConeTriangle,
MOI.LogDetConeSquare,
MOI.PositiveSemidefiniteConeTriangle,
MOI.Scaled{MOI.PositiveSemidefiniteConeTriangle},
MOI.PositiveSemidefiniteConeSquare,
MOI.HermitianPositiveSemidefiniteConeTriangle,
MOI.AllDifferent,
MOI.Circuit,
MOI.CountAtLeast,
MOI.CountBelongs,
MOI.CountDistinct,
MOI.CountGreaterThan,
MOI.Cumulative,
MOI.Path,
),
(
MOI.PowerCone,
MOI.DualPowerCone,
MOI.SOS1,
MOI.SOS2,
MOI.BinPacking,
MOI.Table,
),
(Nonlinear, MOI.ScalarNonlinearFunction),
(MOI.ScalarAffineFunction, MOI.ScalarQuadraticFunction),
(MOI.VectorOfVariables, MOI.VectorNonlinearFunction),
(MOI.VectorAffineFunction, MOI.VectorQuadraticFunction)
)
# Indicator is handled by UniversalFallback.
# Reified is handled by UniversalFallback.
# Scaled is handled by UniversalFallback.
const Model{T} = MOI.Utilities.UniversalFallback{InnerModel{T}}
struct Options
print_compact::Bool
warn::Bool
differentiation_backend::MOI.Nonlinear.AbstractAutomaticDifferentiation
use_nlp_block::Union{Bool,Nothing}
generic_names::Bool
end
function get_options(m::Model)
return get(
m.model.ext,
:MOF_OPTIONS,
Options(
false,
false,
MOI.Nonlinear.SparseReverseMode(),
nothing,
false,
),
)
end
"""
Model(;
print_compact::Bool = false,
warn::Bool = false,
differentiation_backend::MOI.Nonlinear.AbstractAutomaticDifferentiation =
MOI.Nonlinear.SparseReverseMode(),
use_nlp_block::Union{Bool,Nothing} = nothing,
coefficient_type::Type{T} = Float64,
generic_names::Bool = false,
) where {T}
Create an empty instance of FileFormats.MOF.Model.
Keyword arguments are:
- `print_compact::Bool=false`: print the JSON file in a compact format without
spaces or newlines.
- `warn::Bool=false`: print a warning when variables or constraints are renamed
- `differentiation_backend::MOI.Nonlinear.AbstractAutomaticDifferentiation = MOI.Nonlinear.SparseReverseMode()`:
automatic differentiation backend to use when reading models with nonlinear
constraints and objectives.
`use_nlp_block::Bool=true`: if `true` parse `"ScalarNonlinearFunction"`
into an `MOI.NLPBlock`. If `false`, `"ScalarNonlinearFunction"` are parsed as
`MOI.ScalarNonlinearFunction` functions.
- `coefficient_type::Type{T} = Float64`: the supported type to use when reading
and writing files.
- `generic_names::Bool=false`: strip all names in the model and replace them
with the generic names `C\$i` and `R\$i` for the i'th column and row
respectively.
"""
function Model(;
print_compact::Bool = false,
warn::Bool = false,
differentiation_backend::MOI.Nonlinear.AbstractAutomaticDifferentiation = MOI.Nonlinear.SparseReverseMode(),
use_nlp_block::Union{Bool,Nothing} = nothing,
coefficient_type::Type{T} = Float64,
generic_names::Bool = false,
kwargs...,
) where {T}
if !isempty(kwargs)
error(
"The MOF file format does not support the keyword arguments: ",
kwargs...,
)
end
model = MOI.Utilities.UniversalFallback(InnerModel{T}())
model.model.ext[:MOF_OPTIONS] = Options(
print_compact,
warn,
differentiation_backend,
use_nlp_block,
generic_names,
)
return model
end
Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.MOF.Model")
include("read.jl")
include("write.jl")
end