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

**URL:** <https://discourse.julialang.org/t/how-to-fix-error-method-error-objects-of-type-genericaffexpr-float64-variableref-are-not-callable/58495>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, optimization\
**Created:** [April 3, 2021, 12:22pm UTC](https://discourse.julialang.org/t/how-to-fix-error-method-error-objects-of-type-genericaffexpr-float64-variableref-are-not-callable/58495 "2021-04-03T12:22:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)\
**Post date:** [April 3, 2021, 12:22pm UTC](https://discourse.julialang.org/t/how-to-fix-error-method-error-objects-of-type-genericaffexpr-float64-variableref-are-not-callable/58495/1 "2021-04-03T12:22:40Z")

</div>

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.

```julia
using JuMP
using TaylorSeries

```

S and N are sets

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

```

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

```julia
profit(N::Array{Float64,1}) = sum((price[s])*(d[s,n]) for n in N for s in S if s == S[4]) 

```

```julia
∫⬩dn(p::Taylor1) = integrate(p)

```

```julia
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

```

```julia
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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [April 4, 2021, 10:28pm UTC](https://discourse.julialang.org/t/how-to-fix-error-method-error-objects-of-type-genericaffexpr-float64-variableref-are-not-callable/58495/2 "2021-04-04T22:28:43Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)\
**Post date:** [April 5, 2021, 12:22pm UTC](https://discourse.julialang.org/t/how-to-fix-error-method-error-objects-of-type-genericaffexpr-float64-variableref-are-not-callable/58495/3 "2021-04-05T12:22:35Z")

</div>

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?

```julia
example2_netprofit = Model()

```

```julia

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

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

```

---

<div class="post-metadata">

**Author:** ![metab0t](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/metab0t/32/15551_2.png) [@metab0t](https://discourse.julialang.org/u/metab0t)\
**Post date:** [April 5, 2021, 12:40pm UTC](https://discourse.julialang.org/t/how-to-fix-error-method-error-objects-of-type-genericaffexpr-float64-variableref-are-not-callable/58495/4 "2021-04-05T12:40:13Z")

</div>

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.
