How to minimize a non-linear function with constraints in c#?

0

I would like to minimize the following function

enter image description here

with constraints

$$w_i \geq 0, \sqrt{w_1^2 + w_2^2} = 1$$

in C#. I tried to do it with Math.Net's Newton Method, but I can't figure out how to do it. How can I minimize the function programmatically in C# for given $F_1, F_2$?

Update: After the comment of @MinosIllyrien I tried the following, but I don't get the syntax:

_f1 = 0.3; // Global fields.
_f2 = 0.7;

var minimizer = new NewtonMinimizer(1E-4, 100, false);
var objectiveFunction = ObjectiveFunction.ScalarDerivative(FunctionToMinimize, GradientOfFunctionToMinimize);
var firstGuess = CreateVector.DenseOfArray(new[] {0.5});
var minimalWeight1 = minimizer.FindMinimum(objectiveFunction, firstGuess).MinimizingPoint;

private double GradientOfFunctionToMinimize(double w1){
  return _f1 - (w1 * _f2) / Math.Sqrt(1 - Math.Pow(w1, 2));
}

private double FunctionToMinimize(double w1){
  return w1 * _f1 + Math.Sqrt(1 - Math.Pow(w1, 2)) * _f2;
}

This does not work, because FindMinimum method requires IObjectiveFunction as function and not IScalarObjectiveFunction...

Update 2: I tried a solution from Google:

var solver = Solver.CreateSolver("GLOP");
Variable w1 = solver.MakeNumVar(0.0, double.PositiveInfinity, "w1");
Variable w2 = solver.MakeNumVar(0.0, double.PositiveInfinity, "w2");

solver.Add(Math.Sqrt(w1*w1 + w2*w2) == 1);

This throws the error that *-operator cannot be used for "Variable" and "Variable". Someone any ideas?

1

0

w₁² + w₂² = 1 is basically the unit circle. The unit circle can also be described by the following parametric equation:

(cos t, sin t)

In other words, for every pair (w₁, w₂), there is an angle t for which w₁ = cos t and w₂ = sin t.

With that substitution, the function becomes:

y = F₁ cos t + F₂ sin t

w₁ ≥ 0, w₂ ≥ 0 restricts t to a single quadrant. This leaves you with a very simple constraint, that consists of a single variable:

0 ≤ t ≤ ½π

By the way, the function can be simplified to:

y = R cos(t - α)

where R = √(F₁² + F₂²) and α = atan2(F₂, F₁)

This is a simple sine wave. Without the constraint on t, its range would be [-R, R], making the minimum -R. But the constraint limits the domain and thereby the range:

  • If F₁ < 0 and F₂ < 0, then the minimum is at w₁ = -F₁ / R, w₂ = -F₂ / R, with y = -R
  • For 0 < F₁ ≤ F₂, a minimum is at w₁ = 1, w₂ = 0, with y = F
  • For 0 < F₂ ≤ F₁, a minimum is at w₁ = 0, w₂ = 1, with y = F

Notes:

  • if F₁ = F₂ > 0, then you have two minima.
  • if F₁ = F₂ = 0, then y is just flat zero everywhere.

In code:

_f1 = 0.3;
_f2 = 0.7;

if (_f1 == 0.0 && _f2 == 0.0) {
    Console.WriteLine("Constant y = 0 across the entire domain");
}
else if (_f1 < 0.0 && _f2 < 0.0) {
    var R = Math.sqrt(_f1 * _f1 + _f2 * _f2);
    Console.WriteLine($"Minimum y = {-R} at w1 = {-_f1 / R}, w2 = {-_f2 / R}");
}
else {
    if (_f1 <= _f2) {
        Console.WriteLine($"Minimum y = {_f1} at w1 = 1, w2 = 0");
    }
    if (_f1 >= _f2) {
        Console.WriteLine($"Minimum y = {_f2} at w1 = 0, w2 = 1");
    }
}
2021-11-26 10:11:03

You'll also need to check that both w1 and w2 are non-negative. If not, the solution will be either (0,1) or (1,0).
Mark Pattison

@MarkPattison Thank you for pointing this out; I edited my answer accordingly.
Ruud Helderman

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................