Search code examples
pythonlinear-programmingpulpirr

Is there a way to implement the IRR algorithm in PuLP?


Numpy has a function which allows me to calculate the IRR of an array of floats. My problem is that I am trying to use it within a PuLP problem, and the array that I want to pass to the function is compossed of the variables of the problem. Here i

    problem = pulp.LpProblem("TIR_MINIMIZE", pulp.LpMaximize)
    price_ppa = pulp.LpVariable("price_ppa")
    price_production = []

    for i in range(10):
        price_production.append(price_ppa * annual_production[i])
        # anual_production is an array of values calculated outside the function

    irr = numpy.irr(price_production)

    # CONSTRAINTS #####################################################################################
    problem += irr>= 0.075

    objective_function = -irr
    problem += objective_function

    #####################################################################################################
    problem.solve()

And this code doesn't work because numpy.irr is expecting an array of floats, while I pass it an array of LpAffineExpressions. My question is, is there a way to implement this in a somewhat easy way? I have tried to implement the algorithm manually, but I can't do it inside the PuLP constraint definition.


Solution

  • Theory

    The IRR computation is inherently nonlinear, so this cannot be handled in a PuLP model. PuLP only deals with linear models.

    Practice

    You are calling numpy.irr but this must be called while the LP solver is running. That is not allowed. LP solvers cannot call external functions.