Commit b329535a by Tianqi Yang

feat(markov): add support of supplement

- Add support of supplement
parent 1d46f501
No preview for this file type
...@@ -8,8 +8,9 @@ int main () ...@@ -8,8 +8,9 @@ int main ()
{ {
int lambda; int lambda;
double p; double p;
cin >> lambda >> p; int threshold;
cin >> lambda >> p >> threshold;
for ( int i = 0; i < 10; ++i ) { for ( int i = 0; i < 10; ++i ) {
cout << setprecision ( 13 ) << ( 1 - simulate_prob ( lambda, p, SIM_DAYS ) ) << endl; cout << setprecision ( 13 ) << simulate_prob ( lambda, p, SIM_DAYS, threshold ) << endl;
} }
} }
\ No newline at end of file
...@@ -59,7 +59,7 @@ void State::add_coupon ( int c ) ...@@ -59,7 +59,7 @@ void State::add_coupon ( int c )
double simulate_prob ( double lambda, double prob_day, int sim_times ) double simulate_prob ( double lambda, double prob_day, int sim_times, int supplement_threshold )
{ {
mt19937 rand_gen ( static_cast < unsigned > ( chrono::system_clock::now ().time_since_epoch ().count () ) ); mt19937 rand_gen ( static_cast < unsigned > ( chrono::system_clock::now ().time_since_epoch ().count () ) );
poisson_distribution < int > poisson ( lambda ); poisson_distribution < int > poisson ( lambda );
...@@ -70,10 +70,16 @@ double simulate_prob ( double lambda, double prob_day, int sim_times ) ...@@ -70,10 +70,16 @@ double simulate_prob ( double lambda, double prob_day, int sim_times )
int day = geometric ( rand_gen ) + 1; int day = geometric ( rand_gen ) + 1;
current_state.pass_day ( day ); current_state.pass_day ( day );
int current_cost = poisson ( rand_gen ); int current_cost = poisson ( rand_gen );
total_cost += current_cost;
int current_coupon = current_state.use_coupon ( current_cost, static_cast < int > ( floor ( current_cost * COUPON_RATIO + 1e-9 ) ) / COUPON_VALUE ); int current_coupon = current_state.use_coupon ( current_cost, static_cast < int > ( floor ( current_cost * COUPON_RATIO + 1e-9 ) ) / COUPON_VALUE );
int remain_cost = current_cost - current_coupon * COUPON_VALUE;
int need_to_ceil = COUPON_COST - remain_cost % COUPON_COST;
if ( need_to_ceil <= supplement_threshold ) {
current_cost += need_to_ceil;
remain_cost += need_to_ceil;
}
total_cost += current_cost;
used_coupon += current_coupon; used_coupon += current_coupon;
current_state.add_coupon ( ( current_cost - current_coupon * COUPON_VALUE ) / COUPON_COST ); current_state.add_coupon ( remain_cost / COUPON_COST );
} }
return ( static_cast < double > ( used_coupon * COUPON_VALUE ) / total_cost ); return ( static_cast < double > ( used_coupon * COUPON_VALUE ) / total_cost );
} }
...@@ -26,4 +26,4 @@ private: ...@@ -26,4 +26,4 @@ private:
std::vector < int > coupon_count; // i-th term indicates the number of coupons expired in the i-th day std::vector < int > coupon_count; // i-th term indicates the number of coupons expired in the i-th day
}; };
double simulate_prob ( double lambda, double prob_day, int sim_times ); double simulate_prob ( double lambda, double prob_day, int sim_times, int supplement_threshold = 0 );
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment