-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
97 lines (69 loc) · 2.64 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
from typing import Generator, Tuple, Iterable, Dict
import numpy as np
from typing_extensions import TypedDict
import blueprintString
from Blueprint.Blueprint import Blueprint
from Blueprint.BlueprintWrapper import BlueprintWrapper, BlueprintWrapperDict
from Blueprint.Entity import Entity
from Blueprint.Exceptions.UnknownEntityException import UnknownEntityException
from Grid import Grid
from data import iter_stored_blueprints, load_blueprints
def iter_individual_blueprints(verbose: False) -> Generator[Tuple[str, Blueprint], None, None]:
# passed = 0
for k, bpDict in iter_stored_blueprints():
# if k == "-MK87s2dm3zOKGYlwaVl":
# passed = 1
# print("passed")
# if passed == 0:
# continue
# print(k)
try:
wrapper = BlueprintWrapper(**bpDict)
except KeyError:
continue
except UnknownEntityException as e:
if verbose:
print(f"Uknown entity in {k}: {e}")
continue
except Exception as e:
raise Exception(f"Error processing blueprint with key {k}.") from e
for bp in wrapper.iter_items():
yield k, bp
def one_hot_encode_grid(grid: Grid, entity_index_map: Dict[str, int]):
(l, w) = grid.grid.shape
one_hot_grid = np.zeros((l, w, len(entity_index_map)), dtype=int)
for i in range(len(grid.grid)):
for j in range(len(grid.grid[0])):
entity: Entity = grid.grid[i][j]
if entity is None:
continue
one_hot_grid[i][j][entity_index_map[entity.name]] = 1
return one_hot_grid
def lookup(e: Entity):
if e is None or e.name not in entity_index_map:
return -1, False
return entity_index_map[e.name], True
def one_hot_encode_grid_vec(grid: Grid, entity_index_map: Dict[str, int]):
encoding_size = len(entity_index_map)
out = np.zeros((grid.grid.size, encoding_size), dtype=bool)
a, b = vf(grid.grid)
out[np.arange(a.size), np.ravel(a)] = np.ravel(b)
out = out.reshape(grid.grid.shape + (encoding_size,))
return out
def grid_entities():
iterator = iter_individual_blueprints()
for i, (k, bp) in enumerate(iterator):
g = Grid(bp)
encoding = one_hot_encode_grid_vec(g, entity_index_map)
def fill_entity_dict():
entity_index_map = {}
index = 0
for k in Entity.get_entity_size_dict().keys():
if not str(k).endswith("remnants"):
entity_index_map[str(k)] = index
index += 1
return entity_index_map
vf = np.vectorize(lookup, otypes=[int, bool])
entity_index_map = fill_entity_dict()
grid_entities()