11// ============================================================================
2- // I B E X
2+ // I B E X
33// File : ibex_Solver.cpp
44// Author : Gilles Chabert
55// Copyright : Ecole des Mines de Nantes (France)
@@ -17,12 +17,16 @@ using namespace std;
1717namespace ibex {
1818
1919Solver::Solver (Ctc& ctc, Bsc& bsc, CellBuffer& buffer) :
20- ctc (ctc), bsc(bsc), buffer(buffer), time_limit(-1 ), cell_limit(-1 ), trace(0 ), time(0 ), impact(BitSet::all(ctc.nb_var)) {
20+ ctc (ctc), visitor( NULL ), bsc(bsc), buffer(buffer), time_limit(-1 ), cell_limit(-1 ), trace(0 ), time(0 ), impact(BitSet::all(ctc.nb_var)) {
2121
2222 nb_cells=0 ;
2323
2424}
2525
26+ void Solver::setVistor (SolverVisitor *visitor) {
27+ this ->visitor = visitor;
28+ }
29+
2630void Solver::start (const IntervalVector& init_box) {
2731 buffer.flush ();
2832
@@ -35,18 +39,12 @@ void Solver::start(const IntervalVector& init_box) {
3539
3640 // add data required by the bisector
3741 bsc.add_backtrackable (*root);
38-
3942 buffer.push (root);
4043
41- int nb_var=init_box.size ();
42-
43- IntervalVector tmpbox (ctc.nb_var );
44-
4544 Timer::start ();
46-
4745}
4846
49- bool Solver::next (std::vector<IntervalVector>& sols) {
47+ void Solver::iterate (std::vector<IntervalVector>& sols) {
5048 try {
5149 while (!buffer.empty ()) {
5250
@@ -61,6 +59,9 @@ bool Solver::next(std::vector<IntervalVector>& sols) {
6159 else // root node : impact set to 1 for all variables
6260 impact.fill (0 ,ctc.nb_var -1 );
6361
62+ if (visitor != NULL )
63+ visitor->visit (c->box ,EMPTY_BOOL );
64+
6465 ctc.contract (c->box ,impact);
6566
6667 if (c->box .is_empty ()) {
@@ -87,38 +88,32 @@ bool Solver::next(std::vector<IntervalVector>& sols) {
8788 if (cell_limit >=0 && nb_cells>=cell_limit) throw CellLimitException ();}
8889
8990 catch (NoBisectableVariableException&) {
90- new_sol (sols, c->box );
91- delete buffer.pop ();
92- return !buffer.empty ();
93- // note that we skip time_limit_check() here.
94- // In the case where "next" is called by "solve",
95- // and if time has exceeded, the exception will be raised by the
96- // very next call to "next" anyway. This holds, unless "next" finds
97- // new solutions again and again endlessly. So there is a little risk
98- // of uncaught timeout in this case (but this case is probably already
99- // an error case).
91+ if (visitor != NULL )
92+ visitor->visit (c->box ,MAYBE );
93+
94+ sols.push_back (c->box );
95+ delete buffer.pop ();
10096 }
10197 time_limit_check ();
10298 }
10399 }
104100 catch (TimeOutException&) {
105- cout << " time limit " << time_limit << " s. reached " << endl; return false ;
101+ cout << " time limit " << time_limit << " s. reached " << endl;
106102 }
107103 catch (CellLimitException&) {
108104 cout << " cell limit " << cell_limit << " reached " << endl;
109105 }
110106
111107 Timer::stop ();
112108 time+= Timer::VIRTUAL_TIMELAPSE ();
113-
114- return false ;
115-
116109}
117110
118111vector<IntervalVector> Solver::solve (const IntervalVector& init_box) {
119112 vector<IntervalVector> sols;
113+
120114 start (init_box);
121- while (next (sols)) { }
115+ iterate(sols);
116+
122117 return sols;
123118}
124119
@@ -129,12 +124,4 @@ void Solver::time_limit_check () {
129124 Timer::start ();
130125}
131126
132-
133- void Solver::new_sol (vector<IntervalVector> & sols, IntervalVector & box) {
134- sols.push_back (box);
135- cout.precision (12 );
136- if (trace >=1 )
137- cout << " sol " << sols.size () << " nb_cells " << nb_cells << " " << sols[sols.size ()-1 ] << endl;
138- }
139-
140127} // end namespace ibex
0 commit comments