Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 14 additions & 5 deletions app/context/symbolic/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sympy import Basic, Equality, im, I
from sympy import re as real_part
from sympy.core.relational import Relational
from sympy.logic.boolalg import BooleanAtom
from sympy.logic.boolalg import BooleanAtom, BooleanFunction

from lambdafeedback.criteria import where_substitutions
from lambdafeedback.expression_parsing import SympyParsingConfig, parse_expression
Expand Down Expand Up @@ -89,11 +89,17 @@ def do_comparison(comparison_symbol: str, expression: Basic) -> bool | None:
return result


def is_inequality_or_boolean(expr: object) -> bool:
# Inequalities (e.g. 8 < 9) and booleans (e.g. 5 < 1.43 evaluated to False) are not supported
# and cannot be subtracted, so they cannot be compared like other expressions.
def cannot_be_subtracted(expr: object) -> bool:
# Relations (e.g. 8 < 9), boolean values (e.g. 5 < 1.43 evaluated to False) and boolean functions
# (e.g. `x^2` with strict syntax, where `^` is XOR) raise TypeError when subtracted.
# NOTE: Not checking for Boolean, since Symbol is a subclass of Boolean.
return isinstance(expr, (Relational, BooleanAtom)) and not isinstance(expr, Equality)
return isinstance(expr, (Relational, BooleanAtom, BooleanFunction))


def is_inequality_or_boolean(expr: object) -> bool:
# Inequalities and booleans are not supported, so they cannot be compared like other expressions.
# Equalities are handled separately by check_equality.
return cannot_be_subtracted(expr) and not isinstance(expr, Equality)


def check_equality(
Expand Down Expand Up @@ -141,6 +147,9 @@ def check_equality(

def check_order(criterion: ExprNode, parameters: EvaluationParameters) -> bool | None:
lhs_expr, rhs_expr = create_expressions_for_comparison(criterion, parameters)
if cannot_be_subtracted(lhs_expr) or cannot_be_subtracted(rhs_expr):
# The order of a relation or a boolean can't be determined
return None
result = do_comparison(criterion.content, lhs_expr - rhs_expr)
return result

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion app/docs/criteria_graphs/symbolic_equality_mermaid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion app/docs/criteria_graphs/symbolic_other_mermaid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion app/docs/criteria_graphs/symbolic_where_mermaid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion app/docs/criteria_graphs/symbolic_written_as_mermaid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions app/tests/symbolic_evaluation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,28 @@ def test_warning_inappropriate_symbol(self):
assert "NOTATION_WARNING_EXPONENT" in result["tags"]
assert "NOTATION_WARNING_FACTORIAL" in result["tags"]

@pytest.mark.parametrize(
"response, answer, criteria",
[
# With strict syntax `^` is XOR, so `x^2` is a boolean expression, which can't be subtracted
("x^2", "x**2", None),
("x^2", "1", "response > answer"),
("x^2", "1", "answer < response"),
],
)
def test_strict_caret_in_response_is_graded_instead_of_crashing(self, response, answer, criteria):
params = {"strict_syntax": True}
if criteria is not None:
params["criteria"] = criteria
result = evaluation_function(response, answer, params, include_test_data=True)
assert result["is_correct"] is False
assert "NOTATION_WARNING_EXPONENT" in result["tags"]

def test_strict_caret_in_answer_is_a_task_configuration_error(self):
with pytest.raises(TaskConfigurationError) as info:
evaluation_function("x(x)", "x^2", {"strict_syntax": True})
assert info.value.args[1] == "RESERVED_EXPRESSION_INEQUALITY_answer"

@pytest.mark.parametrize(
"response,answer",
[
Expand Down
Loading