# Solving an SDP repeatedly where the matrix data changes in each repetition

**URL:** https://discourse.julialang.org/t/solving-an-sdp-repeatedly-where-the-matrix-data-changes-in-each-repetition/123271
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [November 30, 2024, 2:22am UTC](https://discourse.julialang.org/t/solving-an-sdp-repeatedly-where-the-matrix-data-changes-in-each-repetition/123271 "2024-11-30T02:22:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)
#### Post date: [November 30, 2024, 2:22am UTC](https://discourse.julialang.org/t/solving-an-sdp-repeatedly-where-the-matrix-data-changes-in-each-repetition/123271/1 "2024-11-30T02:22:45Z")

</div>

Dear All,

I am trying to solve the following semidefinite optimization problem in `JuMP` that I want to solve repeatedly:

\begin{align\*} p^{\star}(\alpha)=\text{minimize}\_{X,y} &\; \mathbf{tr}CX+\sum\_{i=1}^{n}c\_{i}y\_{i}\\ \text{subject to } & X\succeq0,\\ & X+\sum\_{i=1}^{n}y\_{i}A\_{i}(\alpha) + B(\alpha) \succeq0 ,\\ & X\in\mathbb{S}^{m\times m},\;y\in\mathbb{R}^{n}, \end{align\*}

where in each repetition, the model stays the same except the symmetric matrices A\_{i}(\alpha), B\_i(\alpha) change, which in turn depends on a vector \alpha\in\mathbb{R}^{p}. The vector \alpha changes for each repetition and the rules that generate the matrices A\_{i}(\alpha), B\_{i}(\alpha) from \alpha is fairly complicated (solution to another SDP). In my current implementation, I am rebuilding the model from scratch every time I solve it for a different \alpha, which seems to be expensive. What would be the best way of solving such a problem in `JuMP` without recreating the model every time?

---

<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: [November 30, 2024, 5:42am UTC](https://discourse.julialang.org/t/solving-an-sdp-repeatedly-where-the-matrix-data-changes-in-each-repetition/123271/3 "2024-11-30T05:42:05Z")

</div>

What solver? Have you benchmarked the overhead of creating the JuMP model?

---

<div class="post-metadata">

### Author: ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)
#### Post date: [November 30, 2024, 5:29pm UTC](https://discourse.julialang.org/t/solving-an-sdp-repeatedly-where-the-matrix-data-changes-in-each-repetition/123271/5 "2024-11-30T17:29:06Z")

</div>

I am using Mosek. No, I have not benchmarked, but this SDP solution is being passed to Gurobi as part of a heuristic solution, so I want the solution time to be as small as possible. Building the model every time I call the heuristic solution will be a bottleneck for my use case.

---

<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: [November 30, 2024, 9:46pm UTC](https://discourse.julialang.org/t/solving-an-sdp-repeatedly-where-the-matrix-data-changes-in-each-repetition/123271/6 "2024-11-30T21:46:37Z")

</div>

I’d benchmark it first. You can also improve things by formulating the model close to the form that Mosek expects.

Use `print_active_bridges(model)` to see how we’re reformulating. Here’s a tutorial: [Example: ellipsoid approximation · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/conic/ellipse_approx/#Alternative-formulations)

You probably need something like this:

```julia
model = Model(Mosek.Optimizer)
@variable(model, X[1:m, 1:m], PSD)
@variable(model, y[1:n])
@variable(model, Z[1:m, 1:m], PSD)
@constraint(model, c_con, X + sum(y[i] * A[i] for i in 1:m) + B .== Z)

```

You could also benchmark doing something like:

```Julia
for i in 1:m
    set_normalized_coefficient.(c_con, y[i], A_new[i])
end
set_normalized_rhs.(c_con, -B_new)

```

Note that you need `-B` because JuMP will move it to the right-hand side.

---

<div class="post-metadata">

### Author: ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)
#### Post date: [November 30, 2024, 10:47pm UTC](https://discourse.julialang.org/t/solving-an-sdp-repeatedly-where-the-matrix-data-changes-in-each-repetition/123271/7 "2024-11-30T22:47:53Z")

</div>

Okay sounds great, thanks very much @odow !
