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

support for function incur in constraint functions #188

Closed
hanfeng0114 opened this issue Aug 17, 2023 · 5 comments
Closed

support for function incur in constraint functions #188

hanfeng0114 opened this issue Aug 17, 2023 · 5 comments

Comments

@hanfeng0114
Copy link

Does pyvsc support function invoking in constraint function?

@hanfeng0114
Copy link
Author

hanfeng0114 commented Aug 17, 2023

Like the following feature:
'
@vsc.constraint
def basic_c(self):
with vsc.foreach(self.GroupList, idx=True, it=False) as idx:
self.GroupList[idx]<= self.mThreadNum
with vsc.foreach(self.GroupList, idx=True, it=False) as idx:
self.GroupList[idx]<= self.mThreadNum
with vsc.foreach(self.GroupList, idx=True, it=False) as idx:
with vsc.if_then(self.GroupList[idx] > 1):
self.GroupListScore[idx] == 1
with vsc.else_then():
self.GroupListScore[idx] == 0
self.GroupListScore.sum < self.getSum(self.GroupListScore)
self.GroupList.sum == self.mThreadNum

        def getSum(self, aList):
            return sum(aList)

'

@mballance
Copy link
Member

Can you provide a full example that includes the definition of data fields?
Currently, PyVSC invokes functions when the constraint is built. So, getSum would be invoked once to obtain the size of the GroupListScore. Obviously, this behavior might not be what is desired.

@hanfeng0114
Copy link
Author

hanfeng0114 commented Aug 18, 2023

For example, The following code doesn't run well for line 94:
image

Line 94 doesn't have caculation functions.
My question is how to meet similar requirement?

@mballance
Copy link
Member

Hi @hanfeng0114,
Thank you for the full example. I created a small testcase based on the example. The error I receive from running this testcase is shown below:

  File "/project/fun/pyvsc/pyvsc-20230821/src/vsc/rand_obj.py", line 208, in build_field_model
    fo.c(self)
  File "/project/fun/pyvsc/pyvsc-20230821/ve/unit/test_scalar_array.py", line 348, in basic_c
    self.GroupList.sum == self.mThreadNum * self.mSharePercent/100
  File "/project/fun/pyvsc/pyvsc-20230821/src/vsc/types.py", line 84, in __eq__
    return self.bin_expr(BinExprType.Eq, rhs)
  File "/project/fun/pyvsc/pyvsc-20230821/src/vsc/types.py", line 72, in bin_expr
    to_expr(rhs)
  File "/project/fun/pyvsc/pyvsc-20230821/src/vsc/types.py", line 338, in to_expr
    raise Exception("Element \"" + str(t) + "\" isn't recognized, and doesn't provide to_expr (type=%s)" % str(type(t)))
Exception: Element "6.4" isn't recognized, and doesn't provide to_expr (type=<class 'float'>)

The core issue is that the right-hand side of the equation produces a floating-point result and PyVSC doesn't currently handle this. I receive proper results if I explicitly convert the right-hand side to an integer:

    self.GroupList.sum == int(self.mThreadNum * self.mSharePercent/100)

I recommend using explicit conversion for now. In the future, I will either improve the error messaging or provide direct support for using floating-point expressions in integer constraint expressions.

mballance added a commit that referenced this issue Aug 22, 2023
- (#188) - Automatically convert floating-point numbers used in constraints
  to integers

Signed-off-by: Matthew Ballance <[email protected]>
@mballance
Copy link
Member

The 0.8.4 release contains changes to automatically convert floating-point number used in constraints to integers. I believe this is the best way to address this issue.
Thank you for the issue report!

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