How to fix Error: Method error: objects of type GenericAffExpr{Float64, VariableRef} are not callable

I would like to create a Taylor expansion of an objective function but I am having trouble as the objective function is a Generic affline expression. Is there a way to use the convert () to convert the form?

This is the MWE.

using JuMP
using TaylorSeries

S and N are sets

N = Array(1:100);
S = ["s1","s2","s3","s4"];

Price is a parameter and d is a variable within the milp model.

profit(N::Array{Float64,1}) = sum((price[s])*(d[s,n]) for n in N for s in S if s == S[4]) 
∫⬩dn(p::Taylor1) = integrate(p)
function TaylorSurrogate(f, p0)
    
    p = copy(p0)
    pnew = p0 + ∫⬩dn(f(p))
    
    while (pnew - p) != 0
        p = pnew
        pnew = p0 + ∫⬩dn(f(p))
    end
    
    return p
        
end
degree = 3

p0 = Taylor1([1.0], degree)

SurrogateApproximation = TaylorSurrogate(profit(N::Array{Float64,1}), p0)

The full error:

MethodError: objects of type GenericAffExpr{Float64,VariableRef} are not callable

Stacktrace:
[1] TaylorSurrogate(::GenericAffExpr{Float64,VariableRef}, ::Taylor1{Float64}) at .\In[120]:4
[2] top-level scope at In[129]:1
[3] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

Is there a way to convert objects of GenericAffExpr{Float64, Variable} to a form that is callable into a function?

Thank you

Please remember to provide a minimal working example. Your code doesn’t define price or d.

Your TaylorSurrogate looks like it requires a function as the first argument, but you are passing a GenericAffExpr.

1 Like

Yes I understand that. I would like to find out if I can use convert() to convert the GenericAffExpr to a function and how would I use it?

example2_netprofit = Model()

price = OrderedDict(
"s1" => 0,
"s2" => 0,
"s3" => 0,
"s4" => 5
)

@variables example2_netprofit begin
    d[s in S, n in N] >= 0
end

Expression in JuMP cannot be converted to free function directly IMHO.
You can refer to Symbolics.jl and its build_function to create function from expression dynamically.

1 Like