Microsimulation API

/home/marcle/src/R/microsimulation/src/simple-example.cc
Go to the documentation of this file.
1 #include <microsimulation.h>
2
3 namespace {
4
5 enum state_t {Healthy,Cancer,Death};
6
7 enum event_t {toOtherDeath, toCancer, toCancerDeath};
8
9 typedef std::map<std::string, std::vector<double> > Report;
10
11 class SimplePerson : public ssim::cProcess
12 {
13 public:
14 int id;
15 state_t state;
16 Report report;
17 SimplePerson() : id(-1) {};
18 void init();
19 virtual void handleMessage(const ssim::cMessage* msg);
20 void reporting(string name, double value);
21 };
22
26 void SimplePerson::init() {
27 id++;
28 state = Healthy;
29 double tm = R::rweibull(8.0,85.0);
30 scheduleAt(tm,toOtherDeath);
31 scheduleAt(R::rweibull(3.0,90.0),toCancer);
32 }
33
34 void SimplePerson::reporting(std::string name, double value) {
35 report[name].push_back(value);
36 }
37
41 void SimplePerson::handleMessage(const ssim::cMessage* msg) {
42
43 reporting("id", double(id));
44 reporting("startTime", previousEventTime);
45 reporting("endtime", ssim::now());
46 reporting("state", double(state));
47 reporting("event", double(msg->kind));
48
49 switch(msg->kind) {
50
51 case toOtherDeath:
52 case toCancerDeath:
53 ssim::Sim::stop_process();
54 break;
55
56 case toCancer:
57 state = Cancer;
58 if (R::runif(0.0,1.0) < 0.5)
59 scheduleAt(ssim::now() + R::rweibull(2.0,10.0), toCancerDeath);
60 break;
61
62 default:
63 REprintf("No valid kind of event\n");
64 break;
65
66 } // switch
67
68 if (id % 10000 == 0) Rcpp::checkUserInterrupt();
69
70 } // handleMessage()
71
72 RcppExport SEXP callSimplePerson(SEXP parms) {
73 SimplePerson person;
74 Rcpp::RNGScope scope;
75 Rcpp::List parmsl(parms);
76 int n = Rcpp::as<int>(parmsl["n"]);
77 for (int i = 0; i < n; i++) {
78 ssim::Sim::create_process(&person);
79 ssim::Sim::run_simulation();
80 ssim::Sim::clear();
81 }
82 return Rcpp::wrap(person.report);
83 }
84
85 } // anonymous namespace
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
Rcpp::wrap
SEXP wrap(const std::vector< std::pair< T1, T2 > > v)
ssim::Sim::create_process
static ProcessId create_process(Process *)
creates a new process
Definition: ssim.cc:108
callSimplePerson
SEXP callSimplePerson(SEXP)
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
ssim::cProcess::init
virtual void init()=0
Abstract method to initialise a given process.
ssim::Sim::clear
static void clear()
clears out internal data structures
Definition: ssim.cc:115
ssim::Sim::run_simulation
static void run_simulation()
starts execution of the simulation
Definition: ssim.cc:148
illnessDeath::toCancerDeath
@ toCancerDeath
Definition: illness-death.cpp:11
illnessDeath::toCancer
@ toCancer
Definition: illness-death.cpp:11
ssim::Sim::stop_process
static void stop_process()
stops the execution of the current process
Definition: ssim.cc:241
ssim::cProcess::handleMessage
virtual void handleMessage(const cMessage *msg)=0
Abstract method to handle each message.
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