Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maxconstraint fixes #44

Merged
merged 7 commits into from
May 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions maraboupy/MarabouNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ def participatesInPLConstraint(self, x):
x: (int) variable to check
"""
# ReLUs
fs, bs = zip(*self.reluList)
if x in fs or x in bs:
return True
if self.reluList:
fs, bs = zip(*self.reluList)
if x in fs or x in bs:
return True
# Max constraints
for elems, var in self.maxList:
if x in elems or x==var:
Expand Down
57 changes: 29 additions & 28 deletions src/engine/MaxConstraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,21 @@ void MaxConstraint::notifyLowerBound( unsigned variable, double value )

_lowerBounds[variable] = value;

if ( FloatUtils::gt( value, _maxLowerBound ) )
if ( _elements.exists(variable) && FloatUtils::gt( value, _maxLowerBound ) )
{
_maxLowerBound = value;
List<unsigned> toRemove;
double maxValueNotRemoved = FloatUtils::negativeInfinity();
unsigned maxVarNotRemoved=0;
unsigned maxVarNotRemoved = 0;
for ( auto element : _elements )
{
if ( _upperBounds.exists( element ) &&
FloatUtils::lt( _upperBounds[element], value ) )
FloatUtils::lt( _upperBounds[element], value ) )
{
toRemove.append( element );
}
else if ( _assignment.exists( element ) &&
FloatUtils::gt( _assignment[element], maxValueNotRemoved ) )
FloatUtils::gt( _assignment[element], maxValueNotRemoved ) )
{
maxValueNotRemoved = _assignment[element];
maxVarNotRemoved = element;
Expand All @@ -126,14 +126,16 @@ void MaxConstraint::notifyLowerBound( unsigned variable, double value )
{
if ( FloatUtils::isFinite( maxValueNotRemoved ) )
_maxIndex = maxVarNotRemoved;
else _maxIndex = *( _elements.begin() );
else
_maxIndex = *( _elements.begin() );
}
}

if (_elements.exists( variable ) && variable != _maxIndex &&
_lowerBounds.exists( variable ) && _upperBounds.exists( variable ) &&
FloatUtils::areEqual( _lowerBounds[variable], _upperBounds[variable] ) )
if ( _elements.exists( variable ) && variable != _maxIndex &&
_lowerBounds.exists( variable ) && _upperBounds.exists( variable ) &&
FloatUtils::areEqual( _lowerBounds[variable], _upperBounds[variable] ) )
eliminateVariable( variable, _lowerBounds[variable] );

checkForFixedPhaseOnAlterationToBounds();
}

Expand All @@ -150,11 +152,11 @@ void MaxConstraint::notifyUpperBound( unsigned variable, double value )

_upperBounds[variable] = value;

if ( FloatUtils::lt( value, _maxLowerBound ) )
if ( _elements.exists( variable ) && FloatUtils::lt( value, _maxLowerBound ) )
{
if ( _lowerBounds.exists( variable ) )
_lowerBounds.erase( variable );
if (_upperBounds.exists( variable ) )
if ( _upperBounds.exists( variable ) )
_upperBounds.erase( variable );
if ( _assignment.exists ( variable ) )
_assignment.erase ( variable );
Expand All @@ -164,25 +166,26 @@ void MaxConstraint::notifyUpperBound( unsigned variable, double value )
if ( variable == _maxIndex )
{
double maxValueNotRemoved = FloatUtils::negativeInfinity();
unsigned maxVarNotRemoved=0;
for (auto elem: _elements )
unsigned maxVarNotRemoved = 0;
for ( auto elem : _elements )
{
if ( _assignment.exists( elem ) &&
FloatUtils::gt( _assignment[elem], maxValueNotRemoved ) )
FloatUtils::gt( _assignment[elem], maxValueNotRemoved ) )
{
maxValueNotRemoved = _assignment[elem];
maxVarNotRemoved = elem;
}
}
if ( FloatUtils::isFinite( maxValueNotRemoved ) )
_maxIndex = maxVarNotRemoved;
else _maxIndex = *( _elements.begin() );
else
_maxIndex = *( _elements.begin() );
}
}

if (_elements.exists( variable ) && variable != _maxIndex &&
_lowerBounds.exists( variable ) && _upperBounds.exists( variable ) &&
FloatUtils::areEqual( _lowerBounds[variable], _upperBounds[variable] ) )
if ( _elements.exists( variable ) && variable != _maxIndex &&
_lowerBounds.exists( variable ) && _upperBounds.exists( variable ) &&
FloatUtils::areEqual( _lowerBounds[variable], _upperBounds[variable] ) )
eliminateVariable( variable, _lowerBounds[variable] );

checkForFixedPhaseOnAlterationToBounds();
Expand Down Expand Up @@ -226,23 +229,21 @@ void MaxConstraint::checkForFixedPhaseOnAlterationToBounds()
void MaxConstraint::getEntailedTightenings( List<Tightening> &tightenings ) const
{
// Lower and upper bounds for the f variable
double fLB = _lowerBounds.get( _f );
double fUB = _upperBounds.get( _f );
double fLB = _lowerBounds.exists( _f ) ? _lowerBounds.get( _f ) : FloatUtils::negativeInfinity();
double fUB = _upperBounds.exists( _f ) ? _upperBounds.get( _f ) : FloatUtils::infinity();

// Compute the maximal bounds (lower and upper) for the elements
double maxElementLB = FloatUtils::negativeInfinity();
double maxElementUB = FloatUtils::negativeInfinity();

for ( const auto &element : _elements )
{
if ( !_lowerBounds.exists( element ) )
maxElementLB = FloatUtils::infinity();
else
{
if ( _lowerBounds.exists( element ) )
maxElementLB = FloatUtils::max( _lowerBounds[element], maxElementLB );

if ( !_upperBounds.exists( element ) )
if ( !_upperBounds.exists( element ) )
maxElementUB = FloatUtils::infinity();
else
else
maxElementUB = FloatUtils::max( _upperBounds[element], maxElementUB );
}

Expand Down Expand Up @@ -422,13 +423,13 @@ bool MaxConstraint::constraintObsolete() const

void MaxConstraint::eliminateVariable( unsigned var, double value )
{
if( _lowerBounds.exists( var ) )
if ( _lowerBounds.exists( var ) )
_lowerBounds.erase( var );
if( _upperBounds.exists( var ) )
if ( _upperBounds.exists( var ) )
_upperBounds.erase( var );

if ( !_lowerBounds.exists( _f ) ||
FloatUtils::lt( value, _lowerBounds.get( _f ) ) )
FloatUtils::gt( value, _lowerBounds.get( _f ) ) )
_lowerBounds[_f] = value;

_eliminated.insert( var );
Expand Down
62 changes: 62 additions & 0 deletions src/engine/tests/Test_MaxConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,68 @@ class MaxConstraintTestSuite : public CxxTest::TestSuite

}

void test_max_var_elims()
{
unsigned f = 1;
Set<unsigned> elements;

elements.insert( 2 );
elements.insert( 3 );

MaxConstraint max( f, elements );

max.notifyUpperBound( 2, 8 );
max.notifyLowerBound( 2, 1 );
max.notifyUpperBound( 3, 6 );
max.notifyLowerBound( 3, 1 );

max.notifyUpperBound( 1, 10 );
max.notifyLowerBound( 1, 0 );
TS_ASSERT( max.getParticipatingVariables().exists( 2 ) );
TS_ASSERT( max.getParticipatingVariables().exists( 3 ) );

max.notifyLowerBound( 1, 6 );
TS_ASSERT( max.getParticipatingVariables().exists( 2 ) );
TS_ASSERT( max.getParticipatingVariables().exists( 3 ) );

max.notifyLowerBound( 2, 7 );
TS_ASSERT( max.getParticipatingVariables().exists( 2 ) );
TS_ASSERT( !max.getParticipatingVariables().exists( 3 ) );
}

void test_get_entailed_tightenings()
{
unsigned f = 1;
Set<unsigned> elements;

elements.insert( 2 );
elements.insert( 3 );

MaxConstraint max( f, elements );

max.notifyLowerBound( 2, 1 );
max.notifyUpperBound( 2, 8 );
// No lower bound for 3
max.notifyUpperBound( 3, 6 );

List<Tightening> tightenings;
TS_ASSERT_THROWS_NOTHING( max.getEntailedTightenings( tightenings ) );

// expect f to be in the range [1, 8]
TS_ASSERT_EQUALS( tightenings.size(), 2U );
auto it = tightenings.begin();

TS_ASSERT_EQUALS( it->_variable, 1U );
TS_ASSERT_EQUALS( it->_value, 8 );
TS_ASSERT_EQUALS( it->_type, Tightening::UB );

++it;

TS_ASSERT_EQUALS( it->_variable, 1U );
TS_ASSERT_EQUALS( it->_value, 1 );
TS_ASSERT_EQUALS( it->_type, Tightening::LB );
}

void test_max_obsolete()
{
unsigned f = 1;
Expand Down