Skip to content

Commit 26d2c15

Browse files
committed
Added tests for the new Equality constraint matrices function
1 parent baa1e4f commit 26d2c15

2 files changed

Lines changed: 299 additions & 2 deletions

File tree

problem/optimization_problem.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func (op *OptimizationProblem) LinearInequalityConstraintMatrices() (symbolic.KM
369369
continue
370370
}
371371
// Skip this constraint if it is not an inequality
372-
if constraint.Sense == symbolic.SenseEqual {
372+
if constraint.ConstrSense() == symbolic.SenseEqual {
373373
continue
374374
}
375375
switch c := constraint.(type) {
@@ -466,7 +466,7 @@ func (op *OptimizationProblem) LinearEqualityConstraintMatrices() (symbolic.KMat
466466
continue
467467
}
468468
// Skip this constraint if it is not an equality
469-
if constraint.Sense != symbolic.SenseEqual {
469+
if constraint.ConstrSense() != symbolic.SenseEqual {
470470
continue
471471
}
472472
switch c := constraint.(type) {

testing/problem/optimization_problem_test.go

Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,3 +1406,300 @@ func TestOptimizationProblem_LinearInequalityConstraintMatrices4(t *testing.T) {
14061406
6, len(b))
14071407
}
14081408
}
1409+
1410+
/*
1411+
TestOptimizationProblem_LinearInequalityConstraintMatrices5
1412+
Description:
1413+
1414+
Tests the LinearInequalityConstraintMatrices function with a simple problem
1415+
that looks like the one in TestOptimizationProblem_LinearInequalityConstraintMatrices1.
1416+
The problem will have:
1417+
- a constant objective
1418+
- 2 variables,
1419+
- a single linear inequality constraint,
1420+
- and a single linear equality constraint.
1421+
The result should be a matrix with 1 row and 2 columns.
1422+
*/
1423+
func TestOptimizationProblem_LinearInequalityConstraintMatrices5(t *testing.T) {
1424+
// Constants
1425+
p1 := problem.NewProblem("TestOptimizationProblem_LinearInequalityConstraintMatrices5")
1426+
v1 := p1.AddVariable()
1427+
p1.AddVariable()
1428+
c1 := v1.LessEq(1.0)
1429+
c2 := v1.Eq(2.0)
1430+
1431+
p1.Constraints = append(p1.Constraints, c1)
1432+
p1.Constraints = append(p1.Constraints, c2)
1433+
1434+
// Create good objective
1435+
p1.Objective = *problem.NewObjective(
1436+
symbolic.K(3.14),
1437+
problem.SenseMaximize,
1438+
)
1439+
1440+
// Algorithm
1441+
A, b := p1.LinearInequalityConstraintMatrices()
1442+
1443+
// Check that the number of rows is as expected.
1444+
if A.Dims()[0] != 1 {
1445+
t.Errorf("expected the number of rows to be %v; received %v",
1446+
1, A.Dims()[0])
1447+
}
1448+
1449+
// Check that the number of columns is as expected.
1450+
if A.Dims()[1] != 2 {
1451+
t.Errorf("expected the number of columns to be %v; received %v",
1452+
2, A.Dims()[1])
1453+
}
1454+
1455+
// Check that the number of elements in b is as expected.
1456+
if len(b) != 1 {
1457+
t.Errorf("expected the number of elements in b to be %v; received %v",
1458+
1, len(b))
1459+
}
1460+
}
1461+
1462+
/*
1463+
TestOptimizationProblem_LinearEqualityConstraintMatrices1
1464+
Description:
1465+
1466+
Tests the LinearEqualityConstraintMatrices function with a simple problem.
1467+
The problem will have:
1468+
- a constant objective
1469+
- 2 variables,
1470+
- and a single linear equality constraint.
1471+
The result should be a matrix with 1 row and 2 columns.
1472+
*/
1473+
func TestOptimizationProblem_LinearEqualityConstraintMatrices1(t *testing.T) {
1474+
// Constants
1475+
p1 := problem.NewProblem("TestOptimizationProblem_LinearEqualityConstraintMatrices1")
1476+
v1 := p1.AddVariable()
1477+
p1.AddVariable()
1478+
c1 := v1.Eq(1.0)
1479+
1480+
p1.Constraints = append(p1.Constraints, c1)
1481+
1482+
// Create good objective
1483+
p1.Objective = *problem.NewObjective(
1484+
symbolic.K(3.14),
1485+
problem.SenseMaximize,
1486+
)
1487+
1488+
// Algorithm
1489+
A, b := p1.LinearEqualityConstraintMatrices()
1490+
1491+
// Check that the number of rows is as expected.
1492+
if A.Dims()[0] != 1 {
1493+
t.Errorf("expected the number of rows to be %v; received %v",
1494+
1, A.Dims()[0])
1495+
}
1496+
1497+
// Check that the number of columns is as expected.
1498+
if A.Dims()[1] != 2 {
1499+
t.Errorf("expected the number of columns to be %v; received %v",
1500+
2, A.Dims()[1])
1501+
}
1502+
1503+
// Check that the number of elements in b is as expected.
1504+
if len(b) != 1 {
1505+
t.Errorf("expected the number of elements in b to be %v; received %v",
1506+
1, len(b))
1507+
}
1508+
}
1509+
1510+
/*
1511+
TestOptimizationProblem_LinearEqualityConstraintMatrices2
1512+
Description:
1513+
1514+
Tests the LinearEqualityConstraintMatrices function with a simple problem.
1515+
The problem will have:
1516+
- a constant objective
1517+
- 2 variables,
1518+
- and two scalar linear equality constraints.
1519+
The result should be a matrix with 2 rows and 2 columns.
1520+
*/
1521+
func TestOptimizationProblem_LinearEqualityConstraintMatrices2(t *testing.T) {
1522+
// Constants
1523+
p1 := problem.NewProblem("TestOptimizationProblem_LinearEqualityConstraintMatrices2")
1524+
vv1 := p1.AddVariableVector(2)
1525+
c1 := vv1.AtVec(0).Eq(1.0)
1526+
c2 := vv1.AtVec(1).Eq(2.0)
1527+
1528+
p1.Constraints = append(p1.Constraints, c1)
1529+
p1.Constraints = append(p1.Constraints, c2)
1530+
1531+
// Create good objective
1532+
p1.Objective = *problem.NewObjective(
1533+
symbolic.K(3.14),
1534+
problem.SenseMaximize,
1535+
)
1536+
1537+
// Algorithm
1538+
A, b := p1.LinearEqualityConstraintMatrices()
1539+
1540+
// Check that the number of rows is as expected.
1541+
if A.Dims()[0] != 2 {
1542+
t.Errorf("expected the number of rows to be %v; received %v",
1543+
2, A.Dims()[0])
1544+
}
1545+
1546+
// Check that the number of columns is as expected.
1547+
if A.Dims()[1] != 2 {
1548+
t.Errorf("expected the number of columns to be %v; received %v",
1549+
2, A.Dims()[1])
1550+
}
1551+
1552+
// Check that the number of elements in b is as expected.
1553+
if len(b) != 2 {
1554+
t.Errorf("expected the number of elements in b to be %v; received %v",
1555+
2, len(b))
1556+
}
1557+
}
1558+
1559+
/*
1560+
TestOptimizationProblem_LinearEqualityConstraintMatrices3
1561+
Description:
1562+
1563+
Tests the LinearEqualityConstraintMatrices function with a simple problem.
1564+
The problem will have:
1565+
- a constant objective
1566+
- 3 variables,
1567+
- and a single vector linear equality constraint.
1568+
The result should be a matrix with 3 rows and 3 columns.
1569+
*/
1570+
func TestOptimizationProblem_LinearEqualityConstraintMatrices3(t *testing.T) {
1571+
// Constants
1572+
p1 := problem.NewProblem("TestOptimizationProblem_LinearEqualityConstraintMatrices3")
1573+
vv1 := p1.AddVariableVector(3)
1574+
c1 := vv1.Eq(symbolic.OnesVector(3))
1575+
1576+
p1.Constraints = append(p1.Constraints, c1)
1577+
1578+
// Create good objective
1579+
p1.Objective = *problem.NewObjective(
1580+
symbolic.K(3.14),
1581+
problem.SenseMaximize,
1582+
)
1583+
1584+
// Algorithm
1585+
A, b := p1.LinearEqualityConstraintMatrices()
1586+
1587+
// Check that the number of rows is as expected.
1588+
if A.Dims()[0] != 3 {
1589+
t.Errorf("expected the number of rows to be %v; received %v",
1590+
3, A.Dims()[0])
1591+
}
1592+
1593+
// Check that the number of columns is as expected.
1594+
if A.Dims()[1] != 3 {
1595+
t.Errorf("expected the number of columns to be %v; received %v",
1596+
3, A.Dims()[1])
1597+
}
1598+
1599+
// Check that the number of elements in b is as expected.
1600+
if len(b) != 3 {
1601+
t.Errorf("expected the number of elements in b to be %v; received %v",
1602+
3, len(b))
1603+
}
1604+
}
1605+
1606+
/*
1607+
TestOptimizationProblem_LinearEqualityConstraintMatrices4
1608+
Description:
1609+
1610+
Tests the LinearEqualityConstraintMatrices function with a simple problem.
1611+
The problem will have:
1612+
- a constant objective
1613+
- 3 variables,
1614+
- and two vector linear equality constraints.
1615+
The result should be a matrix with 6 rows and 3 columns.
1616+
*/
1617+
func TestOptimizationProblem_LinearEqualityConstraintMatrices4(t *testing.T) {
1618+
// Constants
1619+
p1 := problem.NewProblem("TestOptimizationProblem_LinearEqualityConstraintMatrices4")
1620+
vv1 := p1.AddVariableVector(3)
1621+
c1 := vv1.AtVec(0).Plus(symbolic.OnesVector(3)).Eq(symbolic.OnesVector(3))
1622+
c2 := vv1.AtVec(1).Plus(symbolic.OnesVector(3)).Eq(symbolic.OnesVector(3))
1623+
1624+
p1.Constraints = append(p1.Constraints, c1)
1625+
p1.Constraints = append(p1.Constraints, c2)
1626+
1627+
// Create good objective
1628+
p1.Objective = *problem.NewObjective(
1629+
symbolic.K(3.14),
1630+
problem.SenseMaximize,
1631+
)
1632+
1633+
// Algorithm
1634+
A, b := p1.LinearEqualityConstraintMatrices()
1635+
1636+
// Check that the number of rows is as expected.
1637+
if A.Dims()[0] != 6 {
1638+
t.Errorf("expected the number of rows to be %v; received %v",
1639+
6, A.Dims()[0])
1640+
}
1641+
1642+
// Check that the number of columns is as expected.
1643+
if A.Dims()[1] != 3 {
1644+
t.Errorf("expected the number of columns to be %v; received %v",
1645+
3, A.Dims()[1])
1646+
}
1647+
1648+
// Check that the number of elements in b is as expected.
1649+
if len(b) != 6 {
1650+
t.Errorf("expected the number of elements in b to be %v; received %v",
1651+
6, len(b))
1652+
}
1653+
}
1654+
1655+
/*
1656+
TestOptimizationProblem_LinearEqualityConstraintMatrices5
1657+
Description:
1658+
1659+
Tests the LinearEqualityConstraintMatrices function with a simple problem
1660+
that looks like the one in TestOptimizationProblem_LinearEqualityConstraintMatrices1.
1661+
The problem will have:
1662+
- a constant objective
1663+
- 2 variables,
1664+
- a single linear equality constraint,
1665+
- and a single linear inequality constraint.
1666+
The result should be a matrix with 1 row and 2 columns.
1667+
*/
1668+
func TestOptimizationProblem_LinearEqualityConstraintMatrices5(t *testing.T) {
1669+
// Constants
1670+
p1 := problem.NewProblem("TestOptimizationProblem_LinearEqualityConstraintMatrices5")
1671+
v1 := p1.AddVariable()
1672+
p1.AddVariable()
1673+
c1 := v1.Eq(1.0)
1674+
c2 := v1.LessEq(2.0)
1675+
1676+
p1.Constraints = append(p1.Constraints, c1)
1677+
p1.Constraints = append(p1.Constraints, c2)
1678+
1679+
// Create good objective
1680+
p1.Objective = *problem.NewObjective(
1681+
symbolic.K(3.14),
1682+
problem.SenseMaximize,
1683+
)
1684+
1685+
// Algorithm
1686+
A, b := p1.LinearEqualityConstraintMatrices()
1687+
1688+
// Check that the number of rows is as expected.
1689+
if A.Dims()[0] != 1 {
1690+
t.Errorf("expected the number of rows to be %v; received %v",
1691+
1, A.Dims()[0])
1692+
}
1693+
1694+
// Check that the number of columns is as expected.
1695+
if A.Dims()[1] != 2 {
1696+
t.Errorf("expected the number of columns to be %v; received %v",
1697+
2, A.Dims()[1])
1698+
}
1699+
1700+
// Check that the number of elements in b is as expected.
1701+
if len(b) != 1 {
1702+
t.Errorf("expected the number of elements in b to be %v; received %v",
1703+
1, len(b))
1704+
}
1705+
}

0 commit comments

Comments
 (0)