/
debug_env.py
146 lines (115 loc) · 3.42 KB
/
debug_env.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import copy
import typing
from collections import Counter, defaultdict
from dataclasses import astuple, dataclass
from typing import Union, Dict, Generator, Tuple, List, Optional
from pprint import pprint
import numpy as np
import env
from data_types import (
ActionType,
X,
Action,
)
from data_types import (
Resource,
Building,
CoordType,
WorldObject,
Movement,
Worker,
State,
Line,
BuildOrder,
ActionStage,
Command,
Targets,
WorkerAction,
WORLD_SIZE,
Nexus,
)
@dataclass(frozen=True)
class DebugAction(Action):
def next(self) -> ActionType:
if self.reset():
return DebugAction1
return self.next_if_not_reset()
@dataclass(frozen=True)
class DebugAction1(DebugAction):
target: X
worker: X
def to_ints(self) -> Generator[int, None, None]:
yield Targets.index(self.target)
yield self.worker.value - 1
@classmethod
def num_values(cls) -> "DebugAction1":
return cls(target=len(Targets), worker=len(Worker))
def reset(self):
return isinstance(self.target, Resource)
def next_if_not_reset(self) -> ActionType:
return DebugAction2
@classmethod
def parse(cls, a) -> "Action":
parsed = super().parse(a)
assert isinstance(parsed, DebugAction1)
return cls(target=Targets[parsed.target], worker=Worker(parsed.worker + 1))
@dataclass(frozen=True)
class DebugAction2(DebugAction):
i: X
j: X
def to_ints(self) -> Generator[int, None, None]:
yield self.i
yield self.j
@classmethod
def num_values(cls) -> "DebugAction2":
return cls(i=WORLD_SIZE, j=WORLD_SIZE)
def reset(self):
return True
def next_if_not_reset(self) -> ActionType:
raise NotImplementedError
@classmethod
def parse(cls, a) -> "Action":
parsed = super().parse(a)
assert isinstance(parsed, DebugAction2)
return cls(i=parsed.i, j=parsed.j)
@dataclass(frozen=True)
class DebugCompoundAction(ActionStage):
action1: DebugAction1 = None
ptr: int = 0
active: ActionType = DebugAction1
@classmethod
def classes(cls):
yield DebugAction1
def _component_classes(self):
yield self.action1
def worker(self) -> Worker:
assert isinstance(self.action1.worker, Worker)
return self.action1.worker
def command(self) -> Command:
if isinstance(self.action1.target, Building):
assert isinstance(self.action2, DebugAction2)
return self.action1.target.assignment(None)
@dataclass
class Env(env.Env):
def building_allowed(
self,
building: Building,
dependency: Optional[Building],
building_positions: Dict[CoordType, Building],
insufficient_resources: bool,
positions: Dict[WorldObject, CoordType],
assignment_location: CoordType,
) -> bool:
built = self.get_buildings(building_positions)
# print(fg("green"), building, dependency, built, RESET)
return dependency in built + [None] and assignment_location not in [
*building_positions
]
def main(debug_env: bool, **kwargs):
Env(rank=0, eval_steps=500, **kwargs).main()
if __name__ == "__main__":
import argparse
PARSER = argparse.ArgumentParser()
Env.add_arguments(PARSER)
PARSER.add_argument("--random-seed", default=0, type=int)
main(**vars(PARSER.parse_args()))