@File : _descriptor_strategies.py @Time : 2024/06/07 14:29:09 @Author : Alejandro Marrero @Version : 1.0 @Contact : amarrerd@ull.edu.es @License : (C)Copyright 2024, Alejandro Marrero @Desc : Descriptors Strategies for Instance Generation

__property_strategy(attr)

Returns a np.ndarray with the information required of the instances

Parameters:
  • iterable (Iterable[Instance]) –

    Instances to describe

Returns:
  • np.ndarray: Array of the feature descriptors of each instance

Source code in digneapy/_core/descriptors.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def __property_strategy(attr: str):
    """Returns a np.ndarray with the information required of the instances

    Args:
        iterable (Iterable[Instance]): Instances to describe

    Returns:
        np.ndarray: Array of the feature descriptors of each instance
    """
    try:
        if attr not in ("features", "transformed"):
            raise AttributeError()
    except AttributeError:
        raise ValueError(
            f"Object of class Instance does not have a property named {attr}"
        )

    def strategy(iterable: Iterable[Instance]) -> np.ndarray:
        return np.asarray([getattr(i, attr) for i in iterable])

    return strategy

descriptor(key, verbose=False)

Decorator to create new descriptor strategies

Parameters:
  • key (str) –

    Key to refer the descriptor-function

  • verbose (bool, default: False ) –

    Prints a message when the function is registered. Defaults to False.

Source code in digneapy/_core/descriptors.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def descriptor(key: str, verbose: bool = False):
    """Decorator to create new descriptor strategies

    Args:
        key (str): Key to refer the descriptor-function
        verbose (bool, optional): Prints a message when the function is registered. Defaults to False.
    """

    def decorate(func: DescStrategy):
        if verbose:
            print(f"Registering descriptor function: {func.__name__} with key: {key}")
        DESCRIPTORS[key] = func
        return func

    return decorate

instance_strategy(iterable)

It returns the instance information as its descriptor

Parameters:
  • iterable (Iterable[Instance]) –

    Instances to describe

Returns:
  • ndarray

    np.ndarray: Array of descriptor instance (whole instace data)

Source code in digneapy/_core/descriptors.py
89
90
91
92
93
94
95
96
97
98
def instance_strategy(iterable: Iterable[Instance]) -> np.ndarray:
    """It returns the instance information as its descriptor

    Args:
        iterable (Iterable[Instance]): Instances to describe

    Returns:
        np.ndarray: Array of descriptor instance (whole instace data)
    """
    return np.asarray([*iterable])

performance_strategy(iterable)

It generates the performance descriptor of an instance based on the scores of the solvers in the portfolio over such instance

Parameters:
  • iterable (Iterable[Instance]) –

    Instances to describe

Returns:
  • ndarray

    np.ndarray: Array of performance descriptors of each instance

Source code in digneapy/_core/descriptors.py
76
77
78
79
80
81
82
83
84
85
86
def performance_strategy(iterable: Iterable[Instance]) -> np.ndarray:
    """It generates the performance descriptor of an instance
    based on the scores of the solvers in the portfolio over such instance

    Args:
        iterable (Iterable[Instance]): Instances to describe

    Returns:
        np.ndarray: Array of performance descriptors of each instance
    """
    return np.asarray([np.mean(i.portfolio_scores, axis=1) for i in iterable])