Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
Intro2MM-Homework1
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tianqi Yang
Intro2MM-Homework1
Commits
b329535a
Commit
b329535a
authored
Oct 10, 2019
by
Tianqi Yang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(markov): add support of supplement
- Add support of supplement
parent
1d46f501
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
14 additions
and
6 deletions
+14
-6
calculate_one
calculate_one
+0
-0
calculate_one.cpp
calculate_one.cpp
+4
-2
markov_chain.cpp
markov_chain.cpp
+9
-3
markov_chain.h
markov_chain.h
+1
-1
No files found.
calculate_one
View file @
b329535a
No preview for this file type
calculate_one.cpp
View file @
b329535a
...
@@ -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
markov_chain.cpp
View file @
b329535a
...
@@ -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
);
}
}
markov_chain.h
View file @
b329535a
...
@@ -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
);
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment