Module src.olaaaf.distance.distance_function.l1DistanceFunction

Class representing a l1 distance function, also called cityblock or Manhattan.

Classes

class l1DistanceFunction (weights: dict[Variable, Fraction], domaine: Domain = None)

Class representing a l1 distance function, also called cityblock or Manhattan, defined as such:

d(x, y) = \sum_{j=1}^{n}w_j|y_j-x_j|

with w_j weights.

Parameters

weights : a dictionnary of fractions.Fraction with olaaaf.variable.variable.Variable as key
The weights of the l1 distance function
domain : olaaaf.distance.domain.domain.Domain
The olaaaf.distance.domain.domain.Domain on which the distance function is defined.
Expand source code
class l1DistanceFunction(DistanceFunctionOnNumericalTuple):
    r"""
    Class representing a l1 distance function, also called cityblock or Manhattan, defined as such:

    \[
        d(x, y) = \sum_{j=1}^{n}w_j|y_j-x_j|
    \]

    with \(w_j\) weights.

    Parameters
    ----------
    weights : a dictionnary of fractions.Fraction with olaaaf.variable.variable.Variable as key
        The weights of the l1 distance function
    domain: olaaaf.distance.domain.domain.Domain
        The `olaaaf.distance.domain.domain.Domain` on which the distance function is defined.
    """

    _weights : dict[Variable, Fraction]

    def __init__(self, weights : dict[Variable, Fraction], domaine : Domain = None):
        self._domaine = domaine
        self._weights = weights

    def dist(self, x : tuple[Variable], y :tuple[Variable]):
        r"""
        Main method of a distance funciton, allowing to get the distance between two points \(x\) and \(y\).

        Parameters
        ----------
        x, y : tuple of olaaaf.variable.variable.Variable
            The two tuples of `olaaaf.variable.variable.Variable` you which to get the distance between.

        Returns
        -------
        fractions.Fraction
            The distance between \(x\) and \(y\).
        """
        
        if(len(x) != len(y)): raise Exception("x and y are not in the same domaine")
        res = 0
        for i in range(0, len(x)):
            res += self._fractions[i] * abs(x[i] - y[i])
        return res

    def getWeights(self) -> Fraction:
        return self._weights

    def getEpsilon(self) -> Fraction:
        return Fraction(1,1)

Ancestors

Methods

def getEpsilon(self) ‑> fractions.Fraction
def getWeights(self) ‑> fractions.Fraction

Inherited members