Skip to content

instance_data

InstanceData

Instance Data class The constructor provides type validation and conversion from user's input to valid instance data type.

Attributes:

Name Type Description
tensor_data dict[str, ndarray]

tensor value of Placheolder. The key is uuid of the corresponding Placheolder.

jagged_data dict[str, list]

jagged array value of JaggedArray. The key is uuid of the corresponding JaggedArray.

fixed_variables dict[str, dict[tuple[int, ...], float]]

fixed variable values.

indices dict[str, int]

value of Element. The attribute is changing in compile process.

Source code in jijmodeling_transpiler/core/instance_data.py
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
class InstanceData:
    """Instance Data class
    The constructor provides type validation and conversion from user's input to valid instance data type.

    Attributes:
        tensor_data (dict[str, numpy.ndarray]): tensor value of Placheolder. The key is uuid of the corresponding Placheolder.
        jagged_data (dict[str, list]): jagged array value of JaggedArray. The key is uuid of the corresponding JaggedArray.
        fixed_variables (dict[str, dict[tuple[int, ...], float]]): fixed variable values.
        indices (dict[str, int]): value of Element. The attribute is changing in compile process.
    """

    def __init__(
        self,
        instance_data: dict,
        fixed_vars: dict,
        ph_names: list[str],
        indices: typ.Optional[dict] = None,
    ) -> None:
        """

        Args:
            instance_data (dict): user input instance_data.
            fixed_vars (dict): user input fixed_variables.
            ph_vars (list[name]): Placeholder list include user defined model. This is used for validation.
            indices (typ.Optional[dict], optional): value of each Elements. Defaults to None.

        Raises:
            ValueError: user's input is invalid.
        """
        name_key_data = {}

        for ph_v in ph_names:
            if ph_v in instance_data:
                name_key_data[ph_v] = instance_data[ph_v]
            else:
                raise ValueError(f"Placeholder `{ph_v}` is needed in instance_data.")

        # Store data, even if the data is not include of the problem,
        for ph_name, ph_value in instance_data.items():
            if isinstance(ph_name, str):
                if ph_name not in name_key_data:
                    name_key_data[ph_name] = ph_value

        _ph_value = {}
        _jagged_value = {}
        for ph_v in ph_names:
            try:
                _ph_value[ph_v] = np.array(name_key_data[ph_v])
            except ValueError:
                _jagged_value[ph_v] = name_key_data[ph_v]

        for ph_name, ph_value in name_key_data.items():
            if ph_name not in _ph_value:
                try:
                    _ph_value[ph_name] = np.array(ph_value)
                except ValueError:
                    _jagged_value[ph_name] = ph_value

        raise_type_error(
            "fixed_vars",
            fixed_vars,
            dict[str, dict[tuple[int, ...], typ.Union[float, int]]],
        )

        self.tensor_data: dict[str, np.ndarray] = _ph_value
        self.jagged_data: dict[str, list] = _jagged_value
        self.fixed_variables: dict[str, dict[tuple[int, ...], float]] = fixed_vars
        self.indices: dict[str, int] = indices if indices is not None else {}

    def get_value(self, uuid: str, subscripts: tuple[int, ...]) -> float:
        try:
            if uuid in self.tensor_data:
                return self.tensor_data[uuid][subscripts]
            else:
                ph_value = self.jagged_data[uuid]
                for s in subscripts:
                    ph_value = ph_value[s]
                return ph_value
        except IndexError as e:
            raise SubstituteError(f"IndexError: {e}") from e
        except KeyError as e:
            raise SubstituteError(f"KeyError: {e}") from e

    def get_length(self, name: str, subscripts: tuple[int, ...], axis: int) -> int:
        try:
            if name in self.tensor_data:
                return self.tensor_data[name][subscripts].shape[axis]
            else:
                if axis != 0:
                    raise SubstituteError(
                        f"JaggedArray can not get length of axis {axis}"
                    )
                ph_value = self.jagged_data[name]
                for s in subscripts:
                    ph_value = ph_value[s]
                return len(ph_value)
        except IndexError as e:
            raise SubstituteError(f"IndexError: {e}") from e
        except KeyError as e:
            raise SubstituteError(f"KeyError: {e}") from e

__init__(instance_data, fixed_vars, ph_names, indices=None)

Parameters:

Name Type Description Default
instance_data dict

user input instance_data.

required
fixed_vars dict

user input fixed_variables.

required
ph_vars list[name]

Placeholder list include user defined model. This is used for validation.

required
indices Optional[dict]

value of each Elements. Defaults to None.

None

Raises:

Type Description
ValueError

user's input is invalid.

Source code in jijmodeling_transpiler/core/instance_data.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def __init__(
    self,
    instance_data: dict,
    fixed_vars: dict,
    ph_names: list[str],
    indices: typ.Optional[dict] = None,
) -> None:
    """

    Args:
        instance_data (dict): user input instance_data.
        fixed_vars (dict): user input fixed_variables.
        ph_vars (list[name]): Placeholder list include user defined model. This is used for validation.
        indices (typ.Optional[dict], optional): value of each Elements. Defaults to None.

    Raises:
        ValueError: user's input is invalid.
    """
    name_key_data = {}

    for ph_v in ph_names:
        if ph_v in instance_data:
            name_key_data[ph_v] = instance_data[ph_v]
        else:
            raise ValueError(f"Placeholder `{ph_v}` is needed in instance_data.")

    # Store data, even if the data is not include of the problem,
    for ph_name, ph_value in instance_data.items():
        if isinstance(ph_name, str):
            if ph_name not in name_key_data:
                name_key_data[ph_name] = ph_value

    _ph_value = {}
    _jagged_value = {}
    for ph_v in ph_names:
        try:
            _ph_value[ph_v] = np.array(name_key_data[ph_v])
        except ValueError:
            _jagged_value[ph_v] = name_key_data[ph_v]

    for ph_name, ph_value in name_key_data.items():
        if ph_name not in _ph_value:
            try:
                _ph_value[ph_name] = np.array(ph_value)
            except ValueError:
                _jagged_value[ph_name] = ph_value

    raise_type_error(
        "fixed_vars",
        fixed_vars,
        dict[str, dict[tuple[int, ...], typ.Union[float, int]]],
    )

    self.tensor_data: dict[str, np.ndarray] = _ph_value
    self.jagged_data: dict[str, list] = _jagged_value
    self.fixed_variables: dict[str, dict[tuple[int, ...], float]] = fixed_vars
    self.indices: dict[str, int] = indices if indices is not None else {}