# Lagrangian Function

**URL:** <https://discourse.julialang.org/t/lagrangian-function/38287>\
**Category:** Optimization (Mathematical)\
**Created:** [April 27, 2020, 8:01am UTC](https://discourse.julialang.org/t/lagrangian-function/38287 "2020-04-27T08:01:26Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [April 27, 2020, 8:01am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/1 "2020-04-27T08:01:26Z")

</div>

Hello everyone,

I am trying to solve an optimization problem by using the lagrangian function, which package is best suited for this task specially because I need the equality constraint multipliers to be returned as they represent a physical quantity that I need to measure

best regards ![1](https://global.discourse-cdn.com/julialang/original/3X/3/f/3f35f8efc2382002b137f35bbbf1312887d55321.png)

---

<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:** [April 27, 2020, 8:15am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/2 "2020-04-27T08:15:45Z")

</div>

Not an expert in this area but maybe have a look at JuMP, there’s a discussion on Lagrange multipliers here: [Lagrange multipliers (duals) in JuMP 0.19](https://discourse.julialang.org/t/lagrange-multipliers-duals-in-jump-0-19/28328)

---

<div class="post-metadata">

**Author:** ![thisrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thisrod/32/10642_2.png) [@thisrod](https://discourse.julialang.org/u/thisrod)\
**Post date:** [April 27, 2020, 8:24am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/3 "2020-04-27T08:24:40Z")

</div>

Hey there

> [@mayar](#):
>
> by using the lagrangian function

Do you mean that you want to solve a problem in mechanics by numerically minimizing the action as a function of the trajectory? Or are you solving some other kind of constrained optimization problem by the method of Lagrange multipliers? I’m going to guess the latter, please say if that’s wrong.

I don’t know of a numerical method that uses Lagrange multipliers the way you would with pen and paper. However, if you know the minimizer, you should be able to find the gradients of the cost and the constraint functions using [ForwardDiff](https://github.com/JuliaDiff/ForwardDiff.jl). The Lagrange multipliers are just the coefficients of the cost gradient expanded over the constraint gradients, right?

The stock answer to numerical optimization in Julia is [Optim](https://julianlsolvers.github.io/Optim.jl/stable/#algo/manifolds/). I’ve linked to the section of the manual that says how to apply constraints. This is still under development, and you might need to write your own functions to project (tangent) vectors onto the manifold that satisfies your constraint. When I’ve struggled with Optim, the developers have been very helpful.

I hope that is what you were looking for.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [April 27, 2020, 9:45am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/4 "2020-04-27T09:45:40Z")

</div>

It is not clear from your question which symbols are the decision variables and which are constants, and which terms come from equality constraints and which come from inequality constraints. These will determine which package to use, e.g. a linear programming solver, a convex optimization solver or a generic nonlinear optimization solver. JuMP + Ipopt or JuMP + NLopt are probably good places to start if you are new to Julia as the syntax is easy. Optim also has a constrained optimization solver but using it is a bit harder.

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [April 27, 2020, 10:58am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/5 "2020-04-27T10:58:27Z")

</div>

I am solving a power system problem…The objective function, equality and inequality constraints are linear

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [April 27, 2020, 10:59am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/6 "2020-04-27T10:59:27Z")

</div>

but I care about the multiplier of equality and inequality constraints so I want a solver that can enable me to get their values

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [April 27, 2020, 11:05am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/7 "2020-04-27T11:05:34Z")

</div>

my problem is related to power system and it is linear and I am interested in the method of lagrange multiplier as the values of mutlipliers reflect the price of power.

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [April 27, 2020, 11:08am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/8 "2020-04-27T11:08:18Z")

</div>

![xx](https://global.discourse-cdn.com/julialang/original/3X/1/4/14d871375825fb427f55fb75144560a1c1391a13.png)  
This is the problem formulation  
P and theta are my variables, but D and zeta are given

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [April 27, 2020, 11:11am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/9 "2020-04-27T11:11:59Z")

</div>

Any linear programming solver can give you the Lagrangian multipliers without explicitly forming the Lagrangian function. For example, you can use JuMP with GLPK or Clp and use the `dual` function to get the dual variable for any constraint ([Query Solutions · JuMP](http://www.juliaopt.org/JuMP.jl/v0.21.1/solutions/#JuMP.dual)).

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [April 27, 2020, 11:12am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/10 "2020-04-27T11:12:28Z")

</div>

Okaay…I will check

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [April 27, 2020, 11:14am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/11 "2020-04-27T11:14:13Z")

</div>

Cant I use it with Cbc ??

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [April 27, 2020, 11:15am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/12 "2020-04-27T11:15:54Z")

</div>

You could. Cbc is an extension of Clp for mixed integer optimization.

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [April 27, 2020, 11:17am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/13 "2020-04-27T11:17:05Z")

</div>

I cant define the error

 ![d](https://global.discourse-cdn.com/julialang/original/3X/e/3/e38d9873fdbeb802ac6690e49204568177620e4f.png) here

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [April 27, 2020, 11:17am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/14 "2020-04-27T11:17:59Z")

</div>

![line62](https://global.discourse-cdn.com/julialang/original/3X/b/7/b714d4d5e31a7ae19d53804a207c9887f9426f9f.png)  
line 62

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [April 27, 2020, 11:19am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/15 "2020-04-27T11:19:44Z")

</div>

Ya for the dual, you might want to use Clp. I haven’t used these stuff for a while so you will get a better response from experts if you change the domain of the topic to #domain:opt.

---

<div class="post-metadata">

**Author:** ![mayar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mayar/32/16426_2.png) [@mayar](https://discourse.julialang.org/u/mayar)\
**Post date:** [April 27, 2020, 11:20am UTC](https://discourse.julialang.org/t/lagrangian-function/38287/16 "2020-04-27T11:20:38Z")

</div>

Thanks alot

---

<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:** [April 27, 2020, 12:34pm UTC](https://discourse.julialang.org/t/lagrangian-function/38287/17 "2020-04-27T12:34:22Z")

</div>

I’ve gone ahead and changed the domain

---

<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:** [April 27, 2020, 12:42pm UTC](https://discourse.julialang.org/t/lagrangian-function/38287/18 "2020-04-27T12:42:13Z")

</div>

Cbc is for mixed-integer problems. It doesn’t compute duals. Use Clp or GLPK.

Depending on the sign you expect from the Lagrangian, you may want to use `shadow_price` instead of `dual`: [Constraints · JuMP](https://www.juliaopt.org/JuMP.jl/stable/constraints/#JuMP.shadow_price)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 27, 2020, 1:04pm UTC](https://discourse.julialang.org/t/lagrangian-function/38287/19 "2020-04-27T13:04:43Z")

</div>

Note that, given any optimization algorithm, you can easily compute the Lagrange multipliers yourself given the optimum — it’s just a system of linear equations (from the KKT equations) given the gradients of the objective and constraints at the optimum, which you can solve with a backslash operation in Julia.

For example, if you are minimizing f\_0(x) subject to h\_i(x)=0 for i=1\ldots p, and your optimization algorithm gives you an approximate minimum x^\*, then to find the Lagrange multipliers \nu\_i for your constraints you would solve

\nu = \begin{bmatrix} \nabla h\_1 & \cdots & \nabla h\_m \end{bmatrix} \setminus -\nabla f\_0 

where we have a matrix whose columns are the constraint gradients (evaluated at x^\*) and `\` denotes the Julia backslash operation, which will solve the equations in the least-square sense assuming you have fewer constraints than variables x (this is necessary since x^\* will only be an approximate minimum).

If you have inequality constraints, it is the same thing except that you need only include active constraints in your linear system; the Lagrange multipliers of inactive constraints are zero (“complementary slackness”).
