# Formulating objective function, DimensionMismatch

**URL:** https://discourse.julialang.org/t/formulating-objective-function-dimensionmismatch/109016
**Category:** Optimization (Mathematical)
**Tags:** question, jump
**Created:** [January 19, 2024, 4:17pm UTC](https://discourse.julialang.org/t/formulating-objective-function-dimensionmismatch/109016 "2024-01-19T16:17:15Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![legio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/legio/32/206356_2.png) [@legio](https://discourse.julialang.org/u/legio)
#### Post date: [January 19, 2024, 4:17pm UTC](https://discourse.julialang.org/t/formulating-objective-function-dimensionmismatch/109016/1 "2024-01-19T16:17:16Z")

</div>

Hi, I’m very new to Julia, and I am trying to construct a minimization over x for the following objective:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/c/5cd28e74bde7dc144fdfb36f83071937d5a1b128.png)

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):

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

```

---

<div class="post-metadata">

### Author: ![gabo-di](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabo-di/32/206427_2.png) [@gabo-di](https://discourse.julialang.org/u/gabo-di)
#### Post date: [January 19, 2024, 5:04pm UTC](https://discourse.julialang.org/t/formulating-objective-function-dimensionmismatch/109016/2 "2024-01-19T17:04:07Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [January 19, 2024, 5:16pm UTC](https://discourse.julialang.org/t/formulating-objective-function-dimensionmismatch/109016/3 "2024-01-19T17:16:54Z")

</div>

If you can make `μ` a vector you could just write

```julia
@objective(model, Min, (B*x - μ)'*(B*x - μ))

```

---

<div class="post-metadata">

### Author: ![legio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/legio/32/206356_2.png) [@legio](https://discourse.julialang.org/u/legio)
#### Post date: [January 19, 2024, 5:24pm UTC](https://discourse.julialang.org/t/formulating-objective-function-dimensionmismatch/109016/4 "2024-01-19T17:24:18Z")

</div>

~~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.

---

<div class="post-metadata">

### Author: ![legio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/legio/32/206356_2.png) [@legio](https://discourse.julialang.org/u/legio)
#### Post date: [January 19, 2024, 5:26pm UTC](https://discourse.julialang.org/t/formulating-objective-function-dimensionmismatch/109016/5 "2024-01-19T17:26:39Z")

</div>

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!

---

<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: [January 19, 2024, 10:05pm UTC](https://discourse.julialang.org/t/formulating-objective-function-dimensionmismatch/109016/6 "2024-01-19T22:05:19Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![legio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/legio/32/206356_2.png) [@legio](https://discourse.julialang.org/u/legio)
#### Post date: [January 19, 2024, 10:21pm UTC](https://discourse.julialang.org/t/formulating-objective-function-dimensionmismatch/109016/7 "2024-01-19T22:21:29Z")

</div>

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