Cardinal sine doesn't work.

490 views
Skip to first unread message

jdelamot...@gmail.com

unread,
Jul 5, 2021, 5:00:15 PM7/5/21
to Pyomo Forum
Hello everyone. I want to define the cardinal sine function, sinc. I do it as follows:
def Sinc(x):
    if x==0:
        sinc = 1.0
    else:
        sinc = sin(x)/(x)
    return sinc


I used this code in the past. However, I had to swich between computers and now I am getting the following error: 

PyomoException: Cannot convert non-constant expression to bool. This error is usually caused by using an expression in a boolean context such as an if statement. For example, 
    m.x = Var()
    if m.x <= 0:
        ...
would cause this exception.

The line in which I am using the expression is 

def Wind_lammda_definition3(model, i):
    return m.Wind_lammda[i,3]==32.07*Sinc(1.79*(-10. +m.lammda[i,3]*180/np.pi))*Sinc(2.09*(-40. + m.phi[i,3]*180/np.pi)) 
m.Wind_lammda_const3 = Constraint(m.N, rule = Wind_lammda_definition3)

I have no idea why this is happening now. Can it be a issue of version?

Please, any help is appreciated. 

Best regards.
Jaime.

David Woodruff

unread,
Jul 5, 2021, 6:48:05 PM7/5/21
to pyomo...@googlegroups.com
Dear Jaime,
   Using a Pyomo Var in an *if* statement will not do what you want. *If* statements are not passed to solvers and, generally, neither are user-defined functions. 
There may be various ways to get what you want, but they might cause some trouble:
- Maybe there is a solver that knows about sinc (but I don't think so),
- You could add binary Vars to indicate that the argument to sinc is not zero, but unless you already have other binaries, this would dramatically complicate solving the problem.

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/2ff3a322-49ab-4987-a140-1eda974ad635n%40googlegroups.com.

Jaime de la Mota Sanchis

unread,
Jul 6, 2021, 3:39:55 AM7/6/21
to pyomo...@googlegroups.com
Hi David. As I said in my original message, this used to work on my old computer. I am writing this email from it. As you can see in the attached figures, the sinc works here. I need to know why it doesn't work on my new computer.

Can someone please assist me?

Best regards.
Jaime. 

proof.png
proof2.png

David Woodruff

unread,
Jul 6, 2021, 8:38:53 AM7/6/21
to pyomo...@googlegroups.com
My recollection is that in older versions of Pyomo, there was no error message when a Var was used in a Python logical expression, so your code would execute, but not work the way you thought it was working in all cases (I recall that such a logical expression would always be False). 

Reply all
Reply to author
Forward
0 new messages