def run(positions, plotEvery=1): encoder = OneDDepthEncoder(positions=positions, radius=5, wrapAround=True, nPerPosition=28, wPerPosition=3, minVal=0, maxVal=1) fetcher = Fetcher() plotter = Plotter(encoder) learner = QLearner(ACTIIONS, n=1008) lastState = None lastAction = None while True: outputData = fetcher.sync() if outputData is None: continue if fetcher.skippedTimesteps > 0: print ("Warning: skipped {0} timesteps, " "now at {1}").format(fetcher.skippedTimesteps, fetcher.timestep) if not ("reset" in outputData and "ForwardsSweepSensor" in outputData and "steer" in outputData): print ("Warning: Missing data on timestep {0}: {1}".format( fetcher.timestep, outputData)) continue if outputData["reset"]: print "Reset." sensor = outputData["ForwardsSweepSensor"] steer = outputData["steer"] reward = outputData.get("reward") or 0 encoding = encoder.encode(numpy.array(sensor)) if lastState is not None: learner.update(lastState, str(lastAction), encoding, str(steer), reward) value = learner.value(encoding) qValues = {} for action in ACTIIONS: qValues[action] = learner.qValue(encoding, action) fetcher.inputData["qValues"] = qValues fetcher.inputData["bestAction"] = learner.bestAction(encoding) plotter.update(sensor, encoding, steer, reward, value, qValues) # if fetcher.timestep % plotEvery == 0: # plotter.render() lastState = encoding lastAction = steer
def run(): params = model_params.MODEL_PARAMS model = ModelFactory.create(params) model.enableInference({"predictedField": "vector"}) anomalyLikelihoodHelper = anomaly_likelihood.AnomalyLikelihood( claLearningPeriod=100, estimationSamples=100 ) lastPosition = None lastTime = None fetcher = Fetcher() while True: data = fetcher.sync() if data is None: continue if fetcher.skippedTimesteps > 0: print "WARNING: Skipped {0} timesteps.".format(fetcher.skippedTimesteps) time = data["timestamp"] position = np.array([data["x"], data["y"], data["z"]]).astype(int) if lastPosition is None: lastPosition = position lastTime = time radius = calculateRadius(lastPosition, position, lastTime, time) modelInput = { "vector": (position, radius) } result = model.run(modelInput) anomalyScore = result.inferences["anomalyScore"] anomalyLikelihood = anomalyLikelihoodHelper.anomalyProbability( random.random(), anomalyScore ) fetcher.inputData["anomalyScore"] = anomalyScore fetcher.inputData["anomalyLikelihood"] = anomalyLikelihood print anomalyScore, anomalyLikelihood lastPosition = position lastTime = time
def run(): fetcher = Fetcher() while True: outputData = fetcher.sync() if outputData is None: continue print "Timestep: {0}".format(fetcher.timestep) print outputData if fetcher.skippedTimesteps > 0: print ("Warning: skipped {0} timesteps, " "now at {1}").format(fetcher.skippedTimesteps, fetcher.timestep) if outputData["reset"]: print "Reset." print "============"
def run(): fetcher = Fetcher() while True: outputData = fetcher.sync() if outputData is None: continue print "Timestep: {0}".format(fetcher.timestep) print outputData if fetcher.skippedTimesteps > 0: print("Warning: skipped {0} timesteps, " "now at {1}").format(fetcher.skippedTimesteps, fetcher.timestep) if outputData["reset"]: print "Reset." print "============"
def collectDepthData(outputDir=_OUTPUT_DIR): fetcher = Fetcher() holdingDownKey = False while True: outputData = fetcher.sync() if outputData is None or "collectKeyPressed" not in outputData: holdingDownKey = False continue if outputData["collectKeyPressed"] == 0: holdingDownKey = False continue if holdingDownKey: print "Stop holding down the key!" continue print "Collecting data." holdingDownKey = True sensor = outputData["ForwardsSweepSensor"] depthData = np.array(sensor) dt = time.strftime("%y%m%d%H%M%S") depthID = 'depth_'+dt+'.txt' depthOutputPath = os.path.join(os.path.dirname(__file__), outputDir, depthID) np.savetxt(depthOutputPath, depthData) print "Wrote out to ", depthOutputPath
class UnityEnvironment: def __init__(self, ): self.fetcher = Fetcher() self.actions = [[0, 0], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]] self.curr_score = 0 self.curr_step = 0 def reset(self): state, _, _ = self._get_obs() self.curr_score = 0 self.curr_step = 0 self.fetcher.inputData = {'Reset': True} self.fetcher.sync() time.sleep(0.04) state, _, _, _ = self.step(0) return state def step(self, action): hor = self.actions[action][0] vert = self.actions[action][1] self.fetcher.inputData = {'Horizontal': hor, 'Vertical': vert} self.fetcher.sync() time.sleep(0.04) self.curr_step += 1 state, reward, terminal = self._get_obs() if self.curr_step > 1000: terminal = True return state, reward, terminal, [] def numActions(self): return len(self.actions) def _get_obs(self): data = None count = 0 while data is None: count += 1 data = self.fetcher.sync() time.sleep(0.001) if count > 1000: print("Error getting data from server.") new_score = data.get('Score') if new_score is not None: reward = max(new_score - self.curr_score, 0) self.curr_score = new_score else: reward = 0 terminal = data.get('Terminal') or False objects = [] for k, v in data.items(): if 'obj' in k: class_one_hot = [1.0, 0.0] if (v['tag'] == "Player") else [0.0, 1.0] pos = v['position'] vel = v['velocity'] pos = [pos['y'], pos['x'], pos['z']] vel = [vel['y'], vel['x'], vel['z']] objects.append(np.array(class_one_hot + pos + vel)) if (len(objects) == 0): objects = np.zeros((1, 8)) return objects, reward, terminal