Defining constraints using JuMP

while defining constraints in a nonlinear optimization problem ,I face this error:
“MethodError: no method matching *(::DataFrame, ::Vector{VariableRef}))”

can anyone help?

here’s part of my code:

s_matrix_df=DataFrames.DataFrame(s_matrix)
@variable(model,v[1:(size(s_matrix_df,2)-1)]>=0)
@constraint(model,s_matrix_df[:,2:end]*v ==0)```
1 Like

A couple of questions:

  • Why do you wrap your matrix in a DataFrame if you then use it as a matrix afterwards? I don’t think that linear algebra on dataframes is supported in this way.
  • Your constraint asks that a matrix-vector product (result being a vector) should equal 0 (a scalar). This is a dimension mismatch, so you could use a zero vector on the right-hand side, or use .== for broadcasting.
  • You can also make the matrix-vector product explicit with the dot function from LinearAlgebra.
1 Like

Documentation: Constraints · JuMP

First of all thanks a lot for noticing my question :pray:
I’m new to julia and JuMP and all my experience using them has been just a couple of days

Here are my answers to your questions:

1.This matrix was a csv file which I read using CSV and converted it to a DataFrame because in other constraints I need to select some special columns (based on column name).Having worked with pandas in python , I did so. If there is a better way of doing this please inform me.

2.The error I’ve mentioned is not related to the . as I played with the code and deleted and wrote the dot ,but I got same error!(I’ve just accidentally copied the “without dot” version here)

3.Thank you so much ,dot worked for me!!

there’s a problem with another constraint which is nonlinear:
“Unexpected array VariableRef in nonlinear expression. nonlinear expressions may contain only scalar expressions”

here is the constraint:
@NLconstraint(model,[sum(c[j]*v[indx_of_P[j]]) for j in 1:length(c)] ==uptake*g )

I tried to define a function, but it didn’t work
f(x,y)=sum(x[j]*y[indx_of_P[j]] for j in 1:length(x))
@NLconstraint(model,f(c,v)==uptake*g )

Re 2: I expected this error to show up “next”.

For your new question: I think you’ll need to create multiple constraints, one for each component. This is also mentioned in a later part of the documentation.

1 Like