public: vector_type simulate(vector_type const& u, vector_type const& e, real_type na_value = real_type(0)) const { namespace ublas = ::boost::numeric::ublas; namespace ublasx = ::boost::numeric::ublasx; size_type n_obs = u.size(); size_type n_e = e.size(); DCS_ASSERT( n_obs == n_e, throw ::std::logic_error("Size of Input Data and Noise Data does not match.") ); size_type n_a = ublasx::size(a_); // # of output channels size_type n_b = ublasx::size(b_); // # of input channels size_type k_min = 0; size_type k_max = n_obs - ::std::min(static_cast<size_type>(d_*ts_), n_obs); vector_type y(n_obs, na_value); // for (size_type k = 0; k < k_min; ++k) // { // y(k) = e_var_*e(k); // } for (size_type k = k_min; k < k_max; ++k) { y(k) = 0; if (n_a > 0 && k > 0) { size_type nn_a = ::std::min(n_a, k); y(k) -= ublas::inner_prod( //a_, ublas::subrange(a_, 0, nn_a), //::dcs::math::la::subslice(y, k-1, -1, n_a) ublas::subslice(y, k-1, -1, nn_a) ); } if (n_b > 0) { size_type nn_b = ::std::min(n_b, k+1); y(k) += ublas::inner_prod( //b_, ublas::subrange(b_, 0, nn_b), //::dcs::math::la::subslice(u, k, -1, n_b) ublas::subslice(u, k, -1, nn_b) ); } y(k) += e_var_*e(k); } return y; }
matrix_type Reflector(const vector_type& x) { using namespace boost::numeric::ublas; matrix_type F(x.size(), x.size()); Reflector<matrix_type, vector_type>(x, F); return F; }
void setBounds( vector_type const& __lb, vector_type const& __up ) { GST_SMART_ASSERT( __lb.size() == __up.size() )( __lb )( __up )( "inconsistent bounds definition" ); M_lb = __lb; M_ub = __up; M_lb_ub = __up - __lb; GST_SMART_ASSERT( *std::min_element( M_lb_ub.begin(), M_lb_ub.end() ) >= 0 ) ( M_lb )( M_ub )( "lower and upper bounds are not properly defined" ); }
bool is_empty() const { for (unsigned i = 0; i < m_lower.size(); ++i) if (m_lower[i] >= m_upper[i]) return true; return false; }
public: uniform_signal_generator(vector_type const& u_min, vector_type const& u_max, random_generator_type& rng) : rng_(rng), ub_( ::std::numeric_limits<value_type>::infinity()), lb_(-::std::numeric_limits<value_type>::infinity()) { // pre: size(u_min) == size(u_max) DCS_ASSERT(u_min.size() == u_max.size(), DCS_EXCEPTION_THROW(::std::invalid_argument, "Size of min and max vectors does not match")); ::std::size_t n(u_min.size()); for (::std::size_t i = 0; i < n; ++i) { distrs_.push_back(uniform_distribution_type(u_min[i], u_max[i])); } }
bool contains(VecType const &pt, double threshold=1e-10) const { for (unsigned i = 0; i < m_lower.size(); ++i) if (pt[i] < m_lower[i] - threshold || pt[i] >= m_upper[i]+threshold) return false; return true; }
public: vector_type simulate(vector_type const& u, real_type na_value = real_type(0)) const { namespace ublas = ::boost::numeric::ublas; namespace ublasx = ::boost::numeric::ublasx; size_type n_obs = u.size(); // # of samples size_type n_a = ublasx::size(a_); // # of output channels size_type n_b = ublasx::size(b_); // # of input channels //size_type k_min = ::std::max(n_a*ts_, (n_b+d_)*ts_)-1; //size_type k_min = (n_b > 0) ? ((n_b+d_)*ts_-1) : 0; size_type k_min = 0; //size_type k_max = n_obs - ::std::min(d_*ts_, n_obs); size_type k_max = n_obs - ::std::min(static_cast<size_type>(d_*ts_), n_obs); vector_type y(n_obs, na_value); // // Set non-computable outputs to the given N/A value // if (na_value != real_type(0)) // { // // Actually, this setting is done only if the N/A value // // is different from the default value '0'. // for (size_type k = 0; k < k_min; ++k) // { // y(k) = na_value; // } // } for (size_type k = k_min; k < k_max; ++k) { y(k) = 0; if (n_a > 0 && k > 0) { size_type nn_a = ::std::min(n_a, k); y(k) -= ublas::inner_prod( //a_, ublas::subrange(a_, 0, nn_a), //ublas::subslice(y, k-1, -1, n_a) ublas::subslice(y, k-1, -1, nn_a) ); } if (n_b > 0) { size_type nn_b = ::std::min(n_b, k+1); y(k) += ublas::inner_prod( //b_, ublas::subrange(b_, 0, nn_b), //::dcs::math::la::subslice(u, k, -1, n_b) ublas::subslice(u, k, -1, nn_b) ); } } return y; }
void DirScalingMatrix<NumType>::update( value_type const& __Delta, vector_type const& __x, vector_type const& __s, mode_type __mode ) { GST_SMART_ASSERT( __x.size() == M_lb.size() )( __x )( M_lb )( "inconsistent bounds definition" ); GST_SMART_ASSERT( __x.size() == M_ub.size() )( __x )( M_ub )( "inconsistent bounds definition" ); M_value.resize( __x.size(), __x.size(), 0, 0 ); M_jacobian.resize( __x.size(), __x.size(), 0, 0 ); M_zeta = zeta( __x ); vector_type __dl = distanceToLB( __x ); vector_type __du = distanceToUB( __x ); if ( M_zeta * std::min( norm_inf( __dl ), norm_inf( __du ) ) > __Delta ) { // we are in the trust region M_value = identity_matrix<value_type>( M_value.size1(), M_value.size2() ); } else { M_trust_region_active = true; for ( size_t __i = 0; __i < __x.size(); ++__i ) { if ( __s ( __i ) < 0 ) M_value ( __i, __i ) = M_zeta * std::min( 1. , __dl( __i ) ) / __Delta; else M_value ( __i, __i ) = M_zeta * std::min( 1. , __du( __i ) ) / __Delta; } } if ( __mode == WITH_JACOBIAN ) { for ( size_t i = 0; i < __x.size(); i++ ) { if ( ( __s( i ) < 0 ) && ( __x( i ) < M_lb( i ) + __Delta ) ) { M_jacobian ( i, i ) = M_zeta / __Delta; } else if ( ( __s ( i ) > 0 ) && ( __x ( i ) > M_ub( i ) - __Delta ) ) { M_jacobian ( i, i ) = -M_zeta / __Delta; } else { M_jacobian ( i, i ) = 0; } } } }
decorated_tuple(cow_pointer_type d, const vector_type& v) : super(tuple_impl_info::statically_typed) , m_decorated(std::move(d)), m_mapping(v) { # ifdef CPPA_DEBUG const cow_pointer_type& ptr = m_decorated; // prevent detaching # endif CPPA_REQUIRE(ptr->size() >= sizeof...(ElementTypes)); CPPA_REQUIRE(v.size() == sizeof...(ElementTypes)); CPPA_REQUIRE(*(std::max_element(v.begin(), v.end())) < ptr->size()); }
decorated_tuple::cow_ptr decorated_tuple::make(cow_ptr d, vector_type v) { auto ptr = dynamic_cast<const decorated_tuple*>(d.get()); if (ptr) { d = ptr->decorated(); auto& pmap = ptr->mapping(); for (size_t i = 0; i < v.size(); ++i) { v[i] = pmap[v[i]]; } } return make_counted<decorated_tuple>(std::move(d), std::move(v)); }
void do_filter(const vector_type& src, vector_type& dest) { typedef regex_iterator<const Ch*, Ch, Tr> iterator; if (src.empty()) return; iterator first(&src[0], &src[0] + src.size(), re_, flags_); iterator last; const Ch* suffix = 0; // Prevent GCC 2.95 warning. for (; first != last; ++first) { dest.insert( dest.end(), first->prefix().first, first->prefix().second ); string_type replacement = replace_(*first); dest.insert( dest.end(), replacement.begin(), replacement.end() ); suffix = first->suffix().first; } dest.insert(dest.end(), suffix, &src[0] + src.size()); }
value_type NBS::operator() (const vector_type& in, const value_type T, vector_type& out) const { out = vector_type::Zero (in.size()); value_type max_value = value_type(0); for (ssize_t seed = 0; seed != in.size(); ++seed) { if (std::isfinite (in[seed]) && in[seed] >= T && !out[seed]) { BitSet visited (in.size()); visited[seed] = true; vector<size_t> to_expand (1, seed); size_t cluster_size = 0; while (to_expand.size()) { const uint32_t index = to_expand.back(); to_expand.pop_back(); cluster_size++; for (vector<size_t>::const_iterator i = (*adjacency)[index].begin(); i != (*adjacency)[index].end(); ++i) { if (!visited[*i] && std::isfinite(in[*i]) && in[*i] >= T) { visited[*i] = true; to_expand.push_back (*i); } } } max_value = std::max (max_value, value_type(cluster_size)); for (ssize_t i = 0; i != in.size(); ++i) out[i] += (visited[i] ? 1.0 : 0.0) * cluster_size; } } return max_value; }
per_turn_visitor(std::string const& complete_caseid, vector_type const& points) { namespace bg = boost::geometry; if (points.size() > 100u) { // Defensive check. Too much intersections. Don't create anything return; } BOOST_FOREACH(pair_type const& p, points) { mappers.push_back(new mapper_visitor<Point>(complete_caseid, p.second, p.first)); }
box intersect(box<VecType2> const &b2) const { const unsigned dims = m_lower.size(); const vector_type d_vector_lower(dims); const vector_type d_vector_upper(dims); box<vector_type> result(d_vector_lower, d_vector_upper); for (unsigned i = 0; i < dims; ++i) { result.m_lower[i] = std::max(m_lower[i], b2.m_lower[i]); result.m_upper[i] = std::min(m_upper[i], b2.m_upper[i]); } return result; }
vector_type solve(const matrix_type& A, const vector_type& y) { namespace ublas = boost::numeric::ublas; matrix_type A_factorized = A; ublas::permutation_matrix<size_t> pm(y.size()); int singular = lu_factorize(A_factorized, pm); if (singular) throw std::runtime_error("[LinearSolver<LU>::solve()] A is singular."); vector_type result(y); lu_substitute(A_factorized, pm, result); return result; }
DirScalingMatrix( vector_type const& __lb, vector_type const& __ub ) : M_lb( __lb ), M_ub( __ub ), M_lb_ub( M_ub - M_lb ), M_value( __lb.size(), __lb.size(), 0, 0 ), M_jacobian( __lb.size(), __lb.size(), 0, 0 ), M_trust_region_active( false ) { GST_SMART_ASSERT( __lb.size() == __ub.size() )( __lb )( __ub )( "inconsistent bounds definition" ); GST_SMART_ASSERT( *std::min_element( M_lb_ub.begin(), M_lb_ub.end() ) >= 0 ) ( M_lb )( M_ub )( "lower and upper bounds are not properly defined" ); }
void SolverUnconstrained<Data,Problem>::makeStep( vector_type & _x, vector_type & _s, value_type _Delta, f_type& __fx, vector_type & _Tgrad_fx, banded_matrix_type & _Hg, symmetric_matrix_type & _Thess_fxT, symmetric_matrix_type & _Htil ) { M_theta.update( _Delta, _x, _s ); _Tgrad_fx = prod( M_theta(), __fx.gradient( 0 ) ); banded_matrix_type __diag_grad_fx( _E_n, _E_n, 0, 0 ); matrix<value_type> __m( outer_prod( _Tgrad_fx, scalar_vector<value_type>( _Tgrad_fx.size(), 1 ) ) ); __diag_grad_fx = banded_adaptor<matrix<value_type> >( __m, 0, 0 ); _Hg = prod( M_theta.jacobian(), __diag_grad_fx ); _Thess_fxT = prod( M_theta(), prod( __fx.hessian( 0 ), M_theta() ) ); _Htil = _Thess_fxT + _Hg; }
void Reflector(const vector_type& x, matrix_type& F) { using namespace boost::numeric::ublas; typedef typename matrix_type::value_type value_type; unit_vector<value_type> e1(x.size(), 0); //v_k = -sgn( x(1) ) * inner_prod(x) * e1 + x; double x_2 = norm_2(x); boost::numeric::ublas::vector<value_type> v_k((x(0) >= 0 ? x_2 : -1 * x_2) * e1 + x); //v_k = v_k / norm_2(v_k); double norm_vk = norm_2(v_k); if (norm_vk != 0) v_k /= norm_2(v_k); // F = A(k:m,k:n) - 2 * outer_prod(v_k, v_k) * A(k:m,k:n) identity_matrix<value_type> eye(v_k.size()); F = matrix_type(v_k.size(), v_k.size()); F = eye - 2. * outer_prod(v_k, v_k); }
boost::logic::tribool eval(const partial_assignments_type& phi) { typedef std::set<value_type> set_type; set_type collected_values; typedef typename set_type::size_type size_type; size_type set_size = 0; typedef typename vector_type::const_iterator vector_iterator; const vector_iterator& end(variable_vector.end()); for (vector_iterator i = variable_vector.begin(); i != end; ++i) { const domain_type& D(phi(*i)); if (D.size() == 0) return false; if (D.size() == 1) { collected_values.insert(*(i -> second.begin())); if (collected_values == set_size++) return false; } } assert(set_size == collected_values.size()); if (set_size == variable_vector.size()) return true; else return boost::logic::indeterminate; }
bool SolverUnconstrained<Data,Problem>::optimize( vector_type& x ) { M_solver_stats.clear(); // Controlling parameters // trust region radius value_type Delta = M_options.Delta_init; vector_type x_new ( x.size() ); vector_type stot ( x.size() ); value_type norm_Tgrad_fx = this->norm_Theta_x_grad_fx( x, Delta ); value_type norm_s_til = 0; M_prob.copy_x0_to_x( x ); // FIXME: if ( M_options.verbose() ) //M_prob.print_complete( x ); if ( M_solver_stats.collectStats() ) M_solver_stats.push( norm_Tgrad_fx, 0, 0, 0, 0, 0, 0, 0, Delta, 0, 0, 0 ); try { int iter = 0; // // Apply bound constrained Trust region algorithm // while ( iter == 0 || ( iter < M_options.max_TR_iter && norm_Tgrad_fx > M_options.TR_tol ) ) { iter++; int n_CGiter, n_restarts, n_indef, n_crosses_def, n_crosses_indef, n_truss_exit_def, n_truss_exit_indef; value_type _s_til_x_G_til_x_s_til, phi_til, rho, Delta_used = Delta; DVLOG(2) << "\n===================== iter = " << iter << " ==========================="; //DVLOG(2) << "\nx = " << x << "\n"; DVLOG(2) << "\n -> norm_Tgrad_fx = " << norm_Tgrad_fx; /** find an approximate stot for the step to make * solve : * Find \f$\tilde{s}^k = \mathrm{arg}\mathrm{min}_{s \in R^n}{\tilde{\phi}^k : ||s|| < \Delta^k}\f$ */ this->CGstep( x, Delta, stot, norm_s_til, n_CGiter, n_restarts, n_indef, n_crosses_def, n_crosses_indef, n_truss_exit_def, n_truss_exit_indef, _s_til_x_G_til_x_s_til, phi_til ); // x_new = x + stot; f_type __fx_new; M_prob.evaluate( x_new, __fx_new, diff_order<0>() ); f_type __fx; M_prob.evaluate( x, __fx, diff_order<0>() ); // compute actual merit function reduction value_type ared_til = __fx_new.value( 0 ) - __fx.value( 0 ) + 0.5 * _s_til_x_G_til_x_s_til; rho = ared_til / phi_til; ///////////////////////////////////////////////////////////////////////// // Trust region radius calculation: if ( M_options.allow_Trust_radius_calculations ) { if ( rho <= 1e-8 ) Delta = M_options.rho_decrease * Delta; else { x = x + stot; if ( rho > M_options.rho_big ) Delta = std::max( M_options.rho_increase_big*norm_s_til, Delta ); else if ( rho > M_options.rho_small ) Delta = std::max( M_options.rho_increase_small*norm_s_til, Delta ); } norm_Tgrad_fx = this->norm_Theta_x_grad_fx( x, Delta ); } else { x = x + stot; norm_Tgrad_fx = this->norm_Theta_x_grad_fx( x, Delta ); } ///////////////////////////////////////////////////////////////////////// if ( M_solver_stats.collectStats() ) { M_solver_stats.push( norm_Tgrad_fx, n_CGiter, n_restarts, n_indef, n_crosses_def, n_crosses_indef, n_truss_exit_def, n_truss_exit_indef, Delta_used, ared_til, phi_til, rho ); } if ( norm_Tgrad_fx > 1e-5 ) M_prob.setAccuracy( std::min( 1e-1, norm_Tgrad_fx ) ); DVLOG(2) << "norm_Tgrad_fx = " << norm_Tgrad_fx << "\n"; } } catch ( std::exception const& __ex ) { f_type __fx_new; M_prob.evaluate ( x, __fx_new, diff_order<2>() ); if ( norm_inf( __fx_new.gradient( 0 ) ) > 1e-10 ) throw __ex; } vector_type __l( _E_n ), __u( _E_n ); lambda_LS( x, __l, __u ); if ( M_solver_stats.collectStats() ) M_solver_stats.push( x, __l, __u ); return true; }
size_t size() const override final { return m_vector.size(); }
size_t byte_size() const { return m_vector.size() * sizeof(element_type); }
size_t size() const final { return m_vector.size(); }
box enlarged(typename vector_type::value_type scalar) const { return enlarged( boost::numeric::ublas::scalar_vector<typename vector_type::value_type>( m_lower.size(), scalar)); }
vector_type distanceToUB( vector_type const& __x ) const { GST_SMART_ASSERT( __x.size() == M_ub.size() )( __x )( M_ub )( "inconsistent bounds definition" ); return element_div( __x - M_ub, M_lb_ub ); }
sys_func( const vector_type &_R ) : R( _R ) , n( _R.size() ) { }
size_t GetCurrentSize() const { return m_value->size(); }
inline int get_size() { return data.size(); }
void SolverUnconstrained<Data,Problem>::CGstep( vector_type & _x, value_type _Delta, vector_type & _sCG, value_type &norm_s_til, int &_CGiter, int &_n_restarts, int &_n_indef, int &_n_crosses_def, int &_n_crosses_indef, int &_n_truss_exit_def, int &_n_truss_exit_indef, value_type &_s_til_x_G_til_x_s_til, value_type &phi_til ) { bool _restart = false; bool _done = false; _n_restarts = 0; _CGiter = 0; _n_indef = 0; _n_crosses_def = 0; _n_crosses_indef = 0; _n_truss_exit_def = 0; _n_truss_exit_indef = 0; f_type __fx; vector_type _neg_grad_fx ( _x.size() ); vector_type _Tgrad_fx ( _x.size() ); vector_type _Htil_d ( _x.size() ); // Htil * dCG vector_type _Htil_s_til ( _x.size() ); // Htil * s_til vector_type _s_til ( _x.size() ); vector_type _s_til_old ( _x.size() ); vector_type _s_til_eps ( _x.size() ); vector_type _rCG ( _x.size() ); vector_type _rCG_old ( _x.size() ); vector_type _dCG ( _x.size() ); value_type _alpha = 0; value_type _gamma; // d' Htil d value_type _beta; value_type _xi = 0; value_type _tau; // diagonal matrices banded_matrix_type _Hg ( _x.size(), _x.size(), 0, 0 ); // Gtil symmetric_matrix_type _Thess_fxT ( _E_nA, _E_nA ); // Btil symmetric_matrix_type _Htil ( _E_nA, _E_nA ); // Htil DVLOG(2) << "\n\n[value_type SolverUnconstrained<Data,Problem>::CGstep]...\n"; // INITIALIZE: this->makeCauchyStep( _x, _Delta, __fx, _Tgrad_fx, _Hg, _Thess_fxT, _Htil, _neg_grad_fx ); DVLOG(2) << "Trust region active (C) : " << M_theta.isTrustRegionActive() << "\n"; value_type _norm_gtil = norm_2( _Tgrad_fx ); _s_til = zero_vector<value_type>( _s_til.size() ); _s_til_old = _s_til; _rCG = -_Tgrad_fx; _rCG_old = _rCG; _dCG = _rCG; size_t _inner_iter = 0; _CGiter++; while ( !_done ) { if ( _restart ) { _n_restarts++; _CGiter++; // RE-INITIALIZE: _inner_iter = 0; _s_til_eps = _s_til + M_options.deps * _dCG; this->makeStep( _x, _s_til_eps, _Delta, __fx, _Tgrad_fx, _Hg, _Thess_fxT, _Htil ); _norm_gtil = norm_2( _Tgrad_fx ); _Htil_s_til = prod( _Htil, _s_til ); _s_til_old = _s_til; _rCG = -_Tgrad_fx - _Htil_s_til; _rCG_old = _rCG ; _dCG = _rCG; _restart = false; } // // STEP 1: // _gamma = inner_prod( _dCG, prod( _Htil,_dCG ) ); if ( _gamma <= 0 ) { _n_indef++; _tau = this->tau( _s_til, _dCG, _Delta ); if ( ( !M_theta.isTrustRegionActive() ) || ( _CGiter == 1 ) ) { _xi = _tau; } else if ( _inner_iter == 0 ) { _xi = this->xi( _s_til_eps, _dCG, _tau ); } else { _xi = this->xi( _s_til, _dCG, _tau ); } _s_til_old = _s_til; _s_til += _xi * _dCG; if ( _xi < _tau ) { _n_crosses_indef++; _restart = true; } else if ( M_theta.isTrustRegionActive() ) { _sCG = _s_til; _n_truss_exit_indef++; _done = true; } } // // STEP 2: // if ( !_restart && !_done ) { _alpha = inner_prod( _rCG, _rCG ) / _gamma; if ( ( !M_theta.isTrustRegionActive() ) || ( _CGiter == 1 ) ) _xi = _alpha; else if ( _inner_iter == 0 ) _xi = this->xi( _s_til_eps, _dCG, _alpha ); else _xi = this->xi( _s_til, _dCG, _alpha ); _s_til_old = _s_til; _s_til += _xi * _dCG; if ( norm_2( _s_til ) > _Delta ) { _tau = this->tau( _s_til_old, _dCG, _Delta ); _sCG = _s_til_old + _tau * _dCG; _n_truss_exit_def++; _done = true; } } // // STEP 3: // if ( !_restart && !_done ) { if ( _xi >= _alpha ) { _rCG_old = _rCG; _rCG -= _alpha * prod( _Htil, _dCG ); } else if ( M_theta.isTrustRegionActive() ) { _n_crosses_def++; _restart = true; } } if ( norm_2( _rCG ) / _norm_gtil < M_options.CGtol ) { _sCG = _s_til; DVLOG(2) << "\n\nNormal CG exit 1: ||rCG||/||g_til|| = " << norm_2( _rCG ) / _norm_gtil << "\n"; _done = true; } if ( _CGiter >= _x.size() ) { _sCG = _s_til; DVLOG(2) << "\n\nNormal CG exit 2: _CGiter = " << _CGiter << "\n"; _done = true; } // // STEP 4: // if ( !_restart && !_done ) { _beta = inner_prod( _rCG, _rCG ) / inner_prod( _rCG_old, _rCG_old ); DVLOG(2) << "\nbeta = " << _beta << "\n"; _dCG = _rCG + _beta * _dCG; _CGiter++; _inner_iter++; } } _s_til_x_G_til_x_s_til = inner_prod( _sCG, prod( _Hg, _sCG ) ); phi_til = inner_prod( _Tgrad_fx, _sCG ) + 0.5 * inner_prod( _sCG, prod( _Htil,_sCG ) ); norm_s_til = norm_2( _sCG ); // restoring to original space _sCG = prod( M_theta(), _sCG ); }
stat_counter(const char *desc) : m_index(m_counters.size()) { m_counters.push_back(item_type(desc, 0)); m_max_desc = std::max(m_max_desc, m_counters.back().first.length()); }