matt-weinstein / adigator

Matlab Algorithmic Differentiation Toolbox
22 stars 5 forks source link

Unable to use global variable #8

Open WJWest opened 4 years ago

WJWest commented 4 years ago

Hi ADiGator creators,

Thank you so much for the work that you put into this tool!

I am currently busy with my Masters degree where we are solving the minimum lap time optimal control problem for a Formula 1 car. We are using GPOPS as the optimal control solver and want to use ADiGator as the derivatives supplier.

As part of the problem we need to provide the track geometry so that the car does not exceed track limits. This is mostly a lookup-table type of exercise (seeing that the track geometry will not change but the number of points at which we need to evaluate the track changes with each iteration).

In a simple one-lap problem GPOPS will make approximately 500000 calls to the lookup function (spline or interp1) but the size of the mesh only changes about 10 times. A method to reduce the number of calls is to store the track geometry in a global variable and only re-evaluate it when the amount of points in the mesh actually change.

When we use a global variable, ADiGator gives the following error: Error using cada/adigatorVarAnalyzer (line 112) variable 'varName' is either an auxiliary or global variable which was re-assigned to have derivative information - this is not allowed.

As explained above we cannot prevent the global variable from changing size because the number of points at which we need to evaluate the track geometry increases with each mesh refinement.

Can you please help us to address this issue?

Kind regards

matt-weinstein commented 4 years ago

So the constraint on global variables is that you aren't allowed to assign anything to them that has derivatives. With GPOPS, this basically amounts to everything in the input structure.

With all of these issues with flow control, what you may consider doing is to break your problem down into smaller "pure math/derivative" blocks, have adigator operate on those, and then write a wrapper function around those. This would push some level of book-keeping onto your wrapper, but it sounds like it may be worth the investment if you are repeatedly solving the same class of problems.

Something that has always been a "desirement" for adigator is to be able to flag code to be ignored in the derivative sense, but I never got around to implementing it.