Advertisement
kwikshot

AI

Apr 3rd, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Machine Learning!
  4. #
  5. # 1) Probability associate with a binary event (imbalanced coin flip)
  6. # 2) Ada bases her choice on previous outcomes
  7. #
  8. # Chances (depending on how random the computer is):
  9. # Heads: 0.6
  10. # Tails: 0.4
  11. #
  12. # Ratio is number_of_heads/number_of_tails and should give
  13. # the ratio between 0.6/0.4 after enough attempts
  14.  
  15. import random
  16.  
  17. class Computer: # AI class
  18.     successes = 0
  19.     heads = 0
  20.     tails = 0
  21.  
  22.     # Bases choice on how many heads or tails are observed.
  23.     # More heads, machine goes for heads, more tails, machine
  24.     # goes for tails.
  25.     def choose(self):
  26.         if self.heads > self.tails:
  27.             return True
  28.         elif self.heads < self.tails:
  29.             return False
  30.         elif self.heads == self.tails:
  31.             choice = random.randint(0, 1) # Randomly choose here
  32.             if choice == 1:
  33.                 return True
  34.             else:
  35.                 return False
  36.  
  37.     # Completely random, no weighting, used as control
  38.     def randomchoose(self):
  39.         choice = random.randint(0, 1)
  40.         if choice == 1:
  41.             return True
  42.         else:
  43.             return False
  44.  
  45.     # A combination, bases choice on number of heads/tails somewhat,
  46.     # but not completely so probably more realistic
  47.     def realchoose(self):
  48.         if self.heads > self.tails: # More chance of choosing heads
  49.             choice = random.randint(1, 10)
  50.             if choice < 8:
  51.                 return True
  52.             elif choice > 7:
  53.                 return False
  54.         elif self.heads < self.tails: # More chance of choosing tails
  55.             choice = random.randint(1, 10)
  56.             if choice < 8:
  57.                 return False
  58.             elif choice > 7:
  59.                 return True
  60.         elif self.heads == self.tails:
  61.             choice = random.randint(0, 1) # Randomly choose here
  62.             if choice == 1:
  63.                 return True
  64.             else:
  65.                 return False
  66.  
  67. class Coin:
  68.     rawflip = 0.0
  69.  
  70.     def flip(self):
  71.         rawflip = random.randint(1, 10) # From 1 to exclude 0
  72.         if rawflip < 7: #1, 2, 3, 4, 5, 6 (6/10 chance)
  73.             return True
  74.         elif rawflip > 6: #7, 8, 9, 10 (4/10 chance)
  75.             return False
  76.    
  77. def main():
  78.     loop = 0
  79.     ai = Computer()
  80.     coin = Coin()
  81.  
  82.     loop = int(raw_input("How many times do you want ada to play?\n"))
  83.    
  84.     while True:
  85.         print(str(loop) + ".\n")
  86.         loop -= 1
  87.        
  88.         choice = ai.choose()
  89.         result = coin.flip()
  90.            
  91.         if choice == True:
  92.             print("Ada chooses heads")
  93.         else:
  94.             print("Ada chooses tails")
  95.  
  96.         if result == True:
  97.             ai.heads += 1
  98.             print("Coin was heads\n")
  99.         else:
  100.             ai.tails += 1
  101.             print("Coin was tails\n")
  102.        
  103.         if choice == result:
  104.             ai.successes += 1
  105.         if loop == 0:
  106.             break
  107.  
  108.     print("\nAda had " + str(ai.successes) + " successful guesses")
  109.  
  110.     print("\nAda counted:")
  111.     print(str(ai.heads) + " heads")
  112.     print(str(ai.tails) + " tails")
  113.  
  114.     h = float(ai.heads)
  115.     t = float(ai.tails)
  116.    
  117.     print("\nAda estimates that:")
  118.     print("Ratio: " + str(h/t))
  119.     print("P(Heads) = " + str(h/(h+t)))
  120.     print("P(Tails) = " + str(t/(h+t)))
  121.        
  122. if __name__ == "__main__":
  123.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement