Advertisement
Gun16boy

Python u10

Jul 6th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. import random
  2.  
  3. class Player:
  4. idCounter = 0
  5. totalEncounters = 0
  6. strangerEncounters = 0
  7. def __init__(self):
  8. self.score = 0
  9. self.memory = {}
  10. Player.idCounter += 1
  11. self.name = 'Player {0}'.format(Player.idCounter)
  12. def processResult(self, otherName,myResponse,otherResponse):
  13. Player.totalEncounters += 1
  14. if otherName not in self.memory:
  15. Player.strangerEncounters += 1
  16. result = [myResponse, otherResponse]
  17. if otherName in self.memory:
  18. self.memory[otherName].append(result)
  19. else:
  20. self.memory[otherName] = [result]
  21. if myResponse == 'nice' and otherResponse == 'nice':
  22. self.score += 30
  23. elif myResponse == 'nice' and otherResponse == 'nasty':
  24. self.score -= 70
  25. elif myResponse == 'nasty' and otherResponse == 'nice':
  26. self.score += 50
  27. else:
  28. self.score += 0
  29.  
  30. class FriendlyPlayer(Player):
  31. def __init__(self):
  32. Player.__init__(self)
  33. self.name += ' (friendly)'
  34. def respondsTo(self, otherName):
  35. return 'nice'
  36.  
  37. class MeanPlayer(Player):
  38. def __init__(self):
  39. Player.__init__(self)
  40. self.name += ' (mean)'
  41. def respondsTo(self, otherName):
  42. return 'nasty'
  43.  
  44. class MirrorPlayer(Player):
  45. def __init__(self):
  46. Player.__init__(self)
  47. self.name += ' (mirror)'
  48. def respondsTo(self, otherName):
  49. if otherName in self.memory:
  50. return self.memory[otherName][-1][1]
  51. else:
  52. return 'nice'
  53.  
  54. class ProbingPlayer(Player):
  55. def __init__(self):
  56. Player.__init__(self)
  57. self.name += ' (probing)'
  58. def respondsTo(self, otherName):
  59. if otherName in self.memory:
  60. record = self.memory[otherName]
  61. if len (record) == 1:
  62. return 'nice'
  63. elif record [0][1] == 'nice' and record[1][1] == 'nice':
  64. return 'nasty'
  65. elif record [0][1] == 'nasty' and record[1][1] == 'nasty':
  66. return 'nasty'
  67. else:
  68. return 'nice'
  69. else:
  70. return 'nasty'
  71.  
  72. def encounter(player1, player2):
  73. name1, name2 = player1.name, player2.name
  74. response1 = player1.respondsTo(name2)
  75. response2 = player2.respondsTo(name1)
  76. player1.processResult(name2, response1, response2)
  77. player2.processResult(name1, response2, response1)
  78.  
  79. def makePopulation(specs):
  80. population = []
  81. for playerType, number in specs:
  82. for player in range(number):
  83. population.append(playerType())
  84. return population
  85.  
  86. def doGeneration(population, numberOfEncounters):
  87. for encounterNumber in range(numberOfEncounters):
  88. players = random.sample(population, 2)
  89. encounter(players[0], players[1])
  90.  
  91. def doGeneration(population, numberOfEncounters):
  92. possibleIndices = list (range (len (population)))
  93. for encounterNumber in range (numberOfEncounters):
  94. index1, index2 = random.sample(possibleIndices, 2)
  95. while abs (index1-index2) > 3:
  96. index1, index2 = random.sample (possibleIndices)
  97. encounter (population[index1], population[index2])
  98.  
  99. def sortPopulation(population):
  100. scoreList = [[player.score, player.name, type(player)]
  101. for player in population]
  102. scoreList.sort()
  103. return scoreList
  104.  
  105. def report(scoreList):
  106. pattern = '{0:23s}{1:6d}'
  107. for score, name, playerType in scoreList:
  108. print(pattern.format(name, score))
  109.  
  110. def makeNextGeneration(scoreList):
  111. nextGeneration = []
  112. populationSize = len(scoreList)
  113. scoreList = scoreList[int(populationSize/2):]
  114. for score, name, playerType in scoreList:
  115. for number in range(2):
  116. nextGeneration.append(playerType())
  117. return nextGeneration
  118.  
  119. allPlayers = makePopulation([[FriendlyPlayer, 5],
  120. [MeanPlayer, 5],
  121. [MirrorPlayer, 5],
  122. [ProbingPlayer, 5]
  123. ])
  124.  
  125. pattern = '*** Generation: {0} ***\n'
  126. for generationNumber in range(5):
  127. doGeneration(allPlayers, 500)
  128. sortedResults = sortPopulation(allPlayers)
  129. print(pattern.format(generationNumber+1))
  130. report(sortedResults)
  131. allPlayers = makeNextGeneration(sortedResults)
  132. print()
  133.  
  134. print (Player.strangerEncounters/Player.totalEncounters)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement