Applying Logarithmic Transformations for Demand Prediction
AnsweredHi,
I'm currently working on modifying an existing notebook titled "Part One: Avocado Pricing and Supply Using Mathematical Optimization" available at https://colab.research.google.com/github/Gurobi/modeling-examples/blob/master/price_optimization/price_optimization_gcl.ipynb
My objective is to apply logarithmic transformations to the price and units_sold variables, but I'm facing an issue when trying to create an expression for the predicted demand. I would greatly appreciate any assistance or suggestions on how to resolve this issue.
My code:
d = {r: np.exp(coef_dict['Intercept'] + coef_dict['log_price']*np.log(p[r]) + coef_dict['C(region)[T.%s]'%r] + coef_dict['year_index']*(year-2015) + coef_dict['peak']*peak_or_not) for r in R}
for r in R:
print(d[r])
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
src/gurobipy/var.pxi in gurobipy.Var.__getattr__()
src/gurobipy/var.pxi in gurobipy.Var.getAttr()
src/gurobipy/attrutil.pxi in gurobipy.__getattr()
src/gurobipy/attrutil.pxi in gurobipy.__getattrinfo()
AttributeError: 'gurobipy.Var' object has no attribute 'log'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-45-741ec6d3f402> in <cell line: 2>()
1 # Calcular la demanda utilizando el modelo ajustado y las variables transformadas
----> 2 d = {r: np.exp(coef_dict['Intercept'] + coef_dict['log_price']*np.log(p[r]) + coef_dict['C(region)[T.%s]'%r] + coef_dict['year_index']*(year-2015) + coef_dict['peak']*peak_or_not) for r in R}
3
4 for r in R:
5 print(d[r])
<ipython-input-45-741ec6d3f402> in <dictcomp>(.0)
1 # Calcular la demanda utilizando el modelo ajustado y las variables transformadas
----> 2 d = {r: np.exp(coef_dict['Intercept'] + coef_dict['log_price']*np.log(p[r]) + coef_dict['C(region)[T.%s]'%r] + coef_dict['year_index']*(year-2015) + coef_dict['peak']*peak_or_not) for r in R}
3
4 for r in R:
5 print(d[r])
TypeError: loop of ufunc does not support argument 0 of type Var which has no callable log method
-
Hi Florencia,
In the code, p[r] is a Gurobi variable, and in general cannot be passed as an argument to non-Gurobi functions such as numpy.log - there are of course exceptions such as str, type etc.
If you wish to create the log of a Gurobi variable then you need to introduce a new variable for this value, and link it using a addGenConstrLog constraint.
- Riley
0
Please sign in to leave a comment.
Comments
1 comment