def simulate(self):
        """randomly select edges and outcomes to complete the search, and return the value"""
        if self.child_edges is None:
            edge_rejections = len(self.accepted_edges) * [False] + len(
                self.rejected_edges) * [True]
            self.child_edges = get_candidate_edges_fixed_rejections(
                self.accepted_edges + self.rejected_edges,
                edge_rejections,
                self.search_tree.graph,
            )

        new_edges = self.search_tree.rs.choice(
            list(self.child_edges),
            min(self.search_tree.edge_budget - self.level,
                len(self.child_edges)),
        )
        edge_rejections = [
            self.search_tree.rs.random() < e.p_reject for e in new_edges
        ]

        return evaluate_accept_reject_outcome(
            self.accepted_edges +
            [e for e, r in zip(new_edges, edge_rejections) if not r],
            self.rejected_edges +
            [e for e, r in zip(new_edges, edge_rejections) if r],
            self.search_tree.graph,
            use_kpd_data=False,
        )[0]
Esempio n. 2
0
def greedily_get_next_edge(graph, queried_edges, edge_outcomes):

    best_edge_value = -1
    best_edge = None
    succeeded_list, failed_list = succeeded_failed_edges(
        queried_edges, edge_outcomes)
    for e in graph.all_edge_list:
        if e not in queried_edges:
            e_success_value, _ = evaluate_accept_reject_outcome(
                succeeded_list + [e], failed_list, graph)
            e_fail_value, _ = evaluate_accept_reject_outcome(
                succeeded_list, failed_list + [e], graph)
            edge_value = (
                1 - e.p_reject) * e_success_value + e.p_reject * e_fail_value
            if edge_value > best_edge_value:
                best_edge = e
                best_edge_value = edge_value
    return best_edge
Esempio n. 3
0
    def greedily_get_next_edge_timelimit(graph, queried_edges, edge_rejections,
                                         tree):
        best_edge = None

        rejected_edges = []
        accepted_edges = []
        for e, rejected in zip(queried_edges, edge_rejections):
            if rejected:
                rejected_edges.append(e)
            else:
                accepted_edges.append(e)

        start_time = time.time()
        candidate_edges = get_candidate_edges_fixed_rejections(
            queried_edges, edge_rejections, graph)

        if len(candidate_edges) == 0:
            logger.info(
                f"multistage greedy ran out of edges on stage {len(queried_edges)}"
            )
            return None

        best_edge_value = -1
        for e in candidate_edges:
            if (time.time() - start_time) > time_limit:
                logger.info(
                    f"multistage greedy ran out of time on stage {len(queried_edges)}"
                )
                if best_edge is None:
                    return np.random.choice(graph.candidate_edges)
                else:
                    return best_edge

            e_success_value, _ = evaluate_accept_reject_outcome(
                accepted_edges + [e], rejected_edges, graph)
            e_fail_value, _ = evaluate_accept_reject_outcome(
                accepted_edges, rejected_edges + [e], graph)
            edge_value = (
                1 - e.p_reject) * e_success_value + e.p_reject * e_fail_value
            if edge_value > best_edge_value:
                best_edge = e
                best_edge_value = edge_value

        return best_edge
    def get_outcome_value(self):
        if self._outcome_value is None:
            self._outcome_value, _ = evaluate_accept_reject_outcome(
                self.accepted_edges,
                self.rejected_edges,
                self.search_tree.graph,
                use_kpd_data=False,
            )

        return self._outcome_value
Esempio n. 5
0
    def get_multistage_realization_results(
        outcome_number,
        edge_list,
        edge_rejections,
        graph,
        method_name,
        deterministic_sol,
        fail_aware_sol,
        other_data={},
    ):
        """take a list of queried edges from a multistage experiment (in order they were queried), and a dict of
        pre-determined edge rejections.

        for each edge in the sequence, calculate the intermediate value of querying each edge. compare to the value of

        return a list of strings to be written to the outcomes file

        """
        result_str_list = []

        for edge_budget in range(1, args.max_query_edges + 1):
            # for each subset of edges...
            accepted_edges = []
            rejected_edges = []
            num_queried_edges = min(edge_budget, len(edge_list))
            for e in edge_list[:num_queried_edges]:
                if edge_rejections[e.index]:
                    rejected_edges.append(e)
                else:
                    accepted_edges.append(e)

            total_weight, kpd_data = evaluate_accept_reject_outcome(
                accepted_edges, rejected_edges, graph, use_kpd_data=False
            )

            other_data["edge_list"] = [e.index for e in edge_list[0:num_queried_edges]]
            other_data["reject_list"] = [
                edge_rejections[e.index] for e in edge_list[0:num_queried_edges]
            ]

            # calculate the realized weight of deterministic and failure-aware
            edge_acceptance = [
                not edge_rejections[e.index] for e in edge_list[0:num_queried_edges]
            ]
            det_score = eval_sol_on_outcome(
                edge_list[0:num_queried_edges],
                edge_acceptance,
                deterministic_sol,
                graph,
            )
            fail_aware_score = eval_sol_on_outcome(
                edge_list[0:num_queried_edges], edge_acceptance, fail_aware_sol, graph
            )

            result_str = (
                DELIMITER.join(9 * ["{}"]).format(
                    graph.name,
                    num_queried_edges,  # num stages
                    edge_budget,  # edge budget
                    method_name,
                    outcome_number,
                    total_weight,
                    det_score,
                    fail_aware_score,
                    other_data,
                )
                + "\n"
            )

            result_str_list.append(result_str)

        return result_str_list