/home/marcle/src/R/microsimulation/src/simple-example.cc
Go to the documentation of this file.
#include <microsimulation.h>
namespace {
enum state_t {Healthy,Cancer,Death};
enum event_t {toOtherDeath, toCancer, toCancerDeath};
typedef std::map<std::string, std::vector<double> > Report;
class SimplePerson : public ssim::cProcess
{
public:
int id;
state_t state;
Report report;
SimplePerson() : id(-1) {};
void init();
virtual void handleMessage(const ssim::cMessage* msg);
void reporting(string name, double value);
};
void SimplePerson::init() {
id++;
state = Healthy;
double tm = R::rweibull(8.0,85.0);
scheduleAt(tm,toOtherDeath);
scheduleAt(R::rweibull(3.0,90.0),toCancer);
}
void SimplePerson::reporting(std::string name, double value) {
report[name].push_back(value);
}
void SimplePerson::handleMessage(const ssim::cMessage* msg) {
reporting("id", double(id));
reporting("startTime", previousEventTime);
reporting("endtime", ssim::now());
reporting("state", double(state));
reporting("event", double(msg->kind));
switch(msg->kind) {
case toOtherDeath:
case toCancerDeath:
ssim::Sim::stop_process();
break;
case toCancer:
state = Cancer;
if (R::runif(0.0,1.0) < 0.5)
scheduleAt(ssim::now() + R::rweibull(2.0,10.0), toCancerDeath);
break;
default:
REprintf("No valid kind of event\n");
break;
} // switch
if (id % 10000 == 0) Rcpp::checkUserInterrupt();
} // handleMessage()
RcppExport SEXP callSimplePerson(SEXP parms) {
SimplePerson person;
Rcpp::RNGScope scope;
Rcpp::List parmsl(parms);
int n = Rcpp::as<int>(parmsl["n"]);
for (int i = 0; i < n; i++) {
ssim::Sim::create_process(&person);
ssim::Sim::run_simulation();
ssim::Sim::clear();
}
return Rcpp::wrap(person.report);
}
} // 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