| |
| |
|
|
| from typing import TYPE_CHECKING, Any, Callable, cast |
|
|
| import robolab.constants |
| from robolab.core.task.predicate_logic import get_task_conditional_func |
| from robolab.core.utils.function_loader import func_as_str, get_callable_info |
|
|
| if TYPE_CHECKING: |
| from robolab.core.task.subtask import Subtask |
|
|
| def process_subtasks_as_str(subtasks: list['Subtask'] | None) -> str: |
| """ |
| Process a list of Subtask objects into a human-readable string representation. |
| |
| Args: |
| subtasks: List of Subtask objects or None |
| |
| Returns: |
| A string representation of all subtasks, formatted as: |
| "subtask_name(objects=[...], logical=...) score=X" |
| Multiple subtasks are separated by semicolons. |
| """ |
| if not subtasks: |
| return "" |
|
|
| |
| from robolab.core.task.subtask import Subtask |
|
|
| subtask_strs = [] |
| for subtask in subtasks: |
| if not isinstance(subtask, Subtask): |
| |
| subtask_strs.append(str(subtask)) |
| continue |
|
|
| |
| |
| conditions_dict = cast(dict[str, list[tuple[Callable, float]]], subtask.conditions) |
| group_names = list(conditions_dict.keys()) |
|
|
| |
| if subtask.logical == "choose": |
| subtask_str = f"{subtask.name}(groups={group_names}, logical={subtask.logical}, k={subtask.K}) score={subtask.score}" |
| else: |
| subtask_str = f"{subtask.name}(groups={group_names}, logical={subtask.logical}) score={subtask.score}" |
|
|
| subtask_strs.append(subtask_str) |
|
|
| return "; ".join(subtask_strs) |
|
|
| def count_stages_and_conditions(subtasks: list['Subtask'] | None) -> tuple[int, int]: |
| """ |
| Count the number of sequential stages and atomic conditions. |
| |
| Args: |
| subtasks: List of Subtask objects or None |
| |
| Returns: |
| Tuple of (num_sequential_stages, num_atomic_conditions): |
| - num_sequential_stages: Number of Subtask objects (sequential phases) in the list |
| - num_atomic_conditions: Total count of atomic condition checks |
| - For "all" logical: sum of all conditions across all objects |
| - For "any" logical: count from one representative object (since only one needs to complete) |
| - For "choose" logical: count from k objects (since k need to complete) |
| """ |
| if not subtasks: |
| return 0, 0 |
|
|
| |
| from robolab.core.task.subtask import Subtask |
|
|
| num_sequential_stages = len(subtasks) |
| num_atomic_conditions = 0 |
|
|
| for subtask in subtasks: |
| if not isinstance(subtask, Subtask): |
| |
| continue |
|
|
| |
| conditions_dict = cast(dict[str, list[tuple[Callable, float]]], subtask.conditions) |
|
|
| |
| if subtask.logical == "all": |
| |
| for object_subtask_list in conditions_dict.values(): |
| num_atomic_conditions += len(object_subtask_list) |
|
|
| elif subtask.logical == "any": |
| |
| if conditions_dict: |
| first_object = list(conditions_dict.keys())[0] |
| num_atomic_conditions += len(conditions_dict[first_object]) |
|
|
| elif subtask.logical == "choose": |
| |
| if conditions_dict and subtask.K is not None: |
| |
| first_object = list(conditions_dict.keys())[0] |
| conditions_per_object = len(conditions_dict[first_object]) |
| num_atomic_conditions += conditions_per_object * subtask.K |
|
|
| return num_sequential_stages, num_atomic_conditions |
|
|
|
|
| def count_subtasks(subtasks: list['Subtask'] | None) -> int: |
| """ |
| Count the total number of subtasks (manipulation actions) required to |
| complete all sequential stages, accounting for logical mode. |
| |
| For each stage, the number of subtasks is determined by the logical mode |
| and the number of object groups in its conditions dict: |
| - "all": every object group must complete -> num_object_groups |
| - "any": only one object group must complete -> 1 |
| - "choose": exactly K object groups must complete -> K |
| |
| The total is summed across all sequential stages. |
| |
| Args: |
| subtasks: List of Subtask objects or None |
| |
| Returns: |
| Total number of subtasks across all sequential stages. |
| """ |
| if not subtasks: |
| return 0 |
|
|
| from robolab.core.task.subtask import Subtask |
|
|
| total = 0 |
| for subtask in subtasks: |
| if not isinstance(subtask, Subtask): |
| continue |
|
|
| conditions_dict = cast(dict[str, list[tuple[Callable, float]]], subtask.conditions) |
| num_groups = len(conditions_dict) |
|
|
| if subtask.logical == "all": |
| total += num_groups |
| elif subtask.logical == "any": |
| total += 1 |
| elif subtask.logical == "choose": |
| total += subtask.K if subtask.K is not None else num_groups |
|
|
| return total |
|
|
|
|
| from robolab.constants import DIFFICULTY_THRESHOLDS, SKILL_WEIGHTS |
|
|
|
|
| def compute_difficulty_score( |
| num_subtasks: int, |
| attributes: list[str], |
| ) -> tuple[int, str]: |
| """ |
| Compute a difficulty score and label from the number of subtasks and task attributes. |
| |
| The score combines manipulation volume (num_subtasks) with the hardest |
| required skill (max skill weight from attributes): |
| |
| difficulty_score = num_subtasks + max(skill_weight for each attribute) |
| |
| The label is derived from thresholds defined in DIFFICULTY_THRESHOLDS: |
| - simple: score <= DIFFICULTY_THRESHOLDS[0] |
| - moderate: score <= DIFFICULTY_THRESHOLDS[1] |
| - complex: score > DIFFICULTY_THRESHOLDS[1] |
| |
| Args: |
| num_subtasks: Number of subtask actions (from count_subtasks) |
| attributes: List of task attribute strings (e.g., ['semantics', 'sorting', 'color']). |
| Difficulty-level attributes ('simple', 'moderate', 'complex') are ignored. |
| |
| Returns: |
| Tuple of (difficulty_score, difficulty_label) |
| """ |
| non_diff = [a for a in attributes if a not in ('simple', 'moderate', 'complex')] |
| skill_weight = max((SKILL_WEIGHTS.get(a, 0) for a in non_diff), default=0) |
| score = num_subtasks + skill_weight |
|
|
| simple_max, moderate_max = DIFFICULTY_THRESHOLDS |
| if score <= simple_max: |
| label = 'simple' |
| elif score <= moderate_max: |
| label = 'moderate' |
| else: |
| label = 'complex' |
|
|
| return score, label |
|
|
|
|
| def sanitize_subtask_conditions(conditions) -> dict[str, list[tuple[Callable, float]]]: |
| """ |
| Sanitize the conditions structure. |
| Valid conditions structures: |
| Single condition (atomic subtask) |
| 1. conditions = func ---- single task |
| |
| These conditions (in set or list notation) is processed as a single group, and is subject to logical ('all', 'any', 'choose') |
| |
| 2. conditions = [func1, func2] ---- list notation, assumed to have the same score. |
| 3. conditions = {func1, func2} ---- set notation, assumed to have the same score. |
| 4. conditions = [(func1, score), (func2, score)] ---- list of tuples, the scores are specified |
| 5. conditions = {(func1, score), (func2, score)} ---- set of tuples, the scores are specified |
| |
| If the conditions are provided in the form of a dictionary, then: |
| - within each group, the conditions are checked sequentially |
| - between groups, logical is applied |
| |
| 4. conditions = {group1: func1, group2: func2} |
| 5. conditions = {group1: [func1, func2], group2: [func3, func4]} ---- groups are subject to logical. funcs are evaluated sequentially, and assumed to have the same score. |
| 6. conditions = {group1: {func1, func2}, group2: {func3, func4}} ---- set notation. groups are subject to logical. funcs are evaluated sequentially, and assumed to have the same score. |
| 7. conditions = {group1: {(func1, score), (func2, score)}, group2: {(func3, score), (func4, score)}} ---- set notation. groups are subject to logical. funcs are evaluated sequentially, and the scores are specified. |
| 8. conditions = {group1: [(func1, score), (func2, score)], group2: [(func3, score), (func4, score)]} ---- list notation. groups are subject to logical. funcs are evaluated sequentially, and the scores are specified. |
| |
| This function unifies all of the different above structures into a single type, which is the 8th type. |
| |
| Returns: |
| A dictionary mapping group names to lists of (function, score) tuples. |
| """ |
| |
| if isinstance(conditions, dict): |
| if len(conditions) == 0: |
| raise ValueError("conditions dict cannot be empty") |
|
|
| sanitized_dict = {} |
| for group, obj_conditions in conditions.items(): |
|
|
| |
| if callable(obj_conditions): |
| sanitized_dict[group] = [(obj_conditions, 1.0)] |
|
|
| if robolab.constants.DEBUG: |
| print(f"[Sanitize Subtask Conditions] Converting single callable to list for group '{group}': {obj_conditions} -> {sanitized_dict[group]}") |
|
|
| |
| elif isinstance(obj_conditions, list): |
| if len(obj_conditions) == 0: |
| raise ValueError(f"conditions for group '{group}' cannot be empty") |
|
|
| first_elem = obj_conditions[0] |
|
|
| |
| if isinstance(first_elem, tuple): |
| |
| for i, condition in enumerate(obj_conditions): |
| if not isinstance(condition, tuple): |
| raise TypeError(f"condition at index {i} for group '{group}' must be a tuple, got {type(condition)}") |
| if len(condition) != 2: |
| raise ValueError(f"condition tuple at index {i} for group '{group}' must have 2 elements (func, score), got {len(condition)}") |
| if not callable(condition[0]): |
| raise TypeError(f"first element of condition tuple at index {i} for group '{group}' must be callable") |
| if not isinstance(condition[1], (int, float)): |
| raise TypeError(f"second element of condition tuple at index {i} for group '{group}' must be a number (score)") |
| sanitized_dict[group] = obj_conditions |
|
|
| elif callable(first_elem): |
| |
| for i, condition in enumerate(obj_conditions): |
| if not callable(condition): |
| raise TypeError(f"condition at index {i} for group '{group}' must be callable, got {type(condition)}") |
| equal_score = 1.0 / len(obj_conditions) |
| sanitized_dict[group] = [(func, equal_score) for func in obj_conditions] |
|
|
| if robolab.constants.DEBUG: |
| print(f"[Sanitize Subtask Conditions] Converting list of callables to tuples for group '{group}': {obj_conditions} -> {sanitized_dict[group]}") |
|
|
| else: |
| raise ValueError(f"list elements for group '{group}' must be either tuples (func, score) or callables, got {type(first_elem)}") |
|
|
| |
| elif isinstance(obj_conditions, set): |
| if len(obj_conditions) == 0: |
| raise ValueError(f"conditions for group '{group}' cannot be empty") |
|
|
| |
| obj_conditions_list = list(obj_conditions) |
| first_elem = obj_conditions_list[0] |
|
|
| |
| if isinstance(first_elem, tuple): |
| |
| for i, condition in enumerate(obj_conditions_list): |
| if not isinstance(condition, tuple): |
| raise TypeError(f"condition at index {i} for group '{group}' must be a tuple, got {type(condition)}") |
| if len(condition) != 2: |
| raise ValueError(f"condition tuple at index {i} for group '{group}' must have 2 elements (func, score), got {len(condition)}") |
| if not callable(condition[0]): |
| raise TypeError(f"first element of condition tuple at index {i} for group '{group}' must be callable") |
| if not isinstance(condition[1], (int, float)): |
| raise TypeError(f"second element of condition tuple at index {i} for group '{group}' must be a number (score)") |
| sanitized_dict[group] = obj_conditions_list |
| if robolab.constants.DEBUG: |
| print(f"[Sanitize Subtask Conditions] Converting set of tuples to list for group '{group}'") |
|
|
| elif callable(first_elem): |
| |
| for i, condition in enumerate(obj_conditions_list): |
| if not callable(condition): |
| raise TypeError(f"condition at index {i} for group '{group}' must be callable, got {type(condition)}") |
| equal_score = 1.0 / len(obj_conditions_list) |
| sanitized_dict[group] = [(func, equal_score) for func in obj_conditions_list] |
|
|
| if robolab.constants.DEBUG: |
| print(f"[Sanitize Subtask Conditions] Converting set of callables to tuples for group '{group}'") |
|
|
| else: |
| raise ValueError(f"set elements for group '{group}' must be either tuples (func, score) or callables, got {type(first_elem)}") |
|
|
| else: |
| raise TypeError(f"conditions for group '{group}' must be a callable, list, or set, got {type(obj_conditions)}") |
|
|
| return sanitized_dict |
|
|
| |
| elif isinstance(conditions, list): |
| if len(conditions) == 0: |
| raise ValueError("conditions list cannot be empty") |
|
|
| |
| first_elem = conditions[0] |
|
|
| if isinstance(first_elem, tuple): |
| sanitized_conditions = {} |
| |
| for i, condition in enumerate(conditions): |
| if not isinstance(condition, tuple): |
| raise TypeError(f"condition at index {i} must be a tuple, got {type(condition)}") |
| if len(condition) != 2: |
| raise ValueError(f"condition tuple at index {i} must have 2 elements (func, score), got {len(condition)}") |
| if not callable(condition[0]): |
| raise TypeError(f"first element of condition tuple at index {i} must be callable") |
| if not isinstance(condition[1], (int, float)): |
| raise TypeError(f"second element of condition tuple at index {i} must be a number (score)") |
| sanitized_conditions[f"group{i+1}"] = [condition] |
|
|
| if robolab.constants.DEBUG: |
| print(f"[Sanitize Subtask Conditions] Converting list of tuples to dict: {conditions} converted to {sanitized_conditions}") |
|
|
| return sanitized_conditions |
|
|
| elif callable(first_elem): |
| |
| equal_score = 1.0 / len(conditions) |
| sanitized_conditions = {} |
| |
| for i, condition in enumerate(conditions): |
| if not callable(condition): |
| raise TypeError(f"condition at index {i} must be callable, got {type(condition)}") |
| sanitized_conditions[f"group{i+1}"] = [(condition, equal_score)] |
|
|
| if robolab.constants.DEBUG: |
| print(f"[Sanitize Subtask Conditions] Converting list of callables to dict: {conditions} converted to {sanitized_conditions}") |
|
|
| return sanitized_conditions |
|
|
| else: |
| raise ValueError(f"list elements must be either tuples (func, score) or callables, got {type(first_elem)}") |
|
|
| |
| elif isinstance(conditions, set): |
| if len(conditions) == 0: |
| raise ValueError("conditions set cannot be empty") |
|
|
| |
| conditions_list = list(conditions) |
| first_elem = conditions_list[0] |
|
|
| |
| if isinstance(first_elem, tuple): |
| sanitized_conditions = {} |
| |
| for i, condition in enumerate(conditions_list): |
| if not isinstance(condition, tuple): |
| raise TypeError(f"condition at index {i} must be a tuple, got {type(condition)}") |
| if len(condition) != 2: |
| raise ValueError(f"condition tuple at index {i} must have 2 elements (func, score), got {len(condition)}") |
| if not callable(condition[0]): |
| raise TypeError(f"first element of condition tuple at index {i} must be callable") |
| if not isinstance(condition[1], (int, float)): |
| raise TypeError(f"second element of condition tuple at index {i} must be a number (score)") |
| sanitized_conditions[f"group{i+1}"] = [condition] |
|
|
| if robolab.constants.DEBUG: |
| print(f"[Sanitize Subtask Conditions] Converting set of tuples to dict: {conditions} converted to {sanitized_conditions}") |
|
|
| return sanitized_conditions |
|
|
| elif callable(first_elem): |
| |
| equal_score = 1.0 / len(conditions_list) |
| sanitized_conditions = {} |
| |
| for i, condition in enumerate(conditions_list): |
| if not callable(condition): |
| raise TypeError(f"condition at index {i} must be callable, got {type(condition)}") |
| sanitized_conditions[f"group{i+1}"] = [(condition, equal_score)] |
|
|
| if robolab.constants.DEBUG: |
| print(f"[Sanitize Subtask Conditions] Converting set of callables to dict: {conditions} converted to {sanitized_conditions}") |
|
|
| return sanitized_conditions |
|
|
| else: |
| raise ValueError(f"set elements must be either tuples (func, score) or callables, got {type(first_elem)}") |
|
|
| |
| elif callable(conditions): |
| sanitized_conditions = { |
| "conditions": [(conditions, 1.0)] |
| } |
|
|
| if robolab.constants.DEBUG: |
| print(f"[Sanitize Subtask Conditions] Converting single callable to dict: {conditions} converted to {sanitized_conditions}") |
|
|
| return sanitized_conditions |
|
|
| else: |
| raise ValueError(f"conditions must be a callable, list, set, or dict, got {type(conditions)}") |
|
|
|
|
| def normalize_conditions_scores(conditions: dict[str, list[tuple[Callable, float]]]) -> dict[str, list[tuple[Callable, float]]]: |
| """ |
| Normalize the scores of the conditions. |
| """ |
| normalized_conditions = {} |
| for group_name, list_of_conditions_func_tuple in conditions.items(): |
| total_score = sum(score for (condition, score) in list_of_conditions_func_tuple) |
| total_score = max(total_score, 1e-9) |
| normalized_conditions[group_name] = [(condition, score / total_score) for (condition, score) in list_of_conditions_func_tuple] |
| return normalized_conditions |
|
|