Mentions légales du service

Skip to content

Variable indices don't work as expected

No idea why, but variables indices don't work as expected. It is an issue in both the master branch and after my patch in the bugfix-stable-indices branch.

Consider the following example:

import infrared as ir
import random
from typing import *

random.seed(1)

# We first generate a list of variable groups
# Each variable group is a tuple composed of:
# - a name (random string of length 5)
# - a number of variable in this group (int)
# - an upper bound (int) so that the domain of the group is [0, ..., ub]

variables: List[Tuple[str, int, int]] = []
ALPHABET="abcdefghijklmnoqrstuvwxyz"

for _ in range(10):

    name = ""
    for _ in range(5):
        char_idx = random.randint(0, len(ALPHABET)-1)
        name += ALPHABET[char_idx]

    count = random.randint(1, 5)

    domain_ub = random.randint(1, 10)

    variables.append((name, count, domain_ub))

# Assert that all names are unique
assert len(set([ name for name, _, _ in variables ])) == len(variables), "variable groups must have unique names"


# We add all these variables into an Infrared model

model = ir.Model()

for name, count, domain_ub in variables:
    model.add_variables(count, (0,domain_ub), name=name)


# We sample assignments
sampler = ir.Sampler(model)
for _ in range(10):
    sample = sampler.sample().values()
    # We check that the assignments have the required length
    assert len(sample) == sum([ count for _, count, _ in variables ])

    # We check that for each variable: its assignment is in the specified domain
    for name, count, domain_ub in variables:
        domain = list(range(0, domain_ub+1))

        var_idx = model.idx((name, count))[0]
        assert 0 <= var_idx and var_idx < len(sample), f"variable index is not in bounds: {var_idx} VS {len(sample)} for variable ({name} {count} {domain})"
        val = sample[var_idx]

        assert val in domain, f"=var({name} {count}): {val} not in {domain}"

Depending on the seed it fails in either of thoses ways:

(seed=1)
AssertionError: variable index is not in bounds: 24 VS 24 for variable (zoqvm 2 [0, 1, 2])
(seed=2)
AssertionError: =var(bcclf 3): 9 not in [0, 1, 2, 3, 4, 5]