Ejemplo n.º 1
0
  double Trajectory::force(double nextTime, int nextEvent, 
                            const vector<int>& ids, rng::RngStream* rng,
                            const void* pars) 
  {
    // store this info so that it can be accessed by the model
    curState.curBranch = ids;
    curState.time = time;
    curState.nextTime = nextTime-time;

    // get the transition of the next event
    const TransitionType* tt = model->getObsType(model->mapType(nextEvent));

    // setup the transition
    StateTransition st(nextTime-lastEventTime(),*tt,getState(),pars,nextEvent);

    // probability that the event happened
    double trueRate = 0.0;
    double dw = tt->applyRate(curState,pars,trueRate);

    // apply the branch changes
    int ret = tt->applyBranch(curState,rng,st,pars);

    // cerr << ">>>>> " << ret << endl;

    if (ret < 0) {
//      cerr << *this << endl;
//      EpiState x = curState;
//      x += st.getTrans();
//      double dt = st.atTime();
//      cerr << setw(12) << time+dt << " " << x << " ";
//      cerr << " " << st;
//      cerr << endl;
//      return 0.0;
      dw = 0.0;
    } else if (ret < 100) {
      // adjust the weight
      dw *= exp(st.relprob);
    }

    if (store_trans) transitions.push_back(st);

    // apply the state change
    addTransition(st);

    // cerr << "New branch State: " << curState.branches.to_json(false) << endl;

    // update the time
    time = nextTime;

    return dw;
  }
Ejemplo n.º 2
0
MSBoolean MSDoubleClick::isDoubleClick(const XEvent *ev_)
{
   if (ev_->type==ButtonPress||ev_->type==ButtonRelease)
   {
      if (ev_->xbutton.time-lastEventTime()<=applicationDoubleClickInterval())
      {
	 eventTime(0);
	 return MSTrue;
      }
      else
      {
	 eventTime(ev_->xbutton.time);
	 return MSFalse;
      }
   }
   else return MSFalse;
}
Ejemplo n.º 3
0
  int Trajectory::step(double maxTime, 
                      const void* pars, 
                      rng::RngStream* rng, 
                      bool adjZero) 
  {
    // get array of next event rates
    double nextTime = 0.0;
    double transTime = 0.0;
    double r = 0.0;

    int nextEvent = 0;
    int num_events = 0;

    double dw = 0.0;
    double rate = 0.0;
    double unrate = 0.0;

    int ret = 0;

    const TransitionType* nextTrans;

    // set time to EpiState
    curState.time = time;

    // calculate all rates
    double totalRate = model->calculateTransRates(curState,transRates,trueRates);
    double totalTrue = std::accumulate(trueRates.begin(),trueRates.end(),0.0);

    while (ret <= 0) {
      if (totalRate <= 0.0) {
        time = maxTime;
        ret = 4;
        break;
      }

      // sample next event time
      rng->uniform(1,&r);
      nextTime =  -1.0/totalRate * log(r);

      if (time+nextTime < maxTime) {
        // sample next event type and calculate rate of the event
        nextEvent = model->chooseTransition(rng,transRates);

        // get rate of chosen event (transRates is a cumulative array)
        rate = transRates[nextEvent] - ((nextEvent>0) ? transRates[nextEvent-1] : 0.0);

        // get the appropriate transition
        nextTrans = model->getTType(nextEvent);
        // - time interval
        transTime = time+nextTime - lastEventTime();
        // generate the transition
        StateTransition st(transTime,*nextTrans,curState,pars,nextEvent,time+nextTime);

        // get potential new state (without copying branch information)
        EpiState newState;
        newState.copyNoBranches(curState);
        newState += st.trans;

        // get probability of new state
        newState.nextTime = nextTime;

        dw = nextTrans->applyProb(newState,model->getPars());

        if (dw > 0.0 || ! adjZero) 
          // check if the proposed state is feasible (prob > 0)
        {
          // if yes,
          // add branch transformations
          if (model->do_branches) {
            curState.nextTime = nextTime;
            nextTrans->applyBranch(curState,rng,st,pars);
            dw *= exp(st.relprob);
          }

          // add transition to list
          if (store_trans) transitions.push_back(st);

          // adapt state
          addTransition(st);

          // multiply probability
          updateProb(dw);
          // updateLogProb(st.relprob);

          // advance time
          time += nextTime;
          ++num_events;

#ifdef DEBUG
          if (ret < 0) {
            cout << ascii::cyan << "      legal event" 
                  << " (" << nextEvent << ")."
                  << " w = " << dw << ", penalty = " << unrate 
                  << ", total rate = " << totalRate << "."  
                  << ascii::end << endl;
          }
#endif

          ret = 1; // exit loop
        } else {
          // if no,
          // make this transition illegal in this particular step
#ifdef DEBUG
          cout << ascii::cyan
                << "      illegal event (" << nextEvent << ")."
                << " w = " << dw << ", penalty = " << rate 
                << ", total rate = " << totalRate << "." 
                << ascii::end << endl;
#endif
          // adjust cumulative rates
          for (size_t i = nextEvent; i < transRates.size(); ++i) {
            transRates[i] -= rate;
          }
          totalRate = transRates.back();

          // condition on not seeing this event
          unrate += rate;

          // redo loop with modified rates
          ret = -1;
        }
      } else {
#ifdef DEBUG
        if (ret < 0) {
          cout << "No event. Total rate = " << totalRate << "." << endl;
        }
#endif

        nextTime = maxTime - time;
        time = maxTime;
        ret = 2; // exit loop
      }
    }
    
    if (totalRate < 0.0) {
#ifdef DEBUG
      cout << "\033[1;31m";
      cout << "Negative rate (" << ret << "): " << totalRate
            << "  ES = " << curState;
      cout << "\033[0m" << endl;
#endif
      return -1;
    }

    // condition on the illegal events ('prob' is in logarithmic scale)
    // 'unrate' is the sum of all the rates that were conditioned on not
    // happening
    // if (unrate > 0.0) 
    {
      // updateLogProb(-unrate*nextTime);
      double dR = totalTrue - totalRate;
#ifdef DEBUG
      if (dR != 0) {
        cout << "      difference in rates: " << totalTrue << " - " << totalRate << endl;
      }
#endif
      updateLogProb(-dR*nextTime);
    }

    return ret;
    // return num_events;
    // return (ret != 2) ? num_events : -2;
  }