def get_correction_factor(trial, conf, client): rnd.seed(conf['pyseedlist'][trial]) with tables.openFile('tmp', 'w') as fileh: tbl = fileh.createVLArray( '/', 'conf', tables.ObjectAtom(), title='Configuration used to generate the stored data.') tbl.append(conf) fileh.createArray( '/', 'repeat', [trial], title='Number of repeat run.') num_steps = conf['duration_in_steps'] kernel = instantiate(*conf['kernel']) predictor = instantiate(*conf['predictor'], prefix_args=(kernel,)) if 'bounds' in conf: predictor.bounds = conf['bounds'] if 'priors' in conf: for i in range(len(conf['priors'])): predictor.priors[i] = instantiate(*conf['priors'][i]) recorder = TaskPlumeRecorder(fileh, client, predictor, 1) err_recorder = ErrorRecorder(fileh, client, predictor, 1) target_chooser = behaviors.ChainTargetChoosers([ behaviors.SurroundArea(conf['area'], conf['margin']), behaviors.AcquisitionFnTargetChooser( instantiate(*conf['acquisition_fn'], predictor=predictor), conf['area'], conf['margin'], conf['grid_resolution'])]) controller = behaviors.FollowWaypoints( target_chooser, conf['target_precision']) updater = instantiate( *conf['updater'], predictor=predictor, plume_recorder=recorder) controller.observers.append(updater) behavior = controller.velocity_controller if conf['full_record']: client = ControlsRecorder(fileh, client, num_steps) sim_controller = Controller(client, controller, behavior) sim_controller.init_new_sim(conf['seedlist'][trial]) recorder.init(conf) err_recorder.init(conf) volume = np.product(np.diff(conf['area'], axis=1)) print volume test_x = err_recorder.test_x.T return np.sqrt(len(test_x) / np.sum( 1.0 / gaussian_kde(test_x)(test_x) ** 2) / volume)
def _do_trial(self, trial): self.client.reset_seed(self.conf['seedlist'][trial]) self.client.reset() x = gen_probe_locations(self.client, self.conf) rnd.shuffle(x) y = np.asarray(self.client.get_samples(x)) train_x = x[:self.conf['train_size']] train_y = y[:self.conf['train_size']] test_x = x[self.conf['train_size']:] test_y = y[self.conf['train_size']:] if self.conf['measurement_noise_var'] > 0: train_y += np.sqrt(self.conf['measurement_noise_var']) * rnd.randn( len(train_y)) for measure in self.measures: mz = measure(ZeroPredictor(), test_x, test_y) m_group = self.fileh.getNode('/', measure.name) zero_pred = self.fileh.getNode(m_group, 'zero_pred') for k, name in enumerate(measure.return_value_names): self.fileh.getNode(zero_pred, name)[trial] = mz[k] for i, j in np.ndindex( len(self.conf['lengthscales']), len(self.conf['variances'])): lengthscale = self.conf['lengthscales'][i] variance = self.conf['variances'][j] logger.info('Trial {}, lengthscale={}, variance={}'.format( trial, lengthscale, variance)) kernel = instantiate( *self.conf['kernel'], args=(lengthscale, variance)) gp = prediction.OnlineGP(kernel, self.conf['noise_var']) try: gp.fit(train_x, train_y) for measure in self.measures: m = measure(gp, test_x, test_y) m_group = self.fileh.getNode('/', measure.name) gp_pred = self.fileh.getNode(m_group, 'gp_pred') for k, name in enumerate(measure.return_value_names): self.fileh.getNode(gp_pred, name)[i, j, trial] = m[k] except LinAlgError: for measure in self.measures: m_group = self.fileh.getNode('/', measure.name) gp_pred = self.fileh.getNode(m_group, 'gp_pred') for k, name in enumerate(measure.return_value_names): self.fileh.getNode(gp_pred, name)[i, j, trial] = np.nan logger.info('Trial {}, likelihood optimization'.format(trial)) max_likelihood_idx = np.unravel_index(np.argmax( self.fileh.root.log_likelihood.gp_pred.value.read()[:, :, trial]), (len(self.conf['lengthscales']), len(self.conf['variances']))) lengthscale = self.conf['lengthscales'][max_likelihood_idx[0]] variance = 1.0 kernel = instantiate( *self.conf['kernel'], args=(lengthscale, variance)) try: gp = prediction.LikelihoodGP( kernel, self.conf['noise_var'], self.conf['train_size']) gp.bounds = [(0, None), (1, 1)] gp.fit(train_x, train_y) self.fileh.root.likelihood_optimization.lengthscales[trial] = \ kernel.lengthscale self.fileh.root.likelihood_optimization.variances[trial] = \ kernel.variance for measure in self.measures: group = self.fileh.getNode( '/likelihood_optimization', measure.name) m = measure(gp, test_x, test_y) for i, name in enumerate(measure.return_value_names): self.fileh.getNode(group, name)[trial] = m[i] except LinAlgError: for measure in self.measures: group = self.fileh.getNode( '/likelihood_optimization', measure.name) for i, name in enumerate(measure.return_value_names): self.fileh.getNode(group, name)[trial] = np.nan
def do_simulation_run(trial, output_filename, conf, client): rnd.seed(conf['pyseedlist'][trial]) with tables.openFile(output_filename, 'w') as fileh: tbl = fileh.createVLArray( '/', 'conf', tables.ObjectAtom(), title='Configuration used to generate the stored data.') tbl.append(conf) fileh.createArray( '/', 'repeat', [trial], title='Number of repeat run.') num_steps = conf['duration_in_steps'] kernel = instantiate(*conf['kernel']) predictor = instantiate(*conf['predictor'], prefix_args=(kernel,)) if 'bounds' in conf: predictor.bounds = conf['bounds'] if 'priors' in conf: for i in range(len(conf['priors'])): predictor.priors[i] = instantiate(*conf['priors'][i]) recorder = TaskPlumeRecorder(fileh, client, predictor, num_steps) err_recorder = ErrorRecorder(fileh, client, predictor, num_steps) updater = instantiate( *conf['updater'], predictor=predictor, plume_recorder=recorder) acq_behavior = behaviors.AcquisitionFnTargetChooser( instantiate(*conf['acquisition_fn'], predictor=predictor), conf['area'], conf['margin'], conf['grid_resolution']) if 'noise_search' in conf: if conf['noise_search'] == 'wind': tc_factory = behaviors.WindBasedPartialSurroundFactory( client, conf['area'], conf['margin']) else: tc_factory = behaviors.SurroundAreaFactory( conf['area'], conf['margin']) surrounder = behaviors.SurroundUntilFound(updater, tc_factory) surrounder.observers.append(recorder) target_chooser = behaviors.ChainTargetChoosers( [surrounder, acq_behavior]) maxv = 4 else: target_chooser = behaviors.ChainTargetChoosers([ behaviors.SurroundArea(conf['area'], conf['margin']), acq_behavior]) maxv = 6 controller = behaviors.FollowWaypoints( target_chooser, conf['target_precision'], behaviors.VelocityTowardsWaypointController( maxv, maxv, target_chooser.get_effective_area())) controller.observers.append(updater) behavior = controller.velocity_controller if conf['full_record']: client = ControlsRecorder(fileh, client, num_steps) sim_controller = Controller(client, controller, behavior) sim_controller.init_new_sim(conf['seedlist'][trial]) recorder.init(conf) err_recorder.init(conf) sim_controller.add_recorder(recorder) sim_controller.add_recorder(err_recorder) if hasattr(behavior, 'targets') and conf['full_record']: targets_recorder = TargetsRecorder( fileh, behavior, client.numUAVs, num_steps) targets_recorder.init() sim_controller.add_recorder(targets_recorder) try: sim_controller.run(num_steps) except Exception as err: err_tbl = fileh.createVLArray( '/', 'exception', tables.ObjectAtom(), title='Exception which was raised.') err_tbl.append(err) raise finally: try: if conf['full_record']: store_obj(fileh, fileh.createGroup('/', 'gp'), predictor) else: recorder.prune() except: pass