SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
MaxSMT_LinearSearch.h
Go to the documentation of this file.
1 #pragma once
2 
3 namespace smtrat {
4 namespace maxsmt {
5 
6 template<typename Solver>
8  Solver& mSolver;
9  const std::vector<FormulaT>& softClauses;
10 
12  mSolver.add(formula);
13  for (auto it = mSolver.formulaBegin(); it != mSolver.formulaEnd(); ++it) {
14  if (it->formula() == formula) {
15  return it;
16  }
17  }
18  assert(false && "Formula was not added correctly to backend. Expected to find formula.");
19  return mSolver.formulaEnd();
20  }
21 
22 public:
23  MaxSMTBackend(Solver& solver, const std::vector<FormulaT>& softClauses) : mSolver(solver), softClauses(softClauses) {}
24 
25  Answer run() {
26  // add all soft clauses with relaxation var
27  // for i = 0; i < soft.size; i++:
28  // check sat for \sum relaxation var <= i to determine if we have to disable some constraint
29  // if sat return;
30 
31  std::vector<carl::Variable> relaxationVars;
32  for (const auto& clause : softClauses) {
33  carl::Variable relaxationVar = carl::fresh_boolean_variable();
34  mSolver.add(FormulaT(carl::FormulaType::OR, clause, FormulaT(relaxationVar)));
35 
36  relaxationVars.push_back(relaxationVar);
37  }
38 
39  Poly relaxationPoly;
40  for (carl::Variable& var : relaxationVars) {
41  relaxationPoly = relaxationPoly + var;
42  }
43 
44  // initially all constraints must be enabled
45  ConstraintT initialRelaxationConstraint(relaxationPoly - Rational(0),carl::Relation::LEQ);
46  ModuleInput::iterator previousRelaxationConstraint = addToSolver(FormulaT(initialRelaxationConstraint));
47 
48  for (unsigned i = 1; i < softClauses.size(); i++) {
49  SMTRAT_LOG_DEBUG("smtrat.maxsmt.linear", "Trying to check SAT for " << (i - 1) << " disabled soft constraints...");
50 
51  Answer ans = mSolver.check();
52  if (ans == Answer::SAT) return Answer::SAT;
53 
54  std::cout << "REMOVE " << previousRelaxationConstraint->formula() << std::endl;
55 
56  mSolver.remove(previousRelaxationConstraint);
57 
58  ConstraintT relaxationConstraint(relaxationPoly - Rational(i),carl::Relation::LEQ);
59  previousRelaxationConstraint = addToSolver(FormulaT(relaxationConstraint));
60  }
61  return Answer::SAT;
62  }
63 
64 };
65 
66 }
67 }
super::iterator iterator
Passing through the list::iterator.
Definition: ModuleInput.h:144
MaxSMTBackend(Solver &solver, const std::vector< FormulaT > &softClauses)
int var(Lit p)
Definition: SolverTypes.h:64
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
mpq_class Rational
Definition: types.h:19
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35