SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
MaxSMT_MSU3.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 namespace smtrat {
6 namespace maxsmt {
7 
8 template<typename Solver>
9 class MaxSMTBackend<Solver, MaxSMTStrategy::MSU3> {
10  Solver& mSolver;
11  const std::vector<FormulaT>& softClauses;
12 
14  FormulasT fs;
15  for (const auto& f : mSolver.formula()) fs.push_back(f.formula());
16  // TODO reuse solver: but then, formulaPositionMap cannot be reused; maybe add interface to Manager for disabling clauses
17  Solver tmpSolver;
18  for (const auto& f : fs) tmpSolver.add(f);
20  }
21 
23  mSolver.add(formula);
24  for (auto it = mSolver.formulaBegin(); it != mSolver.formulaEnd(); ++it) {
25  if (it->formula() == formula) {
26  return it;
27  }
28  }
29  assert(false && "Formula was not added correctly to backend. Expected to find formula.");
30  return mSolver.formulaEnd();
31  }
32 
33 public:
34  MaxSMTBackend(Solver& solver, const std::vector<FormulaT>& softClauses) : mSolver(solver), softClauses(softClauses) {}
35 
36  Answer run() {
37  std::map<FormulaT, ModuleInput::iterator> formulaPositionMap;
38 
39  // initialise data structures
40  for (const auto& clause : softClauses) {
41  // add clause backend and remember where we put it so we can easily remove it later
42  ModuleInput::iterator it = addToSolver(clause);
43  formulaPositionMap[clause] = it;
44  }
45 
46  std::vector<carl::Variable> relaxationVars;
47  std::vector<FormulaT> relaxedConstraints;
48 
49  std::vector<FormulaT> newSoftClauses;
50 
51  for (unsigned i = 0; i < softClauses.size(); i++) {
52  // rebuild polynomial since we add relaxation vars incrementally
53  Poly relaxationPoly;
54  for (carl::Variable& var : relaxationVars) {
55  relaxationPoly = relaxationPoly + var;
56  }
57 
58  ConstraintT relaxationConstraint(relaxationPoly - Rational(i),carl::Relation::LEQ);
59  SMTRAT_LOG_DEBUG("smtrat.maxsmt.msu3", FormulaT(relaxationConstraint));
60  ModuleInput::iterator relaxationFormula = addToSolver(FormulaT(relaxationConstraint));
61 
62  SMTRAT_LOG_DEBUG("smtrat.maxsmt.msu3", "Checking SAT for up to " << i << " disabled constraints.");
63  Answer ans = mSolver.check();
64  if (ans == Answer::SAT) return Answer::SAT;
65 
66  // We extract directly from the backend because newly added formulas get deleted from the infeasible subset!
67  auto core = computeUnsatCore();
68  if (core.first != Answer::UNSAT) {
69  return core.first;
70  }
71  SMTRAT_LOG_DEBUG("smtrat.maxsmt.msu3", "Got unsat core " << core.second);
72  for (const auto& f : core.second) { // for all soft constraints in unsat core...
73  if (std::find(softClauses.begin(), softClauses.end(), f) == softClauses.end() &&
74  std::find(newSoftClauses.begin(), newSoftClauses.end(), f) == newSoftClauses.end() ) continue;
75 
76  // check whether we already relaxed f
77  bool alreadyRelaxed = std::find(relaxedConstraints.begin(), relaxedConstraints.end(), f) != relaxedConstraints.end();
78  if (alreadyRelaxed) continue;
79 
80  carl::Variable relaxationVar = carl::fresh_boolean_variable();
81  mSolver.remove(formulaPositionMap[f]); // first erase non-relaxed clause
82  addToSolver(FormulaT(carl::FormulaType::OR, f, FormulaT(relaxationVar))); // ...then add relaxed clause
83 
84  relaxedConstraints.push_back(f);
85  relaxationVars.push_back(relaxationVar);
86  }
87 
88  mSolver.remove(relaxationFormula); // remove cardinality constraint again
89  }
90 
91  // from here, none of the soft clauses can be satisfied
92  return Answer::SAT;
93  }
94 };
95 
96 }
97 }
super::iterator iterator
Passing through the list::iterator.
Definition: ModuleInput.h:144
std::pair< Answer, FormulasT > compute_core(const FormulasT &formulas)
Definition: UnsatCore.h:53
ModuleInput::iterator addToSolver(const FormulaT &formula)
Definition: MaxSMT_MSU3.h:22
MaxSMTBackend(Solver &solver, const std::vector< FormulaT > &softClauses)
Definition: MaxSMT_MSU3.h:34
int var(Lit p)
Definition: SolverTypes.h:64
static bool find(V &ts, const T &t)
Definition: Alg.h:47
Class to create the formulas for axioms.
carl::Formula< Poly > FormulaT
Definition: types.h:37
MaxSMTStrategy
Definition: MaxSMT.h:9
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
Answer
An enum with the possible answers a Module can give.
Definition: types.h:57
carl::Constraint< Poly > ConstraintT
Definition: types.h:29
carl::Formulas< Poly > FormulasT
Definition: types.h:39
mpq_class Rational
Definition: types.h:19
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35