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.

78 lines
2.8 KiB

  1. import time, re, sys, csv, os
  2. from subprocess import call
  3. from os import listdir, system
  4. from os.path import isfile, join, getctime
  5. from dataclasses import dataclass, field
  6. ski_position_to_rgb_map = {1: "230, 138, 0",
  7. 2: "255,153,0",
  8. 3: "255, 184, 77",
  9. 4: "255, 194, 100",
  10. 5: "255, 194, 120",
  11. 6: "255, 210, 130",
  12. 7: "255, 210, 140",
  13. 8: "230, 204, 255",
  14. 9: "204, 153, 255",
  15. 10: "255, 102, 179",
  16. 11: "255, 51, 153",
  17. 12: "255, 0, 255",
  18. 13: "179, 0, 179",
  19. 14: "102, 0, 102"}
  20. def convert(tuples):
  21. return dict(tuples)
  22. @dataclass(frozen=True)
  23. class State:
  24. x: int
  25. y: int
  26. ski_position: int
  27. def default_value():
  28. return {'action' : None, 'choiceValue' : None}
  29. @dataclass(frozen=True)
  30. class StateValue:
  31. ranking: float
  32. choices: dict = field(default_factory=default_value)
  33. def exec(command):
  34. print(f"Executing {command}")
  35. system(f"echo {command} >> list_of_exec")
  36. return system(command)
  37. def fillStateRanking(file_name, match=""):
  38. state_ranking = dict()
  39. try:
  40. with open(file_name, "r") as f:
  41. file_content = f.readlines()
  42. for line in file_content:
  43. if match and skip_line.match(line): continue
  44. stateMapping = convert(re.findall(r"([a-zA-Z_]*[a-zA-Z])=(\d+)?", line))
  45. #print("stateMapping", stateMapping)
  46. choices = convert(re.findall(r"[a-zA-Z_]*(left|right|noop)[a-zA-Z_]*:(-?\d+\.?\d*)", line))
  47. #print("choices", choices)
  48. ranking_value = float(re.search(r"Value:([+-]?(\d*\.\d+)|\d+)", line)[0].replace("Value:",""))
  49. #print("ranking_value", ranking_value)
  50. state = State(int(stateMapping["x"]), int(stateMapping["y"]), int(stateMapping["ski_position"]))
  51. value = StateValue(ranking_value, choices)
  52. state_ranking[state] = value
  53. return state_ranking
  54. except EnvironmentError:
  55. print("TODO file not available. Exiting.")
  56. sys.exit(1)
  57. ranking = fillStateRanking("action_ranking")
  58. sorted_ranking = sorted(ranking.items(), key=lambda x: x[1].ranking)
  59. draw_commands = list()
  60. for state in sorted_ranking[-20:-1]:
  61. print(state)
  62. x = state[0].x
  63. y = state[0].y
  64. markerSize = 2
  65. print(state[0].ski_position)
  66. draw_commands.append(f"-fill 'rgba({ski_position_to_rgb_map[state[0].ski_position]},0.7)' -draw 'rectangle {x-markerSize},{y-markerSize} {x+markerSize},{y+markerSize} '")
  67. command = f"convert init.png {' '.join(draw_commands)} first_try.png"
  68. exec(command)