I am trying to run Optimization.jl (or something else) on an expression I have built up in about 200 lines of code. For example, the output of
println("LOSS FUNCTION:")
println(loss)
println(typeof(loss))
is:
(1.1079731710047782 - xˍt_1)^2 + (2.0 - x_1)^2 + (4.607932711559424 - x_2)^2 + (8.963378140696422 - x_3)^2 + (2.2029940222813673 - xˍt_2)^2 + (4.862596195092209 - xˍt_3)^2 + (xˍt_1 - alpha*x_1)^2 + (xˍt_2 - alpha*x_2)^2 + (xˍt_3 - alpha*x_3)^2
Symbolics.Num
However, the below code fails on the last line:
lossvars = get_variables(loss)
println("LOSS VARIABLES:")
println(lossvars)
f_expr = build_function(loss, lossvars)
println(f_expr)
println(typeof(f_expr))
n = length(lossvars)
d = zeros(n)
println(d)
println(n)
println(typeof(f_expr))
f_expr(d)
The error is:
LoadError: MethodError: objects of type Expr are not callable
The output of the above code is, in case it helps:
LOSS VARIABLES:
Any[xˍt_1, x_1, x_2, x_3, xˍt_2, xˍt_3, alpha]
function (ˍ₋arg1,)
#= /home/orebas/.julia/packages/SymbolicUtils/NJ0fs/src/code.jl:373 =#
#= /home/orebas/.julia/packages/SymbolicUtils/NJ0fs/src/code.jl:374 =#
#= /home/orebas/.julia/packages/SymbolicUtils/NJ0fs/src/code.jl:375 =#
begin
(+)((+)((+)((+)((+)((+)((+)((+)((^)((+)(1.1079731710047782, (*)(-1, ˍ₋arg1[1])), 2), (^)((+)(2.0, (*)(-1, ˍ₋arg1[2])), 2)), (^)((+)(4.607932711559424, (*)(-1, ˍ₋arg1[3])), 2)), (^)((+)(8.963378140696422, (*)(-1, ˍ₋arg1[4])), 2)), (^)((+)(2.2029940222813673, (*)(-1, ˍ₋arg1[5])), 2)), (^)((+)(4.862596195092209, (*)(-1, ˍ₋arg1[6])), 2)), (^)((+)(ˍ₋arg1[1], (*)((*)(-1, ˍ₋arg1[7]), ˍ₋arg1[2])), 2)), (^)((+)(ˍ₋arg1[5], (*)((*)(-1, ˍ₋arg1[7]), ˍ₋arg1[3])), 2)), (^)((+)(ˍ₋arg1[6], (*)((*)(-1, ˍ₋arg1[7]), ˍ₋arg1[4])), 2))
end
end
Expr
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
7
Expr
For some context, I also tried OptimizationFunction, and I also tried using the @syms macro after a different error message suggested to try that. I have been struggling to optimize this function for a while now, and nothing seems to work. Any help would be appreciated. Thanks.