socSolveShell.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "socSolveShell.h"
  2. #include "denseBlas.h"
  3. #include "freeSocpInfo.h"
  4. #include "printFunction.h"
  5. void socSolveShell(int iIter, cmscp_trajInfo** optTraj, cmscp_socpInfo* socpInfo, cmscp_setup* setup)
  6. {
  7. int exitflag_solver = ECOS_FATAL, iphase, i;
  8. double* X = NULL, *temp, obj;
  9. int numMesh, numState, numControl, numParameter, numPhase, *numPhaseOptVar, lengthState, lengthControl, idxOptVarState, idxOptVarPhaseStart, idxOptVarControl, idxOptVarT0, idxOptVarTf, idxOptVarParameter;
  10. cmscp_dimension* dimension;
  11. cmscp_mesh** mesh;
  12. pwork* ecosWork = NULL;
  13. csocp_prob* prob = NULL;
  14. // 问题维数
  15. dimension = setup->dimension;
  16. numPhase = setup->numPhase;
  17. mesh = setup->mesh;
  18. if (strcmp(setup->scpInfo->solver->name, "ecos") == 0)
  19. {
  20. ecosWork = ECOS_setup(socpInfo->numOptVar, socpInfo->ACone1->m, socpInfo->AEqua1->m,
  21. socpInfo->dimCone->l, socpInfo->dimCone->nsoc, socpInfo->dimCone->q, 0, socpInfo->ACone1->v,
  22. socpInfo->ACone1->ptrCol, socpInfo->ACone1->idxRow, socpInfo->AEqua1->v, socpInfo->AEqua1->ptrCol,
  23. socpInfo->AEqua1->idxRow, socpInfo->c, socpInfo->bCone, socpInfo->bEqua);
  24. exitflag_solver = ECOS_solve(ecosWork);
  25. X = ecosWork->x;
  26. }
  27. else
  28. {
  29. // CSOCP
  30. CSOCP_create(&prob);
  31. CSOCP_setdblparam(prob, CSOCP_DBLPARAM_FEASTOL, setup->scpInfo->solver->feastol); // 设置可行性容差
  32. CSOCP_setdblparam(prob, CSOCP_DBLPARAM_ABSTOL, setup->scpInfo->solver->abstol);
  33. CSOCP_setdblparam(prob, CSOCP_DBLPARAM_RELTOL, setup->scpInfo->solver->reltol);
  34. CSOCP_setintparam(prob, CSOCP_INTPARAM_ITERLIMIT, setup->scpInfo->solver->maxit); // 设置最大迭代次数
  35. CSOCP_loadsocp(prob, socpInfo->numOptVar, socpInfo->ACone1->m, socpInfo->AEqua1->m,
  36. socpInfo->dimCone->l, socpInfo->dimCone->nsoc, socpInfo->dimCone->q, socpInfo->ACone1->v,
  37. socpInfo->ACone1->ptrCol, socpInfo->ACone1->idxRow, socpInfo->AEqua1->v, socpInfo->AEqua1->ptrCol,
  38. socpInfo->AEqua1->idxRow, socpInfo->c, socpInfo->bCone, socpInfo->bEqua);
  39. CSOCP_solve(prob);
  40. X = (double*)malloc(socpInfo->numOptVar * sizeof(double));
  41. CSOCP_getsolution(prob, X, NULL, NULL, NULL);
  42. }
  43. // 目标函数
  44. obj = 0;
  45. for ( i = 0; i < socpInfo->numOptVar; i++)
  46. {
  47. obj = obj + socpInfo->c[i] * X[i];
  48. }
  49. setup->scpInfo->obj[iIter] = obj / (cmscp_norm2(socpInfo->c, socpInfo->numOptVar) + 1);
  50. // 插值获取参考轨迹
  51. numPhaseOptVar = socpInfo->numPhaseOptVar;
  52. idxOptVarPhaseStart = 0;
  53. for (iphase = 0; iphase < numPhase; iphase++)
  54. {
  55. numMesh = mesh[iphase]->num;
  56. numState = dimension->phase[iphase]->state;
  57. numControl = dimension->phase[iphase]->control;
  58. numParameter = dimension->phase[iphase]->parameter;
  59. lengthState = numMesh * numState;
  60. lengthControl = numMesh * numControl;
  61. idxOptVarState = idxOptVarPhaseStart;
  62. idxOptVarControl = idxOptVarState + lengthState;
  63. idxOptVarT0 = idxOptVarControl + lengthControl;
  64. idxOptVarTf = idxOptVarT0 + 1;
  65. idxOptVarParameter = idxOptVarTf + 1;
  66. optTraj[iphase] = (cmscp_trajInfo*)malloc(sizeof(cmscp_trajInfo));
  67. optTraj[iphase]->state = (cmscp_floatDemat*)malloc(sizeof(cmscp_floatDemat));
  68. optTraj[iphase]->state->m = numState;
  69. optTraj[iphase]->state->n = numMesh;
  70. optTraj[iphase]->state->v = (double*)malloc(lengthState * sizeof(double));
  71. optTraj[iphase]->control = (cmscp_floatDemat*)malloc(sizeof(cmscp_floatDemat));
  72. optTraj[iphase]->control->m = numControl;
  73. optTraj[iphase]->control->n = numMesh;
  74. optTraj[iphase]->control->v = (double*)malloc(lengthControl * sizeof(double));
  75. optTraj[iphase]->parameter = (double*)malloc(numParameter * sizeof(double));
  76. temp = X + idxOptVarState;
  77. copyFloatVec(temp, optTraj[iphase]->state->v, lengthState, 0);
  78. temp = X + idxOptVarControl;
  79. copyFloatVec(temp, optTraj[iphase]->control->v, lengthControl, 0);
  80. temp = X + idxOptVarParameter;
  81. copyFloatVec(temp, optTraj[iphase]->parameter, numParameter, 0);
  82. temp = X + idxOptVarT0;
  83. optTraj[iphase]->initialtime = *temp;
  84. temp = X + idxOptVarTf;
  85. optTraj[iphase]->finaltime = *temp;
  86. idxOptVarPhaseStart = idxOptVarPhaseStart + numPhaseOptVar[iphase];
  87. }
  88. if (strcmp(setup->scpInfo->solver->name, "ecos") == 0)
  89. {
  90. ECOS_cleanup(ecosWork, 0); // 有一个内存释放不掉
  91. }
  92. else
  93. {
  94. free(X);
  95. CSOCP_free(&prob);
  96. }
  97. freeSocpInfo(&socpInfo, setup, 1); // 释放socpInfo的全部数据空间
  98. }