# How does ModelingToolkit use bounds metadata?

Hi all,

What is the purpose of the bounds symbolic metadata in ModelingToolkit?

When I add bounds to `x` in the following code, I would at first expect the solution for x to be limited between 0 and 2, but this doesn’t seem to be the case.

``````@variables t, x(t) [bounds=(0, 2)]
D = Differential(t)

eqs = [D(x) ~ x]

@named sys = ODESystem(eqs)
prob = ODEProblem(sys, [1], (0.0, 2.0))
sol = ModelingToolkit.solve(prob)

sol.u
# 7-element Vector{Vector{Float64}}:
#  [1.0]
#  [1.105193012902056]
#  [1.4162610240705922]
#  [1.9799122739515003]
#  [2.969677774531698]
#  [4.82849926047279]
#  [7.389050121897671]
``````

Is there another purpose for the bounds metadata that I am not aware of?

As an aside, the purpose of this code was to limit the values variables can take in intermediate steps. I’ve tried using `PositiveDomain()` and other callback functions to achieve the same result, but this doesn’t work for functions with undefined domains (such as negative values for `log(x)`). Is there a way to prevent negative values from occurring for variables at any point in the intermediate calculations?

Thanks.

They aren’t quite used yet, but the idea is for it to be used to set the bounds in an OptimizationSystem, or in any extension that adds a parameter estimation module. We haven’t thought of handling it on the states yet, but that is something that could be done. But yes, it’s documented how it’s used because it’s just not used yet.

@ChrisRackauckas Is there any way to include bounds on the solution in `ModelingToolking` at the moment ? That would be really nice to be able to constraint the search space !

Just put bounds in the OptimizationSystem? The MTK integration with that is improving so I wouldn’t be surprised if that’s handled soon. But getting the mixed integer stuff done was the first priority there.

Hmm, so just to be sure, you mean that a `NonlinearSystem` can only solve for an unconstrained optimization while the `OptimizationSystem` can handle constraints ?

Yes.

Just quickly changing my `NonlinearSystem` with multiple equations, it seems that `OptimizationSystem` does not support multiple equations, is that right ? I get a `ERROR: type OptimizationSystem has no field eqs`

Yes it’s for optimization, not for solving a system of nonlinear equations. It sounds like your problem is unrelated to this thread.

Well, my problem is related to how ModelingToolkit handles bounds (since I first thought that they were taken into account in a `NonlinearSystem`, though I agree my specific case may be quite different from the original post. My problem involves solving a nonlinear system of equations, but I want to constraint some variable space to avoid non-physical solutions that may arise

That’s worth an issue in NonlinearSolve.jl. The nonlinear solvers don’t allow bounds, though in theory they could as a way to set the starting trust region for TR methods. So we could put that together, and then MTK could use the bounds information for NonlinearSystem. Worth an issue and its a good time to bring it up as we are doing a NonlinearSolve push this summer so it could be done by September.

1 Like

When you say that nonlinear solvers do not allow bounds, what do you mean exactly. I’m not familiar with what you put in there (besides Newton like methods), but where do you put constrained Linear Complementary Problems or Quadratic Programming Problems solvers ? Do you consider these nonlinear solvers (I guess I would) ?

Those methods aren’t in NonlinearSolve.jl. Hence the issue.

Ah you meant literally “NonLinearSolve” solvers, sorry.
PS : While we are at it, `ModelingToolkit` and all the gear around it is really an incredible piece of software, you did a wonderful job with that !

Thanks! It’s still early years but I think there’s a nice clear path for it and we’ve moving fast .

You can already do that, see Modeling Optimization Problems · ModelingToolkit.jl

1 Like

Forgot, yes that’s complete.

Hi, I’m doing a bit of cleanup, do you agree that I can close Consider supporting bounds for `NonlinearProblem` resolution · Issue #183 · SciML/NonlinearSolve.jl · GitHub then ?

Doing it in NonlinearSolve isn’t complete and something we should consider doing in the future. I’d keep it open but we probably won’t target it in the short term.