|
| 1 | +/********************* */ |
| 2 | +/*! \file GroundBoundManager.cpp |
| 3 | + ** \verbatim |
| 4 | + ** Top contributors (to current version): |
| 5 | + ** Idan Refaeli, Omri Isac |
| 6 | + ** This file is part of the Marabou project. |
| 7 | + ** Copyright (c) 2017-2025 by the authors listed in the file AUTHORS |
| 8 | + ** in the top-level source directory) and their institutional affiliations. |
| 9 | + ** All rights reserved. See the file COPYING in the top-level source |
| 10 | + ** directory for licensing information.\endverbatim |
| 11 | + ** |
| 12 | + ** [[ Add lengthier description here ]] |
| 13 | +
|
| 14 | +**/ |
| 15 | + |
| 16 | +#include "GroundBoundManager.h" |
| 17 | + |
| 18 | +#include "FloatUtils.h" |
| 19 | + |
| 20 | + |
| 21 | +GroundBoundManager::GroundBoundManager( CVC4::context::Context &ctx ) |
| 22 | + : _context( ctx ) |
| 23 | + , _size( 0 ) |
| 24 | + , _upperGroundBounds( 0 ) |
| 25 | + , _lowerGroundBounds( 0 ) |
| 26 | +{ |
| 27 | + _counter = new ( true ) CVC4::context::CDO<unsigned>( &_context, 0 ); |
| 28 | +} |
| 29 | + |
| 30 | +GroundBoundManager::~GroundBoundManager() |
| 31 | +{ |
| 32 | + _counter->deleteSelf(); |
| 33 | + for ( unsigned i = 0; i < _size; ++i ) |
| 34 | + { |
| 35 | + _upperGroundBounds[i]->deleteSelf(); |
| 36 | + _lowerGroundBounds[i]->deleteSelf(); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +void GroundBoundManager::initialize( unsigned size ) |
| 41 | +{ |
| 42 | + _counter->set( 0 ); |
| 43 | + _size = size; |
| 44 | + |
| 45 | + for ( unsigned i = 0; i < size; ++i ) |
| 46 | + { |
| 47 | + _upperGroundBounds.append( |
| 48 | + new ( true ) CVC4::context::CDList<std::shared_ptr<GroundBoundEntry>>( &_context ) ); |
| 49 | + _lowerGroundBounds.append( |
| 50 | + new ( true ) CVC4::context::CDList<std::shared_ptr<GroundBoundEntry>>( &_context ) ); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +std::shared_ptr<GroundBoundManager::GroundBoundEntry> |
| 55 | +GroundBoundManager::addGroundBound( unsigned index, |
| 56 | + double value, |
| 57 | + Tightening::BoundType boundType, |
| 58 | + bool isPhaseFixing ) |
| 59 | +{ |
| 60 | + const Vector<CVC4::context::CDList<std::shared_ptr<GroundBoundEntry>> *> &temp = |
| 61 | + boundType == Tightening::UB ? _upperGroundBounds : _lowerGroundBounds; |
| 62 | + |
| 63 | + std::shared_ptr<GroundBoundEntry> groundBoundEntry( |
| 64 | + new GroundBoundEntry( _counter->get(), value, nullptr, isPhaseFixing ) ); |
| 65 | + |
| 66 | + if ( !temp[index]->empty() ) |
| 67 | + { |
| 68 | + ASSERT( boundType == Tightening::UB ? FloatUtils::lte( value, temp[index]->back()->val ) |
| 69 | + : FloatUtils::gte( value, temp[index]->back()->val ) ) |
| 70 | + } |
| 71 | + |
| 72 | + temp[index]->push_back( groundBoundEntry ); |
| 73 | + _counter->set( _counter->get() + 1 ); |
| 74 | + |
| 75 | + return temp[index]->back(); |
| 76 | +} |
| 77 | + |
| 78 | +std::shared_ptr<GroundBoundManager::GroundBoundEntry> |
| 79 | +GroundBoundManager::addGroundBound( const std::shared_ptr<PLCLemma> &lemma, bool isPhaseFixing ) |
| 80 | +{ |
| 81 | + Tightening::BoundType isUpper = lemma->getAffectedVarBound(); |
| 82 | + unsigned index = lemma->getAffectedVar(); |
| 83 | + |
| 84 | + const Vector<CVC4::context::CDList<std::shared_ptr<GroundBoundEntry>> *> &temp = |
| 85 | + isUpper == Tightening::UB ? _upperGroundBounds : _lowerGroundBounds; |
| 86 | + |
| 87 | + std::shared_ptr<GroundBoundEntry> groundBoundEntry( |
| 88 | + new GroundBoundEntry( _counter->get(), lemma->getBound(), lemma, isPhaseFixing ) ); |
| 89 | + |
| 90 | + if ( !temp[index]->empty() ) |
| 91 | + { |
| 92 | + ASSERT( isUpper == Tightening::UB |
| 93 | + ? FloatUtils::lte( lemma->getBound(), temp[index]->back()->val ) |
| 94 | + : FloatUtils::gte( lemma->getBound(), temp[index]->back()->val ) ) |
| 95 | + } |
| 96 | + |
| 97 | + temp[index]->push_back( groundBoundEntry ); |
| 98 | + _counter->set( _counter->get() + 1 ); |
| 99 | + |
| 100 | + return temp[index]->back(); |
| 101 | +} |
| 102 | + |
| 103 | +double GroundBoundManager::getGroundBound( unsigned index, Tightening::BoundType boundType ) const |
| 104 | +{ |
| 105 | + const Vector<CVC4::context::CDList<std::shared_ptr<GroundBoundEntry>> *> &temp = |
| 106 | + boundType == Tightening::UB ? _upperGroundBounds : _lowerGroundBounds; |
| 107 | + return temp[index]->back()->val; |
| 108 | +} |
| 109 | + |
| 110 | +std::shared_ptr<GroundBoundManager::GroundBoundEntry> |
| 111 | +GroundBoundManager::getGroundBoundEntry( unsigned int index, Tightening::BoundType boundType ) const |
| 112 | +{ |
| 113 | + const Vector<CVC4::context::CDList<std::shared_ptr<GroundBoundEntry>> *> &temp = |
| 114 | + boundType == Tightening::UB ? _upperGroundBounds : _lowerGroundBounds; |
| 115 | + return temp[index]->back(); |
| 116 | +} |
| 117 | + |
| 118 | +std::shared_ptr<GroundBoundManager::GroundBoundEntry> |
| 119 | +GroundBoundManager::getGroundBoundEntryUpToId( unsigned index, |
| 120 | + Tightening::BoundType boundType, |
| 121 | + unsigned id ) const |
| 122 | +{ |
| 123 | + const Vector<CVC4::context::CDList<std::shared_ptr<GroundBoundEntry>> *> &temp = |
| 124 | + boundType == Tightening::UB ? _upperGroundBounds : _lowerGroundBounds; |
| 125 | + |
| 126 | + if ( id == 0 ) |
| 127 | + return ( *temp[index] )[0]; |
| 128 | + |
| 129 | + for ( int i = temp[index]->size() - 1; i >= 0; --i ) |
| 130 | + { |
| 131 | + const std::shared_ptr<GroundBoundEntry> entry = ( *temp[index] )[i]; |
| 132 | + if ( entry->id < id ) |
| 133 | + return entry; |
| 134 | + } |
| 135 | + ASSERT( id <= 2 * temp.size() ); |
| 136 | + return ( *temp[index] )[0]; |
| 137 | +} |
| 138 | + |
| 139 | +double GroundBoundManager::getGroundBoundUpToId( unsigned index, |
| 140 | + Tightening::BoundType boundType, |
| 141 | + unsigned id ) const |
| 142 | +{ |
| 143 | + return getGroundBoundEntryUpToId( index, boundType, id )->val; |
| 144 | +} |
| 145 | + |
| 146 | +Vector<double> GroundBoundManager::getAllGroundBounds( Tightening::BoundType boundType ) const |
| 147 | +{ |
| 148 | + const Vector<CVC4::context::CDList<std::shared_ptr<GroundBoundEntry>> *> &temp = |
| 149 | + boundType == Tightening::UB ? _upperGroundBounds : _lowerGroundBounds; |
| 150 | + Vector<double> tops = Vector<double>( 0 ); |
| 151 | + |
| 152 | + for ( const auto &GBList : temp ) |
| 153 | + tops.append( GBList->back()->val ); |
| 154 | + |
| 155 | + return tops; |
| 156 | +} |
| 157 | + |
| 158 | +unsigned GroundBoundManager::getCounter() const |
| 159 | +{ |
| 160 | + return _counter->get(); |
| 161 | +} |
0 commit comments