void Kernel::initScheduler() { srand_(time_()); _handlerManager->getIrqHandler()->registerHandler(HalTimerDriver::irqNumberForTimer(GPTIMER1), interrupted); HalTimerDriver::init(GPTIMER1, GPT_IRQMODE_OVERFLOW, 10); HalTimerDriver::start(GPTIMER1); }
void main() { char *hizuke[8], *jikan[8]; int itim; date_(hizuke); utime_(jikan); uitime_(&itim); printf("date : %s\n",hizuke); printf("utime : %s\n",jikan); printf("time : %d\n",time_()); printf("uitime: %d\n",itim); }
static void update_player(t_player *player, double *tdt) { fds c; double dt; (void)tdt; if (!player) return ; c = player->client; dt = time_d(player->foodlt); player->foodlt = time_(); player->foodt -= (dt / (((double)delay_life / get_delay()) * get_time())); player->food = (uint)abs(player->foodt); if (player->foodt <= 0) c ? (void)net_close_msg(c, "mort") : (void)player_destroy(player); else if (player->foodt > 0) timer_helper(((player->foodt * delay_life) * (get_time() / get_delay()))); }
static bool scheduler_a(fds c, _time dt, bool (*callb)(fds, void*), void *data) { t_client *info; t_scheduler *schedule; if (!c || !callb || !(info = c->trick)) return (false); schedule = &info->schedule; if ((schedule->state)) return (false); memset(schedule, 0, sizeof(*schedule)); schedule->state = (char)true; schedule->st = time_(); schedule->lt = schedule->st; schedule->dt = dt; schedule->callback = callb; schedule->data = data; return (true); }
bool event_dispatch(char *name, void *data, double delay) { t_event *evnt; bool out; out = false; if (!name || !(evnt = calloc(1, sizeof(*evnt)))) return (false); evnt->name = strdup(name); evnt->data = data; evnt->delay = delay; evnt->st = time_(); evnt->lt = evnt->st; if (delay > 0.0) out = set_new_event(evnt); else { out = eventm_dispatch(evnt); free(evnt); } return (out); }
// Initialize the coefficients needed for computing the Kalman Filter at future times as a function of // the time series at time, where time_(itime-1) < time < time_(itime) void KalmanFilterp::InitializeCoefs(double time, unsigned int itime, double ymean, double yvar) { kalman_gain_ = PredictionVar_ * rotated_ma_coefs_.t() / yvar; // initialize the coefficients for predicting the state vector at coefs(time_predict|time_predict) state_const_ = state_vector_ - kalman_gain_ * ymean; state_slope_ = kalman_gain_; // update the state one-step prediction error variance PredictionVar_ -= yvar * (kalman_gain_ * kalman_gain_.t()); // coefs(time_predict|time_predict) --> coefs(time[i+1]|time_predict) double dt = std::abs(time_(itime) - time); rho_ = arma::exp(omega_ * dt); state_const_ = rho_ % state_const_; state_slope_ = rho_ % state_slope_; // update the predicted state covariance matrix PredictionVar_ = (rho_ * rho_.t()) % (PredictionVar_ - StateVar_) + StateVar_; // compute the coefficients for the linear filter at time[ipredict], and compute the variance in the predicted // y[ipredict] yconst_ = std::real( arma::as_scalar(rotated_ma_coefs_ * state_const_) ); yslope_ = std::real( arma::as_scalar(rotated_ma_coefs_ * state_slope_) ); var(itime) = std::real( arma::as_scalar(rotated_ma_coefs_ * PredictionVar_ * rotated_ma_coefs_.t()) ) + yerr_(itime) * yerr_(itime); current_index_ = itime + 1; }
// Return the predicted value and its variance at time assuming a CAR(1) process std::pair<double, double> KalmanFilter1::Predict(double time) { double rho, var_ratio, previous_var; double ypredict_mean, ypredict_var, yprecision; unsigned int ipredict = 0; while (time > time_(ipredict)) { // find the index where time_ > time for the first time ipredict++; if (ipredict == time_.n_elem) { // time is greater than last element of time_, so do forecasting break; } } // Run the Kalman filter up to the point time_[ipredict-1] Reset(); for (int i=1; i<ipredict; i++) { Update(); } if (ipredict == 0) { // backcasting, so initialize the conditional mean and variance to the stationary values ypredict_mean = 0.0; ypredict_var = sigsqr_ / (2.0 * omega_); } else { // predict the value of the time series at time, given the earlier values double dt = time - time_[ipredict-1]; rho = exp(-dt * omega_); previous_var = var(ipredict-1) - yerr_(ipredict-1) * yerr_(ipredict-1); var_ratio = previous_var / var(ipredict-1); // initialize the conditional mean and variance ypredict_mean = rho * mean(ipredict-1) + rho * var_ratio * (y_(ipredict-1) - mean(ipredict-1)); ypredict_var = sigsqr_ / (2.0 * omega_) * (1.0 - rho * rho) + rho * rho * previous_var * (1.0 - var_ratio); } if (ipredict == time_.n_elem) { // Forecasting, so we're done: no need to run interpolation steps std::pair<double, double> ypredict(ypredict_mean, ypredict_var); return ypredict; } yprecision = 1.0 / ypredict_var; ypredict_mean *= yprecision; // Either backcasting or interpolating, so need to calculate coefficients of linear filter as a function of // the predicted time series value, then update the running conditional mean and variance of the predicted // time series value InitializeCoefs(time, ipredict, 0.0, 0.0); yprecision += yslope_ * yslope_ / var(ipredict); ypredict_mean += yslope_ * (y_(ipredict) - yconst_) / var(ipredict); for (int i=ipredict+1; i<time_.n_elem; i++) { UpdateCoefs(); yprecision += yslope_ * yslope_ / var(i); ypredict_mean += yslope_ * (y_(i) - yconst_) / var(i); } ypredict_var = 1.0 / yprecision; ypredict_mean *= ypredict_var; std::pair<double, double> ypredict(ypredict_mean, ypredict_var); return ypredict; }
// Initialize the coefficients used for interpolation and backcasting assuming a CAR(1) process void KalmanFilter1::InitializeCoefs(double time, unsigned int itime, double ymean, double yvar) { yconst_ = 0.0; yslope_ = exp(-std::abs(time_(itime) - time) * omega_); var[itime] = sigsqr_ / (2.0 * omega_) * (1.0 - yslope_ * yslope_) + yerr_(itime) * yerr_(itime); current_index_ = itime + 1; }
// Predict the time series at the input time given the measured time series, assuming a CARMA(p,q) process std::pair<double, double> KalmanFilterp::Predict(double time) { unsigned int ipredict = 0; while (time > time_(ipredict)) { // find the index where time_ > time for the first time ipredict++; if (ipredict == time_.n_elem) { // time is greater than last element of time_, so do forecasting break; } } // Run the Kalman filter up to the point time_[ipredict-1] Reset(); for (int i=1; i<ipredict; i++) { Update(); } double ypredict_mean, ypredict_var, yprecision; if (ipredict == 0) { // backcasting, so initialize the conditional mean and variance to the stationary values ypredict_mean = 0.0; ypredict_var = std::real( arma::as_scalar(rotated_ma_coefs_ * StateVar_ * rotated_ma_coefs_.t()) ); } else { // predict the value of the time series at time, given the earlier values kalman_gain_ = PredictionVar_ * rotated_ma_coefs_.t() / var(ipredict-1); state_vector_ += kalman_gain_ * innovation_; PredictionVar_ -= var(ipredict-1) * (kalman_gain_ * kalman_gain_.t()); double dt = std::abs(time - time_(ipredict-1)); rho_ = arma::exp(omega_ * dt); state_vector_ = rho_ % state_vector_; PredictionVar_ = (rho_ * rho_.t()) % (PredictionVar_ - StateVar_) + StateVar_; // initialize the conditional mean and variance ypredict_mean = std::real( arma::as_scalar(rotated_ma_coefs_ * state_vector_) ); ypredict_var = std::real( arma::as_scalar(rotated_ma_coefs_ * PredictionVar_ * rotated_ma_coefs_.t()) ); } if (ipredict == time_.n_elem) { // Forecasting, so we're done: no need to run interpolation steps std::pair<double, double> ypredict(ypredict_mean, ypredict_var); return ypredict; } yprecision = 1.0 / ypredict_var; ypredict_mean *= yprecision; // Either backcasting or interpolating, so need to calculate coefficients of linear filter as a function of // the predicted time series value, then update the running conditional mean and variance of the predicted // time series value InitializeCoefs(time, ipredict, ypredict_mean / yprecision, ypredict_var); yprecision += yslope_ * yslope_ / var(ipredict); ypredict_mean += yslope_ * (y_(ipredict) - yconst_) / var(ipredict); for (int i=ipredict+1; i<time_.n_elem; i++) { UpdateCoefs(); yprecision += yslope_ * yslope_ / var(i); ypredict_mean += yslope_ * (y_(i) - yconst_) / var(i); } ypredict_var = 1.0 / yprecision; ypredict_mean *= ypredict_var; std::pair<double, double> ypredict(ypredict_mean, ypredict_var); return ypredict; }