mathnet / mathnet-numerics

Math.NET Numerics
http://numerics.mathdotnet.com
MIT License
3.49k stars 897 forks source link

How to write the equality and inequality constraints for optimization #945

Open lsylusiyao opened 2 years ago

lsylusiyao commented 2 years ago

Currently, I'm writing a quadratic minimization problem that looks like this (an example from matlab) 67bac9fbac258ad1580ced80597754b

However, I don't know how to express the inequality constraint c <= Ax <= b (this is simplified here as 0 <= x <= 1 but my problem is a little complex than that) and the equality constraint sum(x) = 1 / 2. I didn't find any of this kind in the docs, examples, and unit tests. Thanks

Arlofin commented 1 year ago

For inequalities: If A is invertible, just rephrase your problem by setting x' = Ax and solving for x'. Or use the inequality constraint 1/A c <= x <= 1/A b, which generalizes to the case that A is not invertible if you appropriately split up the constraints into the invertible and the non-invertible part (the latter are trivial, i.e. absence of constraint). For equalities: Mathematically, an equality Ax = b is equivalent to the inequality b <= Ax <= b. It is in general not advised to use this approach with numeric solvers as it may be unstable (but it still helps sometimes for a quick and dirty solution). Ideally, you would be able to first solve the equations and restrict your optimization problem to the subspace defined by them. E.g. in your example, when you first apply a shift by (-1/2, 0, 0) you can eliminate x3 since the equation can then be read as -x1 - x2 = x3.