@@ -151,39 +151,99 @@ function compute_conflict!(optimizer::AbstractOptimizer)
151
151
end
152
152
153
153
"""
154
- write_to_file(model::ModelLike, filename::String)
154
+ write_to_file(model::ModelLike, filename::String; kwargs... )
155
155
156
156
Write the current model to the file at `filename`.
157
157
158
158
Supported file types depend on the model type.
159
+
160
+ Additional keyword arguments are passed to the `Model` constructor of the
161
+ relevant file format. See, for example, [`FileFormats.LP.Model`](@ref) and
162
+ [`FileFormats.MPS.Model`](@ref).
163
+
164
+ ## Example
165
+
166
+ ```jldoctest
167
+ julia> model = MOI.Utilities.Model{Int}();
168
+
169
+ julia> x, _ = MOI.add_constrained_variable(model, MOI.Interval(2, 3));
170
+
171
+ julia> MOI.set(model, MOI.VariableName(), x, "x");
172
+
173
+ julia> filename = joinpath(tempdir(), "model.lp");
174
+
175
+ julia> MOI.write_to_file(model, filename; coefficient_type = Int);
176
+
177
+ julia> print(read(filename, String))
178
+ minimize
179
+ obj:
180
+ subject to
181
+ Bounds
182
+ 2 <= x <= 3
183
+ End
184
+ ```
159
185
"""
160
- function write_to_file (model:: ModelLike , filename:: String )
161
- dest = FileFormats. Model (; filename = filename )
186
+ function write_to_file (model:: ModelLike , filename:: String ; kwargs ... )
187
+ dest = FileFormats. Model (; filename, kwargs ... )
162
188
copy_to (dest, model)
163
189
write_to_file (dest, filename)
164
190
return
165
191
end
166
192
167
193
"""
168
- read_from_file(model::ModelLike, filename::String)
194
+ read_from_file(model::ModelLike, filename::String; kwargs... )
169
195
170
196
Read the file `filename` into the model `model`. If `model` is non-empty, this
171
197
may throw an error.
172
198
173
199
Supported file types depend on the model type.
174
200
201
+ Additional keyword arguments are passed to the `Model` constructor of the
202
+ relevant file format. See, for example, [`FileFormats.LP.Model`](@ref) and
203
+ [`FileFormats.MPS.Model`](@ref).
204
+
175
205
### Note
176
206
177
- Once the contents of the file are loaded into the model, users can query the
178
- variables via `get(model, ListOfVariableIndices())`. However, some filetypes,
179
- such as LP files, do not maintain an explicit ordering of the variables.
180
- Therefore, the returned list may be in an arbitrary order.
207
+ Once the contents of the file are loaded into the model, you can query the
208
+ variables via `MOI. get(model, MOI. ListOfVariableIndices())`. However, some
209
+ filetypes, such as LP files, do not maintain an explicit ordering of the
210
+ variables. Therefore, the returned list may be in an arbitrary order.
181
211
182
212
To avoid depending on the order of the indices, look up each variable index by
183
- name using `get(model, VariableIndex, "name")`.
213
+ name using `MOI.get(model, MOI.VariableIndex, "name")`.
214
+
215
+ ## Example
216
+
217
+ ```jldoctest
218
+ julia> model = MOI.Utilities.Model{Int}();
219
+
220
+ julia> x, _ = MOI.add_constrained_variable(model, MOI.GreaterThan(2));
221
+
222
+ julia> MOI.set(model, MOI.VariableName(), x, "x");
223
+
224
+ julia> filename = joinpath(tempdir(), "model.lp");
225
+
226
+ julia> MOI.write_to_file(model, filename; coefficient_type = Int);
227
+
228
+ julia> new_model = MOI.Utilities.Model{Int}();
229
+
230
+ julia> MOI.read_from_file(new_model, filename; coefficient_type = Int);
231
+
232
+ julia> print(new_model)
233
+ Minimize ScalarAffineFunction{Int64}:
234
+ (0)
235
+
236
+ Subject to:
237
+
238
+ VariableIndex-in-GreaterThan{Int64}
239
+ x >= (2)
240
+
241
+ julia> MOI.get(new_model, MOI.VariableIndex, "x")
242
+ MOI.VariableIndex(1)
243
+ ```
184
244
"""
185
- function read_from_file (model:: ModelLike , filename:: String )
186
- src = FileFormats. Model (; filename = filename )
245
+ function read_from_file (model:: ModelLike , filename:: String ; kwargs ... )
246
+ src = FileFormats. Model (; filename, kwargs ... )
187
247
read_from_file (src, filename)
188
248
copy_to (model, src)
189
249
return
0 commit comments