Skip to content

Adding Equality constraint breaks variables / gradients computation #16

Description

@JeremyBois

Hi,

Description

Adding an equality constraint silently break the optimization. Independent variables becomes NaN for all iteration steps (excepting the first one) breaking gradient computation too.

Notes

  • I can add inequality constraints
  • I test writing equality constraints with delegate, lambda, class method --> Same result
  • I'm using latest version of NLoptNet
  • Tried with NLopt 2.6.2 and 2.6.1 --> Same result

Minimal breaking example

Below the minimal code that can be used to reproduce the behaviour.

public void TestSLSQP()
{
    var gradientsHistory = new List<double[]>();
    var variablesHistory = new List<double[]>();
    double? finalScore;
    NloptResult result;

    using (var solver = new NLoptSolver(NLoptAlgorithm.LD_SLSQP, 2, maximumIterations: 20))
    {
        var initialValue = new[] { 1.234, 5.678 };
        solver.SetLowerBounds(new[] { double.NegativeInfinity, 0.0000000000001 });

        int count = 0;
        solver.SetMinObjective((variables, gradient) =>
        {
            if (gradient != null)
            {
                gradient[0] = 0.0;
                gradient[1] = variables[1];
            }
            count++;

            var iterationGradients = new double[2];
            Array.Copy(gradient, iterationGradients, 2);
            gradientsHistory.Add(iterationGradients);

            var iterationValues = new double[2];
            Array.Copy(variables, iterationValues, 2);
            variablesHistory.Add(iterationValues);

            return variables[1];
        });

        // @BUG This is the line that broke everything
        solver.AddEqualZeroConstraint(TestEq);
        // @BUG This is the line that broke everything

        result = solver.Optimize(initialValue, out finalScore);
    }
}

private double TestEq(double[] values, double[] grads)
{
    return values.Length + grads.Length;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions