// 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(); } }
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; }
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_solver(SMTConfig& c, Egraph & e, Enode * l_int, vector<Enode*> invs, unordered_map<Enode*, int>& enode_to_rp_id) : m_config(c), m_egraph(e), m_int(l_int), m_invs(invs), m_enode_to_rp_id(enode_to_rp_id), m_stepControl(c.nra_ODE_step), m_time(nullptr) { // Pick the right flow_map (var |-> ODE) using current mode m_mode = l_int->getCdr()->getCar()->getValue(); map<string, Enode *> & flow_map = m_egraph.flow_maps[string("flow_") + to_string(m_mode)]; m_time = l_int->getCdr()->getCdr()->getCdr()->getCar(); string time_str = m_time->getCar()->getName(); // i.e. "time_1" m_step = stoi(time_str.substr(time_str.find_last_of("_") + 1)); // i.e. 1 Enode * var_list = l_int->getCdr()->getCdr()->getCdr()->getCdr(); // Collect _0, _t variables from variable list in integral literal while (!var_list->isEnil()) { string name = var_list->getCar()->getCar()->getName(); size_t second_ = name.find_last_of("_"); size_t first_ = name.find_last_of("_", second_ - 1); string name_prefix, name_postfix; if (first_ == string::npos) { name_prefix = name.substr(0, second_); name_postfix = name.substr(second_); } else { name_prefix = name.substr(0, first_); name_postfix = name.substr(first_); } if (flow_map.find(name_prefix) == flow_map.end()) { cerr << name_prefix << " is not found in flow_map." << endl; assert(flow_map.find(name_prefix) != flow_map.end()); } Enode * rhs = flow_map[name_prefix]; stringstream ss; rhs->print_infix(ss, true, name_postfix); if (rhs->isConstant() && rhs->getValue() == 0.0) { // If RHS of ODE == 0.0, we treat it as a parameter in CAPD m_pars.push_back(var_list->getCar()); m_par_list.push_back(name); } else { // Otherwise, we treat it as an ODE variable. m_0_vars.push_back(var_list->getCar()); m_t_vars.push_back(var_list->getCdr()->getCar()); m_var_list.push_back(name); m_ode_list.push_back(ss.str()); } var_list = var_list->getCdr()->getCdr(); } // join var_list to make diff_var, ode_list to diff_fun_forward string diff_var = ""; if (!m_var_list.empty()) { diff_var = "var:" + join(m_var_list, ", ") + ";"; } string diff_fun_forward = ""; string diff_fun_backward = ""; if (!m_ode_list.empty()) { diff_fun_forward = "fun:" + join(m_ode_list, ", ") + ";"; diff_fun_backward = "fun: -" + join(m_ode_list, ", -") + ";"; } // construct diff_sys_forward (string to CAPD) string diff_par; if (m_par_list.size() > 0) { diff_par = "par:" + join(m_par_list, ", ") + ";"; m_diff_sys_forward = diff_par; m_diff_sys_backward = diff_par; } m_diff_sys_forward += diff_var + diff_fun_forward; m_diff_sys_backward += diff_var + diff_fun_backward; DREAL_LOG_INFO << "diff_par : " << diff_par; DREAL_LOG_INFO << "diff_var : " << diff_var; DREAL_LOG_INFO << "diff_fun_forward : " << diff_fun_forward; DREAL_LOG_INFO << "diff_fun_backward : " << diff_fun_backward; DREAL_LOG_INFO << "diff_sys_forward : " << m_diff_sys_forward; DREAL_LOG_INFO << "diff_sys_backward : " << m_diff_sys_backward; for (auto ode_str : m_ode_list) { string const func_str = diff_par + diff_var + "fun:" + ode_str + ";"; m_funcs.push_back(IFunction(func_str)); }; m_inv = extract_invariants(); }
bool ode_solver::contain_NaN(C0Rect2Set const & s) { return contain_NaN(IVector(s)); }
bool ode_solver::check_invariant(C0Rect2Set & s, IVector const & inv) { IVector v(s); bool r = check_invariant(v, inv); s = C0Rect2Set(v); return r; }
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)); }