microsimulation.h
Go to the documentation of this file.
#ifndef MICROSIMULATION_H
#define MICROSIMULATION_H
#include <RcppCommon.h>
#include <unordered_map>
#include <tuple>
#include <vector>
#include <utility>
#include <map>
#include <string>
/* https://stackoverflow.com/questions/7110301/generic-hash-for-tuples-in-unordered-map-unordered-set for a generic hash function for std::tuple
*/
#include <tuple>
namespace std{
namespace
{
// Code from boost
// Reciprocal of the golden ratio helps spread entropy
// and handles duplicates.
// See Mike Seymour in magic-numbers-in-boosthash-combine:
// http://stackoverflow.com/questions/4948780
template <class T>
inline void hash_combine(std::size_t& seed, T const& v)
{
seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
// Recursive template code derived from Matthieu M.
template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl
{
static void apply(size_t& seed, Tuple const& tuple)
{
HashValueImpl<Tuple, Index-1>::apply(seed, tuple);
hash_combine(seed, std::get<Index>(tuple));
}
};
template <class Tuple>
struct HashValueImpl<Tuple,0>
{
static void apply(size_t& seed, Tuple const& tuple)
{
hash_combine(seed, std::get<0>(tuple));
}
};
}
template <typename ... TT>
struct hash<std::tuple<TT...>>
{
size_t
operator()(std::tuple<TT...> const& tt) const
{
size_t seed = 0;
HashValueImpl<std::tuple<TT...> >::apply(seed, tt);
return seed;
}
};
template <typename T1, typename T2>
struct hash<std::pair<T1,T2>>
{
size_t
operator()(std::pair<T1,T2> const& p) const
{
size_t seed = 0;
hash_combine(seed, p.first);
hash_combine(seed, p.second);
return seed;
}
};
}
namespace Rcpp {
// vectors of pairs: recursively defined
template <class T1, class T2>
SEXP wrap(const std::vector<std::pair<T1,T2> > v);
// vectors tuples: enumerated cases
template <class T1, class T2>
SEXP wrap(const std::vector<std::tuple<T1,T2> > v);
template <class T1, class T2, class T3>
SEXP wrap(const std::vector<std::tuple<T1,T2,T3> > v);
template <class T1, class T2, class T3, class T4>
SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4> > v);
template <class T1, class T2, class T3, class T4, class T5>
SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4,T5> > v);
template <class T1, class T2, class T3, class T4, class T5, class T6>
SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4,T5,T6> > v);
template <class T1, class T2, class T3, class T4, class T5, class T6, class T7>
SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4,T5,T6,T7> > v);
template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8> > v);
template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9>
SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9> > v);
template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10>
SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10> > v);
// maps defined in terms of vectors
template <class T1, class T2>
SEXP wrap_map(const std::map<T1,T2> v);
template <class T1a, class T1b, class T2>
SEXP wrap_map(const std::map<std::pair<T1a,T1b>,T2> v,
std::string key, std::string name1, std::string name2);
template <class T1a, class T1b, class T1c, class T2>
SEXP wrap_map(const std::map<std::tuple<T1a,T1b,T1c>,T2> v,
std::string key, std::string name1, std::string name2, std::string name3);
template <class T1, class T2, class T3, class T4, class T5>
SEXP wrap_map(const std::map<std::pair<std::tuple<T1,T2,T3>,T4>,T5> v,
std::string key, std::string name1, std::string name2);
// unordered_maps defined in terms of vectors
template <class T1, class T2>
SEXP wrap_map(const std::unordered_map<T1,T2> v);
template <class T1a, class T1b, class T2>
SEXP wrap_map(const std::unordered_map<std::pair<T1a,T1b>,T2> v,
std::string key, std::string name1, std::string name2);
template <class T1a, class T1b, class T1c, class T2>
SEXP wrap_map(const std::unordered_map<std::tuple<T1a,T1b,T1c>,T2> v,
std::string key, std::string name1, std::string name2, std::string name3);
template <class T1, class T2, class T3, class T4, class T5>
SEXP wrap_map(const std::unordered_map<std::pair<std::tuple<T1,T2,T3>,T4>,T5> v,
std::string key, std::string name1, std::string name2);
} // namespace Rcpp
#include <Rcpp.h>
#include <R.h>
#include <Rmath.h>
#include <Rdefines.h>
#include <R_ext/Random.h>
#include <siena/ssim.h>
#include <RngStream.h>
#include <rcpp_table.h>
#include <string>
#include <algorithm>
#include <functional>
#include <set>
namespace ssim {
using std::string;
using std::vector;
using std::map;
using std::pair;
using std::greater;
// forward declarations
class cProcess;
#define WithRNG(rng,expr) (rng->set(), expr)
class cMessage : public ssim::Event {
public:
cMessage(const short k = -1, const string n = "", const ProcessId process_id = -1,
const ProcessId sender_process_id = -1) :
kind(k), name(n), sendingTime(-1.0), timestamp(0), process_id(process_id),
sender_process_id(sender_process_id) { }
// currently no setters (keep it lightweight?)
short getKind() { return kind; }
string getName() { return name; }
Time getTimestamp() { return timestamp; }
Time getSendingTime() {return sendingTime; }
short kind;
string name;
Time sendingTime, timestamp;
ProcessId process_id, sender_process_id;
string str() const {
std::ostringstream stringStream;
stringStream << "kind=";
stringStream << kind;
stringStream << ",name=";
stringStream << name;
string str = stringStream.str();
return str;
}
// this does NOT include schedulePriority
};
inline void
Cancel(std::function<bool(const cMessage * msg)> pred) {
return Sim::ignore_event([pred](const Event * e) {
const cMessage * msg = dynamic_cast<const cMessage *>(e);
return (msg != 0 && pred(msg));
});
}
inline void RemoveKind(short kind) {
Cancel([kind](const cMessage * msg) { return msg->kind == kind; });
}
inline void CancelKind(short kind) {
Cancel([kind](const cMessage * msg) { return msg->kind == kind; });
}
inline void RemoveName(string name) {
Cancel([name](const cMessage * msg) { return msg->name == name; });
}
inline void CancelName(string name) {
Cancel([name](const cMessage * msg) { return msg->name == name; });
}
inline void CancelEvents() {
Cancel([](const cMessage * msg) { return true; });
}
class cProcess : public ssim::ProcessWithPId {
public:
Time startTime, previousEventTime;
cProcess(Time startTime = Time(0.0)) : startTime(startTime), previousEventTime(startTime) { }
virtual void handleMessage(const cMessage * msg) = 0;
virtual void init() = 0;
// see also `void stop()`
virtual void scheduleAt(Time t, cMessage * msg) { // virtual or not?
msg->timestamp = t;
msg->sendingTime = Sim::clock();
msg->process_id = msg->sender_process_id = pid();
Sim::self_signal_event(msg, t - Sim::clock());
}
virtual void scheduleAt(Time t, string name) {
scheduleAt(t, new cMessage(-1,name));
}
virtual void scheduleAt(Time t, short k) {
scheduleAt(t, new cMessage(k,""));
}
virtual void send(ProcessId process_id, Time t, cMessage * msg) { // virtual or not?
msg->timestamp = t;
msg->process_id = process_id;
msg->sender_process_id = pid();
msg->sendingTime = Sim::clock();
Sim::signal_event(process_id, msg, t - Sim::clock());
}
virtual void send(ProcessId process_id, Time t, string name) {
send(process_id, t, new cMessage(-1,name));
}
virtual void send(ProcessId process_id, Time t, short kind) {
send(process_id, t, new cMessage(kind,""));
}
void cancel(std::function<bool(const cMessage * msg)> pred) {
Cancel([pred,this](const cMessage * msg) {
return pred(msg) &&
msg->process_id == this->pid();
});
}
void cancel_kind(short kind) {
cancel([kind,this](const cMessage * msg) {
return msg->kind == kind &&
msg->process_id == this->pid();
});
}
void cancel_name(string name) {
cancel([name,this](const cMessage * msg) {
return msg->name == name &&
msg->process_id == this->pid();
});
}
void cancel_events() {
cancel([this](const cMessage * msg) {
return msg->process_id == this->pid();
});
}
double previous() {
return previousEventTime;
}
void initialize() { init(); previousEventTime = startTime; }
virtual void process_event(const ssim::Event * e) { // virtual or not?
const cMessage * msg;
if ((msg = dynamic_cast<const cMessage *>(e)) != 0) {
handleMessage(msg);
previousEventTime = Sim::clock();
} else {
// cf. cerr, specialised for R
REprintf("cProcess is only written to receive cMessage events\n");
}
}
};
typedef Time simtime_t;
Time simTime();
Time now();
class Means {
public:
double mean() { return _sum/_n; }
double var() {return (long double)_n/(_n-1)*(_sumsq/_n - mean()*mean()); }
int n() {return _n;}
double sum() {return _sum;}
double sd() {return sqrt(var()); }
Means() : _n(0), _sum(0.0), _sumsq(0.0) {}
Means* operator +=(const double value) {
_n++;
_sum += (long double) value;
_sumsq += (long double)value * (long double)value;
return this;
}
SEXP wrap() {
return Rcpp::DataFrame::create(_("n")=n(),
_("mean")=mean(),
_("var")=var(),
_("sd")=sd(),
_("se")=sd()/sqrt(n()),
_("sum")=sum(),
_("sumsq")=_sumsq
);
}
//friend std::ostream& operator<<(std::ostream& os, Means& m);
private:
int _n;
long double _sum, _sumsq;
};
class Rpexp {
public:
Rpexp() {} // blank default constructor
Rpexp(double *hin, double *tin, int nin) : n(nin) {
int i;
H.resize(n);
t.resize(n);
h.resize(n);
H[0]=0.0; h[0]=hin[0]; t[0]=tin[0];
if (n>1) {
for(i=1;i<n;i++) {
h[i]=hin[i]; t[i]=tin[i];
H[i] = H[i-1]+(t[i]-t[i-1])*h[i-1];
}
}
}
/* Rpexp(vector<double> hin, vector<double> tin) : h(hin), t(tin) { */
/* n = h.size(); */
/* H.resize(n); */
/* H[0] = 0.0; */
/* if (n>1) { */
/* for(i=1;i<n;i++) { */
/* H[i] = H[i-1]+(t[i]-t[i-1])*h[i-1]; */
/* } */
/* } */
/* } */
double rand(double u, double from = 0.0) {
double v = 0.0, H0 = 0.0, tstar = 0.0;
int i = 0, i0 = 0;
if (from > 0.0) {
i0 = (from >= t[n-1]) ? (n-1) : int(lower_bound(t.begin(), t.end(), from) - t.begin())-1;
H0 = H[i0] + (from - t[i0])*h[i0];
}
v = -log(u) + H0;
i = (v >= H[n-1]) ? (n-1) : int(lower_bound(H.begin(), H.end(), v) - H.begin())-1;
tstar = t[i]+(v-H[i])/h[i];
return tstar;
}
private:
vector<double> H, h, t;
int n;
};
double rweibullHR(double shape, double scale, double hr);
static int counter_id = 0;
class Rng : public RngStream {
public:
typedef double result_type;
result_type operator()() { return RandU01(); }
result_type min() { return 0.0; }
result_type max() { return 1.0; }
Rng() : RngStream() { id = ++counter_id; }
virtual ~Rng();
void seed(const double seed[6]) {
SetSeed(seed);
}
void set();
void nextSubstream() { ResetNextSubstream(); }
int id;
};
extern "C" { // functions that will be called from R
void r_create_current_stream();
void r_remove_current_stream();
void r_set_user_random_seed(double * seed);
void r_get_user_random_seed(double * seed);
/* /\** */
/* @brief A utility function to move to the next user random stream for the simulation. */
/* *\/ */
/* void r_next_rng_stream(); */
void r_next_rng_substream();
void r_rng_advance_substream(double * seed, int * n);
void test_rstream2(double * x);
} // extern "C"
inline double discountedInterval(double start, double end, double discountRate) {
if (discountRate == 0.0) return end - start;
else return (pow(1.0+discountRate,-start) - pow(1.0+discountRate,-end)) / log(1.0+discountRate);
}
inline double discountedInterval(double y, double start, double end, double discountRate) {
if (discountRate == 0.0) return y*(end - start);
else return y*(pow(1.0+discountRate,-start) - pow(1.0+discountRate,-end)) / log(1.0+discountRate);
}
inline double discountedPoint(double time, double discountRate) {
return discountRate <= 0.0 ? 1.0 : pow(1.0+discountRate,-time);
}
inline double discountedPoint(double y, double time, double discountRate) {
return discountRate <= 0.0 ? y : y*pow(1.0+discountRate,-time);
}
template <class T>
std::vector<std::vector<T> > transpose(const std::vector<std::vector<T> > data) {
std::vector<std::vector<T> > result(data[0].size(),
std::vector<T>(data.size()));
for (typename std::vector<T>::size_type i = 0; i < data[0].size(); i++)
for (typename std::vector<T>::size_type j = 0; j < data.size(); j++) {
result[i][j] = data[j][i];
}
return result;
}
template< class State, class Event, class Time = double, class Utility = double>
class EventReport {
public:
typedef std::set<Time, std::greater<Time> > Partition; // NB: greater<Time> cf. lesser<Time>
typedef typename Partition::iterator Iterator;
typedef std::pair<State,Time> Pair;
typedef std::unordered_map<pair<State,Time>, int > PrevMap;
typedef std::unordered_map<pair<State,Time>, Utility > UtilityMap;
typedef std::unordered_map<pair<State,Time>, Time > PtMap;
typedef std::unordered_map<std::tuple<State,Event,Time>, int > EventsMap;
typedef vector<Utility> IndividualUtilities;
EventReport(Utility discountRate = 0.0, bool outputUtilities = true, int size = 1, Time startReportAge = Time(0), bool indiv = false) :
discountRate(discountRate), outputUtilities(outputUtilities), startReportAge(startReportAge), id(0), indiv(indiv) {
_vector.resize(size);
setPartition(startReportAge);
}
void resize(int size) {
_vector.resize(size);
}
void setPartition(const vector<Time> v) {
copy(v.begin(), v.end(), inserter(_partition, _partition.begin()));
}
void setPartition(const Time start = 0.0, const Time finish = 100.0,
const Time delta = 1.0,
const Time maxTime = Time(1.0e100)) {
_partition.clear();
for (Time t=start; t<=finish; t+=delta) _partition.insert(t);
_partition.insert(maxTime);
}
void setIndivN(const int n) {
resize(n);
indiv = true;
}
void setStartReportAge(const Time a) {
startReportAge = a;
}
void clear() {
_ut.clear();
_pt.clear();
_events.clear();
_prev.clear();
_partition.clear();
_vector.clear();
current = Utility(0);
}
void individualReset () {
if (now() >= startReportAge)
mean_utilities += double(current);
if (indiv) {
if (id>=int(_vector.size()))
REprintf("Vector too small in EventReport: use resize(int) method");
else
_vector[id] = (now() >= startReportAge) ? double(current) : NA_REAL;
}
current = Utility(0);
id++;
}
Utility discountedUtilities(Time a, Time b, Utility utility = 1.0) {
if (discountRate == 0.0) return utility * (b-a);
else if (a==b) return 0.0;
else if (discountRate>0.0) {
double alpha = log(1.0+discountRate);
return utility/alpha*(exp(-a*alpha) - exp(-b*alpha));
}
else {
REprintf("discountRate less than zero.");
return 0.0;
}
}
void addBrief(const Time lhs, const Time rhs, const Utility utility = 1) {
if (rhs >= startReportAge)
current += discountedUtilities(std::max<Time>(lhs-startReportAge,Time(0)), rhs-startReportAge, utility);
}
void add(const State state, const Event event, const Time lhs, const Time rhs, const Utility utility = 1, int index = 0 /* deprecated argument */) {
addBrief(lhs, rhs, utility);
Iterator lo, hi, it, last;
lo = _partition.lower_bound(lhs);
hi = _partition.lower_bound(rhs);
last = _partition.begin(); // because it is ordered by greater<Time>
++_events[std::make_tuple(state,event,*hi)];
bool iterating;
for(it = lo, iterating = true; iterating; iterating = (it != hi), --it) { // decrement for greater<Time>
if (lhs<=(*it) && (*it)<rhs) // cadlag
++_prev[Pair(state,*it)];
if (it == last) {
if (outputUtilities) {
Utility u = discountedUtilities(std::max<Time>(lhs,*it), rhs, utility);
_ut[Pair(state,*it)] += u;
}
_pt[Pair(state,*it)] += rhs - std::max<Time>(lhs,*it);
}
else {
Time next_value = *(--it); it++; // decrement/increment for greater<Time>
if (outputUtilities) {
Utility u = discountedUtilities(std::max<Time>(lhs,*it), std::min<Time>(rhs,next_value), utility);
_ut[Pair(state,*it)] += u;
}
_pt[Pair(state,*it)] += std::min<Time>(rhs,next_value) - std::max<Time>(lhs,*it);
}
}
}
template<class T>
void append_map(T & base_map, T & new_map) {
typename T::iterator it;
for (it = new_map.begin(); it != new_map.end(); ++it)
base_map[it->first] += it->second;
}
void append(EventReport<State,Event,Time,Utility> & er) {
append_map<PrevMap>(_prev,er._prev);
append_map<EventsMap>(_events,er._events);
append_map<PtMap>(_pt,er._pt);
append_map<UtilityMap>(_ut,er._ut);
_vector.insert(_vector.end(), er._vector.begin(), er._vector.end());
}
SEXP wrap() {
using namespace Rcpp;
if (_events.size() == 0) return List::create();
else if (outputUtilities)
return List::create(_("pt") = wrap_map<State,Time,Time>(_pt,"Key","age","pt"),
_("ut") = wrap_map<State,Time,Utility>(_ut,"Key","age","utility"),
_("events") = wrap_map<State,Event,Time,int>(_events,"Key","event","age","number"),
_("prev") = wrap_map<State,Time,int>(_prev,"Key","age","number"));
else
return List::create(_("pt") = wrap_map<State,Time,Time>(_pt,"Key","age","pt"),
_("events") = wrap_map<State,Event,Time,int>(_events,"Key","event","age","number"),
_("prev") = wrap_map<State,Time,int>(_prev,"Key","age","number"));
}
SEXP wrap_indiv() {
return Rcpp::wrap(_vector);
}
SEXP wrap_means() {
return mean_utilities.wrap();
}
Utility discountRate, current;
bool outputUtilities;
Partition _partition;
PrevMap _prev;
UtilityMap _ut;
PtMap _pt;
EventsMap _events;
IndividualUtilities _vector;
Means mean_utilities;
Time startReportAge;
int id;
bool indiv;
};
template< class State, class Event = short, class Time = double, class Utility = double, class Cost = double>
class SummaryReport {
public:
typedef std::set<Time, std::greater<Time> > Partition; // NB: greater<Time> cf. lesser<Time>
typedef typename Partition::iterator Iterator;
typedef std::pair<State,Time> Pair;
typedef std::unordered_map<pair<State,Time>, int > PrevMap;
typedef std::unordered_map<pair<State,Time>, Utility > UtilityMap;
typedef std::unordered_map<pair<State,Time>, Time > PtMap;
typedef std::unordered_map<std::tuple<State,Event,Time>, int > EventMap;
typedef std::unordered_map<pair<State,Time>, Cost > CostMap;
typedef std::vector<Cost> IndividualCosts;
typedef std::vector<Utility> IndividualUtilities;
SummaryReport(int n = 1, bool indivp = true, Utility discountRate = 0.0) : n(n), indivp(indivp) {
resize(indivp ? n : 1);
setDiscountRate(discountRate);
setUtility(1.0);
setCost(0.0);
}
void resize(int size) {
_indivUtilities.resize(size);
_indivCosts.resize(size);
}
void setDiscountRate(Utility discountRate) {
setUtilityDiscountRate(discountRate);
setCostDiscountRate(discountRate);
}
void setUtilityDiscountRate(Utility discountRate) {
this->utilityDiscountRate = discountRate;
utilityAlpha = log(1.0+utilityDiscountRate);
}
void setCostDiscountRate(Cost discountRate) {
this->costDiscountRate = discountRate;
costAlpha = log(1.0+costDiscountRate);
}
void setPartition(const vector<Time> v) {
copy(v.begin(), v.end(), inserter(_partition, _partition.begin()));
}
void setPartition(const Time start, const Time finish, const Time delta,
const Time maxTime = Time(1.0e100)) {
_partition.clear();
for (Time t=start; t<=finish; t+=delta) _partition.insert(t);
_partition.insert(maxTime);
}
void setUtility(Utility _utility) {
this->utility = _utility;
}
void setCost(Cost _cost) {
this->cost = _cost;
}
void clear() {
_ut.clear();
_pt.clear();
_events.clear();
_prev.clear();
_partition.clear();
_costs.clear();
_indivUtilities.clear();
_indivCosts.clear();
}
void add(const State state, const Event event, const Time lhs, const Time rhs, int index = 0) {
// Time lhs = process->previousEventTime;
// Time rhs = ssim::now();
if (!indivp) index = 0;
Iterator lo, hi, it, last;
lo = _partition.lower_bound(lhs);
hi = _partition.lower_bound(rhs);
last = _partition.begin(); // because it is ordered by greater<Time>
++_events[std::make_tuple(state,event,*hi)];
bool iterating;
for(it = lo, iterating = true; iterating; iterating = (it != hi), --it) { // decrement for greater<Time>
if (lhs<=(*it) && (*it)<rhs) // cadlag
++_prev[Pair(state,*it)];
if (it == last) {
Utility u = discountedUtilityInterval(std::max<Time>(lhs,*it), rhs, utility);
_ut[Pair(state,*it)] += u;
_indivUtilities[index] += u;
Cost c = discountedCostInterval(std::max<Time>(lhs,*it), rhs, cost);
_costs[Pair(state,*it)] += c;
_indivCosts[index] += c;
_pt[Pair(state,*it)] += rhs - std::max<Time>(lhs,*it);
}
else {
Time next_value = *(--it); it++; // decrement/increment for greater<Time>
Utility u = discountedUtilityInterval(std::max<Time>(lhs,*it), std::min<Time>(rhs,next_value), utility);
_ut[Pair(state,*it)] += u;
_indivUtilities[index] += u;
Cost c = discountedCostInterval(std::max<Time>(lhs,*it), std::min<Time>(rhs,next_value), cost);
_costs[Pair(state,*it)] += c;
_indivCosts[index] += c;
_pt[Pair(state,*it)] += std::min<Time>(rhs,next_value) - std::max<Time>(lhs,*it);
}
}
}
// void addPointCost(const State state, const Time time, const Cost cost, const int index = 0) {
void addPointCost(const State state, const Cost cost, int index = 0) {
if (!indivp) index = 0;
Time time = ssim::now();
Time time_lhs = * _partition.lower_bound(time);
Cost c = discountedCost(time,cost);
_costs[Pair(state,time_lhs)] += c;
_indivCosts[index] += c;
}
void append(SummaryReport<State,Event,Time,Utility> & er) {
append_map<PrevMap>(_prev,er._prev);
append_map<EventMap>(_events,er._events);
append_map<PtMap>(_pt,er._pt);
append_map<UtilityMap>(_ut,er._ut);
append_map<CostMap>(_costs,er._costs);
_indivUtilities.insert(_indivUtilities.end(), er._indivUtilities.begin(), er._indivUtilities.end());
_indivCosts.insert(_indivCosts.end(), er._indivCosts.begin(), er._indivCosts.end());
}
Rcpp::List asList() {
using namespace Rcpp;
if (_events.size() == 0) return List::create();
Rcpp::DataFrame indiv =
Rcpp::DataFrame::create(_("utilities")=_indivUtilities,
_("costs")=_indivCosts);
Rcpp::List out = List::create(_("pt") = wrap_map<State,Time,Time>(_pt,"Key","age","pt"),
_("ut") = wrap_map<State,Time,Utility>(_ut,"Key","age","utility"),
_("events") = wrap_map<State,Event,Time,int>(_events,"Key","event","age","number"),
_("prev") = wrap_map<State,Time,int>(_prev,"Key","age","number"),
_("costs") = wrap_map<State,Time,Cost>(_costs,"Key","age","cost"),
_("n")=n,
_("indivp")=indivp,
_("utilityDiscountRate")=utilityDiscountRate,
_("costDiscountRate")=costDiscountRate,
_("indiv")=indiv);
out.attr("class")="SummaryReport";
return out;
}
Utility discountedUtilityInterval(Time a, Time b, Utility utility) {
if (a == b || utility == 0.0) return 0.0;
else if (utilityDiscountRate == 0.0) return utility * (b-a);
else if (utilityDiscountRate>0.0) {
return utility/utilityAlpha*(exp(-a*utilityAlpha) - exp(-b*utilityAlpha));
}
else {
REprintf("utilityDiscountRate less than zero: %f.\n", utilityDiscountRate);
return 0.0;
}
}
Cost discountedCostInterval(Time a, Time b, Cost cost) {
if (a == b || cost == 0.0) return 0.0;
else if (costDiscountRate == 0.0) return cost * (b-a);
else if (costDiscountRate>0.0) {
return cost/costAlpha*(exp(-a*costAlpha) - exp(-b*costAlpha));
}
else {
REprintf("costDiscountRate less than zero: %f.\n", costDiscountRate);
return 0.0;
}
}
Cost discountedCost(Time a, Cost cost) {
if (cost == 0.0) return 0.0;
else if (costDiscountRate == 0.0) return cost;
else if (costDiscountRate>0)
return cost*exp(-a*costAlpha);
else {
REprintf("costDiscountRate less than zero: %f.\n", costDiscountRate);
return 0;
}
}
template<class T>
void append_map(T & base_map, T & new_map) {
typename T::iterator it;
for (it = new_map.begin(); it != new_map.end(); ++it)
base_map[it->first] += it->second;
}
int n;
bool indivp;
Partition _partition;
PrevMap _prev;
UtilityMap _ut;
PtMap _pt;
EventMap _events;
CostMap _costs;
IndividualUtilities _indivUtilities;
IndividualCosts _indivCosts;
// cProcess *process;
Utility utilityDiscountRate, utilityAlpha, utility;
Cost costDiscountRate, costAlpha, cost;
};
template< class State, class Time = double, class Cost = double>
class CostReport {
public:
typedef std::set<Time, std::greater<Time> > Partition;
typedef std::pair<State,Time> Pair;
typedef CostReport<State,Time,Cost> This;
typedef std::unordered_map<pair<State,Time>, Cost > Table;
typedef std::vector<Cost> IndividualCosts;
CostReport(Cost discountRate = 0, int size = 1, Time startReportAge = Time(0), bool indiv = false) : discountRate(discountRate), startReportAge(startReportAge), id(0), indiv(indiv) {
_vector.resize(size);
}
void individualReset () {
if (now() >= startReportAge)
mean_costs += double(current);
if (indiv) {
if (id>=int(_vector.size()))
REprintf("Vector too small in CostReport: use resize(int) method");
else
_vector[id] = (now() >= startReportAge) ? double(current) : NA_REAL;
}
current = Cost(0);
id++;
}
void setIndivN(const int n) {
resize(n);
indiv = true;
}
void setStartReportAge(const Time a) {
startReportAge = a;
}
Cost discountedCost(Time a, Cost cost) {
if (discountRate == 0.0) return cost;
else if (discountRate>0.0)
return cost/pow(1+discountRate,a);
else {
REprintf("discountRate less than zero.");
return 0;
}
}
void setPartition(const vector<Time> v) {
copy(v.begin(), v.end(), inserter(_partition, _partition.begin()));
}
void setPartition(const Time start, const Time finish, const Time delta,
const Time maxTime = Time(1.0e100)) {
_partition.clear();
for (Time t=start; t<=finish; t+=delta) _partition.insert(t);
_partition.insert(maxTime);
}
void clear() {
_table.clear();
_partition.clear();
_vector.clear();
current = Cost(0);
}
void resize(int size) {
_vector.resize(size);
}
void append(This & new_report) { // assuming that discountRate and _partition are the same for both reports
typename Table::iterator it;
_vector.insert(_vector.start(), new_report._vector.begin(), new_report._vector.end());
for(it = new_report._table.begin(); it != new_report._table.end(); ++it) {
_table[it->first] += it->second;
}
}
void add(const State state, const Time time, const Cost cost, const int index = 0 /* deprecated argument */) {
Time time_lhs = * _partition.lower_bound(time);
Cost c = discountedCost(time,cost);
_table[Pair(state,time_lhs)] += c;
if (startReportAge == 0.0)
current += c;
else
if (time>=startReportAge)
current += discountedCost(time-startReportAge,cost);
}
SEXP wrap() {
return Rcpp::wrap_map(_table,"Key","age","cost");
}
SEXP wrap_indiv() {
return Rcpp::wrap(_vector);
}
SEXP wrap_means() {
return mean_costs.wrap();
}
Cost discountRate, current;
Partition _partition;
Table _table;
IndividualCosts _vector;
Means mean_costs;
Time startReportAge;
int id;
bool indiv;
};
template<class T = double>
class SimpleReport {
public:
typedef map<string,vector<T> > Map;
void record(string field, T value) {
_data[field].push_back(value);
}
void revise(string field, T value) {
if (!_data[field].empty())
_data[field].pop_back();
_data[field].push_back(value);
}
void clear() { _data.clear(); }
SEXP wrap() {
return Rcpp::wrap(_data);
}
void append(SimpleReport<T> & obj) {
for(typename Map::iterator it = obj._data.begin(); it != obj._data.end(); ++it) {
_data[it->first].insert(_data[it->first].end(), it->second.begin(), it->second.end());
}
}
Map _data;
};
} // namespace ssim
namespace Rcpp {
template <class T1, class T2>
SEXP wrap(const vector<pair<T1,T2> > v) {
vector<T1> v1;
vector<T2> v2;
typename vector<pair<T1,T2> >::const_iterator it;
for (it=v.begin(); it<v.end(); ++it) {
v1.push_back(it->first);
v2.push_back(it->second);
}
return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2));
}
template <class T1, class T2>
SEXP wrap(const vector<std::tuple<T1,T2> > v) {
int i, n=v.size();
vector<T1> v1(n);
vector<T2> v2(n);
typename vector<std::tuple<T1,T2> >::const_iterator it;
for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
v1[i] = std::get<0>(*it);
v2[i] = std::get<1>(*it);
}
return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2));
}
template <class T1, class T2, class T3>
SEXP wrap(const vector<std::tuple<T1,T2,T3> > v) {
int i, n=v.size();
vector<T1> v1(n);
vector<T2> v2(n);
vector<T3> v3(n);
typename vector<std::tuple<T1,T2,T3> >::const_iterator it;
for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
v1[i] = std::get<0>(*it);
v2[i] = std::get<1>(*it);
v3[i] = std::get<2>(*it);
}
return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),_("Var3")=wrap(v3));
}
template <class T1, class T2, class T3, class T4>
SEXP wrap(const vector<std::tuple<T1,T2,T3,T4> > v) {
int i, n=v.size();
vector<T1> v1(n);
vector<T2> v2(n);
vector<T3> v3(n);
vector<T4> v4(n);
typename vector<std::tuple<T1,T2,T3,T4> >::const_iterator it;
for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
v1[i] = std::get<0>(*it);
v2[i] = std::get<1>(*it);
v3[i] = std::get<2>(*it);
v4[i] = std::get<3>(*it);
}
return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
_("Var3")=wrap(v3),_("Var4")=wrap(v4));
}
template <class T1, class T2, class T3, class T4, class T5>
SEXP wrap(const vector<std::tuple<T1,T2,T3,T4,T5> > v) {
int i, n=v.size();
vector<T1> v1(n);
vector<T2> v2(n);
vector<T3> v3(n);
vector<T4> v4(n);
vector<T5> v5(n);
typename vector<std::tuple<T1,T2,T3,T4,T5> >::const_iterator it;
for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
v1[i] = std::get<0>(*it);
v2[i] = std::get<1>(*it);
v3[i] = std::get<2>(*it);
v4[i] = std::get<3>(*it);
v5[i] = std::get<4>(*it);
}
return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
_("Var3")=wrap(v3),_("Var4")=wrap(v4),
_("Var5")=wrap(v5));
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
SEXP wrap(const vector<std::tuple<T1,T2,T3,T4,T5,T6> > v) {
int i, n=v.size();
vector<T1> v1(n);
vector<T2> v2(n);
vector<T3> v3(n);
vector<T4> v4(n);
vector<T5> v5(n);
vector<T6> v6(n);
typename vector<std::tuple<T1,T2,T3,T4,T5,T6> >::const_iterator it;
for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
v1[i] = std::get<0>(*it);
v2[i] = std::get<1>(*it);
v3[i] = std::get<2>(*it);
v4[i] = std::get<3>(*it);
v5[i] = std::get<4>(*it);
v6[i] = std::get<5>(*it);
}
return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
_("Var3")=wrap(v3),_("Var4")=wrap(v4),
_("Var5")=wrap(v5),_("Var6")=wrap(v6));
}
template <class T1, class T2, class T3, class T4, class T5, class T6, class T7>
SEXP wrap(const vector<std::tuple<T1,T2,T3,T4,T5,T6,T7> > v) {
int i, n=v.size();
vector<T1> v1(n);
vector<T2> v2(n);
vector<T3> v3(n);
vector<T4> v4(n);
vector<T5> v5(n);
vector<T6> v6(n);
vector<T7> v7(n);
typename vector<std::tuple<T1,T2,T3,T4,T5,T6,T7> >::const_iterator it;
for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
v1[i] = std::get<0>(*it);
v2[i] = std::get<1>(*it);
v3[i] = std::get<2>(*it);
v4[i] = std::get<3>(*it);
v5[i] = std::get<4>(*it);
v6[i] = std::get<5>(*it);
v7[i] = std::get<6>(*it);
}
return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
_("Var3")=wrap(v3),_("Var4")=wrap(v4),
_("Var5")=wrap(v5),_("Var6")=wrap(v6),
_("Var7")=wrap(v7));
}
template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
SEXP wrap(const vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8> > v) {
int i, n=v.size();
vector<T1> v1(n);
vector<T2> v2(n);
vector<T3> v3(n);
vector<T4> v4(n);
vector<T5> v5(n);
vector<T6> v6(n);
vector<T7> v7(n);
vector<T8> v8(n);
typename vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8> >::const_iterator it;
for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
v1[i] = std::get<0>(*it);
v2[i] = std::get<1>(*it);
v3[i] = std::get<2>(*it);
v4[i] = std::get<3>(*it);
v5[i] = std::get<4>(*it);
v6[i] = std::get<5>(*it);
v7[i] = std::get<6>(*it);
v8[i] = std::get<7>(*it);
}
return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
_("Var3")=wrap(v3),_("Var4")=wrap(v4),
_("Var5")=wrap(v5),_("Var6")=wrap(v6),
_("Var7")=wrap(v7),_("Var8")=wrap(v8));
}
template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9>
SEXP wrap(const vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9> > v) {
int i, n=v.size();
vector<T1> v1(n);
vector<T2> v2(n);
vector<T3> v3(n);
vector<T4> v4(n);
vector<T5> v5(n);
vector<T6> v6(n);
vector<T7> v7(n);
vector<T8> v8(n);
vector<T9> v9(n);
typename vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9> >::const_iterator it;
for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
v1[i] = std::get<0>(*it);
v2[i] = std::get<1>(*it);
v3[i] = std::get<2>(*it);
v4[i] = std::get<3>(*it);
v5[i] = std::get<4>(*it);
v6[i] = std::get<5>(*it);
v7[i] = std::get<6>(*it);
v8[i] = std::get<7>(*it);
v9[i] = std::get<8>(*it);
}
return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
_("Var3")=wrap(v3),_("Var4")=wrap(v4),
_("Var5")=wrap(v5),_("Var6")=wrap(v6),
_("Var7")=wrap(v7),_("Var8")=wrap(v8),
_("Var9")=wrap(v9));
}
template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10>
SEXP wrap(const vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10> > v) {
int i, n=v.size();
vector<T1> v1(n);
vector<T2> v2(n);
vector<T3> v3(n);
vector<T4> v4(n);
vector<T5> v5(n);
vector<T6> v6(n);
vector<T7> v7(n);
vector<T8> v8(n);
vector<T9> v9(n);
vector<T10> v10(n);
typename vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10> >::const_iterator it;
for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
v1[i] = std::get<0>(*it);
v2[i] = std::get<1>(*it);
v3[i] = std::get<2>(*it);
v4[i] = std::get<3>(*it);
v5[i] = std::get<4>(*it);
v6[i] = std::get<5>(*it);
v7[i] = std::get<6>(*it);
v8[i] = std::get<7>(*it);
v9[i] = std::get<8>(*it);
v10[i] = std::get<9>(*it);
}
return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
_("Var3")=wrap(v3),_("Var4")=wrap(v4),
_("Var5")=wrap(v5),_("Var6")=wrap(v6),
_("Var7")=wrap(v7),_("Var8")=wrap(v8),
_("Var9")=wrap(v9),_("Var10")=wrap(v10));
}
template <class T1, class T2>
SEXP wrap_map(const std::map<T1,T2> v) {
int i;
int n = v.size();
vector<T1> x(n);
vector<T2> y(n);
typename std::map<T1,T2>::const_iterator it;
for (it=v.begin(), i=0; it != v.end(); ++it, ++i) {
x[i] = (*it).first;
y[i] = (*it).second;
}
return DataFrame::create(_("Key")=wrap(x),_("Value")=wrap(y));
}
// Special cases for wrap_map:
//
// map<pair<Tstate,T>, int > _prev;
// map<pair<Tstate,T>, T > _pt;
// map<std::tuple<Tstate,Tevent,T>, int > _events;
template <class T1a, class T1b, class T1c, class T2>
SEXP wrap_map(const std::map<std::tuple<T1a,T1b,T1c>,T2> v,
std::string key, std::string name1, std::string name2, std::string name3) {
typedef std::tuple<T1a,T1b,T1c> Tuple;
int i;
int n = v.size();
vector<T1a> xa(n);
vector<T1b> xb(n);
vector<T1c> xc(n);
vector<T2> y(n);
typename std::map<Tuple,T2>::const_iterator it;
for (it=v.begin(), i=0; it != v.end(); ++it, ++i) {
xa[i] = get<0>((*it).first);
xb[i] = get<1>((*it).first);
xc[i] = get<2>((*it).first);
y[i] = (*it).second;
}
return Rcpp::DataFrame::create(_[key]=Rcpp::wrap(xa), _[name1]=Rcpp::wrap(xb),
_[name2]=Rcpp::wrap(xc), _[name3]=Rcpp::wrap(y));
}
template <class T1a, class T1b, class T2>
SEXP wrap_map(const std::map<std::pair<T1a,T1b>,T2> v,
std::string key, std::string name1, std::string name2) {
typedef std::pair<T1a,T1b> Pair;
int i;
int n = v.size();
vector<T1a> xa(n);
vector<T1b> xb(n);
vector<T2> y(n);
typename std::map<Pair,T2>::const_iterator it;
for (it=v.begin(), i=0; it != v.end(); ++it, ++i) {
xa[i] = (*it).first.first;
xb[i] = (*it).first.second;
y[i] = (*it).second;
}
return Rcpp::DataFrame::create(_[key]=Rcpp::wrap(xa), _[name1]=Rcpp::wrap(xb),
_[name2]=Rcpp::wrap(y));
}
template <class T1, class T2>
SEXP wrap_map(const std::unordered_map<T1,T2> ov) {
std::map<T1,T2> v(ov.begin(), ov.end());
return wrap_map<T1,T2>(v);
}
template <class T1a, class T1b, class T1c, class T2>
SEXP wrap_map(const std::unordered_map<std::tuple<T1a,T1b,T1c>,T2> ov,
std::string key, std::string name1, std::string name2, std::string name3) {
std::map<std::tuple<T1a,T1b,T1c>,T2> v(ov.begin(), ov.end());
return wrap_map<T1a, T1b, T1c, T2>(v, key, name1, name2, name3);
}
template <class T1a, class T1b, class T2>
SEXP wrap_map(const std::unordered_map<std::pair<T1a,T1b>,T2> ov,
std::string key, std::string name1, std::string name2) {
std::map<std::pair<T1a,T1b>,T2> v(ov.begin(), ov.end());
return wrap_map<T1a, T1b, T2>(v, key, name1, name2);
}
template <class T1, class T2, class T3, class T4, class T5>
SEXP wrap_map(const std::map<std::pair<std::tuple<T1,T2,T3>,T4>,T5> v,
std::string name1, std::string name2) {
typedef std::tuple<T1,T2,T3> Tuple;
typedef std::pair<Tuple,T4> Pair;
int i;
int n = v.size();
vector<T1> x1(n);
vector<T2> x2(n);
vector<T3> x3(n);
vector<T4> x4(n);
vector<T5> y(n);
typename std::map<Pair,T5>::const_iterator it;
for (it=v.begin(), i=0; it != v.end(); ++it, ++i) {
Tuple tuple = (*it).first.first;
x1[i] = std::get<0>(tuple);
x2[i] = std::get<1>(tuple);
x3[i] = std::get<2>(tuple);
x4[i] = (*it).first.second;
y[i] = (*it).second;
}
DataFrame out = Rcpp::DataFrame::create(Rcpp::Named("col1")=wrap(x1),
Rcpp::Named("col2")=wrap(x2),
Rcpp::Named("col3")=wrap(x3),
Rcpp::Named(name1)=wrap(x4),
Rcpp::Named(name2)=wrap(y));
return out;
}
template <class T1, class T2, class T3, class T4, class T5>
SEXP wrap_map(const std::unordered_map<std::pair<std::tuple<T1,T2,T3>,T4>,T5> ov,
std::string name1, std::string name2) {
std::map<std::pair<std::tuple<T1,T2,T3>,T4>,T5> v(ov.begin(), ov.end());
return wrap_map<T1,T2,T3,T4,T5>(v, name1, name2);
}
} // Rcpp namespace
namespace R {
double rnormPos(double mean, double sd);
double rllogis(double shape, double scale);
double rllogis_trunc(double shape, double scale, double left);
double rgompertz(double shape = 1.0, double rate = 1.0);
}
#endif
ssim::Rng::Rng
Rng()
Definition: microsimulation.h:554
ssim::SummaryReport::_indivCosts
IndividualCosts _indivCosts
Definition: microsimulation.h:993
ssim::SummaryReport::add
void add(const State state, const Event event, const Time lhs, const Time rhs, int index=0)
Definition: microsimulation.h:875
ssim::CostReport::CostReport
CostReport(Cost discountRate=0, int size=1, Time startReportAge=Time(0), bool indiv=false)
Definition: microsimulation.h:1010
ssim::r_create_current_stream
void r_create_current_stream()
A utility function to create the current_stream. Used when initialising the microsimulation package i...
Definition: microsimulation.cc:32
ssim::CostReport::wrap_means
SEXP wrap_means()
Definition: microsimulation.h:1082
ssim::SummaryReport::Iterator
Partition::iterator Iterator
Definition: microsimulation.h:819
ssim::SummaryReport
SummaryReport class for collecting statistics on person-time, prevalence, events and costs.
Definition: microsimulation.h:816
ssim::CancelKind
void CancelKind(short kind)
CancelKind is a function to remove messages with the given kind from the queue.
Definition: microsimulation.h:265
ssim::SummaryReport::PtMap
std::unordered_map< pair< State, Time >, Time > PtMap
Definition: microsimulation.h:823
ssim::CostReport::setIndivN
void setIndivN(const int n)
Definition: microsimulation.h:1025
ssim::EventReport::resize
void resize(int size)
Definition: microsimulation.h:681
ssim::Rng::~Rng
virtual ~Rng()
Definition: microsimulation.cc:21
ssim::SummaryReport::setPartition
void setPartition(const vector< Time > v)
Definition: microsimulation.h:850
ssim::cMessage
cMessage class for OMNET++ API compatibility. This provides a heavier message class than Sim::Event,...
Definition: microsimulation.h:211
ssim::CostReport::discountedCost
Cost discountedCost(Time a, Cost cost)
Definition: microsimulation.h:1032
ssim::SimpleReport::record
void record(string field, T value)
record adds a value for a given field into the SimpleReport
Definition: microsimulation.h:1108
ssim::Rpexp::Rpexp
Rpexp(double *hin, double *tin, int nin)
Definition: microsimulation.h:494
ssim::SummaryReport::asList
Rcpp::List asList()
Definition: microsimulation.h:927
ssim::EventReport::_partition
Partition _partition
Definition: microsimulation.h:800
ssim::SummaryReport::costDiscountRate
Cost costDiscountRate
Definition: microsimulation.h:996
ssim::CostReport::discountRate
Cost discountRate
Definition: microsimulation.h:1085
ssim::cProcess::send
virtual void send(ProcessId process_id, Time t, cMessage *msg)
send to a given process at time t a message msg. Adds the sendingTime, process_id and sender_process_...
Definition: microsimulation.h:353
ssim::EventReport::id
int id
Definition: microsimulation.h:808
ssim::CostReport::_table
Table _table
Definition: microsimulation.h:1087
ssim::cProcess::cancel_events
void cancel_events()
Remove all of the messages for this process.
Definition: microsimulation.h:402
ssim::SummaryReport::setDiscountRate
void setDiscountRate(Utility discountRate)
Definition: microsimulation.h:838
ssim::EventReport::add
void add(const State state, const Event event, const Time lhs, const Time rhs, const Utility utility=1, int index=0)
Definition: microsimulation.h:738
ssim::SummaryReport::discountedCostInterval
Cost discountedCostInterval(Time a, Time b, Cost cost)
Definition: microsimulation.h:957
ssim::cMessage::process_id
ProcessId process_id
Definition: microsimulation.h:225
ssim::SummaryReport::discountedUtilityInterval
Utility discountedUtilityInterval(Time a, Time b, Utility utility)
Definition: microsimulation.h:946
ssim::EventReport::wrap
SEXP wrap()
Definition: microsimulation.h:779
it
GNU Free Documentation License March Inc Temple MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed PREAMBLE The purpose of this License is to make a or other written document free in the sense of with or without modifying it
Definition: fdl.txt:15
ssim::EventReport::Iterator
Partition::iterator Iterator
Definition: microsimulation.h:669
ssim::SimpleReport::_data
Map _data
_data class member of a map from strings to vector<T>.
Definition: microsimulation.h:1141
ssim::CostReport::resize
void resize(int size)
Definition: microsimulation.h:1056
ssim::cProcess
cProcess class for OMNET++ API compatibility.
Definition: microsimulation.h:314
ssim::EventReport::startReportAge
Time startReportAge
Definition: microsimulation.h:807
Rcpp::wrap
SEXP wrap(const std::vector< std::pair< T1, T2 > > v)
ssim::SummaryReport::_partition
Partition _partition
Definition: microsimulation.h:986
ssim::SummaryReport::append
void append(SummaryReport< State, Event, Time, Utility > &er)
Definition: microsimulation.h:918
ssim::EventReport::setStartReportAge
void setStartReportAge(const Time a)
Definition: microsimulation.h:698
ssim::simtime_t
Time simtime_t
simtime_t typedef for OMNET++ API compatibility
Definition: microsimulation.h:439
ssim::Sim::signal_event
static void signal_event(ProcessId p, const Event *e)
signal an event to the given process immediately
Definition: ssim.cc:276
ssim::CostReport::setPartition
void setPartition(const vector< Time > v)
Definition: microsimulation.h:1041
ssim::EventReport::discountedUtilities
Utility discountedUtilities(Time a, Time b, Utility utility=1.0)
Definition: microsimulation.h:722
ssim::SummaryReport::IndividualUtilities
std::vector< Utility > IndividualUtilities
Definition: microsimulation.h:827
ssim::SummaryReport::append_map
void append_map(T &base_map, T &new_map)
Definition: microsimulation.h:979
Rcpp::wrap_map
SEXP wrap_map(const std::map< T1, T2 > v)
Definition: microsimulation.h:1371
ssim::Means::Means
Means()
Definition: microsimulation.h:462
R::rgompertz
double rgompertz(double shape, double rate)
Random draw from a Gompertz distribution.
Definition: microsimulation.cc:119
ssim::SummaryReport::resize
void resize(int size)
Definition: microsimulation.h:834
ssim::CostReport::_partition
Partition _partition
Definition: microsimulation.h:1086
ssim::discountedInterval
double discountedInterval(double start, double end, double discountRate)
Simple function to calculate the integral between the start and end times for (1+kappa)^(-u),...
Definition: microsimulation.h:616
RngStream.h
ssim::simTime
Time simTime()
simTime() function for OMNET++ API compatibility
Definition: microsimulation.cc:13
ssim::cMessage::getKind
short getKind()
Definition: microsimulation.h:218
ssim::ProcessWithPId::pid
ProcessId pid() const
process id of this process.
Definition: ssim.cc:298
ssim::SummaryReport::SummaryReport
SummaryReport(int n=1, bool indivp=true, Utility discountRate=0.0)
Definition: microsimulation.h:828
ssim::EventReport::append_map
void append_map(T &base_map, T &new_map)
Definition: microsimulation.h:767
ssim::EventReport::_ut
UtilityMap _ut
Definition: microsimulation.h:802
ssim::SummaryReport::CostMap
std::unordered_map< pair< State, Time >, Cost > CostMap
Definition: microsimulation.h:825
ssim::SummaryReport::n
int n
Definition: microsimulation.h:984
R::rllogis_trunc
double rllogis_trunc(double shape, double scale, double left)
rllogis_trunc function for a random covariate from a log-logistic distribution with shape and scale w...
Definition: microsimulation.cc:113
ssim::cProcess::cProcess
cProcess(Time startTime=Time(0.0))
Definition: microsimulation.h:317
ssim::cProcess::send
virtual void send(ProcessId process_id, Time t, short kind)
sends to a given process at time t a message msg with a specific kind.
Definition: microsimulation.h:369
ssim::Rng::max
result_type max()
Definition: microsimulation.h:553
ssim::SummaryReport::_ut
UtilityMap _ut
Definition: microsimulation.h:988
ssim::SummaryReport::Partition
std::set< Time, std::greater< Time > > Partition
Definition: microsimulation.h:818
ssim::EventReport::wrap_indiv
SEXP wrap_indiv()
Definition: microsimulation.h:792
ssim::r_set_user_random_seed
void r_set_user_random_seed(double *inseed)
A utility function to set the user random seed for the simulation.
Definition: microsimulation.cc:43
ssim::SimpleReport::Map
map< string, vector< T > > Map
Map typedef for a map of strings to vector<T>
Definition: microsimulation.h:1104
copy
it can be used for any textual regardless of subject matter or whether it is published as a printed book We recommend this License principally for works whose purpose is instruction or reference APPLICABILITY AND DEFINITIONS This License applies to any manual or other work that contains a notice placed by the copyright holder saying it can be distributed under the terms of this License The refers to any such manual or work Any member of the public is a and is addressed as you A Modified Version of the Document means any work containing the Document or a portion of either copied or with modifications and or translated into another language A Secondary Section is a named appendix or a front matter section of the Document that deals exclusively with the relationship of the publishers or authors of the Document to the Document s overall if the Document is in part a textbook of a Secondary Section may not explain any mathematics The relationship could be a matter of historical connection with the subject or with related or of ethical or political position regarding them The Invariant Sections are certain Secondary Sections whose titles are as being those of Invariant in the notice that says that the Document is released under this License The Cover Texts are certain short passages of text that are as Front Cover Texts or Back Cover in the notice that says that the Document is released under this License A Transparent copy of the Document means a machine readable copy
Definition: fdl.txt:65
ssim::cMessage::getTimestamp
Time getTimestamp()
Definition: microsimulation.h:220
ssim::CostReport::wrap_indiv
SEXP wrap_indiv()
Definition: microsimulation.h:1079
ssim::cProcess::startTime
Time startTime
Definition: microsimulation.h:316
ssim::now
Time now()
now() function for compatibility with C++SIM
Definition: microsimulation.cc:9
ssim::EventReport::current
Utility current
Definition: microsimulation.h:798
ssim::cProcess::send
virtual void send(ProcessId process_id, Time t, string name)
sends to a given process at time t a message msg with a specific name.
Definition: microsimulation.h:363
ssim::ProcessId
int ProcessId
process identifier type
Definition: ssim.h:55
ssim::EventReport::individualReset
void individualReset()
Definition: microsimulation.h:710
ssim::cProcess::scheduleAt
virtual void scheduleAt(Time t, string name)
schedules at time t a message msg with a specific name to the current process.
Definition: microsimulation.h:340
ssim::EventReport::append
void append(EventReport< State, Event, Time, Utility > &er)
Definition: microsimulation.h:772
ssim::SummaryReport::setUtility
void setUtility(Utility _utility)
Definition: microsimulation.h:859
ssim::EventReport::wrap_means
SEXP wrap_means()
Definition: microsimulation.h:795
ssim::RemoveKind
void RemoveKind(short kind)
RemoveKind is a function to remove messages with the given kind from the queue.
Definition: microsimulation.h:256
ssim::CostReport::individualReset
void individualReset()
Definition: microsimulation.h:1013
ssim::cProcess::previousEventTime
Time previousEventTime
Definition: microsimulation.h:316
ssim::Sim::ignore_event
static void ignore_event(EventPredicate pred)
Definition: ssim.cc:131
ssim::Event
basic event in the simulation.
Definition: ssim.h:106
ssim::RemoveName
void RemoveName(string name)
RemoveName is a function to remove messages with the given name from the queue.
Definition: microsimulation.h:274
ssim::EventReport::clear
void clear()
Definition: microsimulation.h:701
ssim::SimpleReport
SimpleReport class for collecting data for homogeneous fields of type T with string names.
Definition: microsimulation.h:1099
ssim::cProcess::scheduleAt
virtual void scheduleAt(Time t, cMessage *msg)
schedules at time t a message msg to the current process. Adds the sendingTime, process_id and sender...
Definition: microsimulation.h:331
ssim::EventReport::setIndivN
void setIndivN(const int n)
Definition: microsimulation.h:694
ssim::CostReport::clear
void clear()
Definition: microsimulation.h:1050
ssim::Rng::seed
void seed(const double seed[6])
Definition: microsimulation.h:556
ssim::Rpexp::Rpexp
Rpexp()
Definition: microsimulation.h:493
ssim::cProcess::scheduleAt
virtual void scheduleAt(Time t, short k)
schedules at time t a message msg with a specific kind to the current process.
Definition: microsimulation.h:346
ssim::SummaryReport::setCost
void setCost(Cost _cost)
Definition: microsimulation.h:862
ssim::cMessage::sendingTime
Time sendingTime
Definition: microsimulation.h:224
ssim::EventReport::mean_utilities
Means mean_utilities
Definition: microsimulation.h:806
ssim::EventReport::discountRate
Utility discountRate
Definition: microsimulation.h:798
ssim::cProcess::previous
double previous()
Returns the value of the previous event time for this process.
Definition: microsimulation.h:410
ssim::RngStream::SetSeed
bool SetSeed(const double seed[6])
Definition: RngStream.cpp:402
ssim::CostReport::add
void add(const State state, const Time time, const Cost cost, const int index=0)
Definition: microsimulation.h:1066
ssim::Rpexp::t
vector< double > t
Definition: microsimulation.h:531
R::rllogis
double rllogis(double shape, double scale)
rllogis function for a random covariate from a log-logistic distribution with shape and scale....
Definition: microsimulation.cc:108
ssim::Means::_sum
long double _sum
Definition: microsimulation.h:482
ssim::r_remove_current_stream
void r_remove_current_stream()
A utility function to remove the current_stream. Used when finalising the microsimulation package in ...
Definition: microsimulation.cc:38
ssim::SimpleReport::revise
void revise(string field, T value)
revise changes the last value in a given field. Reminder: std::map::operator[] creates an element for...
Definition: microsimulation.h:1115
ssim::SummaryReport::_pt
PtMap _pt
Definition: microsimulation.h:989
ssim::Rpexp::n
int n
Definition: microsimulation.h:532
ssim::Rpexp
Rpexp is a random number generator class for piecewise constant hazards. Given time lower bounds t an...
Definition: microsimulation.h:491
ssim::CostReport::This
CostReport< State, Time, Cost > This
Definition: microsimulation.h:1007
ssim::SummaryReport::setUtilityDiscountRate
void setUtilityDiscountRate(Utility discountRate)
Definition: microsimulation.h:842
ssim::ProcessWithPId
utility Process class providing a utility interface with the simulator.
Definition: ssim.h:212
ssim::SummaryReport::IndividualCosts
std::vector< Cost > IndividualCosts
Definition: microsimulation.h:826
ssim::cProcess::init
virtual void init()=0
Abstract method to initialise a given process.
ssim::SummaryReport::UtilityMap
std::unordered_map< pair< State, Time >, Utility > UtilityMap
Definition: microsimulation.h:822
ssim::Rpexp::rand
double rand(double u, double from=0.0)
Definition: microsimulation.h:517
ssim::RngStream::ResetNextSubstream
void ResetNextSubstream()
Definition: RngStream.cpp:381
ssim::transpose
std::vector< std::vector< T > > transpose(const std::vector< std::vector< T > > data)
Function to transpose a vector of vectors. This assumes that all inner vectors have the same size and...
Definition: microsimulation.h:651
ssim::Means::mean
double mean()
Definition: microsimulation.h:457
ssim::SummaryReport::_prev
PrevMap _prev
Definition: microsimulation.h:987
ssim::EventReport::UtilityMap
std::unordered_map< pair< State, Time >, Utility > UtilityMap
Definition: microsimulation.h:672
ssim::CostReport::Table
std::unordered_map< pair< State, Time >, Cost > Table
Definition: microsimulation.h:1008
ssim::CostReport::setStartReportAge
void setStartReportAge(const Time a)
Definition: microsimulation.h:1029
ssim::EventReport::Pair
std::pair< State, Time > Pair
Definition: microsimulation.h:670
ssim::SimpleReport::append
void append(SimpleReport< T > &obj)
append another SimpleReport, which is useful for aggregating multiple reports.
Definition: microsimulation.h:1133
std::hash< std::tuple< TT... > >::operator()
size_t operator()(std::tuple< TT... > const &tt) const
Definition: microsimulation.h:80
ssim::EventReport::IndividualUtilities
vector< Utility > IndividualUtilities
Definition: microsimulation.h:675
rcpp_table.h
ssim::SummaryReport::utilityDiscountRate
Utility utilityDiscountRate
Definition: microsimulation.h:995
ssim::Means::sd
double sd()
Definition: microsimulation.h:461
ssim::EventReport::addBrief
void addBrief(const Time lhs, const Time rhs, const Utility utility=1)
Definition: microsimulation.h:734
Rcpp::wrap_map
SEXP wrap_map(const std::unordered_map< std::pair< std::tuple< T1, T2, T3 >, T4 >, T5 > ov, std::string name1, std::string name2)
Definition: microsimulation.h:1482
ssim::cMessage::name
string name
Definition: microsimulation.h:223
ssim::EventReport::PtMap
std::unordered_map< pair< State, Time >, Time > PtMap
Definition: microsimulation.h:673
ssim::SummaryReport::addPointCost
void addPointCost(const State state, const Cost cost, int index=0)
Definition: microsimulation.h:910
ssim::EventReport::setPartition
void setPartition(const Time start=0.0, const Time finish=100.0, const Time delta=1.0, const Time maxTime=Time(1.0e100))
Definition: microsimulation.h:687
ssim::Means::n
int n()
Definition: microsimulation.h:459
R
Definition: microsimulation.cc:101
ssim::cProcess::cancel_name
void cancel_name(string name)
Given a name, remove the messages for this process.
Definition: microsimulation.h:393
ssim::SummaryReport::EventMap
std::unordered_map< std::tuple< State, Event, Time >, int > EventMap
Definition: microsimulation.h:824
ssim::CancelEvents
void CancelEvents()
CancelEvents is a function to remove messages from the queue.
Definition: microsimulation.h:291
ssim::Means::_n
int _n
Definition: microsimulation.h:481
ssim::Sim::self_signal_event
static void self_signal_event(const Event *e)
signal an event to the current process immediately
Definition: ssim.cc:268
ssim::EventReport::_pt
PtMap _pt
Definition: microsimulation.h:803
ssim::Means::_sumsq
long double _sumsq
Definition: microsimulation.h:482
ssim::Sim::clock
static Time clock()
returns the current virtual time for the current process
Definition: ssim.cc:264
ssim::SummaryReport::PrevMap
std::unordered_map< pair< State, Time >, int > PrevMap
Definition: microsimulation.h:821
ssim::Rng::result_type
double result_type
Definition: microsimulation.h:550
ssim::cMessage::getName
string getName()
Definition: microsimulation.h:219
ssim::r_rng_advance_substream
void r_rng_advance_substream(double *inoutseed, int *n)
A utility function to advance the random sub-stream n steps for a specified seed.
Definition: microsimulation.cc:64
ssim.h
ssim::EventReport::_vector
IndividualUtilities _vector
Definition: microsimulation.h:805
ssim::Rng::min
result_type min()
Definition: microsimulation.h:552
ssim::SummaryReport::clear
void clear()
Definition: microsimulation.h:865
ssim::SummaryReport::indivp
bool indivp
Definition: microsimulation.h:985
ssim::CostReport::append
void append(This &new_report)
Definition: microsimulation.h:1059
ssim::Time
double Time
virtual time type
Definition: ssim.h:75
ssim::CostReport::Partition
std::set< Time, std::greater< Time > > Partition
Definition: microsimulation.h:1005
ssim::SummaryReport::discountedCost
Cost discountedCost(Time a, Cost cost)
Definition: microsimulation.h:968
ssim::EventReport::Partition
std::set< Time, std::greater< Time > > Partition
Definition: microsimulation.h:668
ssim::Rpexp::H
vector< double > H
Definition: microsimulation.h:531
std::hash< std::pair< T1, T2 > >::operator()
size_t operator()(std::pair< T1, T2 > const &p) const
Definition: microsimulation.h:91
ssim
name space for the Siena simulator.
Definition: microsimulation.cc:3
ssim::CostReport::mean_costs
Means mean_costs
Definition: microsimulation.h:1089
ssim::EventReport
EventReport class for collecting statistics on person-time, prevalence and numbers of events.
Definition: microsimulation.h:666
ssim::Means::sum
double sum()
Definition: microsimulation.h:460
ssim::SummaryReport::setPartition
void setPartition(const Time start, const Time finish, const Time delta, const Time maxTime=Time(1.0e100))
Definition: microsimulation.h:853
ssim::rweibullHR
double rweibullHR(double shape, double scale, double hr)
Random Weibull distribution for a given shape, scale and hazard ratio.
Definition: microsimulation.cc:5
ssim::cMessage::str
string str() const
Definition: microsimulation.h:226
ssim::RngStream::RandU01
double RandU01()
Definition: RngStream.cpp:566
ssim::cProcess::handleMessage
virtual void handleMessage(const cMessage *msg)=0
Abstract method to handle each message.
ssim::CostReport::IndividualCosts
std::vector< Cost > IndividualCosts
Definition: microsimulation.h:1009
ssim::SummaryReport::_indivUtilities
IndividualUtilities _indivUtilities
Definition: microsimulation.h:992
ssim::r_get_user_random_seed
void r_get_user_random_seed(double *outseed)
A utility function to set the user random seed for the simulation.
Definition: microsimulation.cc:52
ssim::EventReport::EventsMap
std::unordered_map< std::tuple< State, Event, Time >, int > EventsMap
Definition: microsimulation.h:674
ssim::CostReport::Pair
std::pair< State, Time > Pair
Definition: microsimulation.h:1006
ssim::SimpleReport::clear
void clear()
clear the report data
Definition: microsimulation.h:1123
ssim::SummaryReport::Pair
std::pair< State, Time > Pair
Definition: microsimulation.h:820
ssim::Rng::id
int id
Definition: microsimulation.h:561
ssim::Cancel
void Cancel(std::function< bool(const cMessage *msg)> pred)
Cancel is a function to cancel messages who satisfy a predicate.
Definition: microsimulation.h:244
ssim::Rng::operator()
result_type operator()()
Definition: microsimulation.h:551
ssim::discountedPoint
double discountedPoint(double time, double discountRate)
Simple function to calculate 1/(1+discountRate)^(time)
Definition: microsimulation.h:633
ssim::cMessage::cMessage
cMessage(const short k=-1, const string n="", const ProcessId process_id=-1, const ProcessId sender_process_id=-1)
Definition: microsimulation.h:213
ssim::cProcess::cancel
void cancel(std::function< bool(const cMessage *msg)> pred)
Given a predicate, remove the messages for this process.
Definition: microsimulation.h:375
ssim::CostReport::setPartition
void setPartition(const Time start, const Time finish, const Time delta, const Time maxTime=Time(1.0e100))
Definition: microsimulation.h:1044
ssim::EventReport::outputUtilities
bool outputUtilities
Definition: microsimulation.h:799
ssim::r_next_rng_substream
void r_next_rng_substream()
A utility function to move to the next user random stream for the simulation.
Definition: microsimulation.cc:60
ssim::SummaryReport::_costs
CostMap _costs
Definition: microsimulation.h:991
ssim::CostReport::wrap
SEXP wrap()
Definition: microsimulation.h:1076
ssim::EventReport::setPartition
void setPartition(const vector< Time > v)
Definition: microsimulation.h:684
ssim::CostReport
CostReport class for collecting statistics on costs.
Definition: microsimulation.h:1003
ssim::cProcess::process_event
virtual void process_event(const ssim::Event *e)
Implements the method required by ssim::ProcessWithPId This requires that the event* can be cast to a...
Definition: microsimulation.h:424
ssim::CostReport::startReportAge
Time startReportAge
Definition: microsimulation.h:1090
ssim::cMessage::timestamp
Time timestamp
Definition: microsimulation.h:224
ssim::test_rstream2
void test_rstream2(double *x)
Simple test of the random streams (with a stupid name)
Definition: microsimulation.cc:86
R::rnormPos
double rnormPos(double mean, double sd)
rnorm function constrained to be positive. This uses brute-force re-sampling rather than conditioning...
Definition: microsimulation.cc:102
ssim::SummaryReport::_events
EventMap _events
Definition: microsimulation.h:990
ssim::Means::operator+=
Means * operator+=(const double value)
Definition: microsimulation.h:463
ssim::Means::var
double var()
Definition: microsimulation.h:458
Rcpp
Definition: microsimulation.h:101
ssim::Rng
Definition: microsimulation.h:548
ssim::CancelName
void CancelName(string name)
CancelName is a function to remove messages with the given name from the queue.
Definition: microsimulation.h:282
ssim::CostReport::_vector
IndividualCosts _vector
Definition: microsimulation.h:1088
ssim::cMessage::sender_process_id
ProcessId sender_process_id
Definition: microsimulation.h:225
ssim::cProcess::cancel_kind
void cancel_kind(short kind)
Given a kind, remove the messages for this process.
Definition: microsimulation.h:384
ssim::CostReport::indiv
bool indiv
Definition: microsimulation.h:1092
ssim::Rpexp::h
vector< double > h
Definition: microsimulation.h:531
ssim::counter_id
static int counter_id
C++ wrapper class for the RngStream library. set() sets the current R random number stream to this st...
Definition: microsimulation.h:547
ssim::Rng::set
void set()
Definition: microsimulation.cc:26
ssim::CostReport::id
int id
Definition: microsimulation.h:1091
ssim::EventReport::indiv
bool indiv
Definition: microsimulation.h:809
ssim::EventReport::EventReport
EventReport(Utility discountRate=0.0, bool outputUtilities=true, int size=1, Time startReportAge=Time(0), bool indiv=false)
Definition: microsimulation.h:676
ssim::Means::wrap
SEXP wrap()
Definition: microsimulation.h:469
ssim::ProcessWithPId::process_id
ProcessId process_id
Definition: ssim.h:238
ssim::cMessage::getSendingTime
Time getSendingTime()
Definition: microsimulation.h:221
ssim::cMessage::kind
short kind
Definition: microsimulation.h:222
ssim::cProcess::initialize
void initialize()
Implements the method required by ssim::ProcessWithPId. Calls init() and sets the previousEventTime t...
Definition: microsimulation.h:417
ssim::EventReport::_events
EventsMap _events
Definition: microsimulation.h:804
ssim::SimpleReport::wrap
SEXP wrap()
wrap the report as a DataFrame or a List
Definition: microsimulation.h:1127
ssim::EventReport::PrevMap
std::unordered_map< pair< State, Time >, int > PrevMap
Definition: microsimulation.h:671
ssim::Rng::nextSubstream
void nextSubstream()
Definition: microsimulation.h:560
ssim::EventReport::_prev
PrevMap _prev
Definition: microsimulation.h:801
ssim::RngStream
Definition: RngStream.h:30
ssim::Means
Utility class to incrementally add values to calculate the mean, sum, variance and standard deviation...
Definition: microsimulation.h:455
ssim::SummaryReport::setCostDiscountRate
void setCostDiscountRate(Cost discountRate)
Definition: microsimulation.h:846