Microsimulation API: /home/marcle/src/R/microsimulation/inst/include/microsimulation.h Source File

Microsimulation API

microsimulation.h
Go to the documentation of this file.
1
30 #ifndef MICROSIMULATION_H
31 #define MICROSIMULATION_H
32
33 #include <RcppCommon.h>
34 #include <unordered_map>
35 #include <tuple>
36 #include <vector>
37 #include <utility>
38 #include <map>
39 #include <string>
40
41 /* https://stackoverflow.com/questions/7110301/generic-hash-for-tuples-in-unordered-map-unordered-set for a generic hash function for std::tuple
42 */
43 #include <tuple>
44 namespace std{
45 namespace
46 {
47 // Code from boost
48 // Reciprocal of the golden ratio helps spread entropy
49 // and handles duplicates.
50 // See Mike Seymour in magic-numbers-in-boosthash-combine:
51 // http://stackoverflow.com/questions/4948780
52 template <class T>
53 inline void hash_combine(std::size_t& seed, T const& v)
54 {
55 seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
56 }
57 // Recursive template code derived from Matthieu M.
58 template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
59 struct HashValueImpl
60 {
61 static void apply(size_t& seed, Tuple const& tuple)
62 {
63 HashValueImpl<Tuple, Index-1>::apply(seed, tuple);
64 hash_combine(seed, std::get<Index>(tuple));
65 }
66 };
67 template <class Tuple>
68 struct HashValueImpl<Tuple,0>
69 {
70 static void apply(size_t& seed, Tuple const& tuple)
71 {
72 hash_combine(seed, std::get<0>(tuple));
73 }
74 };
75 }
76 template <typename ... TT>
77 struct hash<std::tuple<TT...>>
78 {
79 size_t
80 operator()(std::tuple<TT...> const& tt) const
81 {
82 size_t seed = 0;
83 HashValueImpl<std::tuple<TT...> >::apply(seed, tt);
84 return seed;
85 }
86 };
87 template <typename T1, typename T2>
88 struct hash<std::pair<T1,T2>>
89 {
90 size_t
91 operator()(std::pair<T1,T2> const& p) const
92 {
93 size_t seed = 0;
94 hash_combine(seed, p.first);
95 hash_combine(seed, p.second);
96 return seed;
97 }
98 };
99 }
100
101 namespace Rcpp {
102
103 // vectors of pairs: recursively defined
104 template <class T1, class T2>
105 SEXP wrap(const std::vector<std::pair<T1,T2> > v);
106
107 // vectors tuples: enumerated cases
108 template <class T1, class T2>
109 SEXP wrap(const std::vector<std::tuple<T1,T2> > v);
110
111 template <class T1, class T2, class T3>
112 SEXP wrap(const std::vector<std::tuple<T1,T2,T3> > v);
113
114 template <class T1, class T2, class T3, class T4>
115 SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4> > v);
116
117 template <class T1, class T2, class T3, class T4, class T5>
118 SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4,T5> > v);
119
120 template <class T1, class T2, class T3, class T4, class T5, class T6>
121 SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4,T5,T6> > v);
122
123 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7>
124 SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4,T5,T6,T7> > v);
125
126 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
127 SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8> > v);
128
129 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9>
130 SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9> > v);
131
132 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10>
133 SEXP wrap(const std::vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10> > v);
134
135 // maps defined in terms of vectors
136 template <class T1, class T2>
137 SEXP wrap_map(const std::map<T1,T2> v);
138
139 template <class T1a, class T1b, class T2>
140 SEXP wrap_map(const std::map<std::pair<T1a,T1b>,T2> v,
141 std::string key, std::string name1, std::string name2);
142
143 template <class T1a, class T1b, class T1c, class T2>
144 SEXP wrap_map(const std::map<std::tuple<T1a,T1b,T1c>,T2> v,
145 std::string key, std::string name1, std::string name2, std::string name3);
146
147 template <class T1, class T2, class T3, class T4, class T5>
148 SEXP wrap_map(const std::map<std::pair<std::tuple<T1,T2,T3>,T4>,T5> v,
149 std::string key, std::string name1, std::string name2);
150
151 // unordered_maps defined in terms of vectors
152 template <class T1, class T2>
153 SEXP wrap_map(const std::unordered_map<T1,T2> v);
154
155 template <class T1a, class T1b, class T2>
156 SEXP wrap_map(const std::unordered_map<std::pair<T1a,T1b>,T2> v,
157 std::string key, std::string name1, std::string name2);
158
159 template <class T1a, class T1b, class T1c, class T2>
160 SEXP wrap_map(const std::unordered_map<std::tuple<T1a,T1b,T1c>,T2> v,
161 std::string key, std::string name1, std::string name2, std::string name3);
162
163 template <class T1, class T2, class T3, class T4, class T5>
164 SEXP wrap_map(const std::unordered_map<std::pair<std::tuple<T1,T2,T3>,T4>,T5> v,
165 std::string key, std::string name1, std::string name2);
166
167
168 } // namespace Rcpp
169
170 #include <Rcpp.h>
171 #include <R.h>
172 #include <Rmath.h>
173 #include <Rdefines.h>
174 #include <R_ext/Random.h>
175
176 #include <siena/ssim.h>
177 #include <RngStream.h>
178 #include <rcpp_table.h>
179
180 #include <string>
181 #include <algorithm>
182 #include <functional>
183 #include <set>
184
185 namespace ssim {
186
187 using std::string;
188 using std::vector;
189 using std::map;
190 using std::pair;
191 using std::greater;
192
193 // forward declarations
194 class cProcess;
195
200 #define WithRNG(rng,expr) (rng->set(), expr)
201
211 class cMessage : public ssim::Event {
212 public:
213 cMessage(const short k = -1, const string n = "", const ProcessId process_id = -1,
214 const ProcessId sender_process_id = -1) :
215 kind(k), name(n), sendingTime(-1.0), timestamp(0), process_id(process_id),
216 sender_process_id(sender_process_id) { }
217 // currently no setters (keep it lightweight?)
218 short getKind() { return kind; }
219 string getName() { return name; }
220 Time getTimestamp() { return timestamp; }
221 Time getSendingTime() {return sendingTime; }
222 short kind;
223 string name;
224 Time sendingTime, timestamp;
225 ProcessId process_id, sender_process_id;
226 string str() const {
227 std::ostringstream stringStream;
228 stringStream << "kind=";
229 stringStream << kind;
230 stringStream << ",name=";
231 stringStream << name;
232 string str = stringStream.str();
233 return str;
234 }
235 // this does NOT include schedulePriority
236 };
237
243 inline void
244 Cancel(std::function<bool(const cMessage * msg)> pred) {
245 return Sim::ignore_event([pred](const Event * e) {
246 const cMessage * msg = dynamic_cast<const cMessage *>(e);
247 return (msg != 0 && pred(msg));
248 });
249 }
250
256 inline void RemoveKind(short kind) {
257 Cancel([kind](const cMessage * msg) { return msg->kind == kind; });
258 }
259
265 inline void CancelKind(short kind) {
266 Cancel([kind](const cMessage * msg) { return msg->kind == kind; });
267 }
268
274 inline void RemoveName(string name) {
275 Cancel([name](const cMessage * msg) { return msg->name == name; });
276 }
282 inline void CancelName(string name) {
283 Cancel([name](const cMessage * msg) { return msg->name == name; });
284 }
285
291 inline void CancelEvents() {
292 Cancel([](const cMessage * msg) { return true; });
293 }
294
295
314 class cProcess : public ssim::ProcessWithPId {
315 public:
316 Time startTime, previousEventTime;
317 cProcess(Time startTime = Time(0.0)) : startTime(startTime), previousEventTime(startTime) { }
321 virtual void handleMessage(const cMessage * msg) = 0;
325 virtual void init() = 0;
326 // see also `void stop()`
331 virtual void scheduleAt(Time t, cMessage * msg) { // virtual or not?
332 msg->timestamp = t;
333 msg->sendingTime = Sim::clock();
334 msg->process_id = msg->sender_process_id = pid();
335 Sim::self_signal_event(msg, t - Sim::clock());
336 }
340 virtual void scheduleAt(Time t, string name) {
341 scheduleAt(t, new cMessage(-1,name));
342 }
346 virtual void scheduleAt(Time t, short k) {
347 scheduleAt(t, new cMessage(k,""));
348 }
353 virtual void send(ProcessId process_id, Time t, cMessage * msg) { // virtual or not?
354 msg->timestamp = t;
355 msg->process_id = process_id;
356 msg->sender_process_id = pid();
357 msg->sendingTime = Sim::clock();
358 Sim::signal_event(process_id, msg, t - Sim::clock());
359 }
363 virtual void send(ProcessId process_id, Time t, string name) {
364 send(process_id, t, new cMessage(-1,name));
365 }
369 virtual void send(ProcessId process_id, Time t, short kind) {
370 send(process_id, t, new cMessage(kind,""));
371 }
375 void cancel(std::function<bool(const cMessage * msg)> pred) {
376 Cancel([pred,this](const cMessage * msg) {
377 return pred(msg) &&
378 msg->process_id == this->pid();
379 });
380 }
384 void cancel_kind(short kind) {
385 cancel([kind,this](const cMessage * msg) {
386 return msg->kind == kind &&
387 msg->process_id == this->pid();
388 });
389 }
393 void cancel_name(string name) {
394 cancel([name,this](const cMessage * msg) {
395 return msg->name == name &&
396 msg->process_id == this->pid();
397 });
398 }
402 void cancel_events() {
403 cancel([this](const cMessage * msg) {
404 return msg->process_id == this->pid();
405 });
406 }
410 double previous() {
411 return previousEventTime;
412 }
417 void initialize() { init(); previousEventTime = startTime; }
424 virtual void process_event(const ssim::Event * e) { // virtual or not?
425 const cMessage * msg;
426 if ((msg = dynamic_cast<const cMessage *>(e)) != 0) {
427 handleMessage(msg);
428 previousEventTime = Sim::clock();
429 } else {
430 // cf. cerr, specialised for R
431 REprintf("cProcess is only written to receive cMessage events\n");
432 }
433 }
434 };
435
439 typedef Time simtime_t;
440
444 Time simTime();
445
449 Time now();
450
455 class Means {
456 public:
457 double mean() { return _sum/_n; }
458 double var() {return (long double)_n/(_n-1)*(_sumsq/_n - mean()*mean()); }
459 int n() {return _n;}
460 double sum() {return _sum;}
461 double sd() {return sqrt(var()); }
462 Means() : _n(0), _sum(0.0), _sumsq(0.0) {}
463 Means* operator +=(const double value) {
464 _n++;
465 _sum += (long double) value;
466 _sumsq += (long double)value * (long double)value;
467 return this;
468 }
469 SEXP wrap() {
470 return Rcpp::DataFrame::create(_("n")=n(),
471 _("mean")=mean(),
472 _("var")=var(),
473 _("sd")=sd(),
474 _("se")=sd()/sqrt(n()),
475 _("sum")=sum(),
476 _("sumsq")=_sumsq
477 );
478 }
479 //friend std::ostream& operator<<(std::ostream& os, Means& m);
480 private:
481 int _n;
482 long double _sum, _sumsq;
483 };
484
491 class Rpexp {
492 public:
493 Rpexp() {} // blank default constructor
494 Rpexp(double *hin, double *tin, int nin) : n(nin) {
495 int i;
496 H.resize(n);
497 t.resize(n);
498 h.resize(n);
499 H[0]=0.0; h[0]=hin[0]; t[0]=tin[0];
500 if (n>1) {
501 for(i=1;i<n;i++) {
502 h[i]=hin[i]; t[i]=tin[i];
503 H[i] = H[i-1]+(t[i]-t[i-1])*h[i-1];
504 }
505 }
506 }
507 /* Rpexp(vector<double> hin, vector<double> tin) : h(hin), t(tin) { */
508 /* n = h.size(); */
509 /* H.resize(n); */
510 /* H[0] = 0.0; */
511 /* if (n>1) { */
512 /* for(i=1;i<n;i++) { */
513 /* H[i] = H[i-1]+(t[i]-t[i-1])*h[i-1]; */
514 /* } */
515 /* } */
516 /* } */
517 double rand(double u, double from = 0.0) {
518 double v = 0.0, H0 = 0.0, tstar = 0.0;
519 int i = 0, i0 = 0;
520 if (from > 0.0) {
521 i0 = (from >= t[n-1]) ? (n-1) : int(lower_bound(t.begin(), t.end(), from) - t.begin())-1;
522 H0 = H[i0] + (from - t[i0])*h[i0];
523 }
524 v = -log(u) + H0;
525 i = (v >= H[n-1]) ? (n-1) : int(lower_bound(H.begin(), H.end(), v) - H.begin())-1;
526 tstar = t[i]+(v-H[i])/h[i];
527 return tstar;
528 }
529
530 private:
531 vector<double> H, h, t;
532 int n;
533 };
534
535
539 double rweibullHR(double shape, double scale, double hr);
540
541
547 static int counter_id = 0;
548 class Rng : public RngStream {
549 public:
550 typedef double result_type;
551 result_type operator()() { return RandU01(); }
552 result_type min() { return 0.0; }
553 result_type max() { return 1.0; }
554 Rng() : RngStream() { id = ++counter_id; }
555 virtual ~Rng();
556 void seed(const double seed[6]) {
557 SetSeed(seed);
558 }
559 void set();
560 void nextSubstream() { ResetNextSubstream(); }
561 int id;
562 };
563
564
565 extern "C" { // functions that will be called from R
566
571 void r_create_current_stream();
572
577 void r_remove_current_stream();
578
582 void r_set_user_random_seed(double * seed);
583
587 void r_get_user_random_seed(double * seed);
588
589 /* /\** */
590 /* @brief A utility function to move to the next user random stream for the simulation. */
591 /* *\/ */
592 /* void r_next_rng_stream(); */
593
597 void r_next_rng_substream();
598
602 void r_rng_advance_substream(double * seed, int * n);
603
607 void test_rstream2(double * x);
608
609 } // extern "C"
610
611
616 inline double discountedInterval(double start, double end, double discountRate) {
617 if (discountRate == 0.0) return end - start;
618 else return (pow(1.0+discountRate,-start) - pow(1.0+discountRate,-end)) / log(1.0+discountRate);
619 }
620
625 inline double discountedInterval(double y, double start, double end, double discountRate) {
626 if (discountRate == 0.0) return y*(end - start);
627 else return y*(pow(1.0+discountRate,-start) - pow(1.0+discountRate,-end)) / log(1.0+discountRate);
628 }
629
633 inline double discountedPoint(double time, double discountRate) {
634 return discountRate <= 0.0 ? 1.0 : pow(1.0+discountRate,-time);
635 }
639 inline double discountedPoint(double y, double time, double discountRate) {
640 return discountRate <= 0.0 ? y : y*pow(1.0+discountRate,-time);
641 }
642
643
650 template <class T>
651 std::vector<std::vector<T> > transpose(const std::vector<std::vector<T> > data) {
652 std::vector<std::vector<T> > result(data[0].size(),
653 std::vector<T>(data.size()));
654 for (typename std::vector<T>::size_type i = 0; i < data[0].size(); i++)
655 for (typename std::vector<T>::size_type j = 0; j < data.size(); j++) {
656 result[i][j] = data[j][i];
657 }
658 return result;
659 }
660
661
665 template< class State, class Event, class Time = double, class Utility = double>
666 class EventReport {
667 public:
668 typedef std::set<Time, std::greater<Time> > Partition; // NB: greater<Time> cf. lesser<Time>
669 typedef typename Partition::iterator Iterator;
670 typedef std::pair<State,Time> Pair;
671 typedef std::unordered_map<pair<State,Time>, int > PrevMap;
672 typedef std::unordered_map<pair<State,Time>, Utility > UtilityMap;
673 typedef std::unordered_map<pair<State,Time>, Time > PtMap;
674 typedef std::unordered_map<std::tuple<State,Event,Time>, int > EventsMap;
675 typedef vector<Utility> IndividualUtilities;
676 EventReport(Utility discountRate = 0.0, bool outputUtilities = true, int size = 1, Time startReportAge = Time(0), bool indiv = false) :
677 discountRate(discountRate), outputUtilities(outputUtilities), startReportAge(startReportAge), id(0), indiv(indiv) {
678 _vector.resize(size);
679 setPartition(startReportAge);
680 }
681 void resize(int size) {
682 _vector.resize(size);
683 }
684 void setPartition(const vector<Time> v) {
685 copy(v.begin(), v.end(), inserter(_partition, _partition.begin()));
686 }
687 void setPartition(const Time start = 0.0, const Time finish = 100.0,
688 const Time delta = 1.0,
689 const Time maxTime = Time(1.0e100)) {
690 _partition.clear();
691 for (Time t=start; t<=finish; t+=delta) _partition.insert(t);
692 _partition.insert(maxTime);
693 }
694 void setIndivN(const int n) {
695 resize(n);
696 indiv = true;
697 }
698 void setStartReportAge(const Time a) {
699 startReportAge = a;
700 }
701 void clear() {
702 _ut.clear();
703 _pt.clear();
704 _events.clear();
705 _prev.clear();
706 _partition.clear();
707 _vector.clear();
708 current = Utility(0);
709 }
710 void individualReset () {
711 if (now() >= startReportAge)
712 mean_utilities += double(current);
713 if (indiv) {
714 if (id>=int(_vector.size()))
715 REprintf("Vector too small in EventReport: use resize(int) method");
716 else
717 _vector[id] = (now() >= startReportAge) ? double(current) : NA_REAL;
718 }
719 current = Utility(0);
720 id++;
721 }
722 Utility discountedUtilities(Time a, Time b, Utility utility = 1.0) {
723 if (discountRate == 0.0) return utility * (b-a);
724 else if (a==b) return 0.0;
725 else if (discountRate>0.0) {
726 double alpha = log(1.0+discountRate);
727 return utility/alpha*(exp(-a*alpha) - exp(-b*alpha));
728 }
729 else {
730 REprintf("discountRate less than zero.");
731 return 0.0;
732 }
733 }
734 void addBrief(const Time lhs, const Time rhs, const Utility utility = 1) {
735 if (rhs >= startReportAge)
736 current += discountedUtilities(std::max<Time>(lhs-startReportAge,Time(0)), rhs-startReportAge, utility);
737 }
738 void add(const State state, const Event event, const Time lhs, const Time rhs, const Utility utility = 1, int index = 0 /* deprecated argument */) {
739 addBrief(lhs, rhs, utility);
740 Iterator lo, hi, it, last;
741 lo = _partition.lower_bound(lhs);
742 hi = _partition.lower_bound(rhs);
743 last = _partition.begin(); // because it is ordered by greater<Time>
744 ++_events[std::make_tuple(state,event,*hi)];
745 bool iterating;
746 for(it = lo, iterating = true; iterating; iterating = (it != hi), --it) { // decrement for greater<Time>
747 if (lhs<=(*it) && (*it)<rhs) // cadlag
748 ++_prev[Pair(state,*it)];
749 if (it == last) {
750 if (outputUtilities) {
751 Utility u = discountedUtilities(std::max<Time>(lhs,*it), rhs, utility);
752 _ut[Pair(state,*it)] += u;
753 }
754 _pt[Pair(state,*it)] += rhs - std::max<Time>(lhs,*it);
755 }
756 else {
757 Time next_value = *(--it); it++; // decrement/increment for greater<Time>
758 if (outputUtilities) {
759 Utility u = discountedUtilities(std::max<Time>(lhs,*it), std::min<Time>(rhs,next_value), utility);
760 _ut[Pair(state,*it)] += u;
761 }
762 _pt[Pair(state,*it)] += std::min<Time>(rhs,next_value) - std::max<Time>(lhs,*it);
763 }
764 }
765 }
766 template<class T>
767 void append_map(T & base_map, T & new_map) {
768 typename T::iterator it;
769 for (it = new_map.begin(); it != new_map.end(); ++it)
770 base_map[it->first] += it->second;
771 }
772 void append(EventReport<State,Event,Time,Utility> & er) {
773 append_map<PrevMap>(_prev,er._prev);
774 append_map<EventsMap>(_events,er._events);
775 append_map<PtMap>(_pt,er._pt);
776 append_map<UtilityMap>(_ut,er._ut);
777 _vector.insert(_vector.end(), er._vector.begin(), er._vector.end());
778 }
779 SEXP wrap() {
780 using namespace Rcpp;
781 if (_events.size() == 0) return List::create();
782 else if (outputUtilities)
783 return List::create(_("pt") = wrap_map<State,Time,Time>(_pt,"Key","age","pt"),
784 _("ut") = wrap_map<State,Time,Utility>(_ut,"Key","age","utility"),
785 _("events") = wrap_map<State,Event,Time,int>(_events,"Key","event","age","number"),
786 _("prev") = wrap_map<State,Time,int>(_prev,"Key","age","number"));
787 else
788 return List::create(_("pt") = wrap_map<State,Time,Time>(_pt,"Key","age","pt"),
789 _("events") = wrap_map<State,Event,Time,int>(_events,"Key","event","age","number"),
790 _("prev") = wrap_map<State,Time,int>(_prev,"Key","age","number"));
791 }
792 SEXP wrap_indiv() {
793 return Rcpp::wrap(_vector);
794 }
795 SEXP wrap_means() {
796 return mean_utilities.wrap();
797 }
798 Utility discountRate, current;
799 bool outputUtilities;
800 Partition _partition;
801 PrevMap _prev;
802 UtilityMap _ut;
803 PtMap _pt;
804 EventsMap _events;
805 IndividualUtilities _vector;
806 Means mean_utilities;
807 Time startReportAge;
808 int id;
809 bool indiv;
810 };
811
815 template< class State, class Event = short, class Time = double, class Utility = double, class Cost = double>
816 class SummaryReport {
817 public:
818 typedef std::set<Time, std::greater<Time> > Partition; // NB: greater<Time> cf. lesser<Time>
819 typedef typename Partition::iterator Iterator;
820 typedef std::pair<State,Time> Pair;
821 typedef std::unordered_map<pair<State,Time>, int > PrevMap;
822 typedef std::unordered_map<pair<State,Time>, Utility > UtilityMap;
823 typedef std::unordered_map<pair<State,Time>, Time > PtMap;
824 typedef std::unordered_map<std::tuple<State,Event,Time>, int > EventMap;
825 typedef std::unordered_map<pair<State,Time>, Cost > CostMap;
826 typedef std::vector<Cost> IndividualCosts;
827 typedef std::vector<Utility> IndividualUtilities;
828 SummaryReport(int n = 1, bool indivp = true, Utility discountRate = 0.0) : n(n), indivp(indivp) {
829 resize(indivp ? n : 1);
830 setDiscountRate(discountRate);
831 setUtility(1.0);
832 setCost(0.0);
833 }
834 void resize(int size) {
835 _indivUtilities.resize(size);
836 _indivCosts.resize(size);
837 }
838 void setDiscountRate(Utility discountRate) {
839 setUtilityDiscountRate(discountRate);
840 setCostDiscountRate(discountRate);
841 }
842 void setUtilityDiscountRate(Utility discountRate) {
843 this->utilityDiscountRate = discountRate;
844 utilityAlpha = log(1.0+utilityDiscountRate);
845 }
846 void setCostDiscountRate(Cost discountRate) {
847 this->costDiscountRate = discountRate;
848 costAlpha = log(1.0+costDiscountRate);
849 }
850 void setPartition(const vector<Time> v) {
851 copy(v.begin(), v.end(), inserter(_partition, _partition.begin()));
852 }
853 void setPartition(const Time start, const Time finish, const Time delta,
854 const Time maxTime = Time(1.0e100)) {
855 _partition.clear();
856 for (Time t=start; t<=finish; t+=delta) _partition.insert(t);
857 _partition.insert(maxTime);
858 }
859 void setUtility(Utility _utility) {
860 this->utility = _utility;
861 }
862 void setCost(Cost _cost) {
863 this->cost = _cost;
864 }
865 void clear() {
866 _ut.clear();
867 _pt.clear();
868 _events.clear();
869 _prev.clear();
870 _partition.clear();
871 _costs.clear();
872 _indivUtilities.clear();
873 _indivCosts.clear();
874 }
875 void add(const State state, const Event event, const Time lhs, const Time rhs, int index = 0) {
876 // Time lhs = process->previousEventTime;
877 // Time rhs = ssim::now();
878 if (!indivp) index = 0;
879 Iterator lo, hi, it, last;
880 lo = _partition.lower_bound(lhs);
881 hi = _partition.lower_bound(rhs);
882 last = _partition.begin(); // because it is ordered by greater<Time>
883 ++_events[std::make_tuple(state,event,*hi)];
884 bool iterating;
885 for(it = lo, iterating = true; iterating; iterating = (it != hi), --it) { // decrement for greater<Time>
886 if (lhs<=(*it) && (*it)<rhs) // cadlag
887 ++_prev[Pair(state,*it)];
888 if (it == last) {
889 Utility u = discountedUtilityInterval(std::max<Time>(lhs,*it), rhs, utility);
890 _ut[Pair(state,*it)] += u;
891 _indivUtilities[index] += u;
892 Cost c = discountedCostInterval(std::max<Time>(lhs,*it), rhs, cost);
893 _costs[Pair(state,*it)] += c;
894 _indivCosts[index] += c;
895 _pt[Pair(state,*it)] += rhs - std::max<Time>(lhs,*it);
896 }
897 else {
898 Time next_value = *(--it); it++; // decrement/increment for greater<Time>
899 Utility u = discountedUtilityInterval(std::max<Time>(lhs,*it), std::min<Time>(rhs,next_value), utility);
900 _ut[Pair(state,*it)] += u;
901 _indivUtilities[index] += u;
902 Cost c = discountedCostInterval(std::max<Time>(lhs,*it), std::min<Time>(rhs,next_value), cost);
903 _costs[Pair(state,*it)] += c;
904 _indivCosts[index] += c;
905 _pt[Pair(state,*it)] += std::min<Time>(rhs,next_value) - std::max<Time>(lhs,*it);
906 }
907 }
908 }
909 // void addPointCost(const State state, const Time time, const Cost cost, const int index = 0) {
910 void addPointCost(const State state, const Cost cost, int index = 0) {
911 if (!indivp) index = 0;
912 Time time = ssim::now();
913 Time time_lhs = * _partition.lower_bound(time);
914 Cost c = discountedCost(time,cost);
915 _costs[Pair(state,time_lhs)] += c;
916 _indivCosts[index] += c;
917 }
918 void append(SummaryReport<State,Event,Time,Utility> & er) {
919 append_map<PrevMap>(_prev,er._prev);
920 append_map<EventMap>(_events,er._events);
921 append_map<PtMap>(_pt,er._pt);
922 append_map<UtilityMap>(_ut,er._ut);
923 append_map<CostMap>(_costs,er._costs);
924 _indivUtilities.insert(_indivUtilities.end(), er._indivUtilities.begin(), er._indivUtilities.end());
925 _indivCosts.insert(_indivCosts.end(), er._indivCosts.begin(), er._indivCosts.end());
926 }
927 Rcpp::List asList() {
928 using namespace Rcpp;
929 if (_events.size() == 0) return List::create();
930 Rcpp::DataFrame indiv =
931 Rcpp::DataFrame::create(_("utilities")=_indivUtilities,
932 _("costs")=_indivCosts);
933 Rcpp::List out = List::create(_("pt") = wrap_map<State,Time,Time>(_pt,"Key","age","pt"),
934 _("ut") = wrap_map<State,Time,Utility>(_ut,"Key","age","utility"),
935 _("events") = wrap_map<State,Event,Time,int>(_events,"Key","event","age","number"),
936 _("prev") = wrap_map<State,Time,int>(_prev,"Key","age","number"),
937 _("costs") = wrap_map<State,Time,Cost>(_costs,"Key","age","cost"),
938 _("n")=n,
939 _("indivp")=indivp,
940 _("utilityDiscountRate")=utilityDiscountRate,
941 _("costDiscountRate")=costDiscountRate,
942 _("indiv")=indiv);
943 out.attr("class")="SummaryReport";
944 return out;
945 }
946 Utility discountedUtilityInterval(Time a, Time b, Utility utility) {
947 if (a == b || utility == 0.0) return 0.0;
948 else if (utilityDiscountRate == 0.0) return utility * (b-a);
949 else if (utilityDiscountRate>0.0) {
950 return utility/utilityAlpha*(exp(-a*utilityAlpha) - exp(-b*utilityAlpha));
951 }
952 else {
953 REprintf("utilityDiscountRate less than zero: %f.\n", utilityDiscountRate);
954 return 0.0;
955 }
956 }
957 Cost discountedCostInterval(Time a, Time b, Cost cost) {
958 if (a == b || cost == 0.0) return 0.0;
959 else if (costDiscountRate == 0.0) return cost * (b-a);
960 else if (costDiscountRate>0.0) {
961 return cost/costAlpha*(exp(-a*costAlpha) - exp(-b*costAlpha));
962 }
963 else {
964 REprintf("costDiscountRate less than zero: %f.\n", costDiscountRate);
965 return 0.0;
966 }
967 }
968 Cost discountedCost(Time a, Cost cost) {
969 if (cost == 0.0) return 0.0;
970 else if (costDiscountRate == 0.0) return cost;
971 else if (costDiscountRate>0)
972 return cost*exp(-a*costAlpha);
973 else {
974 REprintf("costDiscountRate less than zero: %f.\n", costDiscountRate);
975 return 0;
976 }
977 }
978 template<class T>
979 void append_map(T & base_map, T & new_map) {
980 typename T::iterator it;
981 for (it = new_map.begin(); it != new_map.end(); ++it)
982 base_map[it->first] += it->second;
983 }
984 int n;
985 bool indivp;
986 Partition _partition;
987 PrevMap _prev;
988 UtilityMap _ut;
989 PtMap _pt;
990 EventMap _events;
991 CostMap _costs;
992 IndividualUtilities _indivUtilities;
993 IndividualCosts _indivCosts;
994 // cProcess *process;
995 Utility utilityDiscountRate, utilityAlpha, utility;
996 Cost costDiscountRate, costAlpha, cost;
997 };
998
1002 template< class State, class Time = double, class Cost = double>
1003 class CostReport {
1004 public:
1005 typedef std::set<Time, std::greater<Time> > Partition;
1006 typedef std::pair<State,Time> Pair;
1007 typedef CostReport<State,Time,Cost> This;
1008 typedef std::unordered_map<pair<State,Time>, Cost > Table;
1009 typedef std::vector<Cost> IndividualCosts;
1010 CostReport(Cost discountRate = 0, int size = 1, Time startReportAge = Time(0), bool indiv = false) : discountRate(discountRate), startReportAge(startReportAge), id(0), indiv(indiv) {
1011 _vector.resize(size);
1012 }
1013 void individualReset () {
1014 if (now() >= startReportAge)
1015 mean_costs += double(current);
1016 if (indiv) {
1017 if (id>=int(_vector.size()))
1018 REprintf("Vector too small in CostReport: use resize(int) method");
1019 else
1020 _vector[id] = (now() >= startReportAge) ? double(current) : NA_REAL;
1021 }
1022 current = Cost(0);
1023 id++;
1024 }
1025 void setIndivN(const int n) {
1026 resize(n);
1027 indiv = true;
1028 }
1029 void setStartReportAge(const Time a) {
1030 startReportAge = a;
1031 }
1032 Cost discountedCost(Time a, Cost cost) {
1033 if (discountRate == 0.0) return cost;
1034 else if (discountRate>0.0)
1035 return cost/pow(1+discountRate,a);
1036 else {
1037 REprintf("discountRate less than zero.");
1038 return 0;
1039 }
1040 }
1041 void setPartition(const vector<Time> v) {
1042 copy(v.begin(), v.end(), inserter(_partition, _partition.begin()));
1043 }
1044 void setPartition(const Time start, const Time finish, const Time delta,
1045 const Time maxTime = Time(1.0e100)) {
1046 _partition.clear();
1047 for (Time t=start; t<=finish; t+=delta) _partition.insert(t);
1048 _partition.insert(maxTime);
1049 }
1050 void clear() {
1051 _table.clear();
1052 _partition.clear();
1053 _vector.clear();
1054 current = Cost(0);
1055 }
1056 void resize(int size) {
1057 _vector.resize(size);
1058 }
1059 void append(This & new_report) { // assuming that discountRate and _partition are the same for both reports
1060 typename Table::iterator it;
1061 _vector.insert(_vector.start(), new_report._vector.begin(), new_report._vector.end());
1062 for(it = new_report._table.begin(); it != new_report._table.end(); ++it) {
1063 _table[it->first] += it->second;
1064 }
1065 }
1066 void add(const State state, const Time time, const Cost cost, const int index = 0 /* deprecated argument */) {
1067 Time time_lhs = * _partition.lower_bound(time);
1068 Cost c = discountedCost(time,cost);
1069 _table[Pair(state,time_lhs)] += c;
1070 if (startReportAge == 0.0)
1071 current += c;
1072 else
1073 if (time>=startReportAge)
1074 current += discountedCost(time-startReportAge,cost);
1075 }
1076 SEXP wrap() {
1077 return Rcpp::wrap_map(_table,"Key","age","cost");
1078 }
1079 SEXP wrap_indiv() {
1080 return Rcpp::wrap(_vector);
1081 }
1082 SEXP wrap_means() {
1083 return mean_costs.wrap();
1084 }
1085 Cost discountRate, current;
1086 Partition _partition;
1087 Table _table;
1088 IndividualCosts _vector;
1089 Means mean_costs;
1090 Time startReportAge;
1091 int id;
1092 bool indiv;
1093 };
1094
1098 template<class T = double>
1099 class SimpleReport {
1100 public:
1104 typedef map<string,vector<T> > Map;
1108 void record(string field, T value) {
1109 _data[field].push_back(value);
1110 }
1115 void revise(string field, T value) {
1116 if (!_data[field].empty())
1117 _data[field].pop_back();
1118 _data[field].push_back(value);
1119 }
1123 void clear() { _data.clear(); }
1127 SEXP wrap() {
1128 return Rcpp::wrap(_data);
1129 }
1133 void append(SimpleReport<T> & obj) {
1134 for(typename Map::iterator it = obj._data.begin(); it != obj._data.end(); ++it) {
1135 _data[it->first].insert(_data[it->first].end(), it->second.begin(), it->second.end());
1136 }
1137 }
1141 Map _data;
1142 };
1143
1144 } // namespace ssim
1145
1146 namespace Rcpp {
1147
1148 template <class T1, class T2>
1149 SEXP wrap(const vector<pair<T1,T2> > v) {
1150 vector<T1> v1;
1151 vector<T2> v2;
1152 typename vector<pair<T1,T2> >::const_iterator it;
1153 for (it=v.begin(); it<v.end(); ++it) {
1154 v1.push_back(it->first);
1155 v2.push_back(it->second);
1156 }
1157 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2));
1158 }
1159
1160 template <class T1, class T2>
1161 SEXP wrap(const vector<std::tuple<T1,T2> > v) {
1162 int i, n=v.size();
1163 vector<T1> v1(n);
1164 vector<T2> v2(n);
1165 typename vector<std::tuple<T1,T2> >::const_iterator it;
1166 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1167 v1[i] = std::get<0>(*it);
1168 v2[i] = std::get<1>(*it);
1169 }
1170 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2));
1171 }
1172
1173 template <class T1, class T2, class T3>
1174 SEXP wrap(const vector<std::tuple<T1,T2,T3> > v) {
1175 int i, n=v.size();
1176 vector<T1> v1(n);
1177 vector<T2> v2(n);
1178 vector<T3> v3(n);
1179 typename vector<std::tuple<T1,T2,T3> >::const_iterator it;
1180 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1181 v1[i] = std::get<0>(*it);
1182 v2[i] = std::get<1>(*it);
1183 v3[i] = std::get<2>(*it);
1184 }
1185 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),_("Var3")=wrap(v3));
1186 }
1187
1188
1189 template <class T1, class T2, class T3, class T4>
1190 SEXP wrap(const vector<std::tuple<T1,T2,T3,T4> > v) {
1191 int i, n=v.size();
1192 vector<T1> v1(n);
1193 vector<T2> v2(n);
1194 vector<T3> v3(n);
1195 vector<T4> v4(n);
1196 typename vector<std::tuple<T1,T2,T3,T4> >::const_iterator it;
1197 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1198 v1[i] = std::get<0>(*it);
1199 v2[i] = std::get<1>(*it);
1200 v3[i] = std::get<2>(*it);
1201 v4[i] = std::get<3>(*it);
1202 }
1203 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1204 _("Var3")=wrap(v3),_("Var4")=wrap(v4));
1205 }
1206
1207 template <class T1, class T2, class T3, class T4, class T5>
1208 SEXP wrap(const vector<std::tuple<T1,T2,T3,T4,T5> > v) {
1209 int i, n=v.size();
1210 vector<T1> v1(n);
1211 vector<T2> v2(n);
1212 vector<T3> v3(n);
1213 vector<T4> v4(n);
1214 vector<T5> v5(n);
1215 typename vector<std::tuple<T1,T2,T3,T4,T5> >::const_iterator it;
1216 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1217 v1[i] = std::get<0>(*it);
1218 v2[i] = std::get<1>(*it);
1219 v3[i] = std::get<2>(*it);
1220 v4[i] = std::get<3>(*it);
1221 v5[i] = std::get<4>(*it);
1222 }
1223 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1224 _("Var3")=wrap(v3),_("Var4")=wrap(v4),
1225 _("Var5")=wrap(v5));
1226 }
1227
1228 template <class T1, class T2, class T3, class T4, class T5, class T6>
1229 SEXP wrap(const vector<std::tuple<T1,T2,T3,T4,T5,T6> > v) {
1230 int i, n=v.size();
1231 vector<T1> v1(n);
1232 vector<T2> v2(n);
1233 vector<T3> v3(n);
1234 vector<T4> v4(n);
1235 vector<T5> v5(n);
1236 vector<T6> v6(n);
1237 typename vector<std::tuple<T1,T2,T3,T4,T5,T6> >::const_iterator it;
1238 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1239 v1[i] = std::get<0>(*it);
1240 v2[i] = std::get<1>(*it);
1241 v3[i] = std::get<2>(*it);
1242 v4[i] = std::get<3>(*it);
1243 v5[i] = std::get<4>(*it);
1244 v6[i] = std::get<5>(*it);
1245 }
1246 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1247 _("Var3")=wrap(v3),_("Var4")=wrap(v4),
1248 _("Var5")=wrap(v5),_("Var6")=wrap(v6));
1249 }
1250
1251 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7>
1252 SEXP wrap(const vector<std::tuple<T1,T2,T3,T4,T5,T6,T7> > v) {
1253 int i, n=v.size();
1254 vector<T1> v1(n);
1255 vector<T2> v2(n);
1256 vector<T3> v3(n);
1257 vector<T4> v4(n);
1258 vector<T5> v5(n);
1259 vector<T6> v6(n);
1260 vector<T7> v7(n);
1261 typename vector<std::tuple<T1,T2,T3,T4,T5,T6,T7> >::const_iterator it;
1262 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1263 v1[i] = std::get<0>(*it);
1264 v2[i] = std::get<1>(*it);
1265 v3[i] = std::get<2>(*it);
1266 v4[i] = std::get<3>(*it);
1267 v5[i] = std::get<4>(*it);
1268 v6[i] = std::get<5>(*it);
1269 v7[i] = std::get<6>(*it);
1270 }
1271 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1272 _("Var3")=wrap(v3),_("Var4")=wrap(v4),
1273 _("Var5")=wrap(v5),_("Var6")=wrap(v6),
1274 _("Var7")=wrap(v7));
1275 }
1276
1277 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
1278 SEXP wrap(const vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8> > v) {
1279 int i, n=v.size();
1280 vector<T1> v1(n);
1281 vector<T2> v2(n);
1282 vector<T3> v3(n);
1283 vector<T4> v4(n);
1284 vector<T5> v5(n);
1285 vector<T6> v6(n);
1286 vector<T7> v7(n);
1287 vector<T8> v8(n);
1288 typename vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8> >::const_iterator it;
1289 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1290 v1[i] = std::get<0>(*it);
1291 v2[i] = std::get<1>(*it);
1292 v3[i] = std::get<2>(*it);
1293 v4[i] = std::get<3>(*it);
1294 v5[i] = std::get<4>(*it);
1295 v6[i] = std::get<5>(*it);
1296 v7[i] = std::get<6>(*it);
1297 v8[i] = std::get<7>(*it);
1298 }
1299 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1300 _("Var3")=wrap(v3),_("Var4")=wrap(v4),
1301 _("Var5")=wrap(v5),_("Var6")=wrap(v6),
1302 _("Var7")=wrap(v7),_("Var8")=wrap(v8));
1303 }
1304
1305 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9>
1306 SEXP wrap(const vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9> > v) {
1307 int i, n=v.size();
1308 vector<T1> v1(n);
1309 vector<T2> v2(n);
1310 vector<T3> v3(n);
1311 vector<T4> v4(n);
1312 vector<T5> v5(n);
1313 vector<T6> v6(n);
1314 vector<T7> v7(n);
1315 vector<T8> v8(n);
1316 vector<T9> v9(n);
1317 typename vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9> >::const_iterator it;
1318 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1319 v1[i] = std::get<0>(*it);
1320 v2[i] = std::get<1>(*it);
1321 v3[i] = std::get<2>(*it);
1322 v4[i] = std::get<3>(*it);
1323 v5[i] = std::get<4>(*it);
1324 v6[i] = std::get<5>(*it);
1325 v7[i] = std::get<6>(*it);
1326 v8[i] = std::get<7>(*it);
1327 v9[i] = std::get<8>(*it);
1328 }
1329 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1330 _("Var3")=wrap(v3),_("Var4")=wrap(v4),
1331 _("Var5")=wrap(v5),_("Var6")=wrap(v6),
1332 _("Var7")=wrap(v7),_("Var8")=wrap(v8),
1333 _("Var9")=wrap(v9));
1334 }
1335
1336 template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10>
1337 SEXP wrap(const vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10> > v) {
1338 int i, n=v.size();
1339 vector<T1> v1(n);
1340 vector<T2> v2(n);
1341 vector<T3> v3(n);
1342 vector<T4> v4(n);
1343 vector<T5> v5(n);
1344 vector<T6> v6(n);
1345 vector<T7> v7(n);
1346 vector<T8> v8(n);
1347 vector<T9> v9(n);
1348 vector<T10> v10(n);
1349 typename vector<std::tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10> >::const_iterator it;
1350 for (it=v.begin(), i=0; it<v.end(); ++it, ++i) {
1351 v1[i] = std::get<0>(*it);
1352 v2[i] = std::get<1>(*it);
1353 v3[i] = std::get<2>(*it);
1354 v4[i] = std::get<3>(*it);
1355 v5[i] = std::get<4>(*it);
1356 v6[i] = std::get<5>(*it);
1357 v7[i] = std::get<6>(*it);
1358 v8[i] = std::get<7>(*it);
1359 v9[i] = std::get<8>(*it);
1360 v10[i] = std::get<9>(*it);
1361 }
1362 return List::create(_("Var1")=wrap(v1),_("Var2")=wrap(v2),
1363 _("Var3")=wrap(v3),_("Var4")=wrap(v4),
1364 _("Var5")=wrap(v5),_("Var6")=wrap(v6),
1365 _("Var7")=wrap(v7),_("Var8")=wrap(v8),
1366 _("Var9")=wrap(v9),_("Var10")=wrap(v10));
1367 }
1368
1369
1370 template <class T1, class T2>
1371 SEXP wrap_map(const std::map<T1,T2> v) {
1372 int i;
1373 int n = v.size();
1374 vector<T1> x(n);
1375 vector<T2> y(n);
1376 typename std::map<T1,T2>::const_iterator it;
1377 for (it=v.begin(), i=0; it != v.end(); ++it, ++i) {
1378 x[i] = (*it).first;
1379 y[i] = (*it).second;
1380 }
1381 return DataFrame::create(_("Key")=wrap(x),_("Value")=wrap(y));
1382 }
1383
1384 // Special cases for wrap_map:
1385 //
1386 // map<pair<Tstate,T>, int > _prev;
1387 // map<pair<Tstate,T>, T > _pt;
1388 // map<std::tuple<Tstate,Tevent,T>, int > _events;
1389
1390 template <class T1a, class T1b, class T1c, class T2>
1391 SEXP wrap_map(const std::map<std::tuple<T1a,T1b,T1c>,T2> v,
1392 std::string key, std::string name1, std::string name2, std::string name3) {
1393 typedef std::tuple<T1a,T1b,T1c> Tuple;
1394 int i;
1395 int n = v.size();
1396 vector<T1a> xa(n);
1397 vector<T1b> xb(n);
1398 vector<T1c> xc(n);
1399 vector<T2> y(n);
1400 typename std::map<Tuple,T2>::const_iterator it;
1401 for (it=v.begin(), i=0; it != v.end(); ++it, ++i) {
1402 xa[i] = get<0>((*it).first);
1403 xb[i] = get<1>((*it).first);
1404 xc[i] = get<2>((*it).first);
1405 y[i] = (*it).second;
1406 }
1407 return Rcpp::DataFrame::create(_[key]=Rcpp::wrap(xa), _[name1]=Rcpp::wrap(xb),
1408 _[name2]=Rcpp::wrap(xc), _[name3]=Rcpp::wrap(y));
1409 }
1410
1411
1412 template <class T1a, class T1b, class T2>
1413 SEXP wrap_map(const std::map<std::pair<T1a,T1b>,T2> v,
1414 std::string key, std::string name1, std::string name2) {
1415 typedef std::pair<T1a,T1b> Pair;
1416 int i;
1417 int n = v.size();
1418 vector<T1a> xa(n);
1419 vector<T1b> xb(n);
1420 vector<T2> y(n);
1421 typename std::map<Pair,T2>::const_iterator it;
1422 for (it=v.begin(), i=0; it != v.end(); ++it, ++i) {
1423 xa[i] = (*it).first.first;
1424 xb[i] = (*it).first.second;
1425 y[i] = (*it).second;
1426 }
1427 return Rcpp::DataFrame::create(_[key]=Rcpp::wrap(xa), _[name1]=Rcpp::wrap(xb),
1428 _[name2]=Rcpp::wrap(y));
1429 }
1430
1431
1432 template <class T1, class T2>
1433 SEXP wrap_map(const std::unordered_map<T1,T2> ov) {
1434 std::map<T1,T2> v(ov.begin(), ov.end());
1435 return wrap_map<T1,T2>(v);
1436 }
1437
1438 template <class T1a, class T1b, class T1c, class T2>
1439 SEXP wrap_map(const std::unordered_map<std::tuple<T1a,T1b,T1c>,T2> ov,
1440 std::string key, std::string name1, std::string name2, std::string name3) {
1441 std::map<std::tuple<T1a,T1b,T1c>,T2> v(ov.begin(), ov.end());
1442 return wrap_map<T1a, T1b, T1c, T2>(v, key, name1, name2, name3);
1443 }
1444
1445 template <class T1a, class T1b, class T2>
1446 SEXP wrap_map(const std::unordered_map<std::pair<T1a,T1b>,T2> ov,
1447 std::string key, std::string name1, std::string name2) {
1448 std::map<std::pair<T1a,T1b>,T2> v(ov.begin(), ov.end());
1449 return wrap_map<T1a, T1b, T2>(v, key, name1, name2);
1450 }
1451
1452 template <class T1, class T2, class T3, class T4, class T5>
1453 SEXP wrap_map(const std::map<std::pair<std::tuple<T1,T2,T3>,T4>,T5> v,
1454 std::string name1, std::string name2) {
1455 typedef std::tuple<T1,T2,T3> Tuple;
1456 typedef std::pair<Tuple,T4> Pair;
1457 int i;
1458 int n = v.size();
1459 vector<T1> x1(n);
1460 vector<T2> x2(n);
1461 vector<T3> x3(n);
1462 vector<T4> x4(n);
1463 vector<T5> y(n);
1464 typename std::map<Pair,T5>::const_iterator it;
1465 for (it=v.begin(), i=0; it != v.end(); ++it, ++i) {
1466 Tuple tuple = (*it).first.first;
1467 x1[i] = std::get<0>(tuple);
1468 x2[i] = std::get<1>(tuple);
1469 x3[i] = std::get<2>(tuple);
1470 x4[i] = (*it).first.second;
1471 y[i] = (*it).second;
1472 }
1473 DataFrame out = Rcpp::DataFrame::create(Rcpp::Named("col1")=wrap(x1),
1474 Rcpp::Named("col2")=wrap(x2),
1475 Rcpp::Named("col3")=wrap(x3),
1476 Rcpp::Named(name1)=wrap(x4),
1477 Rcpp::Named(name2)=wrap(y));
1478 return out;
1479 }
1480
1481 template <class T1, class T2, class T3, class T4, class T5>
1482 SEXP wrap_map(const std::unordered_map<std::pair<std::tuple<T1,T2,T3>,T4>,T5> ov,
1483 std::string name1, std::string name2) {
1484 std::map<std::pair<std::tuple<T1,T2,T3>,T4>,T5> v(ov.begin(), ov.end());
1485 return wrap_map<T1,T2,T3,T4,T5>(v, name1, name2);
1486 }
1487
1488
1489
1490 } // Rcpp namespace
1491
1492
1493 namespace R {
1499 double rnormPos(double mean, double sd);
1500
1506 double rllogis(double shape, double scale);
1512 double rllogis_trunc(double shape, double scale, double left);
1521 double rgompertz(double shape = 1.0, double rate = 1.0);
1522 }
1523
1524 #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