Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradient Solvers Stopping After 1 iteration #1

Closed
BrendanVR opened this issue Nov 17, 2014 · 4 comments
Closed

Gradient Solvers Stopping After 1 iteration #1

BrendanVR opened this issue Nov 17, 2014 · 4 comments

Comments

@BrendanVR
Copy link

Not sure what I am doing wrong, but all gradient based algorithms are stopping after one iteration (including your example). Those that do not use gradient information work fine.

@BrannonKing
Copy link
Owner

You're going to have to be very specific on this. Post some failing code. Are you getting an exception? Is your solver going out of scope? What is the result of the optimize call? Do your algorithm choices require multiple parameters? Some require more than one. Some require that set_stopval be called, and I haven't exposed a way to do that yet. What is your platform?

@BrendanVR
Copy link
Author

I am sure this is probably something very embarrassing and minor on my end. Originally I thought the problem came from the fact I was calling your library from F# (and it was some cryptic interop issue). However, I tested the following in C#, using both .net 4.0 and 4.5 on a stock standard laptop running windows 8.1. Ideally I wish to utilize the LBFGS algorithm (which I don't think requires more parameters then I have specified)

namespace ConsoleApplication1
{
class Program
{

    static void Main()
    {
     using (var solver = new NLoptSolver(NLoptAlgorithm.LD_LBFGS, 1, 1e-8, 1000))
        {
            solver.SetMinObjective((variables, gradient) =>
            {
                if (gradient != null)
                    gradient[0] = 2.0 * (variables[0] - 3.0);
                var s1 = variables[0].ToString();
                Console.WriteLine(s1);
                return Math.Pow(variables[0] - 3.0, 2.0) + 4.0;
            });

            double? finalScore;
            var initialValue = new[] { 5.0 };
            var result = solver.Optimize(initialValue, out finalScore);
            var s2 = initialValue[0].ToString();
            Console.WriteLine(result.ToString());
            Console.WriteLine(s2);

        }   
    }
}

}
Which is just your boring vanilla example. When run this produces an output of success, without updating initialValue. The same occurs for any other gradient based algorithm (sometimes with XTol_Reached instead of success). However if I swap in a gradient free algorithm such as LN_COBYLA, everything works as advertised.

@BrannonKing
Copy link
Owner

Thanks for the report. There was an error in the wrapper for marshaling the gradient. It's been repaired. I've uploaded a new package. Try it.

@BrendanVR
Copy link
Author

It all seems to be working :). Thanks for that, now I can optimize things until my heart is content.

BrannonKing pushed a commit that referenced this issue Dec 15, 2021
Setup continuous integration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants