ImperialCollegeLondon / ICLOCS

Imperial College London Optimal Control Software (ICLOCS)
MIT License
75 stars 44 forks source link

Adigator issue #13

Closed VishnuDevA7 closed 4 years ago

VishnuDevA7 commented 4 years ago

I am trying to setup AD using Adigator on a simple system. I have the system dynamics vectorized, I can see that the dynamics outputs are computed correctly (by setting breakpoints and verifying).However, after Adigator passes through the dynamics function code, I receive the error :


Call to adigator: Transforming user function: 'userFunction_Adigator_oneConst_Scaling' taking derivatives wrt: 'Y'... Error using cada/adigatorVarAnalyzer (line 73) ??? Unable to Process Statement: [dynF_org,gConst]=f(X,U,P,T,vdat); Error in adigatortempfunc1 (line 83) [dynF_org,gConst] = adigatorVarAnalyzer('[dynF_org,gConst]=f(X,U,P,T,vdat);',dynF_org,'dynF_org',0,gConst,'gConst',0); Error in adigator (line 510) FunctionInfo = adigatortempfunc1(FunctionInfo,UserFunInputs); Error in genAdigator4ICLOCS (line 138) adigator('userFunction_Adigator_oneConst_Scaling',{gX,gU,gP,gT,data},FuncName,adigatorOptions('overwrite',1)); Error in transcribeOCP_eachPhase (line 1385) data=genAdigator4ICLOCS( options, data, n, m, np, nt, M ); Error in transcribeMultiphaseOCP (line 101) [phaseinfoNLP{i},phasedata{i},options.phaseoptions{i}]=transcribeOCP_eachPhase(problem.phases{i},guess.phases{i},options.phaseoptions{i}); Error in solveMyProblem (line 22) [infoNLP,data,options]=transcribeMultiphaseOCP(problem,guess,options);% Format for NLP solver Error in main_eLeg_ThreePhase (line 10) [solution,MRHistory]=solveMyProblem( problem,guess,options);

Any suggestion about how to debug this error ?

VishnuDevA7 commented 4 years ago

Just to add more info, I think the issue is related to a derivative file 'FuncYY____.m' not being generated. I can see that the dynamics file has AD code generated for it, "Func_Y__.m" . My settings are no different from that of Goddard Rocket where AD is used and the derivative code is successfully generated. Further I am using 'limited-memory' option for ipopt.hessian approximation but the program tries to look for the derivative file Func_YY__.m .

yuanbonie commented 4 years ago

Hi Vishnu,

These errors occur from time to time and I am yet not fully aware of the reasons behind Adigator errors. Most of the time a slight re-organization of the dynamic function will do the trick, but I did not identify any trend when these errors occur and how to avoid them. Apparently they are mostly related to the code-generation error.

VishnuDevA7 commented 4 years ago

Hi Yuanbo, I am able to replicate the exact same errors whenever I try to run any multi-phase problems in the examples (BangBang_TwoPhase or Goddard Rocket_MultiPhase) with 'adigator' option for options.derivatives along with 'limited-memory' option for options.ipopt.hessian_approximation. This does not happen when I use the 'exact' option or a single phase problem. Therefore, I suspect a possible bug in the code, especially in the multi-phase files in the 'usr' folder, could you please check ?

`This is Ipopt version 3.11.8, running with linear solver ma57.

Number of nonzeros in equality constraint Jacobian...: 276 Number of nonzeros in inequality constraint Jacobian.: 1694 Number of nonzeros in Lagrangian Hessian.............: 0

Warning: Error encountered when evaluatinggradCostfunction

In directCollocation (line 197) In costGradient_mp (line 49) In ipopt_auxdata>@(x)funcs.gradient(x,auxdata) (line 16) In ipopt_auxdata (line 40) In solveSingleNLP_DirectCollocation_MultiPhase (line 59) In solveMyProblem (line 23) In main_GoddardRocketThreePhase (line 24) Output argument "solution" (and maybe others) not assigned during call to "directCollocation". Error in costGradient_mp (line 49) grad=directCollocation('gradCost',z,data,i); Error in ipopt_auxdata>@(x)funcs.gradient(x,auxdata) (line 16) funcs_new.gradient = @(x) funcs.gradient(x, auxdata); Error in ipopt_auxdata (line 40) [x, info] = ipopt(x0, funcs_new, options_new); Error in solveSingleNLP_DirectCollocation_MultiPhase (line 59) [z, info] = ipopt_auxdata(NLP.mpinfoNLP.z0,data.mpdata.funcs,opt); Error in solveMyProblem (line 23) [solution,status,data]=solveSingleNLP_DirectCollocation_MultiPhase(infoNLP,data);% Solve the NLP Error in main_GoddardRocketThreePhase (line 24) [solution,MRHistory]=solveMyProblem( problem,guess,options);`

I think in line 27 of getAdigator4ICLOCS.m, the data.data structure is missing the field "AdigatorUserFucntion_YY". When using the 'limited-memory' option I believe this should be enclosed within an if statement since the second derivative file is not generated by Adigator.

yuanbonie commented 4 years ago

Hi Vishnu,

Thanks for the detailed info. I have not been running with this configuration for ages. The bug is now fixed (latest Github version just pushed).

Cheers, Yuanbo

VishnuDevA7 commented 4 years ago

Thank you!

VishnuDevA7 commented 4 years ago

I did a major reorganization of the dynamics code and managed to generate AD code without errors. I identified several syntactic trends which can lead to errors in code generation. Although, I do not know why the errors occur, I believe the following might be useful. Some of the steps I took to avoid code generation errors were :

i) In the dynamics code when constraints g_eq and g_neq are also supplied it is important to not have separate functions computing them. In other words, if a subfunction computes the dynamics inside the dynamics main function, the constraints need to be computed by the same subfunction. The constraints can be computed inside the main dynamics file but this has to be done before the output dx is computed. ii) When vectorizing the code, all vectors must be made out of columns of matrices, never out of rows. iii) It is advisable to avoid transpose or reshape operations on matrices as much as possible.
iv) While assigning intermediate variables with multiple rows of a particular matrix, the generated code almost always contained errors. v) I believe these errors are related to how the 'adigator' function is called by the ICLOCS library for code generation. Some of these errors did not persist when I modified some example files inside the adigator examples folder but tend to show up when generated through ICLOCS.