The equation containing logarithm and powered by variable
回答済みI want to create the equation following this formulation:

And these are my codes containing the variable of y[j,k].
#Lp Total sound pressure level
Sound_pressure = {1:90,2:92,3:95}
Multiply = {}
Buffer_sum = {}
for k in Times:
for j in Machines:
Multiply[j,k] = y[j,k]*LP_level[j]
for k in Times:
Buffer_sum [j,k] = gp.quicksum(math.pow(Multiply[j,k],10) for j in Machines)
for k in Times:
Sound_pressure_level [k] = 10*math.log(Buffer_sum [k])
But, the result showed the error answer as follows:
TypeError: must be real number, not gurobipy.LinExpr
Great thanks for all recommendations.
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Gurobi does not directly support \(\log\) and the power function. You can use the piecewise-linear approximation feature. You can use method addGenConstrPow to model the power function. Please note that all general constraint function accept only single Var objects as inputs. This means that you cannot simply pass your \(\texttt{Multiply}\) LinExpr objects to it. You have to introduce auxilliary variables and additional equality constraint to model \(z_{jk} = \text{Multiply}_{jk}\). The same holds for the addGenConstrLog method.
0 -
Hi,
I got the auxiliary variables (Multiply[j,k]), but the new error was incurred.
#Lp Total sound pressure level
LP_level = {1:90,2:92,3:95}
Multiply_noise = m.addVar(name = 'Multiply_noise')
Expo_noise = m.addVar(name = 'Expo_noise')
Multiply = m.addConstrs((((y[j,k]*LP_level[j])/10) == Multiply_noise for j in Machines for k in Times), name = 'Multiply_noise')
for k in Times:
Expo = m.addGenConstrPow((gp.quicksum(10,Expo_noise,Multiply[j,k]) for j in Machines),"Expo_noise","FuncPiece=1000")The auxiliary variables of Multiply[j,k] are fine, however, I tried to use a similar concept for the m.addGenConstrPow as previously mentioned. The error popped up again as follows,
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-71-e1e3773f2ee8> in <module> 7 8 for k in Times: ----> 9 Expo = m.addGenConstrPow((gp.quicksum(10,Expo_noise,Multiply[j,k]) for j in Machines),"Expo_noise","FuncPiece=1000") src\gurobipy\model.pxi in gurobipy.Model.addGenConstrPow() AttributeError: 'generator' object has no attribute '__cindex__'
Great thanks for all answers.
0 -
Hi Thunwa,
Just as in the \(\log\) case, the addGenContrPow method does not accept LinExpr objects as input arguments (none of the general constraint method does). It only accepts a single input and a single output Var object together with a positive exponent double value.
This means that you have to introduce additional auxiliary variables for each quicksum. Speaking of quicksum, I am not sure what
(gp.quicksum(10,Expo_noise,Multiply[j,k]) for j in Machines)
should mean. Do you want to compute
10 + Expo_noise + gp.quicksum(Multiply[j,k] for j in Machines)
?
Best regards,
Jaromił0 -
Hi Jaromił,

As this equation, I am trying to write the expression of summation over j of "10^((y[j,k]*LP_level[j])/10)".
At the beginning, I assigned the first auxiliary (Multiply_noise) to express (y[j,k]*LP_level[j])/10 for all j,k
Next, I am trying to power those Multiply_noise over "10", Should I assigned another auxiliary for all j,k seperately?
Then, I will take a summation over j for those powered, and put it into Log function again.
Lastly, I multiply it with 10 for all k in order to get LP for all k.
Best regards,
Thunwa
0 -
Hi Thunwa,
Thank you for the clarification. Unfortunately, Gurobi currently does not support the function \(a^x\) where \(a\) is a constant and \(x\) an optimization variable.
Maybe you can reformulate your model in a way that you use the addGenConstrLogA method to model a base 10 logarithm. However, I don't know if this will have any effect on the rest of your model.
Next, I am trying to power those Multiply_noise over "10", Should I assigned another auxiliary for all j,k seperately?
Independently of the \(a^x\) function, yes you would have to define an auxiliary variable for each \(j,k\) combination to model
\[m_{j,k} = y_{j,k} \cdot \text{sound pressure}_{j} \cdot 0.1\]
and then use \(m_{j,k}\) in the corresponding general constraint method.
Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
6件のコメント