# np.random.seed(3452) mdp = env.SyntheticToyFS() state_dim, action_dim, reward_dim = get_space_info(mdp) nextstate_idx = state_dim + action_dim + reward_dim reward_idx = action_dim + state_dim # dataset: s, a, r, s' dataset = evaluation.collect_episodes(mdp, n_episodes=50) check_dataset(dataset, state_dim, action_dim, reward_dim) selector = IFS(estimator=ExtraTreesRegressor(n_estimators=50), scale=True, verbose=1) fs = RFS(feature_selector=selector, features_names=np.array(['S0', 'S1', 'S2', 'S3', 'A0', 'A1']), verbose=1) state, actions, reward, next_states, absorbing = \ split_dataset(dataset, state_dim, action_dim, reward_dim) # print(dataset[:10, :]) fs.fit(state, actions, next_states, reward) selected_features = fs.features_names[fs.get_support()] print('selected features: {}'.format(selected_features)) # this are the selected features, it should be [s0, s2, a0] assert np.all(selected_features == ['S0', 'S2', 'A0']) print(fs.nodes) g = fs.export_graphviz() g.view()
'S' + s for s in selected_states ] + ['Absorbing', 'Finished'] else: support = selected_states + selected_actions + ['R'] + ['S' + s for s in selected_states] + \ ['Absorbing', 'Finished'] # Reduce dataset reduced_dataset = dataframe[ support] # Keep only selected states and actions if args.onehot: reduced_dataset = reduced_dataset[reduced_dataset['A0'].isin( selected_actions_values)] # Remove useless actions reduced_dataset = reduced_dataset.as_matrix() # Save RFS tree tree = fs.export_graphviz(filename=logger.path + 'rfs_tree.gv') tree.save() # Save GV source tree.format = 'pdf' tree.render() # Save PDF # Save RFS dataset if args.save_rfs: np.savetxt(logger.path + 'rfs_dataset.csv', np.append([support], reduced_dataset, axis=0), fmt='%s', delimiter=',') rfs_time = time.time() - rfs_time print('Done RFS. Elapsed time: %s' % rfs_time) else: # The dataset is already reduced
reverse_onehot_actions = [np.where(dataset[i][state_dim:state_dim + action_dim] == 1)[0][0] for i in xrange(len(dataset))] dataframe['A0'] = pd.Series(reverse_onehot_actions) # A0 will be the only action support = selected_states + ['A0', 'R'] + ['S' + s for s in selected_states] + ['Absorbing', 'Finished'] else: support = selected_states + selected_actions + ['R'] + ['S' + s for s in selected_states] + \ ['Absorbing', 'Finished'] # Reduce dataset reduced_dataset = dataframe[support] # Keep only selected states and actions if args.onehot: reduced_dataset = reduced_dataset[reduced_dataset['A0'].isin(selected_actions_values)] # Remove useless actions reduced_dataset = reduced_dataset.as_matrix() # Save RFS tree tree = fs.export_graphviz(filename=logger.path + 'rfs_tree.gv') tree.save() # Save GV source tree.format = 'pdf' tree.render() # Save PDF # Save RFS dataset if args.save_rfs: np.savetxt(logger.path + 'rfs_dataset.csv', np.append([support], reduced_dataset, axis=0), fmt='%s', delimiter=',') rfs_time = time.time() - rfs_time print('Done RFS. Elapsed time: %s' % rfs_time) else: # The dataset is already reduced header = list(dataset[0]) reduced_dataset = dataset[1:] # Remove header