AttributeError: 'LpVariable' object has no attribute 'log'

Viewed 146

I had this problem when trying to solve a optimization problem using pulp.

The code:

import pulp
import numpy as np
import math

prob = pulp.LpProblem("example", pulp.LpMaximize)

# Variable represent number of times device i is used
d = pulp.LpVariable("d", cat=pulp.LpContinuous,lowBound=0,upBound=np.inf)
var = pulp.LpVariable("var", cat=pulp.LpContinuous,lowBound=0,upBound=np.inf)

# The objective function that we want to maximize
n = len(y_arfima)
prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)*math.gamma(t-2*d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2

# Actually solve the problem, this calls GLPK so you need it installed
pulp.GLPK().solve(prob)

# Print out the results
for v in prob.variables():
    print (v.name, "=", v.varValue)

The error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
AttributeError: 'LpVariable' object has no attribute 'log'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12336/3027528854.py in <module>
     11 # The objective function that we want to maximize
     12 n = len(y_arfima)
---> 13 prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)*math.gamma(t-2*d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2
     14 
     15 # Actually solve the problem, this calls GLPK so you need it installed

TypeError: loop of ufunc does not support argument 0 of type LpVariable which has no callable log method

AttributeError Traceback (most recent call last) AttributeError: 'LpVariable' object has no attribute 'log'

The above exception was the direct cause of the following exception:

TypeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_12336/3027528854.py in 11 # The objective function that we want to maximize 12 n = len(y_arfima) ---> 13 prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)math.gamma(t-2d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2 14 15 # Actually solve the problem, this calls GLPK so you need it installed

TypeError: loop of ufunc does not support argument 0 of type LpVariable which has no callable log method

Can you help me?

Thanks!

0 Answers
Related