You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
5.8 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/usr/bin/env python
  2. import json
  3. import sys
  4. import glob
  5. from datetime import datetime
  6. solutionsDir = "solutions/"
  7. class bcolors:
  8. HEADER = '\033[95m'
  9. OKBLUE = '\033[94m'
  10. OKGREEN = '\033[92m'
  11. WARNING = '\033[93m'
  12. FAIL = '\033[91m'
  13. ENDC = '\033[0m'
  14. BOLD = '\033[1m'
  15. UNDERLINE = '\033[4m'
  16. if len(sys.argv) < 2 or sys.argv[1] != "status":
  17. results = json.loads(''.join(sys.stdin.readlines()))
  18. resultsScore = {}
  19. with open('scores') as f:
  20. for line in f:
  21. tmp = line.strip().split(' ')
  22. key = tmp[0]+"."+tmp[1]
  23. if not key in resultsScore:
  24. resultsScore[key] = int(tmp[2])
  25. #else:
  26. # print "Score alredy present for " + tmp[0] + " " + tmp[1]
  27. f.close()
  28. if len(sys.argv) < 2 or sys.argv[1] != "status":
  29. strategy = "unknown" if len(sys.argv) < 2 else sys.argv[1]
  30. for i in range(0, len(results)):
  31. key = str(results[i]['problemId']) + "." + str(results[i]['seed'])
  32. if key in resultsScore:
  33. out_file = open(solutionsDir+str(results[i]['problemId'])+"."+str(results[i]['seed'])+"."+"%.110d"%resultsScore[key]+"."+strategy,"w")
  34. results[i]['tag'] = strategy
  35. out_file.write(json.dumps(results[i]))
  36. out_file.close()
  37. else:
  38. print "Missing score for " + str(results[i]['problemId']) + " " + str(results[i]['seed'])
  39. sys.exit(1)
  40. problemIdForGlob = str(results[0]['problemId']) + '.'
  41. else:
  42. problemIdForGlob = ''
  43. solutionsToSend=[]
  44. scores = {}
  45. lastProblem = ""
  46. for solution in sorted(glob.glob(solutionsDir + problemIdForGlob + '*'), reverse=True):
  47. problemHash = solution[:solution.rfind(".", 0, solution.rfind(".")) if solution.count('.') == 3 else solution.rfind(".")]
  48. if lastProblem != problemHash:
  49. lastProblem = problemHash
  50. with open(solution) as json_data:
  51. partialSolution = json.load(json_data)
  52. json_data.close()
  53. solutionsToSend.append(partialSolution)
  54. scores[lastProblem.replace(solutionsDir, '')] = int(solution.split('.')[2])
  55. if len(sys.argv) < 2 or sys.argv[1] != "status":
  56. print json.dumps(list(reversed(solutionsToSend))).replace(', ', ',').replace(': ', ':')
  57. else:
  58. import subprocess
  59. process = subprocess.Popen(['curl --user :2aaFCkjNIDHAsAIh9iQHc+Y+FGhkM5Z0RQgpO6TL6EA= -X GET https://davar.icfpcontest.org/teams/235/solutions'], shell = True, stdout=subprocess.PIPE)
  60. out, err = process.communicate()
  61. if err == None:
  62. serverResults = {}
  63. serverScores = {}
  64. results = json.loads(out)
  65. for result in results:
  66. #2015-08-08T12:26:42.894Z
  67. date_object = datetime.strptime(result['createdAt'], '%Y-%m-%dT%H:%M:%S.%fZ')
  68. # print str(date_object)
  69. key = str(result['problemId'])+"."+str(result['seed'])
  70. if not key in serverResults or serverResults[key]['createdAt'] < result['createdAt']:
  71. serverResults[key] = result
  72. if not key in serverScores or serverScores[key] < result['score']:
  73. serverScores[key] = result
  74. serverBestResults = {}
  75. for result in results:
  76. key = str(result['problemId'])+"."+str(result['seed'])
  77. if serverScores[key]['score'] != None and result['score'] > serverScores[key]['score']:
  78. if not key in serverBestResults or serverBestResults[key] < result['score']:
  79. serverBestResults[key] = result
  80. print "Submitted solution: %d"%len(serverResults)
  81. for serverBestResultKey in serverBestResults:
  82. if serverBestResultKey in serverBestResults and serverBestResultKey in serverScores:
  83. if serverBestResults[serverBestResultKey]['score'] > serverScores[serverBestResultKey]['score']:
  84. print bcolors.WARNING + "a older solution with better score exists on server: %d %d %s"%(result['problemId'],result['seed'],result['solution']) + bcolors.ENDC
  85. tags = {}
  86. for serverResult in serverResults:
  87. if not serverResults[serverResult]['tag'] in tags:
  88. tags[serverResults[serverResult]['tag']] = 1
  89. else:
  90. tags[serverResults[serverResult]['tag']] += 1
  91. for solutionToSend in solutionsToSend:
  92. key = str(solutionToSend['problemId'])+"."+str(solutionToSend['seed'])
  93. if not key in serverResults:
  94. print bcolors.FAIL + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " not present on server" + bcolors.ENDC
  95. else:
  96. if solutionToSend['solution'] != serverResults[key]['solution']:
  97. print bcolors.FAIL + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " solution differ: " + solutionToSend['solution'] + " != " + serverResults[key]['solution'] + " (server)" + bcolors.ENDC
  98. elif serverResults[key]['score'] == None:
  99. found = False
  100. for i in range(1, len(results) + 1):
  101. result = results[len(results)- i]
  102. if result['problemId'] == solutionToSend['problemId'] and result['seed'] == solutionToSend['seed'] and result['solution'] == solutionToSend['solution'] and scores[key] == result['score']:
  103. found = True
  104. print bcolors.WARNING + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " " + bcolors.OKGREEN + "score " + str(result['score']) + " ok" + bcolors.ENDC + bcolors.WARNING + ", but solution evaluation pending" + bcolors.ENDC
  105. break
  106. if not found:
  107. print bcolors.WARNING + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " solution evaluation pending" + bcolors.ENDC
  108. elif scores[key] != serverResults[key]['score']:
  109. print bcolors.FAIL + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " score differ: " + str(scores[key]) + " != " + str(serverResults[key]['score']) + " (server)" + bcolors.ENDC
  110. elif key in scores and serverScores[key]['score'] > scores[key]:
  111. print bcolors.FAIL + str(solutionToSend['problemId'])+" "+str(solutionToSend['seed']) + " better solution alredy exists on the server: " + serverScores[key]['solution'] + bcolors.ENDC
  112. elif not key in serverScores:
  113. print "cazziammolla"
  114. print "Tags statistics:"
  115. for tag in tags:
  116. print " %s: %d"%(tag, tags[tag])
  117. # print scores
  118. # print type(out)
  119. # print json.dumps(serverResults)
  120. # print len(serverResults)
  121. else:
  122. print "error retriving results from server"
  123. sys.exit(1)
  124. sys.exit(0)