LCOV - code coverage report
Current view: top level - buildbot/coverage/build/src/theory/arith - branch_and_bound.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 71 79 89.9 %
Date: 2026-02-27 11:41:18 Functions: 3 3 100.0 %
Branches: 21 42 50.0 %

           Branch data     Line data    Source code
       1                 :            : /******************************************************************************
       2                 :            :  * This file is part of the cvc5 project.
       3                 :            :  *
       4                 :            :  * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
       5                 :            :  * in the top-level source directory and their institutional affiliations.
       6                 :            :  * All rights reserved.  See the file COPYING in the top-level source
       7                 :            :  * directory for licensing information.
       8                 :            :  * ****************************************************************************
       9                 :            :  *
      10                 :            :  * Branch and bound for arithmetic
      11                 :            :  */
      12                 :            : 
      13                 :            : #include "theory/arith/branch_and_bound.h"
      14                 :            : 
      15                 :            : #include "expr/skolem_manager.h"
      16                 :            : #include "options/arith_options.h"
      17                 :            : #include "proof/eager_proof_generator.h"
      18                 :            : #include "proof/proof_node.h"
      19                 :            : #include "theory/arith/arith_utilities.h"
      20                 :            : #include "theory/rewriter.h"
      21                 :            : #include "theory/theory.h"
      22                 :            : 
      23                 :            : using namespace cvc5::internal::kind;
      24                 :            : 
      25                 :            : namespace cvc5::internal {
      26                 :            : namespace theory {
      27                 :            : namespace arith {
      28                 :            : 
      29                 :      49952 : BranchAndBound::BranchAndBound(Env& env,
      30                 :            :                                TheoryState& s,
      31                 :            :                                InferenceManager& im,
      32                 :      49952 :                                PreprocessRewriteEq& ppre)
      33                 :            :     : EnvObj(env),
      34                 :      49952 :       d_astate(s),
      35                 :      49952 :       d_im(im),
      36                 :      49952 :       d_ppre(ppre),
      37                 :      49952 :       d_pfGen(new EagerProofGenerator(env, userContext()))
      38                 :            : {
      39                 :      49952 : }
      40                 :            : 
      41                 :       3275 : std::vector<TrustNode> BranchAndBound::branchIntegerVariable(TNode var,
      42                 :            :                                                              Rational value)
      43                 :            : {
      44                 :       3275 :   std::vector<TrustNode> lems;
      45                 :       3275 :   NodeManager* nm = nodeManager();
      46                 :       3275 :   Integer floor = value.floor();
      47         [ +  + ]:       3275 :   if (options().arith.brabTest)
      48                 :            :   {
      49         [ +  - ]:       3271 :     Trace("integers") << "branch-round-and-bound enabled" << std::endl;
      50                 :       3271 :     Integer ceil = value.ceiling();
      51                 :       3271 :     Rational f = value - floor;
      52                 :            :     // Multiply by -1 to get abs value.
      53                 :       6542 :     Rational c = (value - ceil) * (-1);
      54         [ +  + ]:       3271 :     Integer nearest = (c > f) ? floor : ceil;
      55                 :            : 
      56                 :            :     // Prioritize trying a simple rounding of the real solution first,
      57                 :            :     // it that fails, fall back on original branch and bound strategy.
      58                 :       6542 :     Node ub = rewrite(nm->mkNode(Kind::LEQ, var, nm->mkConstInt(nearest - 1)));
      59                 :            :     // The rewritten form should be a GEQ literal, otherwise the split returned
      60                 :            :     // by this method will not have its intended effect
      61                 :       3271 :     Assert(ub.getKind() == Kind::GEQ
      62                 :            :            || (ub.getKind() == Kind::NOT && ub[0].getKind() == Kind::GEQ));
      63         [ +  - ]:       3271 :     Node ubatom = ub.getKind() == Kind::NOT ? ub[0] : ub;
      64                 :       6542 :     Node lb = rewrite(nm->mkNode(Kind::GEQ, var, nm->mkConstInt(nearest + 1)));
      65                 :       6542 :     Node right = nm->mkNode(Kind::OR, ub, lb);
      66                 :       6542 :     Node rawEq = nm->mkNode(Kind::EQUAL, var, nm->mkConstInt(nearest));
      67                 :       3271 :     Node eq = rewrite(rawEq);
      68                 :            :     // Also preprocess it before we send it out. This is important since
      69                 :            :     // arithmetic may prefer eliminating equalities.
      70                 :       3271 :     TrustNode teq;
      71         [ +  + ]:       3271 :     if (d_env.theoryOf(eq) == THEORY_ARITH)
      72                 :            :     {
      73                 :       3033 :       teq = d_ppre.ppRewriteEq(eq);
      74         [ +  + ]:       3033 :       eq = teq.isNull() ? eq : teq.getNode();
      75                 :            :     }
      76                 :       3271 :     Node literal = d_astate.getValuation().ensureLiteral(eq);
      77         [ +  - ]:       3271 :     Trace("integers") << "eq: " << eq << "\nto: " << literal << std::endl;
      78                 :       3271 :     d_im.preferPhase(literal, true);
      79                 :       6542 :     Node l = nm->mkNode(Kind::OR, literal, right);
      80         [ +  - ]:       3271 :     Trace("integers") << "l: " << l << std::endl;
      81         [ +  + ]:       3271 :     if (proofsEnabled())
      82                 :            :     {
      83                 :       1299 :       ProofNodeManager* pnm = d_env.getProofNodeManager();
      84                 :       2598 :       Node less = nm->mkNode(Kind::LT, var, nm->mkConstInt(nearest));
      85                 :       2598 :       Node greater = nm->mkNode(Kind::GT, var, nm->mkConstInt(nearest));
      86                 :            :       // TODO (project #37): justify. Thread proofs through *ensureLiteral*.
      87         [ +  - ]:       1299 :       Trace("integers::pf") << "less: " << less << std::endl;
      88         [ +  - ]:       1299 :       Trace("integers::pf") << "greater: " << greater << std::endl;
      89         [ +  - ]:       1299 :       Trace("integers::pf") << "literal: " << literal << std::endl;
      90         [ +  - ]:       1299 :       Trace("integers::pf") << "eq: " << eq << std::endl;
      91         [ +  - ]:       1299 :       Trace("integers::pf") << "rawEq: " << rawEq << std::endl;
      92                 :       1299 :       Pf pfNotLit = pnm->mkAssume(literal.negate());
      93                 :            :       // rewrite notLiteral to notRawEq, using teq.
      94                 :            :       Pf pfNotRawEq =
      95                 :       1299 :           literal == rawEq
      96                 :            :               ? pfNotLit
      97                 :          0 :               : pnm->mkNode(
      98                 :            :                   ProofRule::MACRO_SR_PRED_TRANSFORM,
      99         [ -  - ]:          0 :                   {pfNotLit, teq.getGenerator()->getProofFor(teq.getProven())},
     100                 :       1299 :                   {rawEq.negate()});
     101                 :            :       Pf pfBot =
     102                 :       2598 :           pnm->mkNode(ProofRule::CONTRA,
     103                 :       2598 :                       {pnm->mkNode(ProofRule::ARITH_TRICHOTOMY,
     104                 :       1299 :                                    {pnm->mkAssume(less.negate()), pfNotRawEq},
     105                 :            :                                    {},
     106                 :            :                                    greater),
     107                 :       1299 :                        pnm->mkAssume(greater.negate())},
     108                 :       7794 :                       {});
     109                 :            :       std::vector<Node> assumptions = {
     110                 :       6495 :           literal.negate(), less.negate(), greater.negate()};
     111                 :            :       // Proof of (not (and (not (= v i)) (not (< v i)) (not (> v i))))
     112                 :       2598 :       Pf pfNotAnd = pnm->mkScope(pfBot, assumptions);
     113                 :       2598 :       Pf pfL = pnm->mkNode(ProofRule::MACRO_SR_PRED_TRANSFORM,
     114                 :       1299 :                            {pnm->mkNode(ProofRule::NOT_AND, {pfNotAnd}, {})},
     115                 :       6495 :                            {l});
     116                 :       1299 :       lems.push_back(d_pfGen->mkTrustNode(l, pfL));
     117                 :       1299 :     }
     118                 :            :     else
     119                 :            :     {
     120                 :       1972 :       lems.push_back(TrustNode::mkTrustLemma(l, nullptr));
     121                 :            :     }
     122                 :       3271 :   }
     123                 :            :   else
     124                 :            :   {
     125                 :          8 :     Node ub = rewrite(nm->mkNode(Kind::LEQ, var, nm->mkConstInt(floor)));
     126                 :            :     // Similar to above, the rewritten form should be a GEQ literal, otherwise
     127                 :            :     // the split returned by this method will not have its intended effect
     128                 :          4 :     Assert(ub.getKind() == Kind::GEQ
     129                 :            :            || (ub.getKind() == Kind::NOT && ub[0].getKind() == Kind::GEQ));
     130                 :          4 :     Node lb = ub.notNode();
     131         [ -  + ]:          4 :     if (proofsEnabled())
     132                 :            :     {
     133                 :          0 :       lems.push_back(d_pfGen->mkTrustNode(
     134                 :          0 :           nm->mkNode(Kind::OR, ub, lb), ProofRule::SPLIT, {}, {ub}));
     135                 :            :     }
     136                 :            :     else
     137                 :            :     {
     138                 :          4 :       lems.push_back(
     139                 :          8 :           TrustNode::mkTrustLemma(nm->mkNode(Kind::OR, ub, lb), nullptr));
     140                 :            :     }
     141                 :          4 :   }
     142         [ -  + ]:       3275 :   if (TraceIsOn("integers"))
     143                 :            :   {
     144         [ -  - ]:          0 :     Trace("integers") << "integers: branch & bound:";
     145         [ -  - ]:          0 :     for (const TrustNode& tn : lems)
     146                 :            :     {
     147         [ -  - ]:          0 :       Trace("integers") << " " << tn;
     148                 :            :     }
     149         [ -  - ]:          0 :     Trace("integers") << std::endl;
     150                 :            :   }
     151                 :       6550 :   return lems;
     152                 :       3275 : }
     153                 :            : 
     154                 :       3275 : bool BranchAndBound::proofsEnabled() const
     155                 :            : {
     156                 :       3275 :   return d_env.isTheoryProofProducing();
     157                 :            : }
     158                 :            : 
     159                 :            : }  // namespace arith
     160                 :            : }  // namespace theory
     161                 :            : }  // namespace cvc5::internal

Generated by: LCOV version 1.14