Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use random value as list index in a constraint #177

Open
felixdube opened this issue Feb 16, 2023 · 2 comments
Open

Cannot use random value as list index in a constraint #177

felixdube opened this issue Feb 16, 2023 · 2 comments

Comments

@felixdube
Copy link

I am wondering if it is possible to use a random value as a list index inside a constraint. It does not seem to work as I expect. Here is a minimal example, which produce the following error:
TypeError: list indices must be integers or slices, not rand_uint32_t

import vsc

@vsc.randobj
class my_item():
    def __init__(self):
        self.a = vsc.rand_uint32_t()
        self.b = vsc.rand_uint32_t()
        self.c = [2,4,6,8]        

    @vsc.constraint
    def ab_c(self):
        vsc.solve_order(self.a, self.b)
        self.a < len(self.c)
        self.b < self.c[self.a]	

item = my_item()

I have found a workaround, but it might not be very efficient depending on the size of the list.

    @vsc.constraint
    def ab_c(self):
        vsc.solve_order(self.a, self.b)
        self.a < len(self.c)
        for i in range(len(self.c)):
            with vsc.if_then(self.a == i):
                self.b < self.c[i]
@mballance
Copy link
Member

@felixdube, interestingly enough, your workaround is more or less exactly what would happen "under the hood" to implement the self.b < self.c[self.a] constraint.
Any feedback on how common you see this case to be in your work? If it's common (ie a workaround will be a real pain), that certainly will impact the priority of addressing it sooner vs later.

@felixdube
Copy link
Author

@mballance, thank you for your feedback! I can live with the workaround for now. I'll comeback if ever it becomes a real pain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants