Created
February 5, 2017 14:26
-
-
Save yoneken/5274981a5ed7e5ba9340d132407cd5bd to your computer and use it in GitHub Desktop.
Answer of Probabilistic Robotics Exercise 2-2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
import random | |
import collections | |
( | |
Sunny, | |
Cloudy, | |
Rainy, | |
) = range(0, 3) | |
Weather = ["Sunny", "Cloudy", "Rainy"] | |
def genWeather(today): | |
tomorrow = 0 | |
rnd = random.random() | |
if today == Sunny: | |
if rnd < 0.8: | |
tomorrow = Sunny | |
else: | |
tomorrow = Cloudy | |
elif today == Cloudy: | |
if rnd < 0.4: | |
tomorrow = Sunny | |
elif rnd < 0.8: | |
tomorrow = Cloudy | |
else: | |
tomorrow = Rainy | |
else: # today == Rainy | |
if rnd < 0.2: | |
tomorrow = Sunny | |
elif rnd < 0.8: | |
tomorrow = Cloudy | |
else: | |
tomorrow = Rainy | |
return tomorrow | |
if __name__ == '__main__': | |
aryWeather = [] | |
today = Sunny | |
for i in range(0, 100000): | |
tomorrow = genWeather(today) | |
aryWeather.append(tomorrow) | |
today = tomorrow | |
cnt = collections.Counter(aryWeather) | |
for k, v in cnt.items(): | |
print Weather[k], v, v/100000. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment