Skip to content

variable_map

IntegerBound dataclass

IntegerBound

Attributes:

Name Type Description
lower int | float

lower bound of integer decision variable

upper int | float

upper bound of integer decision variable

Source code in jijmodeling_transpiler/core/variable_map.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
@dataclass
class IntegerBound:
    """IntegerBound

    Attributes:
        lower (int | float): lower bound of integer decision variable
        upper (int | float): upper bound of integer decision variable

    """

    lower: int | float
    upper: int | float

VariableMap dataclass

VarialeMap

This class is used to manage the mapping between the label and the index of the decision variable.

Attributes:

Name Type Description
var_map dict[str, dict[tuple[int, ...], int]]

label, subscripts -> index (int)

var_num int

number of variables

integer_bound dict[str, dict[tuple[int, ...], IntegerBound]]

bounds of integer decision variables.

Source code in jijmodeling_transpiler/core/variable_map.py
 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
@dataclass
class VariableMap:
    """VarialeMap

    This class is used to manage the mapping between the label and the index of the decision variable.

    Attributes:
        var_map (dict[str, dict[tuple[int, ...], int]]): label, subscripts -> index (int)
        var_num (int): number of variables
        integer_bound (dict[str, dict[tuple[int, ...], IntegerBound]]): bounds of integer decision variables.

    """

    var_map: dict[str, dict[tuple[int, ...], int]]
    var_num: int
    integer_bound: dict[str, dict[tuple[int, ...], IntegerBound]]

    def add_variable(self, label: str, subscripts: tuple[int, ...]) -> int:
        """Add a variable to the variable map.

        Args:
            label (str): label of the variable
            subscripts (tuple[int, ...]): subscripts of the variable

        Returns:
            int: index of the variable in the variable map
        """
        if label not in self.var_map:
            self.var_map[label] = {}

        if subscripts not in self.var_map[label]:
            self.var_map[label][subscripts] = self.var_num
            self.var_num += 1
            return self.var_num - 1
        else:
            return self.var_map[label][subscripts]

    def add_int_variable(self, label, subscripts, lower, upper) -> int:
        """Add an integer variable to the variable map."""
        if label not in self.integer_bound:
            self.integer_bound[label] = {}
        self.integer_bound[label][subscripts] = IntegerBound(lower=lower, upper=upper)
        var_index = self.add_variable(label, subscripts)
        return var_index

    def to_serializable(self) -> dict:
        """Convert to a serializable object.

        Returns:
            dict: serializable object

        Examples:
            ```python
            varmap = VariableMap()
            varmap.add_variable("x", (0, 0))
            varmap.add_variable("x", (0, 1))
            varmap.to_serializable()
            # {
            #    "class": "VariableMap",
            #    "var_map": {
            #        "x": {
            #            "subscripts": [
            #                [0, 0],
            #                [0, 1]
            #            ],
            #            "index": [0, 1]
            #        }
            #    },
            #    "var_num": 2,
            #    "integer_bound": {}
            # }
            ```
        """
        varmap_serializable = {}
        for label, subscripts_map in self.var_map.items():
            varmap_serializable[label] = {"subscripts": [], "index": []}
            for subscripts, index in subscripts_map.items():
                varmap_serializable[label]["subscripts"].append(subscripts)
                varmap_serializable[label]["index"].append(index)
        integer_bound_serializable = {}
        for label, subscripts_map in self.integer_bound.items():
            integer_bound_serializable[label] = {"subscripts": [], "bound": []}
            for subscripts, bound in subscripts_map.items():
                integer_bound_serializable[label]["subscripts"].append(subscripts)
                integer_bound_serializable[label]["bound"].append(
                    {"lower": bound.lower, "upper": bound.upper}
                )
        return {
            "class": "VariableMap",
            "var_map": varmap_serializable,
            "var_num": self.var_num,
            "integer_bound": integer_bound_serializable,
        }

    @classmethod
    def from_serializable(cls, data: dict) -> "VariableMap":
        if data["class"] != "VariableMap":
            raise ValueError(f"Invalid class: {data['class']}")
        var_map = {}
        for label, subscripts_map in data["var_map"].items():
            var_map[label] = {
                tuple(subscripts): index
                for subscripts, index in zip(
                    subscripts_map["subscripts"], subscripts_map["index"]
                )
            }
        integer_bound = {}
        for label, subscripts_map in data["integer_bound"].items():
            integer_bound[label] = {
                tuple(subscripts): IntegerBound(bound["lower"], bound["upper"])
                for subscripts, bound in zip(
                    subscripts_map["subscripts"], subscripts_map["bound"]
                )
            }
        return VariableMap(var_map, data["var_num"], integer_bound)

add_int_variable(label, subscripts, lower, upper)

Add an integer variable to the variable map.

Source code in jijmodeling_transpiler/core/variable_map.py
56
57
58
59
60
61
62
def add_int_variable(self, label, subscripts, lower, upper) -> int:
    """Add an integer variable to the variable map."""
    if label not in self.integer_bound:
        self.integer_bound[label] = {}
    self.integer_bound[label][subscripts] = IntegerBound(lower=lower, upper=upper)
    var_index = self.add_variable(label, subscripts)
    return var_index

add_variable(label, subscripts)

Add a variable to the variable map.

Parameters:

Name Type Description Default
label str

label of the variable

required
subscripts tuple[int, ...]

subscripts of the variable

required

Returns:

Name Type Description
int int

index of the variable in the variable map

Source code in jijmodeling_transpiler/core/variable_map.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def add_variable(self, label: str, subscripts: tuple[int, ...]) -> int:
    """Add a variable to the variable map.

    Args:
        label (str): label of the variable
        subscripts (tuple[int, ...]): subscripts of the variable

    Returns:
        int: index of the variable in the variable map
    """
    if label not in self.var_map:
        self.var_map[label] = {}

    if subscripts not in self.var_map[label]:
        self.var_map[label][subscripts] = self.var_num
        self.var_num += 1
        return self.var_num - 1
    else:
        return self.var_map[label][subscripts]

to_serializable()

Convert to a serializable object.

Returns:

Name Type Description
dict dict

serializable object

Examples:

varmap = VariableMap()
varmap.add_variable("x", (0, 0))
varmap.add_variable("x", (0, 1))
varmap.to_serializable()
# {
#    "class": "VariableMap",
#    "var_map": {
#        "x": {
#            "subscripts": [
#                [0, 0],
#                [0, 1]
#            ],
#            "index": [0, 1]
#        }
#    },
#    "var_num": 2,
#    "integer_bound": {}
# }
Source code in jijmodeling_transpiler/core/variable_map.py
 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
109
110
111
def to_serializable(self) -> dict:
    """Convert to a serializable object.

    Returns:
        dict: serializable object

    Examples:
        ```python
        varmap = VariableMap()
        varmap.add_variable("x", (0, 0))
        varmap.add_variable("x", (0, 1))
        varmap.to_serializable()
        # {
        #    "class": "VariableMap",
        #    "var_map": {
        #        "x": {
        #            "subscripts": [
        #                [0, 0],
        #                [0, 1]
        #            ],
        #            "index": [0, 1]
        #        }
        #    },
        #    "var_num": 2,
        #    "integer_bound": {}
        # }
        ```
    """
    varmap_serializable = {}
    for label, subscripts_map in self.var_map.items():
        varmap_serializable[label] = {"subscripts": [], "index": []}
        for subscripts, index in subscripts_map.items():
            varmap_serializable[label]["subscripts"].append(subscripts)
            varmap_serializable[label]["index"].append(index)
    integer_bound_serializable = {}
    for label, subscripts_map in self.integer_bound.items():
        integer_bound_serializable[label] = {"subscripts": [], "bound": []}
        for subscripts, bound in subscripts_map.items():
            integer_bound_serializable[label]["subscripts"].append(subscripts)
            integer_bound_serializable[label]["bound"].append(
                {"lower": bound.lower, "upper": bound.upper}
            )
    return {
        "class": "VariableMap",
        "var_map": varmap_serializable,
        "var_num": self.var_num,
        "integer_bound": integer_bound_serializable,
    }