Skip to content

Commit 5f4c727

Browse files
committed
Added tests for coverage of the case when there are mixtures of scalar and vector constraints
1 parent 26d2c15 commit 5f4c727

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

testing/problem/optimization_problem_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,57 @@ func TestOptimizationProblem_LinearInequalityConstraintMatrices5(t *testing.T) {
14591459
}
14601460
}
14611461

1462+
/*
1463+
TestOptimizationProblem_LinearInequalityConstraintMatrices6
1464+
Description:
1465+
1466+
Tests the LinearInequalityConstraintMatrices function with a simple problem
1467+
that contains a mixture of scalar and vector inequality constraints.
1468+
The problem will have:
1469+
- a constant objective
1470+
- 3 variables,
1471+
- a single vector linear inequality constraint,
1472+
- and a single scalar linear inequality constraint.
1473+
The result should be a matrix with 4 rows and 3 columns.
1474+
*/
1475+
func TestOptimizationProblem_LinearInequalityConstraintMatrices6(t *testing.T) {
1476+
// Constants
1477+
p1 := problem.NewProblem("TestOptimizationProblem_LinearInequalityConstraintMatrices6")
1478+
vv1 := p1.AddVariableVector(3)
1479+
c1 := vv1.LessEq(symbolic.OnesVector(3))
1480+
c2 := vv1.AtVec(0).LessEq(1.0)
1481+
1482+
p1.Constraints = append(p1.Constraints, c1)
1483+
p1.Constraints = append(p1.Constraints, c2)
1484+
1485+
// Create good objective
1486+
p1.Objective = *problem.NewObjective(
1487+
symbolic.K(3.14),
1488+
problem.SenseMaximize,
1489+
)
1490+
1491+
// Algorithm
1492+
A, b := p1.LinearInequalityConstraintMatrices()
1493+
1494+
// Check that the number of rows is as expected.
1495+
if A.Dims()[0] != 4 {
1496+
t.Errorf("expected the number of rows to be %v; received %v",
1497+
4, A.Dims()[0])
1498+
}
1499+
1500+
// Check that the number of columns is as expected.
1501+
if A.Dims()[1] != 3 {
1502+
t.Errorf("expected the number of columns to be %v; received %v",
1503+
3, A.Dims()[1])
1504+
}
1505+
1506+
// Check that the number of elements in b is as expected.
1507+
if len(b) != 4 {
1508+
t.Errorf("expected the number of elements in b to be %v; received %v",
1509+
4, len(b))
1510+
}
1511+
}
1512+
14621513
/*
14631514
TestOptimizationProblem_LinearEqualityConstraintMatrices1
14641515
Description:
@@ -1703,3 +1754,54 @@ func TestOptimizationProblem_LinearEqualityConstraintMatrices5(t *testing.T) {
17031754
1, len(b))
17041755
}
17051756
}
1757+
1758+
/*
1759+
TestOptimizationProblem_LinearEqualityConstraintMatrices6
1760+
Description:
1761+
1762+
Tests the LinearEqualityConstraintMatrices function with a simple problem
1763+
that contains a mixture of scalar and vector equality constraints.
1764+
The problem will have:
1765+
- a constant objective
1766+
- 3 variables,
1767+
- a single vector linear equality constraint,
1768+
- and a single scalar linear equality constraint.
1769+
The result should be a matrix with 4 rows and 3 columns.
1770+
*/
1771+
func TestOptimizationProblem_LinearEqualityConstraintMatrices6(t *testing.T) {
1772+
// Constants
1773+
p1 := problem.NewProblem("TestOptimizationProblem_LinearEqualityConstraintMatrices6")
1774+
vv1 := p1.AddVariableVector(3)
1775+
c1 := vv1.Eq(symbolic.OnesVector(3))
1776+
c2 := vv1.AtVec(0).Eq(1.0)
1777+
1778+
p1.Constraints = append(p1.Constraints, c1)
1779+
p1.Constraints = append(p1.Constraints, c2)
1780+
1781+
// Create good objective
1782+
p1.Objective = *problem.NewObjective(
1783+
symbolic.K(3.14),
1784+
problem.SenseMaximize,
1785+
)
1786+
1787+
// Algorithm
1788+
A, b := p1.LinearEqualityConstraintMatrices()
1789+
1790+
// Check that the number of rows is as expected.
1791+
if A.Dims()[0] != 4 {
1792+
t.Errorf("expected the number of rows to be %v; received %v",
1793+
4, A.Dims()[0])
1794+
}
1795+
1796+
// Check that the number of columns is as expected.
1797+
if A.Dims()[1] != 3 {
1798+
t.Errorf("expected the number of columns to be %v; received %v",
1799+
3, A.Dims()[1])
1800+
}
1801+
1802+
// Check that the number of elements in b is as expected.
1803+
if len(b) != 4 {
1804+
t.Errorf("expected the number of elements in b to be %v; received %v",
1805+
4, len(b))
1806+
}
1807+
}

0 commit comments

Comments
 (0)