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

import time, re, sys, csv, os
from subprocess import call
from os import listdir, system
from os.path import isfile, join, getctime
from dataclasses import dataclass, field
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"}
def convert(tuples):
return dict(tuples)
@dataclass(frozen=True)
class State:
x: int
y: int
ski_position: int
def default_value():
return {'action' : None, 'choiceValue' : None}
@dataclass(frozen=True)
class StateValue:
ranking: float
choices: dict = field(default_factory=default_value)
def exec(command):
print(f"Executing {command}")
system(f"echo {command} >> list_of_exec")
return system(command)
def fillStateRanking(file_name, match=""):
state_ranking = dict()
try:
with open(file_name, "r") as f:
file_content = f.readlines()
for line in file_content:
if match and skip_line.match(line): continue
stateMapping = convert(re.findall(r"([a-zA-Z_]*[a-zA-Z])=(\d+)?", line))
#print("stateMapping", stateMapping)
choices = convert(re.findall(r"[a-zA-Z_]*(left|right|noop)[a-zA-Z_]*:(-?\d+\.?\d*)", line))
#print("choices", choices)
ranking_value = float(re.search(r"Value:([+-]?(\d*\.\d+)|\d+)", line)[0].replace("Value:",""))
#print("ranking_value", ranking_value)
state = State(int(stateMapping["x"]), int(stateMapping["y"]), int(stateMapping["ski_position"]))
value = StateValue(ranking_value, choices)
state_ranking[state] = value
return state_ranking
except EnvironmentError:
print("TODO file not available. Exiting.")
sys.exit(1)
ranking = fillStateRanking("action_ranking")
sorted_ranking = sorted(ranking.items(), key=lambda x: x[1].ranking)
draw_commands = list()
for state in sorted_ranking[-20:-1]:
print(state)
x = state[0].x
y = state[0].y
markerSize = 2
print(state[0].ski_position)
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} '")
command = f"convert init.png {' '.join(draw_commands)} first_try.png"
exec(command)