Formulating objective function, DimensionMismatch

Hi, I’m very new to Julia, and I am trying to construct a minimization over x for the following objective:

where B is a 2x8 matrix, μ is a 2x3 matrix, and x is an 8x3 matrix.

I have tried using the following line to define my objective:
@objective(model, Min, x' * (B' * B) * x - 2 * x' * B' * μ)
Edit (fixed objective):
@objective(model, Min, x' * B' * B * x - μ' * B * x - x' * B' * μ)

However, I keep getting the following error:
DimensionMismatch: array could not be broadcast to match destination
Edit: fixed objective, new error:
The objective function `QuadExpr[x[1,1]² + 2 x[2,1]*x[1,1] ... ]` is not supported by JuMP.
For reference, here is what I see when I print out x, B, and μ (with dummy values):

x: VariableRef[x[1,1] x[1,2] x[1,3]; x[2,1] x[2,2] x[2,3]; x[3,1] x[3,2] x[3,3]; x[4,1] x[4,2] x[4,3]; x[5,1] x[5,2] x[5,3]; x[6,1] x[6,2] x[6,3]; x[7,1] x[7,2] x[7,3]; x[8,1] x[8,2] x[8,3]]

B: [1 1 1 0 0 0 0 0; 0 0 0 1 1 1 1 1]

μ: [2.5 2.5 2.5; 1.5 1.5 1.5]

Maybe you can check your theoretical model? If you are interested in the Frobenius norm you are not using the right equation. In case it is not that what you want, look that your output is a 3x3 matrix, while most optimization problems require a one scalar output, unless you deal with multiobjective optimization.


If you can make μ a vector you could just write

@objective(model, Min, (B*x - μ)'*(B*x - μ))
1 Like

Unless I’m missing something, I should be able to express the squared Euclidean norm as an inner product <Bx - μ, Bx - μ>, which should simplify to the objective?
Thanks for your reply! I checked, and the first term in my objective becomes a 3x3 QuadExpr, and second term is a 3x3 AffineExpr. They seem to be combining okay, but for some reason, JuMP still won’t accept it.

Thank you for your response. I tried this, and my issue now becomes:
The objective function `QuadExpr[x[1,1]² + 2 x[2,1]*x[1,1] ... ]` is not supported by JuMP.

Am I still formulating something wrong?

Edit: I think it works if I sum over the entire objective; huge thanks to everyone who responded!

Hi @legio, welcome to the forum. Nice to see you got it fixed.

Just to chime in and say that @gabo-di’s answer is correct. The problem is that your objective function is a 3x3 matrix. JuMP cannot set a matrix as an objective function. It must be a scalar, or a vector (if you are trying to solve a multi-objective optimization problem).

1 Like

Thanks! I fixed the issue by summing over the matrix : )

1 Like