Ejemplo n.º 1
0
def get_valid_solutions(states_df: pd.DataFrame, input_graph: list) -> tuple:
    """Get the frequency of valid solution, by summing frequencies of lowest
    energy states in the degenrate case, and outputs all valid solutions
    found.

    :param states_df: dataframe containing the states and energy
    :param input_graph: graph defining the problem to be solved

    :return: a tuple made of:
        - input dataframe with a column added to check if solution is valid
        - frequency of solution
        - all valid solutions

    """
    enriched_states_df = states_df.copy()
    enriched_states_df['is_valid'] = False
    states_df.groupby(by='state')
    lowest_energy = states_df['energy'].min()
    lowest_energy_states_df = states_df[states_df['energy'] == lowest_energy]
    solutions = []
    solution_frequency = 0
    for index, row in lowest_energy_states_df.iterrows():
        edges_solution = utils.convert_list_of_strings_to_list_of_tuples(
            eval(row['state']))
        frequency_temp = row['relative_frequency']
        is_valid = graph.is_valid(edges_solution, input_graph)
        enriched_states_df.loc[enriched_states_df['state'] == row['state'],
                               'is_valid'] = is_valid
        if is_valid:
            solution_frequency += frequency_temp
            solutions.append(edges_solution)
    print(f'number of different solutions: {len(solutions)}')
    return enriched_states_df, solution_frequency, solutions
Ejemplo n.º 2
0
 def test_false_cycles_of_length_two_present(self):
     edges_input = [(0, 1), (1, 0), (3, 4), (4, 5), (5, 3)]
     edges_output = [(0, 1), (1, 0), (3, 4), (4, 5), (5, 3)]
     actual_is_legit_value = graph.is_valid(edges_output, edges_input)
     assert actual_is_legit_value is False
Ejemplo n.º 3
0
 def test_false_flow_not_conserved(self):
     edges_input = [(0, 1), (1, 2), (3, 4), (4, 5), (5, 3)]
     edges_output = [(0, 1), (1, 2), (3, 4), (4, 5), (5, 3)]
     actual_is_legit_value = graph.is_valid(edges_output, edges_input)
     assert actual_is_legit_value is False
Ejemplo n.º 4
0
 def test_false_max_one_in_not_respected(self):
     edges_input = [(0, 1), (1, 2), (2, 0), (3, 4), (4, 5), (5, 3), (3, 0)]
     edges_output = [(0, 1), (1, 2), (2, 0), (3, 4), (4, 5), (5, 3), (3, 0)]
     actual_is_legit_value = graph.is_valid(edges_output, edges_input)
     assert actual_is_legit_value is False
Ejemplo n.º 5
0
 def test_false_not_all_vertices_covered(self):
     edges_input = [(0, 1), (1, 2), (2, 0), (3, 4), (4, 5), (5, 3), (0, 3)]
     edges_output = [(0, 1), (1, 2), (2, 0)]
     actual_is_legit_value = graph.is_valid(edges_output, edges_input)
     assert actual_is_legit_value is False
Ejemplo n.º 6
0
 def test_false_no_subset(self):
     edges_input = [(0, 1), (1, 2), (2, 0)]
     edges_output = [(0, 1), (1, 2), (2, 0), (3, 4), (4, 5), (5, 3)]
     actual_is_legit_value = graph.is_valid(edges_output, edges_input)
     assert actual_is_legit_value is False
Ejemplo n.º 7
0
 def test_true(self):
     edges_input = [(0, 1), (1, 2), (2, 0), (3, 4), (4, 5), (5, 3), (0, 3)]
     edges_output = [(0, 1), (1, 2), (2, 0), (3, 4), (4, 5), (5, 3)]
     actual_is_legit_value = graph.is_valid(edges_output, edges_input)
     assert actual_is_legit_value is True