Added parsing data into a file
This commit is contained in:
parent
c969355d70
commit
913dba936f
@ -3,7 +3,8 @@ from DataModels.Road import Road
|
||||
from DataModels.House import House
|
||||
from DataModels.Dump import Dump
|
||||
from config import GRID_WIDTH, GRID_HEIGHT, DELAY, CLOSE_ON_END
|
||||
from utilities import movement, check_moves, save_moveset,parse_list
|
||||
from utilities import movement, check_moves, save_moveset
|
||||
from vowpal_utils import parse_list
|
||||
from Traversal.DFS import DFS
|
||||
from Traversal.BestFS import BestFS
|
||||
from Traversal.BFS import BFS
|
||||
@ -72,7 +73,6 @@ class GC(Cell):
|
||||
self.moves.extend(parse_list(whole_result))
|
||||
save_moveset(self.moves)
|
||||
|
||||
|
||||
def find_houses_BestFS(self, environment):
|
||||
self.algorithm_run = True
|
||||
|
||||
@ -111,7 +111,6 @@ class GC(Cell):
|
||||
self.moves.reverse()
|
||||
save_moveset(self.moves)
|
||||
|
||||
|
||||
def make_actions_from_list(self,environment):
|
||||
now = pygame.time.get_ticks()
|
||||
if len(self.moves)==0 or now - self.old_time <= DELAY:
|
||||
|
18
utilities.py
18
utilities.py
@ -49,20 +49,4 @@ def save_moveset(moveset):
|
||||
results["moveset"].append(moveset)
|
||||
f.seek(0)
|
||||
json.dump(results, f, indent=1)
|
||||
f.close()
|
||||
|
||||
def parse_list(whole_result,current_x,current_y):
|
||||
moves = []
|
||||
print("x,y",current_x,current_y,"list",whole_result)
|
||||
parser = {'[0,1]':"down",'[0,-1]':"up",'[1,0]':"right",'[-1,0]':"left"}
|
||||
for x in range(len(whole_result)):
|
||||
if whole_result[x]=="pick_garbage":
|
||||
moves.append(whole_result[x])
|
||||
else:
|
||||
x_subtraction = whole_result[x][0] - current_x
|
||||
y_subtraction = whole_result[x][1] - current_y
|
||||
current_x = whole_result[x][0]
|
||||
current_y = whole_result[x][1]
|
||||
moves.append(parser[f"[{x_subtraction},{y_subtraction}]"])
|
||||
print(moves)
|
||||
return moves
|
||||
f.close()
|
88
vowpal_utils.py
Normal file
88
vowpal_utils.py
Normal file
@ -0,0 +1,88 @@
|
||||
import re
|
||||
from config import MAP_NAME, GRID_WIDTH, GRID_HEIGHT, GC_X, GC_Y
|
||||
|
||||
COORDINATES_LIST = []
|
||||
MOVES_LIST = []
|
||||
|
||||
with open( MAP_NAME, 'r' ) as map:
|
||||
MAP_CONTENT = map.readlines()[2:]
|
||||
MAP_CONTENT = [list(row.strip().replace(" ","")) for row in MAP_CONTENT]
|
||||
|
||||
moves_mapping = {
|
||||
"pick_garbage": 1,
|
||||
"right": 2,
|
||||
"left": 3,
|
||||
"up": 4,
|
||||
"down": 5
|
||||
}
|
||||
|
||||
def parse_list(whole_result,current_x,current_y):
|
||||
global COORDINATES_LIST, MOVES_LIST
|
||||
COORDINATES_LIST = whole_result.copy()
|
||||
moves = []
|
||||
print("x,y",current_x,current_y,"list",whole_result)
|
||||
parser = {'[0,1]':"down",'[0,-1]':"up",'[1,0]':"right",'[-1,0]':"left"}
|
||||
for x in range(len(whole_result)):
|
||||
if whole_result[x]=="pick_garbage":
|
||||
moves.append(whole_result[x])
|
||||
else:
|
||||
x_subtraction = whole_result[x][0] - current_x
|
||||
y_subtraction = whole_result[x][1] - current_y
|
||||
current_x = whole_result[x][0]
|
||||
current_y = whole_result[x][1]
|
||||
moves.append(parser[f"[{x_subtraction},{y_subtraction}]"])
|
||||
print(moves)
|
||||
MOVES_LIST = moves.copy()
|
||||
generate_input(1.0)
|
||||
return moves
|
||||
|
||||
def generate_input(importance):
|
||||
i = 0
|
||||
input_file_content = []
|
||||
for position in COORDINATES_LIST:
|
||||
|
||||
coords = check_position(position, i)
|
||||
|
||||
label = moves_mapping[MOVES_LIST[i]]
|
||||
tag = re.findall("(map_[0-9]+|map[0-9]+_auto)", MAP_NAME)[0]
|
||||
gc_position = "|GC_Position "+str(coords[0])+","+str(coords[1])
|
||||
input_line = str(label) + " " + str(importance) + " " + tag + gc_position+" |GC_Area "
|
||||
|
||||
area = get_gc_area(coords,1)
|
||||
for a in area:
|
||||
input_line += a + " "
|
||||
|
||||
i += 1
|
||||
input_file_content.append(input_line)
|
||||
print(input_line)
|
||||
|
||||
filename = ".\VowPalInputData\input_" + str(tag) + ".txt"
|
||||
input_file = open(filename,"w+")
|
||||
for line in input_file_content:
|
||||
input_file.write(line+"\n")
|
||||
input_file.close()
|
||||
|
||||
|
||||
def get_gc_area(position, radius):
|
||||
print("get gc area")
|
||||
area = []
|
||||
print("position "+str(position))
|
||||
for x in range(max(0, position[0] - radius), min(position[0] + radius + 1, GRID_WIDTH)):
|
||||
for y in range(max(0, position[1] - radius), min(position[1] + radius + 1, GRID_HEIGHT)):
|
||||
if([x,y] == position):
|
||||
continue
|
||||
print(x,y)
|
||||
area.append(MAP_CONTENT[x][y]+"("+str(x)+","+str(y)+")")
|
||||
return area
|
||||
|
||||
def check_position(position, i):
|
||||
if(type(position) is list):
|
||||
print("LIST")
|
||||
return position
|
||||
elif(position == "pick_garbage"):
|
||||
for j in range(i-1,-1,-1):
|
||||
if(type(COORDINATES_LIST[j]) is list):
|
||||
return COORDINATES_LIST[j]
|
||||
return [GC_X, GC_Y]
|
||||
else:
|
||||
print("An error has ocurred while processing GC position.")
|
Loading…
Reference in New Issue
Block a user