-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlitmc.py
195 lines (146 loc) · 7.75 KB
/
streamlitmc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import streamlit as st
from markovchain import MarkovChain
class StreamlitMC:
def add_region_list(self):
instr = 'Enter regions (comma separated)'
with st.form('chat_input_form'):
# Create two columns; adjust the ratio to your liking
col1, col2 = st.columns([3,1])
# Use the first column for text input
with col1:
prompt = st.text_input(
instr,
placeholder=instr,
label_visibility='collapsed'
)
# Use the second column for the submit button
with col2:
submitted = st.form_submit_button('ENTER')
if prompt and submitted:
# Do something with the inputted text here
st.write(f"Region List: {prompt}")
self.region_list = prompt.split(',')
def button_remove_self_flight(self):
# Initialize the "Remove Self Flight" option
self.remove_self_flight = False
# Display a power button to toggle the "Remove Self Flight" option
self.remove_self_flight = st.sidebar.toggle("Remove Self Flight[Optional]", self.remove_self_flight)
if __name__ == "__main__":
'''
# UAV Markov Chain Simulator
A UAV has been programmed to move between several region.
This simulator will generate possible routes for the UAV based on the inputted region list.
The UAV movement is determined by the Markov Chain using random transition matrix.
## Add Region List
Example: Gunja, Gapyeong, Ttukseom, Isu
'''
obj = StreamlitMC()
obj.add_region_list()
region_updated = False
if 'region_list' not in st.session_state:
st.session_state.region_list = obj.region_list
elif obj.region_list != st.session_state.region_list:
st.session_state.region_list = obj.region_list
region_updated = True
if len(obj.region_list)>1:
st.sidebar.title("Plot Basic Transition Matrix")
plot_base = False
plot_base = st.sidebar.toggle("Plot Basic", plot_base)
obj.button_remove_self_flight()
remove_updated = False
if 'remove_self_flight' not in st.session_state:
st.session_state.remove_self_flight = obj.remove_self_flight
elif obj.remove_self_flight != st.session_state.remove_self_flight:
st.session_state.remove_self_flight = obj.remove_self_flight
remove_updated = True
if plot_base:
mc = MarkovChain(st.session_state.region_list,
st.session_state.remove_self_flight)
if 'mc' not in st.session_state\
or remove_updated\
or region_updated:
st.session_state.mc = mc
st.session_state.mc.plot_base()
st.session_state.base_img = st.session_state.mc.base_img
st.session_state.base_df = st.session_state.mc.base_df
# base_img = mc.plot_base()
'''
## Basic Transition Matrix and its Graph Network
'''
base1, base2 = st.columns(2)
with base1:
st.dataframe(st.session_state.mc.base_df)
with base2:
st.image(st.session_state.mc.base_img)
st.sidebar.title('UAV Flight Simulation')
st.sidebar.text('Choose Departure and Arrival Region.')
start = st.sidebar.selectbox('Departure Region', obj.region_list)
end = st.sidebar.selectbox('Arrival Region', obj.region_list)
node_1_updated = False
if 'start' not in st.session_state:
st.session_state.start = start
elif start != st.session_state.start:
st.session_state.start = start
node_1_updated = True
node_2_updated = False
if 'end' not in st.session_state:
st.session_state.end = end
elif end != st.session_state.end:
st.session_state.end = end
node_2_updated = True
plot_blocked = False
plot_blocked = st.sidebar.toggle("Plot Route", plot_blocked)
if plot_blocked:
'''
## UAV Flight Transition Matrix and its Graph Network
'''
if 'blocked' not in st.session_state \
or remove_updated \
or region_updated \
or node_1_updated \
or node_2_updated:
st.session_state.blocked = True
st.session_state.mc.plot_blocked_node(node_1=st.session_state.start,
node_2=st.session_state.end)
st.session_state.blocked_img = st.session_state.mc.blocked_img
st.session_state.blocked_df = st.session_state.mc.blocked_df
block1, block2 = st.columns(2)
with block1:
st.dataframe(st.session_state.mc.blocked_df)
with block2:
st.image(st.session_state.mc.blocked_img)
f'''
## SIMULATION
This section will simulate the possible routes for the UAV starting
from {start.upper()} and ending in {end.upper()}. The first result will
generate the route randomly based on the transition matrix.
In the second result, it will show the probability of ending in {end.upper()}
in N-step. You can change the N value using the slider below.
'''
n_step = st.slider('### How many step?', 0, 50, 20)
simulate = st.button("SIMULATE")
if simulate:
plot_mode='blocked'
travel_simulated = st.session_state.mc.travel_simulation(start,
end,
plot_mode
)
f'''
## SIMULATION RESULTS
The UAV finished the trip in **{len(travel_simulated)-1} STEP** through
this
**ROUTE: {travel_simulated}**.
'''
travel_img = st.session_state.mc.plot_travel_simulation(plot_mode)
st.image(travel_img)
f'''
## Probability Ending in {end.upper()} in {n_step}-STEP
This plot below shows the probability of ending in {end.upper()}
in {n_step}-step.
'''
plot_prob = st.session_state.mc.plot_prob_ending(init_region=start,
final_region=end,
matrix_df=st.session_state.mc.blocked_df,
n_step=n_step)
st.image(plot_prob[0])
st.write('PROB_VALUES: ', plot_prob[1])