-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathMPS.jl
More file actions
209 lines (177 loc) · 5.82 KB
/
MPS.jl
File metadata and controls
209 lines (177 loc) · 5.82 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
200
201
202
203
204
205
206
207
208
209
# 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 MPS
import ..FileFormats
import MathOptInterface as MOI
import OrderedCollections: OrderedDict
const IndicatorLessThanTrue{T} =
MOI.Indicator{MOI.ACTIVATE_ON_ONE,MOI.LessThan{T}}
const IndicatorGreaterThanTrue{T} =
MOI.Indicator{MOI.ACTIVATE_ON_ONE,MOI.GreaterThan{T}}
const IndicatorEqualToTrue{T} =
MOI.Indicator{MOI.ACTIVATE_ON_ONE,MOI.EqualTo{T}}
const IndicatorLessThanFalse{T} =
MOI.Indicator{MOI.ACTIVATE_ON_ZERO,MOI.LessThan{T}}
const IndicatorGreaterThanFalse{T} =
MOI.Indicator{MOI.ACTIVATE_ON_ZERO,MOI.GreaterThan{T}}
const IndicatorEqualToFalse{T} =
MOI.Indicator{MOI.ACTIVATE_ON_ZERO,MOI.EqualTo{T}}
MOI.Utilities.@model(
Model,
(MOI.ZeroOne, MOI.Integer),
(MOI.EqualTo, MOI.GreaterThan, MOI.LessThan, MOI.Interval),
(),
(
MOI.SOS1,
MOI.SOS2,
IndicatorLessThanTrue,
IndicatorLessThanFalse,
IndicatorGreaterThanTrue,
IndicatorGreaterThanFalse,
IndicatorEqualToTrue,
IndicatorEqualToFalse,
),
(),
(MOI.ScalarAffineFunction, MOI.ScalarQuadraticFunction),
(MOI.VectorOfVariables,),
(MOI.VectorAffineFunction,)
)
function MOI.supports_constraint(
::Model{T},
::Type{MOI.VectorAffineFunction{T}},
::Type{<:Union{MOI.SOS1{T},MOI.SOS2{T}}},
) where {T}
return false
end
function MOI.supports_constraint(
::Model,
::Type{MOI.VariableIndex},
::Type{<:Union{MOI.Parameter,MOI.Semicontinuous,MOI.Semiinteger}},
)
return false
end
function MOI.supports_constraint(
::Model{T},
::Type{MOI.VectorOfVariables},
::Type{
<:Union{
IndicatorLessThanTrue{T},
IndicatorLessThanFalse{T},
IndicatorGreaterThanTrue{T},
IndicatorGreaterThanFalse{T},
IndicatorEqualToTrue{T},
IndicatorEqualToFalse{T},
},
},
) where {T}
return false
end
MOI.supports(::Model, ::MOI.ObjectiveFunction) = false
function MOI.supports(
::Model{T},
::MOI.ObjectiveFunction{
<:Union{
MOI.VariableIndex,
MOI.ScalarAffineFunction{T},
MOI.ScalarQuadraticFunction{T},
},
},
) where {T}
return true
end
@enum(
QuadraticFormat,
kQuadraticFormatCPLEX,
kQuadraticFormatGurobi,
kQuadraticFormatMosek,
)
struct Options
warn::Bool
objsense::Bool
generic_names::Bool
quadratic_format::QuadraticFormat
end
function get_options(m::Model)::Options
return get(
m.ext,
:MPS_OPTIONS,
Options(false, false, false, kQuadraticFormatGurobi),
)
end
"""
Model(;
warn::Bool = false,
print_objsense::Bool = false,
generic_names::Bool = false,
quadratic_format::QuadraticFormat = kQuadraticFormatGurobi,
coefficient_type::Type{T} = Float64,
) where {T}
Create an empty instance of FileFormats.MPS.Model.
Keyword arguments are:
- `warn::Bool=false`: print a warning when variables or constraints are renamed.
- `print_objsense::Bool=false`: print the OBJSENSE section when writing
- `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.
- `quadratic_format::QuadraticFormat = kQuadraticFormatGurobi`: specify the
solver-specific extension used when writing the quadratic components of the
model. Options are `kQuadraticFormatGurobi`, `kQuadraticFormatCPLEX`, and
`kQuadraticFormatMosek`.
- `coefficient_type::Type{T}=Float64`: the supported type to use when reading
and writing files.
"""
function Model(;
warn::Bool = false,
print_objsense::Bool = false,
generic_names::Bool = false,
quadratic_format::QuadraticFormat = kQuadraticFormatGurobi,
coefficient_type::Type{T} = Float64,
) where {T}
model = Model{T}()
model.ext[:MPS_OPTIONS] =
Options(warn, print_objsense, generic_names, quadratic_format)
return model
end
Base.summary(io::IO, ::Model) = print(io, "MOI.FileFormats.MPS.Model")
@enum(VType, VTYPE_CONTINUOUS, VTYPE_INTEGER, VTYPE_BINARY)
include("read.jl")
include("write.jl")
import PrecompileTools
PrecompileTools.@setup_workload begin
PrecompileTools.@compile_workload begin
let
model = Model()
x = MOI.add_variables(model, 4)
for i in 1:4
MOI.set(model, MOI.VariableName(), x[i], "x[$i]")
end
MOI.add_constraint(model, x[1], MOI.LessThan(1.0))
MOI.add_constraint(model, x[2], MOI.GreaterThan(1.0))
MOI.add_constraint(model, x[3], MOI.EqualTo(1.2))
MOI.add_constraint(model, x[4], MOI.Interval(-1.0, 100.0))
MOI.add_constraint(model, x[1], MOI.ZeroOne())
MOI.add_constraint(model, x[2], MOI.Integer())
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = 1.0 * x[1] + 2.0 * x[2] + 3.0
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.add_constraint(model, 1.5 * x[1], MOI.LessThan(1.0))
MOI.add_constraint(model, 1.5 * x[2], MOI.GreaterThan(1.0))
MOI.add_constraint(model, 1.5 * x[3], MOI.EqualTo(1.2))
MOI.add_constraint(model, 1.5 * x[4], MOI.Interval(-1.0, 100.0))
io = IOBuffer()
write(io, model)
end
end
end
# Originally removed by #2421, but this constant was used by extensions like
# BilevelJuMP so I'm re-adding to maintain backwards compatibility.
const SET_TYPES = (
(MOI.LessThan{Float64}, "L"),
(MOI.GreaterThan{Float64}, "G"),
(MOI.EqualTo{Float64}, "E"),
(MOI.Interval{Float64}, "L"),
)
end # module MPS