bool ode_solver::inner_loop_backward(ITaylor & solver, interval prevTime, vector<pair<interval, IVector>> & bucket) { interval const stepMade = solver.getStep(); const ITaylor::CurveType& curve = solver.getCurve(); interval domain = interval(0, 1) * stepMade; list<interval> intvs; if (prevTime.rightBound() < m_T.leftBound()) { interval pre_T = interval(0, m_T.leftBound() - prevTime.rightBound()); domain.setLeftBound(m_T.leftBound() - prevTime.rightBound()); intvs = split(domain, m_config.nra_ODE_grid_size); intvs.push_front(pre_T); } else { intvs = split(domain, m_config.nra_ODE_grid_size); } for (interval subsetOfDomain : intvs) { interval dt = prevTime + subsetOfDomain; IVector v = curve(subsetOfDomain); if (!check_invariant(v, m_inv)) { // TODO(soonhok): invariant return true; } DREAL_LOG_INFO << dt << "\t" << v; if (prevTime + subsetOfDomain.rightBound() > m_T.leftBound()) { bucket.emplace_back(dt, v); } // TODO(soonhok): visualization // if (m_config.nra_json) { // m_trajectory.emplace_back(m_T.rightBound() - (prevTime + subsetOfDomain), v); // } } return false; }
// Rigorous proof of the existence of zero for second iteration of Henon map // Instead of IMap class we use defined above class MapIterator. void HenonProof() { std::cout << "\n\n SECOND ITERATION OF HENON IMap: \n H(x,y) = (1 - a*x*x + y, b*x) where a=1.4, b=0.3 "; IMap Henon("par:a,b;var:x,y;fun:1-a*x*x+y,b*x;"); Henon.setParameter("a",interval(1.4)); Henon.setParameter("b",interval(0.3)); MapIterator IMap(Henon, 2); capd::newton::Krawczyk<MapIterator> henon(IMap); std::cout << "\n\n KRAWCZYK PROOF \n\n "; DVector x(2); // Good quess for zero for second iteration of Henon map x[0] = -10./3; x[1]=131./9; // It is also a center of the set. double size = 1.e-5; // Size of the set in which we will search for a zero try{ capd::newton::KrawczykResult code = henon.proof(x, size); std::cout << resultToText(code) ; std::cout << "\n\n afer " << henon.numberOfIterations << " iteration of Krawczyk method" << "\n set X " << henon.X << "\n Krawczyk operator K " << henon.K << std::endl; } catch(std::exception& e) { std::cout << e.what(); } }
IVector ode_solver::extract_invariants() { map<Enode*, pair<double, double>> inv_map; for (auto inv : m_invs) { Enode * p = inv->getCdr()->getCdr()->getCdr()->getCdr()->getCar(); Enode * op = p->getCar(); bool pos = true; // Handle Negation if (op->getId() == ENODE_ID_NOT) { p = p->getCdr()->getCar(); op = p->getCar(); pos = false; } switch (op->getId()) { case ENODE_ID_GEQ: case ENODE_ID_GT: // Handle >= & > pos = !pos; case ENODE_ID_LEQ: case ENODE_ID_LT: { // Handle <= & < Enode * lhs = pos ? p->getCdr()->getCar() : p->getCdr()->getCdr()->getCar(); Enode * rhs = pos ? p->getCdr()->getCdr()->getCar() : p->getCdr()->getCar(); if (lhs->isVar() && rhs->isConstant()) { if (inv_map.find(lhs) != inv_map.end()) { inv_map[lhs].second = rhs->getValue(); } else { inv_map.emplace(lhs, make_pair(lhs->getLowerBound(), rhs->getValue())); } } else if (lhs->isConstant() && rhs->isVar()) { if (inv_map.find(rhs) != inv_map.end()) { inv_map[rhs].first = lhs->getValue(); } else { inv_map.emplace(rhs, make_pair(lhs->getValue(), rhs->getUpperBound())); } } else { cerr << "ode_solver::extract_invariant: error:" << p << endl; } } break; default: cerr << "ode_solver::extract_invariant: error" << p << endl; } } IVector ret (m_t_vars.size()); unsigned i = 0; for (auto const & m_t_var : m_t_vars) { if (inv_map.find(m_t_var) != inv_map.end()) { auto inv = interval(inv_map[m_t_var].first, inv_map[m_t_var].second); DREAL_LOG_INFO << "Invariant extracted from " << m_t_var << " = " << inv; ret[i++] = inv; } else { auto inv = interval(m_t_var->getLowerBound(), m_t_var->getUpperBound()); DREAL_LOG_INFO << "Default Invariant set for " << m_t_var << " = " << inv; ret[i++] = inv; } } return ret; }
ode_solver::ODE_result ode_solver::simple_ODE_backward(IVector & X_0, IVector const & X_t, interval const & T, IVector const & inv, vector<IFunction> & funcs) { // X_0 = X_0 \cup (X_t - + (d/dt Inv) * T) for (int i = 0; i < X_0.dimension(); i++) { interval & x_0 = X_0[i]; interval const & x_t = X_t[i]; IFunction & dxdt = funcs[i]; for (Enode * par : m_pars) { double lb = get_lb(par); double ub = get_ub(par); string name = par->getCar()->getName(); dxdt.setParameter(name, interval(lb, ub)); } try { interval const new_x_0 = x_t - dxdt(inv) * T; if (!intersection(new_x_0, x_0, x_0)) { DREAL_LOG_INFO << "Simple_ODE: no intersection for X_0"; return ODE_result::UNSAT; } } catch (exception& e) { DREAL_LOG_INFO << "Exception in Simple_ODE: " << e.what(); } } // update IVector_to_varlist(X_0, m_0_vars); return ODE_result::SAT; }
IVector ode_solver::varlist_to_IVector(vector<Enode*> const & vars) { IVector intvs (vars.size()); /* Assign current interval values */ for (unsigned i = 0; i < vars.size(); i++) { Enode* const & var = vars[i]; interval & intv = intvs[i]; double lb = get_lb(var); double ub = get_ub(var); intv = interval(lb, ub); DREAL_LOG_INFO << "The interval on " << var->getCar()->getName() << ": " << intv; } return intvs; }
// For Henon map we iterate Krawczyk and Newton Interval Operators. // In this example Newton Method requires much smaller initial set then Krawczyk to work. void HenonMap() { std::cout << "\n\n HENON map: H(x,y) = (1 - a*x*x + y, b*x) where a=1.4, b=0.3 "; // We define henon map and set its parameters IMap Henon("par:a,b;var:x,y;fun:1-a*x*x+y,b*x;"); Henon.setParameter("a", interval(1.4)); Henon.setParameter("b",interval(0.3)); IVector x0(2), K(2), N(2); // We define an initial set for iterations of taking Krawczyk Operator K[0] = interval(-100,110); K[1]=interval(-100,110); std::cout << "\n\n Interval KRAWCZYK OPERATOR " << "\n for set X = " << K; for(int i=1; i<=5; ++i) { x0=midVector(K); K = capd::newton::KrawczykOperator(x0, K, Henon); std::cout << "\n iteration "<< i << " = " << K; } // We define an initial set for iterations of taking Newton Operator N[0]=interval(-.1,.1); N[1] = interval(0.9,1.1); std::cout << "\n\n Interval NEWTON OPERATOR " << "\n for set X = " << N; for(int i=1; i<=5; ++i) { x0=midVector(N); N = capd::newton::NewtonOperator(x0, N, Henon); std::cout << "\n iteration "<< i << " = " << N; } }
ode_solver::ODE_result ode_solver::compute_backward(vector<pair<interval, IVector>> & bucket) { ODE_result ret = ODE_result::SAT; auto start = high_resolution_clock::now(); bool invariantViolated = false; try { // Set up VectorField IMap vectorField(m_diff_sys_backward); for (Enode * par : m_pars) { double lb = get_lb(par); double ub = get_ub(par); string name = par->getCar()->getName(); vectorField.setParameter(name, interval(lb, ub)); } ITaylor solver(vectorField, m_config.nra_ODE_taylor_order, .001); ITimeMap timeMap(solver); C0Rect2Set s(m_X_t); timeMap.stopAfterStep(true); timeMap.turnOnStepControl(); // TODO(soonhok): visualization // if (m_config.nra_json) { // m_trajectory.clear(); // m_trajectory.emplace_back(m_T.rightBound() - timeMap.getCurrentTime(), IVector(s)); // } interval prevTime(0.); do { // Handle Timeout if (m_config.nra_ODE_timeout > 0.0) { auto end = high_resolution_clock::now(); if (duration_cast<milliseconds>(end - start).count() >= m_config.nra_ODE_timeout) { return ODE_result::TIMEOUT; } } // Check Invariant invariantViolated = !check_invariant(s, m_inv); if (invariantViolated) { // TODO(soonhok): invariant if (timeMap.getCurrentTime().rightBound() < m_T.leftBound()) { ret = ODE_result::UNSAT; } else { ret = ODE_result::SAT; } break; } // Control TimeStep timeMap.turnOnStepControl(); if (m_stepControl > 0 && solver.getStep() < m_stepControl) { timeMap.turnOffStepControl(); solver.setStep(m_stepControl); timeMap.setStep(m_stepControl); } // Move s toward m_T.rightBound() timeMap(m_T.rightBound(), s); if (contain_NaN(s)) { return ODE_result::SAT; } if (m_T.leftBound() <= timeMap.getCurrentTime().rightBound()) { invariantViolated = inner_loop_backward(solver, prevTime, bucket); if (invariantViolated) { // TODO(soonhok): invariant ret = ODE_result::SAT; break; } } prevTime = timeMap.getCurrentTime(); } while (!invariantViolated && !timeMap.completed()); } catch (exception& e) { ret = ODE_result::EXCEPTION; } if (m_config.nra_json) { prune_trajectory(m_T, m_X_0); } return ret; }
void ode_solver::update(rp_box b) { m_b = b; m_X_0 = varlist_to_IVector(m_0_vars); m_X_t = varlist_to_IVector(m_t_vars); m_T = interval(get_lb(m_time), get_ub(m_time)); }