Exemple #1
0
void TestSolver::circle4() {
	const ExprSymbol& x=ExprSymbol::new_("x");
	const ExprSymbol& y=ExprSymbol::new_("y");
	const ExprSymbol& r2=ExprSymbol::new_("r2");

	SystemFactory f;
	f.add_var(x);
	f.add_var(y);
	f.add_var(r2);
	f.add_ctr(sqr(x)+sqr(y)=r2);
	f.add_ctr(sqr(x-1)+sqr(y)=r2);

	double cospi6=0.5;
	double sinpi6=::sqrt(3)/2;

	double _sol1[]={cospi6,sinpi6,1};
	double _sol2[]={cospi6,-sinpi6,1};

	Vector sol1(3,_sol1);
	Vector sol2(3,_sol2);

	System sys(f);
	RoundRobin rr(1e-3);
	CellStack stack;
	CtcHC4 hc4(sys);

	VarSet params(sys.f_ctrs,sys.args[2],false);
	Vector prec(3,1e-3);
	Solver solver(sys,hc4,rr,stack,prec,prec);
	solver.set_params(params);
	IntervalVector box(3);
	box[0]=Interval(-10,10);
	box[1]=Interval(-10,10);
	box[2]=Interval(1,1);
	solver.start(box);

	CovSolverData::BoxStatus status;

	bool res=solver.next(status);
	CPPUNIT_ASSERT(res);
	CPPUNIT_ASSERT(status==CovSolverData::SOLUTION);
	CPPUNIT_ASSERT(solver.get_data().nb_solution()==1);
	CPPUNIT_ASSERT(solver.get_data().solution(0).is_superset(sol1));

	res=solver.next(status);
	CPPUNIT_ASSERT(status==CovSolverData::SOLUTION);
	CPPUNIT_ASSERT(solver.get_data().nb_solution()==2);
	CPPUNIT_ASSERT(solver.get_data().solution(1).is_superset(sol2));

	res=solver.next(status);
	CPPUNIT_ASSERT(!res);
}
Exemple #2
0
void TestSolver::circle2() {
	const ExprSymbol& x=ExprSymbol::new_("x");
	const ExprSymbol& y=ExprSymbol::new_("y");

	SystemFactory f;
	f.add_var(x);
	f.add_var(y);
	f.add_ctr(sqr(x)+sqr(y)=1);
	f.add_ctr(sqr(x-2)+sqr(y)=1);
	double _sol1[]={1,0};
	Vector sol1(2,_sol1);
	System sys(f);
	RoundRobin rr(1e-3);
	CellStack stack;
	CtcHC4 hc4(sys);
	Vector prec(2,1e-3);
	Solver solver(sys,hc4,rr,stack,prec,prec);

	solver.start(IntervalVector(2,Interval(-10,10)));

	CovSolverData::BoxStatus status;

	bool res=solver.next(status);
	CPPUNIT_ASSERT(res);
	CPPUNIT_ASSERT(status==CovSolverData::UNKNOWN);
	CPPUNIT_ASSERT(solver.get_data().nb_unknown()==1);
	CPPUNIT_ASSERT(solver.get_data().unknown(0).is_superset(sol1));

	res=solver.next(status);
	CPPUNIT_ASSERT(!res);
}
Exemple #3
0
void TestSolver::circle3() {
	const ExprSymbol& x=ExprSymbol::new_("x");
	const ExprSymbol& y=ExprSymbol::new_("y");

	SystemFactory f;
	f.add_var(x);
	f.add_var(y);
	f.add_ctr(sqr(x)+sqr(y)=1);
	f.add_ctr(sqr(x-1)+sqr(y)=1);

	double cospi6=0.5;
	double sinpi6=(sqrt(Interval(3))/2).lb();
	f.add_ctr(4*y*abs(y)<=3); // a rigorous way to impose y<=sin(pi/6).

	double _sol1[]={cospi6,sinpi6};
	double _sol2[]={cospi6,-sinpi6};

	Vector sol1(2,_sol1);
	Vector sol2(2,_sol2);
	System sys(f);
	RoundRobin rr(1e-3);
	CellStack stack;
	CtcHC4 hc4(sys);
	Vector prec(2,1e-3);
	Solver solver(sys,hc4,rr,stack,prec,prec);

	solver.start(IntervalVector(2,Interval(-10,10)));


	CovSolverData::BoxStatus status;

	bool res=solver.next(status);
	CPPUNIT_ASSERT(res);
	CPPUNIT_ASSERT(status==CovSolverData::UNKNOWN);
	CPPUNIT_ASSERT(solver.get_data().nb_unknown()==1);
	CPPUNIT_ASSERT(solver.get_data().unknown(0).is_superset(sol1));

	res=solver.next(status);
	CPPUNIT_ASSERT(res);
	CPPUNIT_ASSERT(status==CovSolverData::SOLUTION);
	CPPUNIT_ASSERT(solver.get_data().nb_solution()==1);
	CPPUNIT_ASSERT(solver.get_data().solution(0).is_superset(sol2));

	res=solver.next(status);
	CPPUNIT_ASSERT(!res);
}
int main() {

	ofstream output;
	output.open ("doc-modeling.txt");

	output << "================= this file is generated ==============" << endl;

	{
	output << "! [func-eval-O]" << endl;
	//! [func-eval-C]
	const int nb_rows=2;
	const int nb_cols=2;

	Variable a(nb_rows,nb_cols),b(nb_rows,nb_cols),c(nb_rows,nb_cols);

	Function pA(a,b,c,a);
	Function pB(a,b,c,b);
	Function pC(a,b,c,c);

	Function f(a,b,c, (a+b-c));

	double _A[nb_rows*nb_cols][2]={{2,2},{-1,-1},{-1,-1},{2,2}};
	IntervalMatrix MA(2,2,_A);

	double _B[nb_rows*nb_cols][2]={{1,1},{-1,-1},{1,1},{1,1}};
	IntervalMatrix MB(2,2,_B);

	double _C[nb_rows*nb_cols][2]={{1,1},{0,0},{0,0},{1,1}};
	IntervalMatrix MC(2,2,_C);

	IntervalVector box(3*nb_rows*nb_cols);

	// the backward call on pA will force the sub-vector of
	// "box" that represents the domain of "a" to contain the
	// interval matrix "MA".
	pA.backward(MA,box);

	// idem
	pB.backward(MB,box);
	pC.backward(MC,box);

	IntervalMatrix M = f.eval_matrix(box);

	output << "A+B-C=" << M << endl;
	//! [func-eval-C]
	output << "! [func-eval-O]" << endl;
	}


	{
	output << "! [func-hansen-O]" << endl;
	//! [func-hansen-C]
	Variable x,y;
	Function f(x,y,Return(sqr(x)*y,sqr(y)*x));
	IntervalMatrix H(2,2);
	IntervalVector box(2,Interval(1,2));
	f.hansen_matrix(box,H);
	output << "Hansen matrix:\n" << H << endl;
	//! [func-hansen-C]
	output << "! [func-hansen-O]" << endl;
	}

	{
	output << "! [func-cpp1-O]" << endl;
	//! [func-cpp1-C]
	Variable x("x");
	Variable y("y");
	Function f(x,y,sin(x+y)); // create the function (x,y)->sin(x+y)
	output << f << endl;
	//! [func-cpp1-C]
	output << "! [func-cpp1-O]" << endl;
	}

	{
	//! [func-cpp2-C]
	Variable a,b,c,d,e,f;
	Function _f(a,b,c,d,e,f,a+b+c+d+e+f);
	//! [func-cpp2-C]
	}

	{
	output << "! [func-cpp3-O]" << endl;
	//! [func-cpp3-C]
	Variable x[7]; // not to be confused with x(7)
	Array<const ExprSymbol> vars(7);

	for (int i=0; i<7; i++)
	vars.set_ref(i,x[i]);

	Function f(vars, x[0]+x[1]+x[2]+x[3]+x[4]+x[5]+x[6]);
	output << f << endl;
	//! [func-cpp3-C]
	output << "! [func-cpp3-O]" << endl;
	}

	{
	output << "! [func-cpp4-O]" << endl;
	//! [func-cpp4-C]
	Variable x,y;
	Function f(x,y,ibex::min(x,y));
	output << f << endl;
	//! [func-cpp4-C]
	output << "! [func-cpp4-O]" << endl;
	}

	{
	//! [func-cpp-symbols-C]
	Variable xa,xb,ya,yb;
	Function dist(xa,xb,ya,yb, sqrt(sqr(xa-xb)+sqr(ya-yb)));
	//! [func-cpp-symbols-C]
	}

	{
	//! [func-cpp-vec-args-C]
	Variable a(2);
	Variable b(2);
	Function dist(a,b,sqrt(sqr(a[0]-b[0])+sqr(a[1]-b[1])),"dist");
	//! [func-cpp-vec-args-C]
	}

	{
	//! [func-cpp-compo-C]

	//! [func-cpp-compo-C]
	}


	{
	// ![func-dag-C]
	Variable x;
	const ExprNode& e=cos(x)+1;
	Function f(x,Return(pow(e,2),pow(e,3)));
	// ![func-dag-C]
	}

	{
	output << "![func-iterated-sum-O]" << endl;
	// ![func-iterated-sum-C]
	int N=10;
	Variable x(N,"x");

	const ExprNode* e=&(sqr(x[0]));
	for (int i=1; i<N; i++)
	e = & (*e + sqr(x[i]));

	Function f(x,*e,"f");

	output << f << endl;
	// ![func-iterated-sum-C]
	output << "![func-iterated-sum-O]" << endl;
	}

	{
	output << "![func-apply-array-O]" << endl;
	// ![func-apply-array-C]
	Variable x,y;

	// formal arguments
	Array<const ExprSymbol> vars(2);

	vars.set_ref(0,x);
	vars.set_ref(1,y);

	Function f(vars, x+y,"f");

	// actual arguments
	const ExprSymbol& z=ExprSymbol::new_("z");  // <=> "Variable z" (but more "safe")
	const ExprConstant& c=ExprConstant::new_scalar(1.0);

	// =============================================
	// before release 2.1.6:
//	const ExprNode* args[2];
//	args[0]=&z;
//	args[1]=&c;
	// before release 2.1.6:

	// from release 2.1.6 and subsequents:
	Array<const ExprNode> args(2);
	args.set_ref(0,z);
	args.set_ref(1,c);
	// =============================================

	Function g(z,f(args),"g");

	output << g << endl;
	// ![func-apply-array-C]
	output << "![func-apply-array-O]" << endl;
	}


	{
	output << "![func-diff-O]" << endl;
	// ![func-diff-C]
	Function f("x","y","z","x*y*z");
	Function df(f,Function::DIFF);
	output << "df=" << df << endl;
	// ![func-diff-C]
	output << "![func-diff-O]" << endl;
	}

	{
	Variable x("x"),y("y");

	SystemFactory fac;
	fac.add_var(x);
	fac.add_var(y);
	fac.add_ctr(sqr(x)+sqr(y)<=1);
	fac.add_ctr(y>=sqr(x));
	fac.add_ctr(y+x=1);

	System sys(fac);
	output << "![sys-copy-O]" << endl;
	// ![sys-copy-C]
	output << "original system:"         << endl;
	output << "-------------------------"<< endl;
	output << sys;
	output << "-------------------------"<< endl << endl;

	System sys2(sys,System::INEQ_ONLY);

	output << "system with only inequalities" << endl;
	output << "-------------------------"<< endl;
	output << sys2;
	output << "-------------------------"<< endl << endl;
	// ![sys-copy-C]
	output << "![sys-copy-O]" << endl;


	output << "![sys-normalize-O]" << endl;
	// ![sys-normalize-C]
	output << "original system:"         << endl;
	output << "-------------------------"<< endl;
	output << sys;
	output << "-------------------------"<< endl << endl;

	// normalize the system with a "thickness"
	// set to 0.1
	NormalizedSystem norm_sys(sys,0.1);

	output << "normalized system:"       << endl;
	output << "-------------------------"<< endl;
	output << norm_sys;
	output << "-------------------------"<< endl;
	// ![sys-normalize-C]
	output << "![sys-normalize-O]" << endl;

	}

	{
	Variable x("x"),y("y");

	SystemFactory fac;
	fac.add_var(x);
	fac.add_var(y);
	fac.add_goal(x+y);
	fac.add_ctr(sqr(x)+sqr(y)<=1);
	fac.add_ctr(y<=sqr(x));

	System sys(fac);

	output << "![sys-extended-O]" << endl;
	// ![sys-extended-C]
	output << "original system:"         << endl;
	output << "-------------------------"<< endl;
	output << sys;
	output << "-------------------------"<< endl;
	output << "  number of variables:"   << sys.nb_var  << endl;
	output << "  number of constraints:" << sys.nb_ctr  << endl << endl;

	ExtendedSystem ext_sys(sys);

	output << "extended system:"         << endl;
	output << "-------------------------"<< endl;
	output << ext_sys;
	output << "-------------------------"<< endl;
	output << "  number of variables:"   << ext_sys.nb_var << endl;
	output << "  number of constraints:" << ext_sys.nb_ctr << endl;
	output << "  goal name:"             << ext_sys.goal_name() << endl;
	output << "  goal variable:"         << ext_sys.goal_var() << endl;
	output << "  goal constraint:"       << ext_sys.goal_ctr() << endl;
	// ![sys-extended-C]
	output << "![sys-extended-O]" << endl;

	}
	{
	Variable x("x"),y("y");

	SystemFactory fac;
	fac.add_var(x);
	fac.add_var(y);
	fac.add_goal(x+y);
	fac.add_ctr(sqr(x)+sqr(y)<=1);


	System sys(fac);

	output << "![sys-fritz-john-O]" << endl;
	// ![sys-fritz-john-C]
	output << "original system:"         << endl;
	output << "-------------------------"<< endl;
	output << sys;
	output << "-------------------------"<< endl;

	FritzJohnCond fj(sys);

	output << "Fritz-John system:"       << endl;
	output << "-------------------------"<< endl;
	output << fj << endl;
	output << "-------------------------"<< endl;
	output << "  number of variables:" << fj.nb_var << endl;
	// ![sys-fritz-john-C]
	output << "![sys-fritz-john-O]" << endl;
	}
	output.close();

	return 0;
}
int main() {

	ofstream output;
	output.open ("doc-contractor.txt");

	output << "================= this file is generated ==============" << endl;


	{
	vibes::beginDrawing ();
	vibes::newFigure("ctc-compo");

	//! [ctc-compo-1-C]

	// Create the function corresponding to an
	// hyperplane of angle alpha
	Variable x,y,alpha;
	Function f(x,y,alpha,cos(alpha)*x+sin(alpha)*y);

	// Size of the polygon
	int n=7;

	// Array to store constraints (for cleanup)
	Array<NumConstraint> ctrs(2*n);

	// Arrays to store contractors
	Array<Ctc> ctc_out(n), ctc_in(n);

	for (int i=0; i<n; i++) {
		// create the constraints of the two half-spaces
		// delimited by f(x,y,i*2pi/n)=0
		// and store them in the array
		ctrs.set_ref(2*i,  *new NumConstraint(x,y,f(x,y,i*2*Interval::pi()/n)<=1));
		ctrs.set_ref(2*i+1,*new NumConstraint(x,y,f(x,y,i*2*Interval::pi()/n)>1));

		// create the contractors for these constraints
		// and place them in the arrays
		ctc_out.set_ref(i,*new CtcFwdBwd(ctrs[2*i]));
		ctc_in.set_ref(i, *new CtcFwdBwd(ctrs[2*i+1]));
	}

	// Composition of the "outer" contractors
	CtcCompo ctc_polygon_out(ctc_out);
	// Union of the "inner" contractors
	CtcUnion ctc_polygon_in(ctc_in);
	//! [ctc-compo-1-C]

	SepCtcPair sep(ctc_polygon_in,ctc_polygon_out);
	Set set(2);
	sep.contract(set,0.01);
	ToVibes to_vibes(2);
	set.visit(to_vibes);
	vibes::endDrawing();

	//! [ctc-compo-2-C]
	// ************ cleanup ***************
	for (int i=0; i<n; i++) {
		delete &ctc_in[i];
		delete &ctc_out[i];
	}
	for (int i=0; i<2*n; i++) {
		delete &ctrs[i];
	}
	//! [ctc-compo-2-C]

	}

	{
	output << "! [ctc-propag-O]" << endl;
	//! [ctc-propag-2-C]

	// Load a system of constraints
	System sys(IBEX_BENCHS_DIR "/polynom/DiscreteBoundary-0100.bch");

	// The array of contractors we will use
	Array<Ctc> ctc(sys.nb_ctr);

	for (int i=0; i<sys.nb_ctr; i++)
		// Create contractors from constraints and store them in "ctc"
		ctc.set_ref(i,*new Count(sys.ctrs[i]));
	//! [ctc-propag-2-C]


	//! [ctc-propag-3-C]
	double prec=1e-03;            // Precision upto which we calculate the fixpoint
	//! [ctc-propag-3-C]

	//! [ctc-propag-4-C]
	// =============================== with simple fixpoint ==============================
	Count::count=0;               // initialize the counter

	CtcCompo compo(ctc);          // make the composition of all contractors
	CtcFixPoint fix(compo,prec);  // make the fixpoint

	IntervalVector box=sys.box;   // tested box (load domains written in the file)

	fix.contract(box);

	output << " Number of contractions with simple fixpoint=" << Count::count << endl;
	//! [ctc-propag-4-C]

	//! [ctc-propag-5-C]
	// ================================= with propagation =================================
	Count::count=0;                            // initialize the counter

	CtcPropag propag(ctc, prec);   // Propagation of all contractors

	IntervalVector box2=sys.box;               // tested box (load domains written in the file)

	propag.contract(box2);

	output << " Number of contractions with propagation=" << Count::count << endl;

	output << " Are the results the same? " << (box.rel_distance(box2)<prec? "YES" : "NO") << endl;

	//! [ctc-propag-5-C]

	output << "! [ctc-propag-O]" << endl;

	}

	{
	output << "! [ctc-input-output-O]" << endl;
	// Load a system of constraints
	System sys(IBEX_BENCHS_DIR "/polynom/DiscreteBoundary-0100.bch");

	// The array of contractors we will use
	Array<Ctc> ctc(sys.nb_ctr);

	for (int i=0; i<sys.nb_ctr; i++)
		// Create contractors from constraints and store them in "ctc"
		ctc.set_ref(i,*new Count2(sys.ctrs[i]));
	//! [ctc-propag-2-C]


	//! [ctc-propag-3-C]
	double prec=1e-03;            // Precision upto which we calculate the fixpoint
	//! [ctc-propag-3-C]

	//! [ctc-propag-4-C]
	// =============================== with simple fixpoint ==============================
	Count2::count=0;               // initialize the counter

	CtcPropag propag(ctc, prec);   // Propagation of all contractors

	IntervalVector box2=sys.box;               // tested box (load domains written in the file)

	propag.contract(box2);

	output << " Number of contractions with propagation=" << Count2::count << endl;
	output << "! [ctc-input-output-O]" << endl;
	}

	{
	output << "! [ctc-hc4-O]" << endl;
	//! [ctc-hc4-C]

	// Load a system of equations
	System sys(IBEX_BENCHS_DIR "/others/hayes1.bch");
	// Create the HC4 propagation loop with this system
	CtcHC4 hc4(sys);

	// Test the contraction
	IntervalVector box(sys.box);
	output << " Box before HC4:" << box << endl;
	hc4.contract(box);
	output << " Box after HC4:" << box << endl;

	//! [ctc-hc4-C]
	output << "! [ctc-hc4-O]" << endl;
	}

	{
	output << "! [ctc-inv-O]" << endl;
	//! [ctc-inv-C]

	// Build a contractor on R² wrt (x>=0 and y>=0).

	Function gx("x","y","x");   // build (x,y)->x
	Function gy("x","y","y");   // build (x,y)->y

	NumConstraint geqx(gx,GEQ); // build x>=0
	NumConstraint geqy(gy,GEQ); // build y>=0

	CtcFwdBwd cx(geqx);
	CtcFwdBwd cy(geqy);

	CtcCompo compo(cx,cy);     // final contractor wrt (x>=0, y>=0)

	// Build a mapping from R to R²
	Function f("t","(cos(t),sin(t))");

	// Build the inverse contractor
	CtcInverse inv(compo,f);

	double pi=3.14;
	IntervalVector box(1,Interval(0,2*pi));

	inv.contract(box);
	output << "contracted box (first time):" << box << endl;

	inv.contract(box);
	output << "contracted box (second time):" << box << endl;

	//! [ctc-inv-C]
	output << "! [ctc-inv-O]" << endl;
	}

	{
	output << "! [ctc-polytope-1-O]" << endl;
	//! [ctc-polytope-1-C]

	// build the matrix
	double _A[4]= {1,1,1,-1};
	Matrix A(2,2,_A);

	// build the vector
	Vector b=Vector::zeros(2);

	// create the linear system (with fixed matrix/vector)
	LinearizerFixed lin(A,b);

	// create the contractor w.r.t the linear system
	CtcPolytopeHull ctc(lin);

	// create a box
	IntervalVector box(2,Interval(-1,1));

	// contract it
	output << "box before contraction=" << box << endl;
	ctc.contract(box);
	output << "box after contraction=" << box << endl;

	//! [ctc-polytope-1-C]
	output << "! [ctc-polytope-1-O]" << endl;
	}

	{
	output << "! [ctc-exist-1-O]" << endl;
	//! [ctc-exist-1-C]

	// create a constraint on (x,y)
	Variable x,y;
	NumConstraint c(x,y,sqr(x)+sqr(y)<=1);

	// create domains for x and y
	IntervalVector box_x(1, Interval(-10,10));
	IntervalVector box_y(1, Interval(-1,1));

	// set the precision that controls how much y will be bisected
	double epsilon=1.0;

	// create a contractor on x by transforming y into an
	// existentially-quantified parameter.
	CtcExist exist_y(c,y,box_y,epsilon);

	// contract the domain of x
	output << "box before contraction=" << box_x << endl;
	exist_y.contract(box_x);
	output << "box after contraction=" << box_x << endl;

	//! [ctc-exist-1-C]
	output << "! [ctc-exist-1-O]" << endl;
	}

	{
	output << "! [ctc-exist-2-O]" << endl;
	//! [ctc-exist-2-C]

	// create a conjunction of two constraint on (x,y)
	Variable x,y;
	Function f(x,y,Return(sqr(x)+sqr(2*y-y),y-x));
	IntervalVector z(2);
	z[0]=Interval(0,1);
	z[1]=Interval::zero();
	NumConstraint c(x,y,f(x,y)=z);

	// create domains for y
	IntervalVector box_y(1, Interval(-1,1));

	// observe the result of the contraction for
	// different precision epsilon=10^{-_log}
	for (int _log=0; _log<=8; _log++) {
		// create the domain for x
		IntervalVector box_x(1, Interval(-10,10));

		// create the exist-contractor with the new precision
		CtcExist exist_y(c,y,box_y,::pow(10,-_log));

		// contract the box
		exist_y.contract(box_x);

		output << "epsilon=1e-" << _log << " box after contraction=" << box_x << endl;
	}
	//! [ctc-exist-2-C]
	output << "! [ctc-exist-2-O]" << endl;
	}

	{
	output << "! [ctc-exist-3-O]" << endl;
	//! [ctc-exist-3-C]

	// create a system
	Variable x,y;
	SystemFactory fac;
	fac.add_var(x);
	fac.add_var(y);
	fac.add_ctr(sqr(x)+sqr(2*y-y)<=1);
	fac.add_ctr(x=y);

	System sys(fac);

	CtcHC4 hc4(sys);

	// create domains for y
	IntervalVector box_y(1, Interval(-1,1));

	// Creates the bitset structure that indicates which
	// component are "quantified". The indices vary
	// from 0 to 1 (2 variables only). The bitset is
	// initially empty which means that, by default,
	// all the variables are parameters.
	BitSet vars(2);

	// Add "x" as variable.
	vars.add(0);

	// observe the result of the contraction for
	// different precision epsilon=10^{-_log}
	for (int _log=0; _log<=8; _log++) {
		// create the domain for x
		IntervalVector box_x(1, Interval(-10,10));

		// create the exist-contractor with the new precision
		CtcExist exist_y(hc4,vars,box_y,::pow(10,-_log));

		// contract the box
		exist_y.contract(box_x);

		output << "epsilon=1e-" << _log << " box after contraction=" << box_x << endl;
	}
	//! [ctc-exist-3-C]
	output << "! [ctc-exist-3-O]" << endl;
	}



}
Exemple #6
0
int main() {

ofstream output;
output.open ("doc-covfiles.txt");

output << "================= this file is generated ==============" << endl;

{
//! [cov-ex1-build-C]
// create a simple inequality x^2+y^2<=1.
const ExprSymbol& x=ExprSymbol::new_("x");
const ExprSymbol& y=ExprSymbol::new_("y");
SystemFactory fac;
fac.add_var(x);
fac.add_var(y);
fac.add_ctr(sqr(x)+sqr(y)<=1);

System sys(fac);

// run the solver with a coarse precision (1.0)
// to have a limited number of boxes
DefaultSolver solver(sys,1);

// we set a very short storage capacity
// to have a partial solving only.
solver.cell_limit = 12;

solver.solve(sys.box);
//! [cov-ex1-build-C]

{
output << "! [cov-ex1-list-O]" << endl;
//! [cov-ex1-list-C]
const CovList& cov = solver.get_data();
output << "The list contains " << cov.size() << " boxes" << endl;
for (size_t i=0; i<cov.size(); i++) {
  output << "box n°" << i << " = " << cov[i] << endl;
}
//! [cov-ex1-list-C]
output << "! [cov-ex1-list-O]" << endl;
}

{
output << "! [cov-ex1-IUlist-O]" << endl;
//! [cov-ex1-IUlist-C]
const CovIUList& cov = solver.get_data();
output << "Inner boxes:" << endl;
for (size_t i=0; i<cov.nb_inner(); i++) {
  output << "inner n°" << i << " = " << cov.inner(i) << endl;
}
output << endl << "Unknown boxes:" << endl;
for (size_t i=0; i<cov.nb_unknown(); i++) {
  output << "unknown n°" << i << " = " << cov.unknown(i) << endl;
}
//! [cov-ex1-IUlist-C]
output << "! [cov-ex1-IUlist-O]" << endl;
}

{
output << "! [cov-ex1-SolverData-O]" << endl;
//! [cov-ex1-SolverData-C]
const CovSolverData& cov = solver.get_data();
output << "Inner boxes:" << endl;
for (size_t i=0; i<cov.nb_inner(); i++) {
  output << "inner n°" << i << " = " << cov.inner(i) << endl;
}
output << endl << "Unknown boxes:" << endl;
for (size_t i=0; i<cov.nb_unknown(); i++) {
  output << "unknown n°" << i << " = " << cov.unknown(i) << endl;
}
output << endl << "Pending boxes:" << endl;
for (size_t i=0; i<cov.nb_pending(); i++) {
  output << "Pending n°" << i << " = " << cov.pending(i) << endl;
}
//! [cov-ex1-SolverData-C]
output << "! [cov-ex1-SolverData-O]" << endl;
}
}

{
const ExprSymbol& x=ExprSymbol::new_("x");
const ExprSymbol& y=ExprSymbol::new_("y");
SystemFactory fac;
fac.add_var(x);
fac.add_var(y);
fac.add_ctr(sqr(x)+sqr(y)<=1);

System sys(fac);

//! [cov-ex2-solver-C]

// solve the problem with boundary boxes
// of diameter < 0.1
DefaultSolver solver(sys,0.1,0.1);
solver.solve(sys.box);

//! [cov-ex2-solver-C]

//! [cov-ex2-optim-C]

SystemFactory opt_fac;
opt_fac.add_var(x);
opt_fac.add_var(y);
opt_fac.add_goal(x+y);

System opt(opt_fac);
//! [cov-ex2-optim-C]

output << "! [cov-ex2-run-O]" << endl;
//! [cov-ex2-run-C]
DefaultOptimizer optim(opt);

// run the optimizer with solver data as input covering:
optim.optimize(solver.get_data());

output << " best bound=" << optim.get_loup() << endl;

//! [cov-ex2-run-C]
output << "! [cov-ex2-run-O]" << endl;

}


{
//! [cov-ex3-optim-sys-C]
// build the problem "minimize |x^2+y^2-1|"
// -------------------------------------
Function g("x","y","abs(x^2+y^2-[0.9,1.1])");
SystemFactory opt_fac;
opt_fac.add_var(g.args());
opt_fac.add_goal(g);
System opt(opt_fac);

// we need bounded domain for unconstrained optimization:
opt.box[0]=Interval(-100,100);
opt.box[1]=Interval(-100,100);
// -------------------------------------
//! [cov-ex3-optim-sys-C]

//! [cov-ex3-optim-run-C]
// solve it
DefaultOptimizer optim(opt); // 1 is a very coarse precision
optim.timeout = 1;
optim.extended_COV = false;
optim.optimize(opt.box);
//! [cov-ex3-optim-run-C]

//! [cov-ex3-solver-sys-C]
// Build the system "solve x+y=0"
// -------------------------------------
Function f("x","y","x-y");
NumConstraint c(f);
SystemFactory sys_fac;
sys_fac.add_var(g.args());
sys_fac.add_ctr(c);
System sys(sys_fac);
// -------------------------------------
//! [cov-ex3-solver-sys-C]

output << "! [cov-ex3-solver-run-O]" << endl;
//! [cov-ex3-solver-run-C]
// solve the problem
DefaultSolver solver(sys,0.1,0.1);
solver.solve(optim.get_data());
output << solver.get_data() << endl;

//! [cov-ex3-solver-run-C]
output << "! [cov-ex3-solver-run-O]" << endl;
}

}