# Differentiating through a QP - order of constraints

**URL:** <https://discourse.julialang.org/t/differentiating-through-a-qp-order-of-constraints/119275>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, diffopt\
**Created:** [September 11, 2024, 9:48am UTC](https://discourse.julialang.org/t/differentiating-through-a-qp-order-of-constraints/119275 "2024-09-11T09:48:53Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)\
**Post date:** [September 13, 2024, 7:03am UTC](https://discourse.julialang.org/t/differentiating-through-a-qp-order-of-constraints/119275/3 "2024-09-13T07:03:46Z")

</div>

Thanks for the reply. I am not sure this is the problem, I don’t want to differentiate simultaneously. What I think is happening is when I do this:

```julia
    for cs in constr
        MOI.set(
            model,
            DiffOpt.ForwardConstraintFunction(),
            cs,
            0.0 * index(model[:x][1]) - 1.0, # to indicate the direction vector to get directional derivatives
        )
        DiffOpt.forward_differentiate!(model)
        dwdb = MOI.get.(model, DiffOpt.ForwardVariablePrimal(), model[:x])
        push!(dwdbOut, dwdb)
    end

```

in every iteration DiffOpt wants to differentiate through the current `cs` and that works in the first iteration. In the second iteration I would like to differentiate through the second constraints in a given direction, but DiffOpt keeps the direction for the first constraint that I set in the first iteration. This would explain why changing the order of constraints in the differentiation changes the results.

My current workaround (inspired by [this example](https://jump.dev/DiffOpt.jl/dev/examples/sensitivity-analysis-svm/#Gradient-of-hyperplane-wrt-the-data-point-coordinates)) is

```julia
MOI.set.(
    model,
    DiffOpt.ForwardConstraintFunction(),
    [cons1;cons2],
    [0.0 * index(x[1]) - 1.0;0.0 * index(x[1]) ], # to indicate the direction vector to get directional derivatives
)

DiffOpt.forward_differentiate!(model)
dxh1 = MOI.get.(model, DiffOpt.ForwardVariablePrimal(), x)

MOI.set.(
    model,
    DiffOpt.ForwardConstraintFunction(),
    [cons1;cons2],
    [0.0 * index(x[1]) ;0.0 * index(x[1]) - 1.0 ], # to indicate the direction vector to get directional derivatives
)

DiffOpt.forward_differentiate!(model)
dxh3 = MOI.get.(model, DiffOpt.ForwardVariablePrimal(), x)

```

---

_[View the full topic](https://discourse.julialang.org/t/differentiating-through-a-qp-order-of-constraints/119275)._
