# Defining constraint using JuMP convert String to Expression and Merge Quart Expr and nonlinear Expr

using JuMP,CSV,DataFrames
model = Model()
@variable(model, x[1:3])
@constraint(model, c2, x[1] + 2 * x[3] >= 2)
@constraint(model, c4, 4 <= 2 * x[2] <= 5)
@variable(model,x1[h in 1:5, i in 1:1, j in 1:2, k in 1:1,p in 1:2]);
h=5
i=1
j=2
k=1
p=2
df2=Array{Union{Nothing, Any}}(nothing, h,i,j,k,p);
for h in 1:5
for i in 1:1
for j in 1:2
for k in 1:1
for p in 1:2
# zz=0.05
print(“h==\$h”)
df2[h,i,j,k,p]=eval.(Meta.parse.(df[!,2]))
end
end
end
end
end
zz=@NLexpression(model,sum(x1[h,i,j,k,p]*df2[h,i,j,k,p] for h in 1:5 , i in 1:1, j in 1:2, k in 1:1 ,p in 1:2 ))
return
end

LoadError: Unexpected array QuadExpr[9.206640000000007e-13 x1[5,1,2,1,2]² + 1.8904140000000001e-6 x1[5,1,2,1,2] + 196.3769] in nonlinear expression. Nonlinear expressions may contain only scalar expressions.

the above code, I get this kind of type error,
LoadError: Unexpected array QuadExpr[9.206640000000007e-13 x1[5,1,2,1,2]² + 1.8904140000000001e-6 x1[5,1,2,1,2] + 196.3769] in nonlinear expression. Nonlinear expressions may contain only scalar expressions.

Hi there @Deena_20,

I’ve moved your question to the “Optimization (Mathematical)” section. If you have JuMP questions in future, that’s the place to put them

Since this is your first post, you should also take a read of: Please read: make it easier to help you. It explains how you can format your code to make it easier to read.

What is the contents of `Book2.csv`? `df2[h,i,j,k,p]` is a vector, but it looks like it only has one element, so perhaps you meant `eval(Meta.parse(df[1,2]))`?

In general though, what are you trying to achieve? Storing nonlinear expressions as text and then trying to parse them into JuMP is quite tricky, and there are a number of things that could go wrong. There might be a different approach you could take that doesn’t involve using `eval`.

i will save csv file like this

and i want convert string to expression and mixed with quart expression and nonlinear expression using boardcasting in eval.(Meta.parse(df[!,2])) and this is not df[1,2], its df[!,2]

Will there only be one row? Where is the CSV coming from?

that csv file will have 100 rows of expressions for each condition…

I think you need to check your indices. What is each row of the csv? Do you expect `zz` to be a vector of one expression for each row?

each row will be different expression. zz will be one value per row…

I think you’re looking for something like this:

``````function readBook2(localpath)
df = CSV.read(joinpath(localpath, "Book2.csv"), DataFrame)
return [readBook2_inner(df[i, 2]) for i in 1:size(df, 1)]
end

f = eval(Meta.parse("(model, x1, h, i, j, k, p) -> @expression(model, \$row)"))
df2 = Array{Any}(undef, h, i, j, k, p)
for h=1:5, i=1:1, j=1:2, k=1:1, p=1:2
df2[h, i, j, k, p] = f(model, x1, h, i, j, k, p)
end
return @NLexpression(
model,
sum(x1[h,i,j,k,p] * df2[h,i,j,k,p] for h=1:5, i=1:1, j=1:2, k=1:1, p=1:2),
)
end
``````

But I’ll repeat

In general though, what are you trying to achieve? Storing nonlinear expressions as text and then trying to parse them into JuMP is quite tricky, and there are a number of things that could go wrong. There might be a different approach you could take that doesn’t involve using `eval` .

I get same error to the code,
Unexpected array QuadExpr[9.206640000000007e-13 x1[5,1,2,1,2]² + 1.8904140000000001e-6 x1[5,1,2,1,2] + 196.3769] in nonlinear expression. Nonlinear expressions may contain only scalar expressions

Iterator Issue for Nonlinear Model,
using CSV, DataFrames,JuMP
function NONlinearexp(localpath::String)
mod=Model()
@variable(mod,x[h in 1:5, i in 1:1, j in 1:2, k in 1:1,l in 1:2]);
if df1[1,:Frame_number]==240
xx = @NLexpression(mod,sum(df[1,:Hold1]*x[h,i,j,k,l]) for h in 1:1 , i in 1:1, j in 1:1, k in 1:1 ,l in 1:2)
print(xx)
else
print(“no”)
end
end

showing this kind of ERROR:
Unexpected object Base.Generator{Base.Iterators.ProductIterator{NTuple{5, UnitRange{Int64}}}, var"#14#16"{Array{VariableRef, 5}}}(var"#14#16"{Array{VariableRef, 5}}(VariableRef[x[1,1,1,1,1]; x[2,1,1,1,1]; x[3,1,1,1,1]; x[4,1,1,1,1]; x[5,1,1,1,1]]

VariableRef[x[1,1,2,1,1]; x[2,1,2,1,1]; x[3,1,2,1,1]; x[4,1,2,1,1]; x[5,1,2,1,1]]

VariableRef[x[1,1,1,1,2]; x[2,1,1,1,2]; x[3,1,1,1,2]; x[4,1,1,1,2]; x[5,1,1,1,2]]

VariableRef[x[1,1,2,1,2]; x[2,1,2,1,2]; x[3,1,2,1,2]; x[4,1,2,1,2]; x[5,1,2,1,2]]), Base.Iterators.ProductIterator{NTuple{5, UnitRange{Int64}}}((1:1, 1:1, 1:1, 1:1, 1:2))) (of type Base.Generator{Base.Iterators.ProductIterator{NTuple{5, UnitRange{Int64}}}, var"#14#16"{Array{VariableRef, 5}}} in nonlinear expression.

Please take the time to read Please read: make it easier to help you. It’s easier to help if you format your code and include a minimal working example.

Your issue is likely that you have

``````sum(df[1,:Hold1]*x[h,i,j,k,l]) for h in 1:1
``````

``````sum(df[1,:Hold1]*x[h,i,j,k,l] for h in 1:1
Note the second parenthesis `)`.