Skip to content

Commit 84e04a7

Browse files
Catch Glucose::OutOfMemoryException
1 parent 89fc48d commit 84e04a7

File tree

1 file changed

+98
-54
lines changed

1 file changed

+98
-54
lines changed

src/solvers/sat/satcheck_glucose.cpp

+98-54
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,18 @@ template<typename T>
6565
void satcheck_glucose_baset<T>::set_polarity(literalt a, bool value)
6666
{
6767
assert(!a.is_constant());
68-
add_variables();
69-
solver->setPolarity(a.var_no(), value);
68+
69+
try
70+
{
71+
add_variables();
72+
solver->setPolarity(a.var_no(), value);
73+
}
74+
catch(Glucose::OutOfMemoryException)
75+
{
76+
messaget::error() << "SAT checker ran out of memory" << eom;
77+
status = statust::ERROR;
78+
throw std::bad_alloc();
79+
}
7080
}
7181

7282
const std::string satcheck_glucose_no_simplifiert::solver_text()
@@ -89,26 +99,35 @@ void satcheck_glucose_baset<T>::add_variables()
8999
template<typename T>
90100
void satcheck_glucose_baset<T>::lcnf(const bvt &bv)
91101
{
92-
add_variables();
93-
94-
forall_literals(it, bv)
102+
try
95103
{
96-
if(it->is_true())
97-
return;
98-
else if(!it->is_false())
99-
assert(it->var_no()<(unsigned)solver->nVars());
100-
}
104+
add_variables();
101105

102-
Glucose::vec<Glucose::Lit> c;
106+
forall_literals(it, bv)
107+
{
108+
if(it->is_true())
109+
return;
110+
else if(!it->is_false())
111+
assert(it->var_no() < (unsigned)solver->nVars());
112+
}
113+
114+
Glucose::vec<Glucose::Lit> c;
103115

104-
convert(bv, c);
116+
convert(bv, c);
105117

106-
// Note the underscore.
107-
// Add a clause to the solver without making superflous internal copy.
118+
// Note the underscore.
119+
// Add a clause to the solver without making superflous internal copy.
108120

109-
solver->addClause_(c);
121+
solver->addClause_(c);
110122

111-
clause_counter++;
123+
clause_counter++;
124+
}
125+
catch(Glucose::OutOfMemoryException)
126+
{
127+
messaget::error() << "SAT checker ran out of memory" << eom;
128+
status = statust::ERROR;
129+
throw std::bad_alloc();
130+
}
112131
}
113132

114133
template<typename T>
@@ -123,63 +142,79 @@ propt::resultt satcheck_glucose_baset<T>::prop_solve()
123142
solver->nClauses() << " clauses" << eom;
124143
}
125144

126-
add_variables();
127-
128-
if(!solver->okay())
145+
try
129146
{
130-
messaget::status() <<
131-
"SAT checker inconsistent: instance is UNSATISFIABLE" << eom;
132-
}
133-
else
134-
{
135-
// if assumptions contains false, we need this to be UNSAT
136-
bool has_false=false;
137-
138-
forall_literals(it, assumptions)
139-
if(it->is_false())
140-
has_false=true;
147+
add_variables();
141148

142-
if(has_false)
149+
if(!solver->okay())
143150
{
144-
messaget::status() <<
145-
"got FALSE as assumption: instance is UNSATISFIABLE" << eom;
151+
messaget::status()
152+
<< "SAT checker inconsistent: instance is UNSATISFIABLE" << eom;
146153
}
147154
else
148155
{
149-
Glucose::vec<Glucose::Lit> solver_assumptions;
150-
convert(assumptions, solver_assumptions);
156+
// if assumptions contains false, we need this to be UNSAT
157+
bool has_false = false;
158+
159+
forall_literals(it, assumptions)
160+
if(it->is_false())
161+
has_false = true;
151162

152-
if(solver->solve(solver_assumptions))
163+
if(has_false)
153164
{
154-
messaget::status() <<
155-
"SAT checker: instance is SATISFIABLE" << eom;
156-
status=statust::SAT;
157-
return resultt::P_SATISFIABLE;
165+
messaget::status()
166+
<< "got FALSE as assumption: instance is UNSATISFIABLE" << eom;
158167
}
159168
else
160169
{
161-
messaget::status() <<
162-
"SAT checker: instance is UNSATISFIABLE" << eom;
170+
Glucose::vec<Glucose::Lit> solver_assumptions;
171+
convert(assumptions, solver_assumptions);
172+
173+
if(solver->solve(solver_assumptions))
174+
{
175+
messaget::status() << "SAT checker: instance is SATISFIABLE" << eom;
176+
status = statust::SAT;
177+
return resultt::P_SATISFIABLE;
178+
}
179+
else
180+
{
181+
messaget::status() << "SAT checker: instance is UNSATISFIABLE" << eom;
182+
}
163183
}
164184
}
165-
}
166185

167-
status=statust::UNSAT;
168-
return resultt::P_UNSATISFIABLE;
186+
status = statust::UNSAT;
187+
return resultt::P_UNSATISFIABLE;
188+
}
189+
catch(Glucose::OutOfMemoryException)
190+
{
191+
messaget::error() << "SAT checker ran out of memory" << eom;
192+
status = statust::ERROR;
193+
throw std::bad_alloc();
194+
}
169195
}
170196

171197
template<typename T>
172198
void satcheck_glucose_baset<T>::set_assignment(literalt a, bool value)
173199
{
174200
assert(!a.is_constant());
175201

176-
unsigned v=a.var_no();
177-
bool sign=a.sign();
202+
try
203+
{
204+
unsigned v = a.var_no();
205+
bool sign = a.sign();
178206

179-
// MiniSat2 kills the model in case of UNSAT
180-
solver->model.growTo(v+1);
181-
value^=sign;
182-
solver->model[v]=Glucose::lbool(value);
207+
// MiniSat2 kills the model in case of UNSAT
208+
solver->model.growTo(v + 1);
209+
value ^= sign;
210+
solver->model[v] = Glucose::lbool(value);
211+
}
212+
catch(Glucose::OutOfMemoryException)
213+
{
214+
messaget::error() << "SAT checker ran out of memory" << eom;
215+
status = statust::ERROR;
216+
throw std::bad_alloc();
217+
}
183218
}
184219

185220
template<typename T>
@@ -233,10 +268,19 @@ satcheck_glucose_simplifiert::satcheck_glucose_simplifiert():
233268

234269
void satcheck_glucose_simplifiert::set_frozen(literalt a)
235270
{
236-
if(!a.is_constant())
271+
try
237272
{
238-
add_variables();
239-
solver->setFrozen(a.var_no(), true);
273+
if(!a.is_constant())
274+
{
275+
add_variables();
276+
solver->setFrozen(a.var_no(), true);
277+
}
278+
}
279+
catch(Glucose::OutOfMemoryException)
280+
{
281+
messaget::error() << "SAT checker ran out of memory" << eom;
282+
status = statust::ERROR;
283+
throw std::bad_alloc();
240284
}
241285
}
242286

0 commit comments

Comments
 (0)