Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 84 additions & 27 deletions qbaf_robustness/explanations.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,46 +114,67 @@ def is_pocket_max(qbaf_initial: QBAFramework,


def determine_max_pockets(qbaf_initial: QBAFramework,
qbaf_collection: list[QBAFramework],
topic_argument_1: str,
topic_argument_2: str,
pocket: set[str] = 0,
depth = 0)-> set[list[str]]:
qbaf_collection: list[QBAFramework],
topic_argument_1: str,
topic_argument_2: str,
current_subset: set[str]= set(),
pocket_list: list[set[str]]= [],
depth = 0)-> list[set[str]]:

"""
Returns the set of all maximal pockets with respect to qbaf_initial and qbaf_collection.
Args:
qbaf_initial (QBAFramework): The initial QBAF.
Checks whether pocket is a pocket of consistency with respect to qbaf_initial and qbaf_collection as per the subset relation.

Args:
qbaf_initial (QBAFramework): The initial QBAF.
qbaf_collection (list[QBAFramework]): The collection of QBAFs to be considered.
pocket (set[str]): The pocket to be considered, initialised to the set of all new arguments to start with.
topic_argument_1 (str): The first topic argument to be considered.
topic_argument_2 (str): The second topic argument to be considered.

Returns:
set[list[str]]: returns the set of all maximal pockets with respect to qbaf_intial and qbaf-collection.
bool: Returns True if pocket is a pocket of consistency; False otherwise.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the function signature above it says that it returns a list of strings.

"""

if (depth == 0):
pocket = list(new_arguments(qbaf_initial, qbaf_collection))
considered_subset = set(new_arguments(qbaf_initial, qbaf_collection))
else:
considered_subset = current_subset

if(is_pocket_max(qbaf_initial,
qbaf_collection,
considered_subset,
topic_argument_1,
topic_argument_2)):
return [considered_subset]

if (is_pocket_max(qbaf_initial, qbaf_collection, pocket, topic_argument_1, topic_argument_2)):
return [pocket]

if (len(pocket) == 1 or len(pocket) == 0):
return []

nxt_itr = combinations(pocket, len(pocket)-1)
max_p = list()

for subsets in nxt_itr:
max_p = max_p + determine_max_pockets(qbaf_initial,
qbaf_collection,
topic_argument_1,
topic_argument_2,
list(subsets), depth + 1)

next_itr = [set(x) for x in combinations(considered_subset,
len(considered_subset)-1)]

next_itr_modified = [x for x in next_itr if not any(x.issubset(y)
for y in pocket_list)]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment explaining what this does?
Also modified is not a good name.


return max_p


for subset in next_itr_modified:
pocket_list = pocket_list + determine_max_pockets(qbaf_initial,
qbaf_collection,
topic_argument_1,
topic_argument_2,
subset,
pocket_list,
depth + 1)

if depth != 0:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this condition? Can you add a code comment?

return pocket_list
else:
max_p = [x for x in pocket_list if (not any( (x != y and x.issubset(y)) for y in pocket_list))]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a more descriptive variable name

unique_pockets = []

for x in max_p:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a more descriptive variable name

if x not in unique_pockets:
unique_pockets.append(x)
return unique_pockets



Expand Down Expand Up @@ -227,3 +248,39 @@ def explanation_of_robustness_violation(qbaf_initial: QBAFramework,
explanations.append((pocket, x))

return explanations


def determine_linear_max_pockets(qbaf_initial: QBAFramework,
qbaf_collection: list[QBAFramework],
topic_argument_1: str,
topic_argument_2: str) -> list[list[str]]:

"""
Determines the pockets when the qbaf_collection is a linear chain.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pockets or max pockets?


Args:
qbaf_initial (QBAFramework): The initial QBAF.
qbaf_collection (list[QBAFramework]): The collection of QBAFs to be considered.
topic_argument_1 (str): The first topic argument to be considered.
topic_argument_2 (str): The second topic argument to be considered.

Returns:
set[list[str]]: returns the set of all maximal pockets with respect to qbaf_intial and qbaf-collection.
"""

collection_index = 0
new_args = list(new_arguments(qbaf_initial, qbaf_collection))

while (collection_index < len(qbaf_collection)):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this be a for loop? For loops are simpler than while loops so typically one should use for loops instead of while loops where possible.

if (qbaf_initial.are_strength_consistent(qbaf_collection[collection_index],
topic_argument_1,
topic_argument_2) == False):
break

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this loop do? It looks like it increments the index and executes the check for the if condition but nothing else. So I think it's best to wrap this into its own function and use a return instead of a break (and a for loop, that uses enumerate(list) such that you can use the index). This would also mean that you have to document that new function, which makes it easier to understand why you are doing this :)

else:
collection_index = collection_index+1


args = set(new_arguments(qbaf_initial,[qbaf_collection[collection_index]]))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment above this line describing what the code in the lines below does.

p_set = [set(x) for x in combinations(new_args, len(new_args)-1)]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename this to something more descriptive?

pocket_list = [list(x) for x in p_set if (args.issubset(x) == False)]
return pocket_list
28 changes: 26 additions & 2 deletions tests/test_robustness.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

Q_1 = QBAFramework(['a', 'b'], [0.5, 0.5], [], [], semantics="QuadraticEnergy_model")

Q_3 = QBAFramework(['a', 'b', 'c', 'd', 'e'], [0.5, 0.5, 0.5, 0.5, 0.0], [('c', 'a')], [('d', 'b')], semantics = "QuadraticEnergy_model")

value = pockets_of_consistency(qbaf_initial,
qbaf_collection = [qbaf_u_4, qbaf_u_5],
topic_argument_1 = 'a',
Expand Down Expand Up @@ -255,7 +257,29 @@ def test_max_pockets():
for x in generated_value_4: assert x in expected_value_4
for x in expected_value_4: assert x in generated_value_4

assert generated_value_2 == [[]]
assert generated_value_2 == [set()]



def test_max_linear_pockets():

g_value_1 = determine_linear_max_pockets(qbaf_initial= Q_1,
qbaf_collection = [Q_2],
topic_argument_1 = 'a',
topic_argument_2 = 'b')

g_value_2 = determine_linear_max_pockets(qbaf_initial= Q_1,
qbaf_collection = [Q_2, Q_3],
topic_argument_1 = 'a',
topic_argument_2 = 'b')

generated_value_1 = [set(x) for x in g_value_1]
generated_value_2 = [set(x) for x in g_value_2]

expected_value_1 = [{'c'}, {'d'}]
expected_value_2 = [{'c', 'e'}, {'d', 'e'}]

for x in generated_value_1: assert x in expected_value_1
for x in expected_value_1: assert x in generated_value_1

for x in generated_value_2: assert x in expected_value_2
for x in expected_value_2: assert x in generated_value_2
Loading