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.

99 lines
3.7 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", 2: "255,153,0", 3: "255, 184, 77", 4: "255, 194, 100", 5: "255, 194, 120", 6: "255, 210, 130", 7: "255, 210, 140", 8: "230, 204, 255", 9: "204, 153, 255", 10: "255, 102, 179", 11: "255, 51, 153", 12: "255, 0, 255", 13: "179, 0, 179", 14: "102, 0, 102"}
  7. def importance_color(ranking_value):
  8. low = 0x00ff00
  9. high = 0xff0000
  10. result = int(ranking_value * high + (1-ranking_value) * low)
  11. return f"#{result:06x}"
  12. def model_to_actual(ski_position):
  13. if ski_position == 1:
  14. return 1
  15. elif ski_position in [2,3]:
  16. return 2
  17. elif ski_position in [4,5]:
  18. return 3
  19. elif ski_position in [6,7]:
  20. return 4
  21. elif ski_position in [8,9]:
  22. return 5
  23. elif ski_position in [10,11]:
  24. return 6
  25. elif ski_position in [12,13]:
  26. return 7
  27. elif ski_position == 14:
  28. return 8
  29. def convert(tuples):
  30. return dict(tuples)
  31. @dataclass(frozen=True)
  32. class State:
  33. x: int
  34. y: int
  35. ski_position: int
  36. def default_value():
  37. return {'action' : None, 'choiceValue' : None}
  38. @dataclass(frozen=True)
  39. class StateValue:
  40. ranking: float
  41. choices: dict = field(default_factory=default_value)
  42. def exec(command, verbose=True):
  43. if verbose: print(f"Executing {command}")
  44. system(f"echo {command} >> list_of_exec")
  45. return system(command)
  46. def fillStateRanking(file_name, match=""):
  47. state_ranking = dict()
  48. try:
  49. with open(file_name, "r") as f:
  50. file_content = f.readlines()
  51. for line in file_content:
  52. if match and skip_line.match(line): continue
  53. stateMapping = convert(re.findall(r"([a-zA-Z_]*[a-zA-Z])=(\d+)?", line))
  54. #print("stateMapping", stateMapping)
  55. choices = convert(re.findall(r"[a-zA-Z_]*(left|right|noop)[a-zA-Z_]*:(-?\d+\.?\d*)", line))
  56. #print("choices", choices)
  57. ranking_value = float(re.search(r"Value:([+-]?(\d*\.\d+)|\d+)", line)[0].replace("Value:",""))
  58. #print("ranking_value", ranking_value)
  59. state = State(int(stateMapping["x"]), int(stateMapping["y"]), int(stateMapping["ski_position"]))
  60. value = StateValue(ranking_value, choices)
  61. state_ranking[state] = value
  62. return state_ranking
  63. except EnvironmentError:
  64. print("TODO file not available. Exiting.")
  65. sys.exit(1)
  66. ranking = fillStateRanking("action_ranking_simplified")
  67. sorted_ranking = sorted(ranking.items(), key=lambda x: x[1].ranking)
  68. draw_commands = {1: list(), 2:list(), 3:list(), 4:list(), 5:list(), 6:list(), 7:list(), 8:list()}
  69. exec(f"cp images/empty_scaled_down.png full_first_try_safety.png")
  70. markerSize = 1
  71. for state in sorted_ranking[-500000:-1]:
  72. if state[1].ranking < 0.09: continue
  73. x = state[0].x
  74. y = state[0].y
  75. ski_position = state[0].ski_position
  76. draw_commands[ski_position].append(f"-fill '{importance_color(state[1].ranking)}' -draw 'rectangle {x-markerSize},{y-markerSize} {x+markerSize},{y+markerSize}'") # {state[1].ranking}
  77. for pos, marker in draw_commands.items():
  78. destination = f"first_try_{pos:02}.png"
  79. exec(f"cp images/empty_scaled_down.png {destination}")
  80. i=0
  81. while i < len(marker):
  82. batch = marker[i:i+1000]
  83. command = f"convert {destination} {' '.join(batch)} {destination}"
  84. exec(command, verbose=False)
  85. command = f"convert full_first_try_safety.png {' '.join(batch)} full_first_try_safety.png"
  86. exec(command, verbose=False)
  87. i = i+1000
  88. exec(f"montage first_try*.png -geometry +0+0 -tile x1 tiled_first_try.png")