Microsimulation API
/home/marcle/src/R/microsimulation/src/simple-example2.cc
Go to the documentation of this file.
1 #include <microsimulation.h>
2 
3 namespace {
4 
5 using namespace std;
6 using namespace ssim;
7 
9 
11 
12 class SimplePerson : public cProcess
13 {
14 public:
15  state_t state;
16  int id;
17  SimplePerson(const int i = 0) : id(i) {};
18  void init();
19  virtual void handleMessage(const cMessage* msg);
21 };
22 
24 
28 void SimplePerson::init() {
29  state = Healthy;
30  scheduleAt(R::rweibull(8.0,85.0),toOtherDeath);
31  scheduleAt(R::rweibull(3.0,90.0),toCancer);
32 }
33 
37 void SimplePerson::handleMessage(const cMessage* msg) {
38 
39  SimplePerson::report.add(state,msg->kind,previousEventTime,now());
40 
41  switch(msg->kind) {
42 
43  case toOtherDeath:
44  case toCancerDeath:
45  Sim::stop_process();
46  break;
47 
48  case toCancer:
49  state = Cancer;
50  if (R::runif(0.0,1.0) < 0.5)
51  scheduleAt(now() + R::rweibull(2.0,10.0), toCancerDeath);
52  break;
53 
54  default:
55  REprintf("No valid kind of event\n");
56  break;
57 
58  } // switch
59 
60 } // handleMessage()
61 
62 RcppExport SEXP callSimplePerson2(SEXP parms) {
63  SimplePerson person;
64  Rcpp::RNGScope scope;
65  Rcpp::List parmsl(parms);
66  int n = Rcpp::as<int>(parmsl["n"]);
67 
68  SimplePerson::report.clear();
69  vector<double> ages;
70  for (double age=0.0; age<=100.0; age++) {
71  ages.push_back(age);
72  }
73  ages.push_back(1.0e+6);
74  SimplePerson::report.setPartition(ages);
75 
76  for (int i = 0; i < n; i++) {
77  person = SimplePerson(i);
78  Sim::create_process(&person);
79  Sim::run_simulation();
80  Sim::clear();
81  }
82  return SimplePerson::report.wrap();
83 }
84 
85 } // namespace simpleExample2
ssim::cMessage
cMessage class for OMNET++ API compatibility. This provides a heavier message class than Sim::Event,...
Definition: microsimulation.h:211
illnessDeath::Cancer
@ Cancer
Definition: illness-death.cpp:9
ssim::cProcess
cProcess class for OMNET++ API compatibility.
Definition: microsimulation.h:314
illnessDeath::toOtherDeath
@ toOtherDeath
Definition: illness-death.cpp:11
ssim::now
Time now()
now() function for compatibility with C++SIM
Definition: microsimulation.cc:9
illnessDeath::state_t
state_t
Definition: illness-death.cpp:9
Death
@ Death
Definition: person-r-20121231.cc:41
callSimplePerson2
SEXP callSimplePerson2(SEXP)
illnessDeath::toCancerDeath
@ toCancerDeath
Definition: illness-death.cpp:11
illnessDeath::toCancer
@ toCancer
Definition: illness-death.cpp:11
ssim
name space for the Siena simulator.
Definition: microsimulation.cc:3
ssim::EventReport
EventReport class for collecting statistics on person-time, prevalence and numbers of events.
Definition: microsimulation.h:666
Healthy
@ Healthy
Definition: person-r-20121231.cc:40
illnessDeath::report
EventReport< short, short, double > report
Definition: illness-death.cpp:13
illnessDeath::event_t
event_t
Definition: illness-death.cpp:11
microsimulation.h
ssim::cMessage::kind
short kind
Definition: microsimulation.h:222