matt-weinstein / adigator

Matlab Algorithmic Differentiation Toolbox
22 stars 5 forks source link

Adigator rejects explicit array indices when used with GPOPS-II #10

Closed machalot closed 4 years ago

machalot commented 4 years ago

I have multi-phase GPOPS-II problem in which the Continuous function depends on the time-in-phase. Since I can't find anywhere that GPOPS-II provides time-in-phase, I'm trying to calculate it using

tphase(:) = t1(:) - t1(1);

Adigator rejects this with the following error:

Call to adigator:
Transforming user function: 'MyContinuous'
    taking derivatives wrt:  'V'...
Error using cada/subsref (line 86)
Invalid vectorized subsref

Error in adigatortempfunc1 (line 75)
tphase = t1(:) - t1(1);

Error in adigator (line 544)
  FunctionInfo = adigatortempfunc1(FunctionInfo,UserFunInputs);

Error in adigatorGenFiles4gpops2 (line 204)
adigator(ContFunc,{cinput},ContDeriv1,ADopts);

Is there a way to get around this error, or even better, a suggestion for how to obtain time-in-phase from GPOPS-II?

matt-weinstein commented 4 years ago

This violates vectorization of the phase (all elements of tphase are a function of a single element of a vectorized input), which is a fundamental assumption of the continuous function (both on adigator and gpops side). I am not sure if GPOPS has an option for making the time variable phase-relative, but one approach is to switch to parameters for the phase boundary times. Parameters are made available as vectorized inputs to the continuous function.

machalot commented 4 years ago

GPOPS-II allows time to be whatever you want. My issue is that overall trajectory problem requires linking absolute time across phases, but within a phase the dynamics depend on time-in-phase. I haven't found a direct way to do both. I'm considering just adding a new state with initial value 0 and derivative 1. I will also investigate your suggestion using parameters. Thanks!

P.S. I appreciate your help with GPOPS even though this is not a GPOPS forum. Do you know of any other online discussion forums to get help with GPOPS?

matt-weinstein commented 4 years ago

The point is that the statement tphase = t1(:) - t1(1); violates the vectorization assumption of GPOPS/adigator, so even using finite differences the derivatives of the NLP are going to end up being built wrong.

The GPOPS "parameter" implementation (I think that's what they call it), is equivalent to a state with no dynamics and can be accessed from both the continuous and endpoint functions (so you can set endpoint constraints such that t0_final = t1_init = p1), and then you are granted access to a vectorized value of p1 in the continuous function (so you can compute phase_time = t1-p1). Implementing as a parameter rather than a state just makes it easier on the underlying NLP (less variables, doesn't have to solve the trivial dynamics).

I am unaware of any GPOPS forums, but I have been out of the lab for quite some time now.