MatthewPeterKelly / OptimTraj

A trajectory optimization library for Matlab
MIT License
598 stars 207 forks source link

Error when running toyCar in octave #42

Open samlaf opened 3 years ago

samlaf commented 3 years ago

Hi, I am running toyCar's MAIN.m file, and get this error

error: pivot is zero
error: called from
    gjp at line 42 column 5
    cpiv_bard at line 76 column 4
    __lm_feasible__ at line 302 column 31
    fmincon at line 417 column 24
    directCollocation at line 95 column 33
    trapezoid at line 74 column 10
    optimTraj at line 180 column 24

I am wondering if this is due to me using octave, or if there is a fundamental bug happening. Another "fix" I've had to do is to add

'GradObj', 'off',...
'GradConstr', 'off'

options in optimset in GetDefaultOptions.m. Otherwise directCollocation.m's line 26 flagGradObj = strcmp(Opt.nlpOpt.GradObj,'on'); was raising an error.

Not sure if this is also an issue due to octave or if it's actually an issue with the code.

MatthewPeterKelly commented 3 years ago

OptimTraj is not designed to run in Octave. At the time of writing, it didn't even run at all (no implementation for fmincon). That being said, it looks like someone implemented it, because the code gets farther than it used to!

Do you get a similar error on all of the other example problems?

My guess is that the Octave implementation of fmincon doesn't match all of the internal interface details (like how options are handled) and that is where your problem is coming from. Most of the examples in OptimTraj will default to using finite difference gradients, so you might have more luck with those ones.

samlaf commented 3 years ago

Hi Matthew, thank you for this very nice library, and for answering my message.

There is indeed an optim library in Octave now, which contains fmincon (though it needs to be loaded with pkg load optim). I think the main difference is fmincon's default solver, which doesn't accept initial guesses that don't respect the constraints (which mostly all of your examples do!). So I changed the default solver to octave-sqp. It worked for this example, but still raises issues for other examples (I only tried the minwork example, and got discouraged.)

How/Where do I set finite difference gradients?

MatthewPeterKelly commented 3 years ago

The minimumWork example is a particularly tricky one to solve. I suggest starting with one of the following: cartPole, quadRotor2d, orsimplePendulum`.

Octave SQP sounds like a reasonable choice - good luck!

By default, OptimTraj will solve using finite difference gradients. They are not computed directly by OptimTraj though -- they are done automatically inside of FMINCON. You can provide an analytic gradient, which is better to do if you can.

If the Octave solver requires that you provide a gradient, then I suggest that you try to go through the analytic gradient route. Look at the example code in fiveLinkBiped. The gradients for the optimization are a bit tricky -- there is a lot of "vectorization" going on to make things run fast enough.