def run_simulation(config_filename, graphfilename, n): model = BaseModel(config_filename) tcm = TrueCausalModel(model) tcm.model.save_pgm_as_img(graphfilename) intervention_vars = model.get_intervention_variables() target_value = 1 target = { "variable": model.get_target_variable(), "value": target_value } local_data = dict() for i in range(n): idx_intervention_var = np.random.randint(len(intervention_vars)) action = (intervention_vars[idx_intervention_var], np.random.randint(2)) nature_response = tcm.action_simulator([action[0]], [action[1]]) for k in nature_response: if not k in local_data: local_data[k] = [] local_data[k].append(nature_response[k]) df = pd.DataFrame.from_dict(local_data) model_from_data = generate_approx_model_from_graph(ebunch=model.get_ebunch(), nodes=model.nodes, df=df) for cpd in model_from_data.get_cpds(): logging.info(cpd)
def basic_model_learning(base_path="results/disease-treatment-best-action", experiments=10, rounds=50, plot_id=""): gt_ebunch = [("Reaction", "Lives"), ("Treatment", "Reaction"), ("Treatment", "Lives"), ("Disease", "Lives")] DG = nx.DiGraph([("Reaction", "Lives"), ("Treatment", "Reaction"), ("Treatment", "Lives"), ("Disease", "Lives")]) # causal_order = list(nx.topological_sort(DG)) causal_order = [] # invalid_edges = [("Disease", "Treatment")] invalid_edges = [] COMPLETE_MODEL = BaseModel('configs/model_parameters.json') MODEL_REACTION = BaseModel('configs/model_parameters_reaction.json') MODEL_LIVES = BaseModel('configs/model_parameters_lives.json') nature = TrueCausalModel(COMPLETE_MODEL) nature_reaction = TrueCausalModel(MODEL_REACTION) nature_lives = TrueCausalModel(MODEL_LIVES) nature_arr = [nature, nature_reaction, nature_lives] variables = sorted(["Treatment", "Reaction", "Disease", "Lives"]) intervention_vars = COMPLETE_MODEL.get_intervention_variables() target_value = 1 target = { "variable": COMPLETE_MODEL.get_target_variable(), "value" : target_value } g_truth = {e: 1 for e in DG.edges} n_exploration_steps = 1 global_results = dict() for i in range(experiments): start_time = time.time() base_experiment_filename = f"disease-treatment-run-{i}-rounds-{rounds}" results_data = dict() local_exp_results = dict() data = explore_and_generate_data(nature, intervention_vars, n_steps=n_exploration_steps) df = pd.DataFrame.from_dict(data) connection_tables = create_pij(variables, causal_order, invalid_edges, use_causal_order=False) adj_list = create_graph_from_beliefs_unknown_order(variables, connection_tables) ebunch, nodes = adj_list_to_ebunch_and_nodes(adj_list) approx_model = generate_approx_model_from_graph(ebunch, nodes, df) unknown_model = BaseModel('configs/incomplete_params.json') unknown_model.reset(approx_model, ebunch, nodes) unknown_model.show_graph() connection_probas, rewards = training( variables, rounds, connection_tables, data, unknown_model, nature_arr, target) for key in connection_probas: if key not in global_results: global_results[key] = [] global_results[key].append(connection_probas[key]) local_exp_results[key] = connection_probas[key] results_data[f"gt_{i}"] = g_truth results_data[f"beliefs_{i}"] = local_exp_results results_data[f"training_time_{i}"] = time.time() - start_time results_data[f"rewards_{i}"] = rewards dict_filename = os.path.join( base_path, "mats", base_experiment_filename + ".pickle") with open(dict_filename, "wb") as handle: pickle.dump(results_data, handle, protocol=pickle.HIGHEST_PROTOCOL) labels = [] mean_vectors = [] std_dev_vectors = [] labels_correct_ones = [] mean_vectors_correct_ones = [] std_dev_vectors_correct_ones = [] labels_wrong_ones = [] mean_vectors_wrong_ones = [] std_dev_vectors_wrong_ones = [] for key in global_results: labels += [key] mean_vectors.append(np.mean(global_results[key], axis=0)) std_dev_vectors.append(np.std(global_results[key], axis=0)) if key in gt_ebunch: labels_correct_ones.append(key) mean_vectors_correct_ones.append(mean_vectors[-1]) std_dev_vectors_correct_ones.append(std_dev_vectors[-1]) else: labels_wrong_ones.append(key) mean_vectors_wrong_ones.append(mean_vectors[-1]) std_dev_vectors_wrong_ones.append(std_dev_vectors[-1]) print("{} {} {}".format(key, mean_vectors[-1][-1], std_dev_vectors[-1][-1])) x_axis = np.arange(len(mean_vectors[0])) plot_measures(x_axis, mean_vectors, std_dev_vectors, labels, f"{base_path}/{plot_id}_connection_beliefs_exp_{experiments}_rounds_{rounds}_{intervention_vars}", outside_legend=True) plot_measures(x_axis, mean_vectors_correct_ones, std_dev_vectors_correct_ones, labels_correct_ones, f"{base_path}/{plot_id}_connection_beliefs_correct_exp_{experiments}_rounds_{rounds}_{intervention_vars}", outside_legend=True) plot_measures(x_axis, mean_vectors_wrong_ones, std_dev_vectors_wrong_ones, labels_wrong_ones, f"{base_path}/{plot_id}_connection_beliefs_wrong_exp_{experiments}_rounds_{rounds}_{intervention_vars}", outside_legend=True)