smejak commited on
Commit
a1ca19c
·
1 Parent(s): b3239b5

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +178 -0
app.py ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ from random import normalvariate, random
4
+ import plotly.express as px
5
+
6
+ from cadCAD.configuration.utils import config_sim
7
+ from cadCAD.configuration import Experiment
8
+ from cadCAD.engine import ExecutionContext, Executor
9
+ from cadCAD import configs
10
+ import streamlit as st
11
+
12
+
13
+ # Additional dependencies
14
+
15
+ # For analytics
16
+ import numpy as np
17
+ # For visualization
18
+ import plotly.express as px
19
+ pd.options.plotting.backend = "plotly"
20
+
21
+ st.header('CeSci Value Flow Model')
22
+
23
+ def p_value_flow(params, substep, state_history, previous_state):
24
+ funding = 0
25
+ management_costs = 0
26
+ to_researcher = 0
27
+ to_journal = 0
28
+ salary = 0
29
+ losses = 0
30
+ if random() < params['probability_funding'] and (previous_state['funding_pool'] > funding):
31
+ funding = params['funding_round']
32
+ management_costs = funding * params['alpha']
33
+ to_researcher = funding - management_costs
34
+ losses = management_costs
35
+
36
+ research_value = funding * (1-params['epsilon'])
37
+ losses += to_researcher - research_value
38
+
39
+ salary = research_value * params['beta']
40
+ to_journal = research_value + params['cost_publishing']
41
+ if random() < params['probability_buying']:
42
+ salary = salary - params['cost_buying']
43
+ to_journal += params['cost_buying']
44
+ # losses = funding - to_journal
45
+ return {'update_researcher_funding': to_researcher,
46
+ 'update_funding_pool': -funding,
47
+ 'update_journal': to_journal,
48
+ 'update_researcher_done': salary,
49
+ 'update_losses': losses}
50
+
51
+ def s_researcher_value(params, substep, state_history, previous_state, policy_input):
52
+ research_funding = policy_input['update_researcher_funding']
53
+ research_salary = policy_input['update_researcher_done']
54
+ research_value = previous_state['researcher_value']
55
+
56
+ if research_salary == 0:
57
+ updated_researcher_value = research_funding + research_value
58
+ return 'researcher_value', updated_researcher_value
59
+ else:
60
+ updated_researcher_value = research_salary + research_value
61
+ return 'researcher_value', updated_researcher_value
62
+
63
+ def s_journal_value(params, substep, state_history, previous_state, policy_input):
64
+ to_journal = policy_input['update_journal']
65
+ journal_value = previous_state['journal_value']
66
+
67
+ updated_journal_value = to_journal + journal_value
68
+
69
+ return 'journal_value', updated_journal_value
70
+
71
+ def s_funding_pool(params, substep, state_history, previous_state, policy_input):
72
+ funding_pool = previous_state['funding_pool']
73
+ updated_funding_pool = funding_pool + policy_input['update_funding_pool']
74
+ if updated_funding_pool < 0:
75
+ updated_funding_pool = 0
76
+ return 'funding_pool', updated_funding_pool
77
+
78
+ def s_losses(params, substep, state_history, previous_state, policy_input):
79
+ losses = previous_state['losses']
80
+ updated_losses = losses + policy_input['update_losses']
81
+ return 'losses', updated_losses
82
+
83
+ st.subheader('Initial Value Allocation')
84
+ funding_pool = st.slider('Initial Funding Pool', min_value=1000, max_value=10000, value=1000, step=10)
85
+ researcher_value = st.slider('Initial Researcher Tokens', min_value=0, max_value=1000, value=0, step=1)
86
+ journal_value = st.slider('Initial Journal Tokens', min_value=0, max_value=1000, value=0, step=1)
87
+ st.subheader('Simulation Parameters')
88
+ st.write('Set the funding disbursed each round from the funding pool')
89
+ funding_round = st.slider('Funding Round', min_value=100, max_value=1000, value=100, step=1)
90
+ st.write('Set the relative value leakages in the model.')
91
+ alpha = st.slider('Management Cost Weight', min_value=0., max_value=1., value=0.1, step=0.0001)
92
+ epsilon = st.slider('Work Inefficiency Weight', min_value=0., max_value=1., value=0.1, step=0.0001)
93
+ st.write('Set the portion of grant funding to be used as researcher salary.')
94
+ beta = st.slider('Salary Weight', min_value=0., max_value=1., value=0.4, step=0.0001)
95
+ st.write('Set the cost of publishing to a journal and the cost of getting access to papers.')
96
+ cost_publishing = st.slider('Cost of Publishing', min_value=10., max_value=100., value=10., step=0.1)
97
+ cost_buying = st.slider('Cost of Buying', min_value=10., max_value=100., value=10., step=0.1)
98
+ st.write('Set the probability a researcher will buy access to a paper at each timestep.')
99
+ probability_buying = st.slider('Researcher Probability of Buying', min_value=0., max_value=1., value=0.1, step=0.0001)
100
+ st.write('Set the probability the grant funding agency will disburse funding each round.')
101
+ probability_funding = st.slider('Probability of Disbursing Funding', min_value=0., max_value=1., value=0.9, step=0.0001)
102
+ st.write('Set the number of timesteps in the simulation.')
103
+ timesteps = st.slider('Timesteps', min_value=10, max_value=1000, value=100, step=1)
104
+
105
+ initial_state = {
106
+ 'funding_pool': funding_pool,
107
+ 'researcher_value': researcher_value,
108
+ 'journal_value': journal_value,
109
+ 'losses': 0
110
+ }
111
+
112
+ system_params = {
113
+ 'funding_pool': [funding_pool],
114
+ 'funding_round': [funding_round],
115
+ 'alpha': [alpha],
116
+ 'beta': [beta],
117
+ 'epsilon': [epsilon],
118
+ 'cost_publishing': [cost_publishing],
119
+ 'cost_buying': [cost_buying],
120
+ 'probability_buying': [probability_buying],
121
+ 'probability_funding': [probability_funding]
122
+ }
123
+
124
+ def generate_sim_config(monte_carlo_runs=1,
125
+ timesteps=timesteps,
126
+ system_params=system_params):
127
+ sim_config = config_sim({
128
+ 'N': monte_carlo_runs, # the number of times we'll run the simulation ("Monte Carlo runs")
129
+ 'T': range(timesteps), # the number of timesteps the simulation will run for
130
+ 'M': system_params # the parameters of the system
131
+ })
132
+
133
+ return sim_config
134
+
135
+ def configure_experiment(initial_state,
136
+ partial_state_update_blocks,
137
+ sim_config):
138
+ experiment = Experiment()
139
+ experiment.append_configs(
140
+ initial_state=initial_state,
141
+ partial_state_update_blocks=partial_state_update_blocks,
142
+ sim_configs=sim_config
143
+ )
144
+
145
+ return experiment
146
+
147
+ partial_state_update_blocks = [
148
+ {
149
+ 'policies': {
150
+ 'p_value_flow': p_value_flow
151
+ },
152
+ 'variables': {
153
+ 'funding_pool': s_funding_pool,
154
+ 'researcher_value': s_researcher_value,
155
+ 'journal_value': s_journal_value,
156
+ 'losses': s_losses
157
+ }
158
+ }
159
+ ]
160
+
161
+ def execute_simulation(experiment):
162
+ exec_context = ExecutionContext()
163
+ configs = experiment.configs
164
+ simulation = Executor(exec_context=exec_context, configs=configs)
165
+ raw_result, tensor_field, sessions = simulation.execute()
166
+
167
+ return raw_result
168
+
169
+ if st.button('Run Simulation'):
170
+ sim_config = generate_sim_config()
171
+ experiment = configure_experiment(initial_state, partial_state_update_blocks, sim_config)
172
+ raw_result = execute_simulation(experiment)
173
+ df = pd.DataFrame(raw_result)
174
+ fig1 = df.plot(kind='line', x='timestep', y=['funding_pool','researcher_value', 'journal_value'], width=1000)
175
+ fig2 = df.plot(kind='line', x='timestep', y=['funding_pool', 'losses'], width=1000)
176
+ st.subheader('Results')
177
+ st.plotly_chart(fig1)
178
+ st.plotly_chart(fig2)