|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from corneto._graph import EdgeType, Graph |
| 4 | +from corneto.backend import PicosBackend |
| 5 | + |
| 6 | + |
| 7 | +def _build_acyclic_problem(backend, graph, lb, ub): |
| 8 | + problem = backend.Flow(graph, lb=lb, ub=ub) |
| 9 | + problem += backend.NonZeroIndicator(problem.expr._flow, tolerance=1e-6) |
| 10 | + problem = backend.Acyclic( |
| 11 | + graph, |
| 12 | + problem, |
| 13 | + indicator_positive_var_name="_flow_ipos", |
| 14 | + indicator_negative_var_name="_flow_ineg", |
| 15 | + ) |
| 16 | + return problem |
| 17 | + |
| 18 | + |
| 19 | +def _solve_and_assert_infeasible(problem, backend): |
| 20 | + if isinstance(backend, PicosBackend): |
| 21 | + problem.solve(solver="glpk", primals=False) |
| 22 | + else: |
| 23 | + problem.solve() |
| 24 | + assert problem.expr._flow.value is None |
| 25 | + |
| 26 | + |
| 27 | +def test_acyclic_blocks_forced_directed_2cycle(backend): |
| 28 | + graph = Graph() |
| 29 | + graph.add_edge("A", "B", type=EdgeType.DIRECTED) |
| 30 | + graph.add_edge("B", "A", type=EdgeType.DIRECTED) |
| 31 | + |
| 32 | + problem = _build_acyclic_problem(backend, graph, lb=0, ub=10) |
| 33 | + problem += problem.expr._flow_ipos[0] == 1 |
| 34 | + problem += problem.expr._flow_ipos[1] == 1 |
| 35 | + |
| 36 | + _solve_and_assert_infeasible(problem, backend) |
| 37 | + |
| 38 | + |
| 39 | +def test_acyclic_blocks_forced_directed_plus_reversible_2cycle(backend): |
| 40 | + graph = Graph() |
| 41 | + graph.add_edge("A", "B", type=EdgeType.DIRECTED) |
| 42 | + graph.add_edge("A", "B", type=EdgeType.UNDIRECTED) |
| 43 | + |
| 44 | + lb = np.array([0, -10]) |
| 45 | + ub = np.array([10, 10]) |
| 46 | + problem = _build_acyclic_problem(backend, graph, lb=lb, ub=ub) |
| 47 | + problem += problem.expr._flow_ipos[0] == 1 |
| 48 | + problem += problem.expr._flow_ineg[1] == 1 |
| 49 | + |
| 50 | + _solve_and_assert_infeasible(problem, backend) |
| 51 | + |
| 52 | + |
| 53 | +def test_acyclic_blocks_forced_mixed_3cycle(backend): |
| 54 | + graph = Graph() |
| 55 | + graph.add_edge("A", "B", type=EdgeType.DIRECTED) |
| 56 | + graph.add_edge("B", "C", type=EdgeType.DIRECTED) |
| 57 | + graph.add_edge("A", "C", type=EdgeType.UNDIRECTED) |
| 58 | + |
| 59 | + lb = np.array([0, 0, -10]) |
| 60 | + ub = np.array([10, 10, 10]) |
| 61 | + problem = _build_acyclic_problem(backend, graph, lb=lb, ub=ub) |
| 62 | + # Force A->B, B->C and C->A (negative use of A<->C). |
| 63 | + problem += problem.expr._flow_ipos[0] == 1 |
| 64 | + problem += problem.expr._flow_ipos[1] == 1 |
| 65 | + problem += problem.expr._flow_ineg[2] == 1 |
| 66 | + |
| 67 | + _solve_and_assert_infeasible(problem, backend) |
| 68 | + |
| 69 | + |
| 70 | +def test_acyclic_enforces_constraints_per_sample(backend): |
| 71 | + graph = Graph() |
| 72 | + graph.add_edge("A", "B", type=EdgeType.DIRECTED) |
| 73 | + graph.add_edge("A", "B", type=EdgeType.UNDIRECTED) |
| 74 | + |
| 75 | + lb = np.array([0, -10]) |
| 76 | + ub = np.array([10, 10]) |
| 77 | + problem = backend.Flow(graph, lb=lb, ub=ub, n_flows=2) |
| 78 | + problem += backend.NonZeroIndicator(problem.expr._flow, tolerance=1e-6) |
| 79 | + problem = backend.Acyclic( |
| 80 | + graph, |
| 81 | + problem, |
| 82 | + indicator_positive_var_name="_flow_ipos", |
| 83 | + indicator_negative_var_name="_flow_ineg", |
| 84 | + ) |
| 85 | + |
| 86 | + # Force A->B and B->A (negative use of reversible edge) only in sample 0. |
| 87 | + problem += problem.expr._flow_ipos[0, 0] == 1 |
| 88 | + problem += problem.expr._flow_ineg[1, 0] == 1 |
| 89 | + |
| 90 | + _solve_and_assert_infeasible(problem, backend) |
0 commit comments