Advice on using ModelingToolkit to solve Lagrange Problems

eqs = [ 0 ~ 5x-f, 0 ~ -log(x)-f]

should be

eqs = [ 0 ~ -5x-f, 0 ~ -log(x)-f]

but that creates a problem with complex numbers.

There is no solution if it’s -5x.

I suggest you step back a bit and come up with the full formulation (on paper) of the problem you are trying to solve. In addition, take a look at the documentation for JuMP and Modeling Toolkit. There are lots of examples and tutorials.

If there specific questions with syntax, people may be able to help. But it’s hard to provide general advice about how to plot a solution or interact with data.

OK, that is the problem. I might get an intermediate econ textbook, to come up with a better example.

1 Like

here are my two equations

eqs = [ u ~ 16log(x_1)+9log(x_2), 100 ~ (x_1^2/100)+(x_2^2/36)]

the variables are u x_1 and x_2, and i want to maximize u which is a function u(x_1,x_2)

julia> using JuMP, Ipopt

julia> function solve()
           model = Model(Ipopt.Optimizer)
           @variable(model, x[1:2] >= 0.001, start = 1)
           @NLobjective(model, Max, 16 * log(x[1]) + 9 * log(x[2]))
           @NLconstraint(model, x[1]^2 / 100 + x[2]^2 / 36 == 100)
           optimize!(model)
           @assert termination_status(model) == LOCALLY_SOLVED
           return value.(x)
       end
solve (generic function with 1 method)

julia> solve()
This is Ipopt version 3.13.4, running with linear solver mumps.
NOTE: Other linear solvers might be more efficient (see Ipopt documentation).

Number of nonzeros in equality constraint Jacobian...:        2
Number of nonzeros in inequality constraint Jacobian.:        0
Number of nonzeros in Lagrangian Hessian.............:        4

Total number of variables............................:        2
                     variables with only lower bounds:        2
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:        1
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  0.0000000e+00 1.00e+02 1.19e+01  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1  1.6922650e+02 7.28e+04 6.03e+07  -1.0 1.58e+03    -  6.27e-04 1.00e+00f  1
   2  1.5946715e+02 1.86e+04 3.79e+08  -1.0 8.15e+02   6.0 1.00e+00 1.00e+00h  1
   3  1.4227187e+02 4.62e+03 9.46e+07  -1.0 3.78e+02    -  1.00e+00 1.00e+00h  1
   4  1.2546702e+02 1.13e+03 2.36e+07  -1.0 1.87e+02    -  1.00e+00 1.00e+00h  1
   5  1.1009062e+02 2.60e+02 5.87e+06  -1.0 8.97e+01    -  1.00e+00 1.00e+00h  1
   6  9.8892524e+01 4.69e+01 1.35e+06  -1.0 3.81e+01    -  1.00e+00 1.00e+00h  1
   7  9.4544237e+01 3.74e+00 1.82e+05  -1.0 1.08e+01    -  1.00e+00 1.00e+00h  1
   8  9.4089100e+01 3.38e-02 3.22e+03  -1.0 1.02e+00    -  1.00e+00 1.00e+00h  1
   9  9.4091188e+01 5.90e-06 7.70e-01  -1.0 1.33e-02    -  1.00e+00 1.00e+00h  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  10  1.0032943e+02 5.03e+00 1.77e-01  -1.7 2.08e+01    -  7.29e-01 1.00e+00f  1
  11  1.0359036e+02 1.03e+01 2.84e-02  -1.7 2.50e+01    -  1.00e+00 1.00e+00f  1
  12  1.0239742e+02 2.68e-01 4.61e-03  -1.7 2.93e+00    -  1.00e+00 1.00e+00h  1
  13  1.0236419e+02 7.69e-04 1.14e-05  -1.7 2.52e-01    -  1.00e+00 1.00e+00h  1
  14  1.0236410e+02 6.26e-06 1.01e-07  -3.8 1.47e-02    -  1.00e+00 1.00e+00h  1
  15  1.0236410e+02 4.59e-10 2.27e-11  -5.7 1.26e-04    -  1.00e+00 1.00e+00h  1
  16  1.0236410e+02 5.68e-14 2.56e-14  -8.6 1.29e-06    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 16

                                   (scaled)                 (unscaled)
Objective...............:  -1.0236409660088709e+02    1.0236409660088709e+02
Dual infeasibility......:   2.5600289097858465e-14    2.5600289097858465e-14
Constraint violation....:   5.6843418860808015e-14    5.6843418860808015e-14
Complementarity.........:   2.5059564600335962e-09   -2.5059564600335962e-09
Overall NLP error.......:   2.5059564600335962e-09    5.6843418860808015e-14


Number of objective function evaluations             = 17
Number of objective gradient evaluations             = 17
Number of equality constraint evaluations            = 17
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 17
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 16
Total CPU secs in IPOPT (w/o function evaluations)   =      0.009
Total CPU secs in NLP function evaluations           =      0.000

EXIT: Optimal Solution Found.
2-element Vector{Float64}:
 79.99999999824524
 36.00000000140383
2 Likes

@ChrisRackauckas I got JuMP to work but MTK is a mess, I need to have 3 equations for 3 variables, and I’m not sure if there is a way to specify which equation is maximized.

but MTK is a mess

That’s a little uncharitable. JuMP and MTK serve different use-cases. If you’re doing constrained nonlinear optimization and JuMP works, use JuMP.

2 Likes

I meant my code in MTK. I didn’t mean to imply a problem with MTK.

Is there a way to plot this?

What do you mean plot? The solution is a vector with two elements? It’s up to you how you plot the solution:

https://github.com/JuliaPlots/Plots.jl

1 Like