Generates a grid for the current timestep. See above for full description. :param action: network's prediction of the dot movement :return obs: observation of grid matrix of shape (h,w) :return reward: precision of network's prediction in Euclidean distance
(self, action)
| 174 | self.newPlot = True # One-time flag |
| 175 | |
| 176 | def step(self, action): |
| 177 | """ |
| 178 | Generates a grid for the current timestep. |
| 179 | See above for full description. |
| 180 | |
| 181 | :param action: network's prediction of the dot movement |
| 182 | :return obs: observation of grid matrix of shape (h,w) |
| 183 | :return reward: precision of network's prediction in Euclidean distance |
| 184 | :return done: indicates termination of simulation |
| 185 | :return intercept: indicates a successful intercept this step |
| 186 | """ |
| 187 | |
| 188 | # Increment timestep |
| 189 | self.ts += 1 # Increment timestep |
| 190 | |
| 191 | # If the random rate is high enough, update movement direction. |
| 192 | if random.uniform(0, 1) <= self.randr: |
| 193 | self.dotDir = random.randint( |
| 194 | self.minch, self.choices - 1 |
| 195 | ) # five possible options |
| 196 | |
| 197 | # Initialize empty grid and populate as we update dots. |
| 198 | self.obs = np.zeros((self.h, self.w)) |
| 199 | |
| 200 | # Update network dot according to the network's action. |
| 201 | self.prevRow = self.netDot.row[0] |
| 202 | self.prevCol = self.netDot.col[0] |
| 203 | if action is not None: |
| 204 | self.movePoint(self.netDot, action) |
| 205 | |
| 206 | # self.obs = self.obs/(self.ndots + self.herrs) # normalize |
| 207 | reward, intercept = self.compute_reward() |
| 208 | |
| 209 | # Teleport network dot if intercept is successful. |
| 210 | if intercept and self.teleport: |
| 211 | bh1, bh2 = self.h // 5, 4 * self.h // 5 |
| 212 | bw1, bw2 = self.w // 5, 4 * self.w // 5 |
| 213 | r, c = random.randint(bh1, bh2), random.randint(bw1, bw2) |
| 214 | self.netDot = Dot(r, c, self.decay) |
| 215 | |
| 216 | # Redo grid observation. |
| 217 | self.obs = np.zeros((self.h, self.w)) |
| 218 | self.obs[r, c] = 1 |
| 219 | |
| 220 | # Move all relevant dots in the same direction. |
| 221 | for d in self.dots: |
| 222 | self.movePoint(d) |
| 223 | |
| 224 | # Move distraction dots with individually randomized motion. |
| 225 | for h in self.herrings: |
| 226 | self.movePoint(h, random.randint(self.minch, self.choices - 1)) |
| 227 | |
| 228 | reward = torch.Tensor(np.array(reward)) |
| 229 | done = self.timesteps <= self.ts |
| 230 | |
| 231 | return self.obs, reward, done, intercept |
| 232 | |
| 233 | def reset(self): |
no test coverage detected