Comments (4)
Hi @ishitapvips,
This is an interesting question, given that Python doesn't have a builtin case statement. I could see a couple of approaches. Perhaps you can provide feedback on which would fit best in your application.
Fundamentally, rand-case is the combination of weighted selection and a case statement. We could model this in Python with a weighted selection from a collection of values that represent the case options.
Let's say we're replacing a rand-case statement like the following:
randcase
3 : x = 1;
1 : x = 2;
4 : x = 3;
endcase
This approach would look something like:
branch = vsc.select({ 1 : 3, 2 : 1, 3 : 4})
if branch == 1:
x = 1
elif branch == 2:
x = 2
elif branch == 3:
x = 3
In other words, we specify a map with the key being the branch and the value being the branch weight. select returns the selected branch, and we use a regular if/else statement.
Another approach would be to use lambda expressions. Lambda expressions are pretty limited in Python, so you would likely need to put the actual implementation of the case branches in separate functions. That might look like this:
vsc.randcase([
[3, lambda : do_branch1()],
[1, lambda : do_branch2()],
[4, lambda : do_branch3()]
])
So, a couple approaches to doing this in Python with PyVSC. Any thoughts on which approach would work best for your application?
Thanks and Best Regards,
Matthew
from pyvsc.
Hi Matthew,
As per your convenience, any of the approaches will work for us. But lambda function would be straight forward so can you implement that one?
Thanks & Regards,
Ishita
from pyvsc.
Hi Ishita,
I've added support for a 'randselect' method that performs a weighted selection between a list of lambda functions. Here's an example:
Lines 22 to 36 in 487620a
I'll leave this issue open until you're able to confirm that it works for you.
Best Regards,
Matthew
from pyvsc.
Hi Matthew,
Randselect is working fine for me right now.
If any issue is found later will comment here.
Thanks & Regards,
Ishita Shah
from pyvsc.
Related Issues (20)
- vsc.model.solve_failure.SolveFailure: solve failure [randomly] in riscv-dv
- Adding riscv-dv PyFlow to the PyVSC regression list.
- Note:variable bounds model failed to converge in 100 iterations HOT 2
- Passing type of object rather object in isinstance(object,type) method in to_expr(t) function in vsc.types HOT 5
- rand_int8_t with negative maximum can not be randomized HOT 1
- Supporting inheritance in PyVSC HOT 2
- Performance impart using wilcard bins HOT 4
- Random distribution of 2-variable constraints not uniform HOT 5
- Sampling spams unhelpful messages HOT 1
- Dynamic constraints combined with 'soft' constraining HOT 1
- windows10 install pyboolector failed HOT 4
- Can't have a randsz_list_t of randobj HOT 1
- ModuleNotFoundError: No module named 'pyboolector' HOT 10
- I'm not able to install pyvsc package, it fails when handling the pyboolector module HOT 3
- Selecting a list element from a list of bit-vectors returns an int HOT 3
- Setting constraint_mode to False retains the previous range/bounds of the constraint HOT 1
- Cannot make constraint under if_then soft HOT 13
- Cannot use random value as list index in a constraint HOT 2
- PicklingError when trying to pickle a VSC randobj HOT 2
- What is the relation between pyvsc and portable stimulus (pss)? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyvsc.