fvutils/pyvsc

Set values on randobj and check if it satisfies the constraints

sbhutada opened this issue · 2 comments

Is it possible to do the following?

  • create a randobj with constraints,
  • instantiate the randobj,
  • set specific values to its random parameters
  • and then invoke some infrastructure to verify is the current settings satisfy the constraints

Thanks

You can use try-except blocks to catch randomize_with() failures. You can also run with solve_fail_debug=1 on the failure to see diagnostics for the failure.

import vsc
import random

@vsc.randobj
class my_item_c:
    def __init__(self):
        self.a = vsc.rand_uint8_t()
        self.b = vsc.rand_uint8_t()

    @vsc.constraint
    def ab_c(self):
        self.a + self.b < 10

my_item = my_item_c()

for _ in range(10):
    v = random.randrange(0, 20)
    try:
        with my_item.randomize_with() as it:
            it.a == v
        print(f'(a == {v}) hit')
    except:
        print(f'(a == {v}) unsolvable')
        try:
            with my_item.randomize_with(solve_fail_debug=1) as it:
                it.a == v
        except:
            pass

Output:

(a == 9) hit
Solve failure: set 'solve_fail_debug=1' for more details
(a == 14) unsolvable
Problem Set: 2 constraints
  <unknown>:
    ((a + b) < 10);
  <unknown>:
    (a == 14);

(a == 3) hit
(a == 6) hit
(a == 1) hit
Solve failure: set 'solve_fail_debug=1' for more details
(a == 18) unsolvable
Problem Set: 2 constraints
  <unknown>:
    ((a + b) < 10);
  <unknown>:
    (a == 18);

(a == 2) hit
(a == 0) hit
(a == 4) hit
Solve failure: set 'solve_fail_debug=1' for more details
(a == 18) unsolvable
Problem Set: 2 constraints
  <unknown>:
    ((a + b) < 10);
  <unknown>:
    (a == 18);

There isn't currently a direct method for doing this, since the randomize allows the field values to be overwritten. Please file an enhancement request if this is functionality you need.