Skip to content

Commit 5fc58d9

Browse files
kwesiRutledgeKwesi Rutledge
andauthored
Added fix in the ImpliesThisIsAlsoSatisfied method (#26)
* Added fix in the ImpliesThisIsAlsoSatisfied method * Added test for case with negative coefficients --------- Co-authored-by: Kwesi Rutledge <kwesir@csail.mit.edu>
1 parent 8f261a9 commit 5fc58d9

2 files changed

Lines changed: 73 additions & 9 deletions

File tree

symbolic/scalar_constraint.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,11 @@ func (sc ScalarConstraint) ImpliesThisIsAlsoSatisfied(other Constraint) bool {
367367
otherCCoeffVector := otherC.LeftHandSide.LinearCoeff(otherC.Variables())
368368
otherCCoeff := otherCCoeffVector.AtVec(0)
369369

370-
// If the coefficient of scCoeff is < 0,
371-
// then flip the signs of both sides of the constraint
372-
if scCoeff < 0 {
373-
sc = sc.ScaleBy(-1).(ScalarConstraint)
374-
}
375-
376-
if otherCCoeff < 0 {
377-
otherC = otherC.ScaleBy(-1).(ScalarConstraint)
378-
}
370+
// Scale both constraints
371+
// Note: If the coefficient is negative, then the sense of the constraint will be flipped.
372+
// This is handled in the ScaleBy method.
373+
sc = sc.ScaleBy(1.0 / scCoeff).(ScalarConstraint)
374+
otherC = otherC.ScaleBy(1.0 / otherCCoeff).(ScalarConstraint)
379375

380376
// The implication holds if all of the following are true:
381377
// 1. The sense of sc and otherC are either the same (or one is equality)

testing/symbolic/scalar_constraint_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,74 @@ func TestScalarConstraint_ImpliesThisIsAlsoSatisfied10(t *testing.T) {
19181918
}
19191919
}
19201920

1921+
/*
1922+
TestScalarConstraint_ImpliesThisIsAlsoSatisfied11
1923+
Description:
1924+
1925+
Tests the ImpliesThisIsAlsoSatisfied() method of a scalar constraint.
1926+
This test attempts to catch a bug where the method would note that some
1927+
constraints imply others that they should not actually imply.
1928+
In this case, it seems like we have constraints where, if you compare
1929+
the constants on the right hand side, then they might seem to imply one
1930+
another: (1 <= 2), but when you consider the left hand side's
1931+
coefficients, you see that they do NOT actually imply one another.
1932+
In this case, we have:
1933+
2 x <= 1 and
1934+
10 x <= 2
1935+
as the input constraints. The first constraint does NOT imply the second.
1936+
*/
1937+
func TestScalarConstraint_ImpliesThisIsAlsoSatisfied11(t *testing.T) {
1938+
// Constants
1939+
x := symbolic.NewVariable()
1940+
1941+
// Create constraint
1942+
sc := x.Multiply(2).LessEq(1.0)
1943+
1944+
// Create a second constraint
1945+
sc2 := x.Multiply(10).LessEq(2.0)
1946+
1947+
// Verify that the first constraint does NOT imply the second
1948+
if sc.ImpliesThisIsAlsoSatisfied(sc2) {
1949+
t.Errorf(
1950+
"Expected sc.ImpliesThisIsAlsoSatisfied(sc2) to be false; received true",
1951+
)
1952+
}
1953+
}
1954+
1955+
/*
1956+
TestScalarConstraint_ImpliesThisIsAlsoSatisfied12
1957+
Description:
1958+
1959+
Tests the ImpliesThisIsAlsoSatisfied() method of a scalar constraint.
1960+
This test attempts to catch a bug where the method would note that some
1961+
constraints imply others that they should not actually imply.
1962+
In this case, it seems like we have constraints where, if you compare
1963+
the constants on the right hand side, then they might seem to imply one
1964+
another: (-2 >= -4), but when you consider the left hand side's
1965+
coefficients, you see that they do NOT actually imply one another.
1966+
In this case, we have:
1967+
-2 x <= 2 and
1968+
-10 x <= 4
1969+
as the input constraints. The first constraint does NOT imply the second.
1970+
*/
1971+
func TestScalarConstraint_ImpliesThisIsAlsoSatisfied12(t *testing.T) {
1972+
// Constants
1973+
x := symbolic.NewVariable()
1974+
1975+
// Create constraint
1976+
sc := x.Multiply(-2).LessEq(2.0)
1977+
1978+
// Create a second constraint
1979+
sc2 := x.Multiply(-10).LessEq(4.0)
1980+
1981+
// Verify that the first constraint does NOT imply the second
1982+
if sc.ImpliesThisIsAlsoSatisfied(sc2) {
1983+
t.Errorf(
1984+
"Expected sc.ImpliesThisIsAlsoSatisfied(sc2) to be false; received true",
1985+
)
1986+
}
1987+
}
1988+
19211989
/*
19221990
TestScalarConstraint_AsSimplifiedConstraint1
19231991
Description:

0 commit comments

Comments
 (0)