|
| 1 | +# ___________________________________________________________________________ |
| 2 | +# |
| 3 | +# JuMPIn.jl: JuMP Incidence Graph Analysis |
| 4 | +# Copyright (c) 2023. Triad National Security, LLC. All rights reserved. |
| 5 | +# |
| 6 | +# This program was produced under U.S. Government contract 89233218CNA000001 |
| 7 | +# for Los Alamos National Laboratory (LANL), which is operated by Triad |
| 8 | +# National Security, LLC for the U.S. Department of Energy/National Nuclear |
| 9 | +# Security Administration. All rights in the program are reserved by Triad |
| 10 | +# National Security, LLC, and the U.S. Department of Energy/National Nuclear |
| 11 | +# Security Administration. The Government is granted for itself and others |
| 12 | +# acting on its behalf a nonexclusive, paid-up, irrevocable worldwide license |
| 13 | +# in this material to reproduce, prepare derivative works, distribute copies |
| 14 | +# to the public, perform publicly and display publicly, and to permit others |
| 15 | +# to do so. |
| 16 | +# |
| 17 | +# This software is distributed under the 3-clause BSD license. |
| 18 | +# ___________________________________________________________________________ |
| 19 | + |
| 20 | +module TestIncidenceMatrix |
| 21 | + |
| 22 | +import JuMP as jmp |
| 23 | +import MathOptInterface as moi |
| 24 | +import SparseArrays |
| 25 | +using Test: @test, @test_throws |
| 26 | + |
| 27 | +import JuMPIn as ji |
| 28 | + |
| 29 | +include("models.jl") # Models |
| 30 | +using .Models: make_degenerate_flow_model |
| 31 | + |
| 32 | + |
| 33 | +function test_incidence_matrix_from_constraints_and_variables() |
| 34 | + m = jmp.Model() |
| 35 | + @jmp.variable(m, x[1:3]) |
| 36 | + @jmp.constraint(m, eq1, 2*x[1] + 3*x[2] == 4) |
| 37 | + @jmp.NLconstraint(m, eq2, 2*x[3]^1.5*x[2] == 1) |
| 38 | + constraints = [eq1, eq2] |
| 39 | + variables = [x[1], x[2], x[3]] |
| 40 | + imat = ji.incidence_matrix(constraints, variables) |
| 41 | + pred_row = [1, 1, 2, 2] |
| 42 | + pred_col = [1, 2, 2, 3] |
| 43 | + pred_val = [1.0, 1.0, 1.0, 1.0] |
| 44 | + m = 2 |
| 45 | + n = 3 |
| 46 | + pred_mat = SparseArrays.sparse(pred_row, pred_col, pred_val, m, n) |
| 47 | + @test imat == pred_mat |
| 48 | +end |
| 49 | + |
| 50 | +function test_incidence_matrix_from_incidence_graph() |
| 51 | + m = jmp.Model() |
| 52 | + @jmp.variable(m, x[1:3]) |
| 53 | + @jmp.constraint(m, eq1, 2*x[1] + 3*x[2] == 4) |
| 54 | + @jmp.NLconstraint(m, eq2, 2*x[3]^1.5*x[2] == 1) |
| 55 | + constraints = [eq1, eq2] |
| 56 | + variables = [x[3], x[2], x[1]] |
| 57 | + igraph = ji.IncidenceGraphInterface(constraints, variables) |
| 58 | + imat = ji.incidence_matrix(igraph) |
| 59 | + pred_row = [1, 1, 2, 2] |
| 60 | + pred_col = [3, 2, 2, 1] |
| 61 | + pred_val = [1.0, 1.0, 1.0, 1.0] |
| 62 | + m = 2 |
| 63 | + n = 3 |
| 64 | + pred_mat = SparseArrays.sparse(pred_row, pred_col, pred_val, m, n) |
| 65 | + @test imat == pred_mat |
| 66 | +end |
| 67 | + |
| 68 | + |
| 69 | +function runtests() |
| 70 | + test_incidence_matrix_from_constraints_and_variables() |
| 71 | + test_incidence_matrix_from_incidence_graph() |
| 72 | + return |
| 73 | +end |
| 74 | + |
| 75 | +end # module TestIncidenceGraph |
| 76 | + |
| 77 | +if abspath(PROGRAM_FILE) == @__FILE__ |
| 78 | + TestIncidenceMatrix.runtests() |
| 79 | +end |
0 commit comments