|
| 1 | +# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | + |
| 6 | +using JuMP |
| 7 | +using MathOptFormat |
| 8 | +using Test |
| 9 | + |
| 10 | +@testset "File formats" begin |
| 11 | + @testset "MOF" begin |
| 12 | + model = Model() |
| 13 | + @variable(model, x) |
| 14 | + @constraint(model, my_c, 3 * x >= 1) |
| 15 | + @objective(model, Min, 2 * x^2 + x + 1) |
| 16 | + write_to_file(model, "my_model.mof.json") |
| 17 | + model_2 = read_from_file("my_model.mof.json") |
| 18 | + @test sprint(print, model) == sprint(print, model_2) |
| 19 | + rm("my_model.mof.json") |
| 20 | + end |
| 21 | + @testset "MPS" begin |
| 22 | + model = Model() |
| 23 | + @variable(model, x >= 0) |
| 24 | + @constraint(model, my_c, 3 * x >= 1) |
| 25 | + @objective(model, Min, 2 * x) |
| 26 | + write_to_file(model, "my_model.mps") |
| 27 | + model_2 = read_from_file("my_model.mps") |
| 28 | + @test sprint(print, model) == sprint(print, model_2) |
| 29 | + rm("my_model.mps") |
| 30 | + end |
| 31 | + @testset "LP" begin |
| 32 | + model = Model() |
| 33 | + @variable(model, x >= 0) |
| 34 | + @constraint(model, my_c, 3 * x >= 1) |
| 35 | + @objective(model, Min, 2 * x) |
| 36 | + write_to_file(model, "my_model.lp") |
| 37 | + @test read("my_model.lp", String) == |
| 38 | + "minimize\nobj: 2 x\nsubject to\nmy_c: 3 x >= 1\nBounds\nx >= 0\nEnd\n" |
| 39 | + @test_throws( |
| 40 | + ErrorException("read! is not implemented for LP files."), |
| 41 | + read_from_file("my_model.lp") |
| 42 | + ) |
| 43 | + rm("my_model.lp") |
| 44 | + end |
| 45 | + @testset "CBF" begin |
| 46 | + model = Model() |
| 47 | + @variable(model, X[1:2, 1:2], PSD) |
| 48 | + @constraint(model, my_c, sum(X) >= 1) |
| 49 | + @objective(model, Min, sum(X)) |
| 50 | + write_to_file(model, "my_model.cbf") |
| 51 | + @test read("my_model.cbf", String) == |
| 52 | + "VER\n3\n\nOBJSENSE\nMIN\n\nVAR\n3 1\nF 3\n\nOBJACOORD\n3\n0 1.0\n1 2.0\n2 1.0\n\nCON\n1 1\nL+ 1\n\nACOORD\n3\n0 0 1.0\n0 1 2.0\n0 2 1.0\n\nBCOORD\n1\n0 -1.0\n\nPSDCON\n1\n2\n\nHCOORD\n3\n0 0 0 0 1.0\n0 1 1 0 1.0\n0 2 1 1 1.0\n\n" |
| 53 | + model_2 = read_from_file("my_model.cbf") |
| 54 | + # Note: we replace ' in ' => ' ∈ ' because the unicode doesn't print on |
| 55 | + # Windows systems for some reason. |
| 56 | + @test replace(sprint(print, model_2), " in " => " ∈ ") == |
| 57 | + "Min noname + 2 noname + noname\nSubject to\n [noname + 2 noname + noname - 1] ∈ MathOptInterface.Nonnegatives(1)\n [noname, noname, noname] ∈ MathOptInterface.PositiveSemidefiniteConeTriangle(2)\n" |
| 58 | + rm("my_model.cbf") |
| 59 | + end |
| 60 | + @testset "Base read/write via io" begin |
| 61 | + model = Model() |
| 62 | + @variable(model, x) |
| 63 | + @constraint(model, my_c, 3 * x >= 1) |
| 64 | + @objective(model, Min, 2 * x^2 + x + 1) |
| 65 | + io = IOBuffer() |
| 66 | + @test_throws( |
| 67 | + ErrorException("Unable to infer the file format from an IO stream."), |
| 68 | + write(io, model; format = MathOptFormat.FORMAT_AUTOMATIC) |
| 69 | + ) |
| 70 | + write(io, model; format = MathOptFormat.FORMAT_MOF) |
| 71 | + seekstart(io) |
| 72 | + @test_throws( |
| 73 | + ErrorException("Unable to infer the file format from an IO stream."), |
| 74 | + read(io, Model; format = MathOptFormat.FORMAT_AUTOMATIC) |
| 75 | + ) |
| 76 | + seekstart(io) |
| 77 | + model_2 = read(io, Model; format = MathOptFormat.FORMAT_MOF) |
| 78 | + @test sprint(print, model) == sprint(print, model_2) |
| 79 | + end |
| 80 | +end |
0 commit comments