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

**URL:** <https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, dataframes\
**Created:** [April 25, 2022, 4:31am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967 "2022-04-25T04:31:03Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deena\_20](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Deena\_20](https://discourse.julialang.org/u/Deena_20)\
**Post date:** [April 25, 2022, 4:31am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967/1 "2022-04-25T04:31:03Z")

</div>

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  
function readBook2(localpath::String)  
df = CSV.read(string(localpath,“Book2.csv”),DataFrame)  
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.

---

<div class="post-metadata">

**Author:** ![Deena\_20](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Deena\_20](https://discourse.julialang.org/u/Deena_20)\
**Post date:** [April 25, 2022, 4:37am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967/2 "2022-04-25T04:37:33Z")

</div>

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.

---

<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 25, 2022, 6:14am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967/3 "2022-04-25T06:14:29Z")

</div>

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](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757). 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`.

---

<div class="post-metadata">

**Author:** ![Deena\_20](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Deena\_20](https://discourse.julialang.org/u/Deena_20)\
**Post date:** [April 25, 2022, 6:26am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967/4 "2022-04-25T06:26:53Z")

</div>

i will save csv file like this

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/4/94c979ba71f9dcacc11ed847e6bc1faba4850e55.png)  
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]

---

<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 25, 2022, 7:59am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967/5 "2022-04-25T07:59:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Deena\_20](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Deena\_20](https://discourse.julialang.org/u/Deena_20)\
**Post date:** [April 25, 2022, 9:15am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967/6 "2022-04-25T09:15:22Z")

</div>

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

---

<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 25, 2022, 9:25am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967/7 "2022-04-25T09:25:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Deena\_20](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Deena\_20](https://discourse.julialang.org/u/Deena_20)\
**Post date:** [April 25, 2022, 9:29am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967/8 "2022-04-25T09:29:29Z")

</div>

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

---

<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 25, 2022, 9:38am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967/9 "2022-04-25T09:38:45Z")

</div>

I think you’re looking for something like this:

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

function readBook2_inner(row)
    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` .

---

<div class="post-metadata">

**Author:** ![Deena\_20](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Deena\_20](https://discourse.julialang.org/u/Deena_20)\
**Post date:** [April 25, 2022, 10:09am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967/10 "2022-04-25T10:09:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Deena\_20](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Deena\_20](https://discourse.julialang.org/u/Deena_20)\
**Post date:** [May 2, 2022, 4:36am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967/11 "2022-05-02T04:36:11Z")

</div>

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]);  
df1=CSV.read(string(localpath,“PMM\_getSFHoldsingle.csv”),DataFrame)  
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.

---

<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:** [May 2, 2022, 4:40am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967/12 "2022-05-02T04:40:22Z")

</div>

Please take the time to read [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757). It’s easier to help if you format your code and include a minimal working example.

Your issue is likely that you have

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

```

instead of

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

```

Note the second parenthesis `)`.

---

<div class="post-metadata">

**Author:** ![Deena\_20](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@Deena\_20](https://discourse.julialang.org/u/Deena_20)\
**Post date:** [May 2, 2022, 4:43am UTC](https://discourse.julialang.org/t/defining-constraint-using-jump-convert-string-to-expression-and-merge-quart-expr-and-nonlinear-expr/79967/13 "2022-05-02T04:43:40Z")

</div>

Thank You!
