cmscp_socpInfo.c 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640
  1. #include "cmscp_socpInfo.h"
  2. #include "freeSocpInfo.h"
  3. void socpInfoSparseDim(cmscp_socpInfo** socpInfo0, cmscp_consTypeInfo* consTypeInfo, cmscp_sparseInfo* sparseInfo, cmscp_setup* setup)
  4. {
  5. // 函数功能:获取二阶锥规划问题的参数的维数以及分配相应的空间
  6. // 输入:
  7. // socpInfo0:无维数和未分配二阶锥规划问题参数信息
  8. // consTypeInfo:约束类型信息
  9. // sparseInfo:稀疏(非零元位置)信息
  10. // setup:问题设置参数
  11. // 输出:
  12. // socpInfo0:有维数和已分配二阶锥规划问题参数信息
  13. // 假设函数的自变量按照[x, u, p]排序
  14. // 假设优化变量的排列顺序为
  15. // [{x1}, { u1 }, t01, tf1, p1, Tr1, .., { xi }, { ui }, t0i, tfi, pi, Tri, ..., { xN }, { uN }, t0N, tfN, pN, TrN];
  16. // 其中{ xi } = { xi_1,...,xi_j,...,xi_n }, xi_j表示第i段的第j个状态向量
  17. // { ui } = { ui_1,...,ui_j,...,ui_n }, xi_j表示第i段的第j个控制输入
  18. // t0i, tfi表示第i段初始和终端时刻
  19. // pi表示第i段参数变量
  20. // Tri表示软信赖域边界
  21. // 谢磊:2022 / 6 / 27编写
  22. // 变量声明
  23. int numPhase, numOptVar, * numPhaseOptVar, iphase, numMesh, numState, numControl, numParameter, numRowAEqua, numNzAEqua, numRowAIneq, numNzAIneq, numRowASoc, numNzASoc, numSubASoc, numNlPath, numSubSocPath, * dimSocPath, * contStateNzPtrRow, * contControlNzPtrRow, * contParameterNzPtrRow, numNzPhaseAEqua, numRowPhaseAIneq, numRowPhaseAEqua, numNzPhaseAIneq, iType, numType, * idxType, cnt, idxRow, numPhaseSubASoc, numRowPointASoc, numRowPhaseASoc, idxStartRowSoc, idxEndRowSoc, numNzPhaseASoc, numNlEndp, numSubSocEndp, * dimSocEndp, * endpIniStateNzPtrRow, * endpFinStateNzPtrRow, * endpIniTimeNzPtrRow, * endpFinTimeNzPtrRow, * endpParameterNzPtrRow, numNlLink, numSubSocLink, * dimSocLink, * linkLeftStatefNzPtrRow, * linkLeftTfNzPtrRow, * linkLeftParameterNzPtrRow, * linkRightState0NzPtrRow, * linkRightT0NzPtrRow, * linkRightParameterNzPtrRow, * objEventIniStateNzPos, * objEventFinStateNzPos, * objEventIniTimeNzPos, * objEventFinTimeNzPos, * objEventParameterNzPos;
  24. double* c;
  25. cmscp_dimension* dimension;
  26. cmscp_mesh** mesh;
  27. cmscp_nzPos_cont** contNzPos;
  28. cmscp_nzPos_endp** objEventNzPos;
  29. cmscp_nzPos_endp** endpNzPos;
  30. cmscp_nzPos_link** linkNzPos;
  31. cmscp_consType** endpConsType;
  32. cmscp_consType** pathConsType;
  33. cmscp_consType_bound* boundConsType;
  34. cmscp_consType** linkConsType;
  35. cmscp_consType* eventConsType;
  36. cmscp_consType** boundIniTimeType;
  37. cmscp_consType** boundFinTimeType;
  38. cmscp_consType** boundIniStateType;
  39. cmscp_consType** boundFinStateType;
  40. cmscp_consType** boundStateType;
  41. cmscp_consType** boundControlType;
  42. cmscp_consType** boundParameterType;
  43. cmscp_socpInfo* socpInfo;
  44. // 1. 基本参数
  45. dimension = setup->dimension; // 问题维数信息
  46. mesh = setup->mesh; // 网格信息
  47. numPhase = setup->numPhase; // 阶段数
  48. // 2. 优化变量的个数
  49. numOptVar = 0;
  50. numPhaseOptVar = (int*)malloc(numPhase * sizeof(int));
  51. for (iphase = 0; iphase < numPhase; iphase++)
  52. {
  53. numMesh = mesh[iphase]->num;
  54. numState = dimension->phase[iphase]->state;
  55. numControl = dimension->phase[iphase]->control;
  56. numParameter = dimension->phase[iphase]->parameter;
  57. numPhaseOptVar[iphase] = numMesh * (numState + numControl) + numParameter + 3; // 保留软信赖域边
  58. numOptVar = numOptVar + numPhaseOptVar[iphase];
  59. }
  60. // 3. 目标函数系数
  61. c = (double*)malloc(numOptVar * sizeof(double));
  62. // 4. 统计三类矩阵的非零元个数和行数:不等式约束矩阵AIneq、等式约束矩阵AEqua和二阶锥约束矩阵ASoc
  63. numRowAEqua = 0;// 等式约束矩阵的行数
  64. numNzAEqua = 0;// 等式约束矩阵的非零个数
  65. numRowAIneq = 0;// 不等式约束矩阵的行数
  66. numNzAIneq = 0;// 不等式约束矩阵的非零个数
  67. numRowASoc = 0;// 二阶锥矩阵的行数
  68. numNzASoc = 0; // 二阶锥约束矩阵的非零元
  69. numSubASoc = 0;// 二阶锥矩阵中子二阶锥的个数
  70. contNzPos = sparseInfo->contNzPos;// 连续函数雅可比稀疏信息
  71. endpNzPos = sparseInfo->endpNzPos;// 端点函数雅可比稀疏信息
  72. objEventNzPos = sparseInfo->objEventNzPos;// 目标函数雅可比稀疏信息
  73. linkNzPos = sparseInfo->linkNzPos;// 衔接函数雅可比稀疏信息
  74. // 约束类型
  75. endpConsType = consTypeInfo->endp;
  76. linkConsType = consTypeInfo->link;
  77. pathConsType = consTypeInfo->path;
  78. boundConsType = consTypeInfo->bound;
  79. boundIniTimeType = boundConsType->iniTime;
  80. boundFinTimeType = boundConsType->finTime;
  81. boundIniStateType = boundConsType->iniState;
  82. boundFinStateType = boundConsType->finState;
  83. boundStateType = boundConsType->state;
  84. boundControlType = boundConsType->control;
  85. boundParameterType = boundConsType->parameter;
  86. eventConsType = consTypeInfo->event;
  87. for (iphase = 0; iphase < numPhase; iphase++)
  88. {
  89. // 维数信息
  90. numMesh = mesh[iphase]->num;
  91. numState = dimension->phase[iphase]->state;
  92. numControl = dimension->phase[iphase]->control;
  93. numParameter = dimension->phase[iphase]->parameter;
  94. // 连续函数部分
  95. // 约束维度信息
  96. numNlPath = dimension->phase[iphase]->path->nl;
  97. numSubSocPath = dimension->phase[iphase]->path->soc->n;
  98. dimSocPath = dimension->phase[iphase]->path->soc->q;
  99. // 连续函数雅可比矩阵稀疏信息
  100. contStateNzPtrRow = contNzPos[iphase]->state->ptrRow;
  101. contControlNzPtrRow = contNzPos[iphase]->control->ptrRow;
  102. contParameterNzPtrRow = contNzPos[iphase]->parameter->ptrRow;
  103. // 动力学方程部分
  104. numNzPhaseAEqua = 0;
  105. numNzPhaseAEqua = numNzPhaseAEqua + 2 * contStateNzPtrRow[numState];// 状态变量部分
  106. numNzPhaseAEqua = numNzPhaseAEqua + 2 * contControlNzPtrRow[numState];// 控制变量部分
  107. numNzPhaseAEqua = numNzPhaseAEqua + contParameterNzPtrRow[numState];// 静态参数部分
  108. numNzPhaseAEqua = numNzPhaseAEqua + numState * 2;// 初始和终端时刻部分
  109. numNzPhaseAEqua = (numMesh - 1) * numNzPhaseAEqua;
  110. numNzAEqua = numNzAEqua + numNzPhaseAEqua;
  111. numRowAEqua = numRowAEqua + (numMesh - 1) * numState;
  112. // 路径约束部分
  113. // 不等式和等式部分
  114. numRowPhaseAIneq = 0;// 各段不等式约束的行数
  115. numRowPhaseAEqua = 0;// 各段等式约束的行数
  116. numNzPhaseAIneq = 0;// 各段不等式约束的非零元
  117. numNzPhaseAEqua = 0;// 各段等式约束的非零元
  118. for (iType = 0; iType < 4; iType++)
  119. {
  120. numType = pathConsType[iphase]->type[iType].num;
  121. idxType = pathConsType[iphase]->type[iType].idx;
  122. if (numType)
  123. {
  124. if (iType == 0)
  125. {
  126. numRowPhaseAIneq = numRowPhaseAIneq + 2 * numType;
  127. for (cnt = 0; cnt < numType; cnt++)
  128. {
  129. idxRow = idxType[cnt] + numState; // 在连续函数雅可比矩阵中行位置
  130. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (contStateNzPtrRow[idxRow + 1] - contStateNzPtrRow[idxRow]);
  131. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (contControlNzPtrRow[idxRow + 1] - contControlNzPtrRow[idxRow]);
  132. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (contParameterNzPtrRow[idxRow + 1] - contParameterNzPtrRow[idxRow]);
  133. }
  134. }
  135. else if (iType == 1)
  136. {
  137. numRowPhaseAEqua = numRowPhaseAEqua + numType;
  138. for (cnt = 0; cnt < numType; cnt++)
  139. {
  140. idxRow = idxType[cnt] + numState;
  141. numNzPhaseAEqua = numNzPhaseAEqua + contStateNzPtrRow[idxRow + 1] - contStateNzPtrRow[idxRow];
  142. numNzPhaseAEqua = numNzPhaseAEqua + contControlNzPtrRow[idxRow + 1] - contControlNzPtrRow[idxRow];
  143. numNzPhaseAEqua = numNzPhaseAEqua + contParameterNzPtrRow[idxRow + 1] - contParameterNzPtrRow[idxRow];
  144. }
  145. }
  146. else
  147. {
  148. numRowPhaseAIneq = numRowPhaseAIneq + numType;
  149. for (cnt = 0; cnt < numType; cnt++)
  150. {
  151. idxRow = idxType[cnt] + numState;
  152. numNzPhaseAIneq = numNzPhaseAIneq + contStateNzPtrRow[idxRow + 1] - contStateNzPtrRow[idxRow];
  153. numNzPhaseAIneq = numNzPhaseAIneq + contControlNzPtrRow[idxRow + 1] - contControlNzPtrRow[idxRow];
  154. numNzPhaseAIneq = numNzPhaseAIneq + contParameterNzPtrRow[idxRow + 1] - contParameterNzPtrRow[idxRow];
  155. }
  156. }
  157. }
  158. }
  159. numRowPhaseAIneq = numMesh * numRowPhaseAIneq;
  160. numRowPhaseAEqua = numMesh * numRowPhaseAEqua;
  161. numRowAIneq = numRowAIneq + numRowPhaseAIneq;
  162. numRowAEqua = numRowAEqua + numRowPhaseAEqua;
  163. numNzPhaseAIneq = numMesh * numNzPhaseAIneq;
  164. numNzPhaseAEqua = numMesh * numNzPhaseAEqua;
  165. numNzAIneq = numNzAIneq + numNzPhaseAIneq;
  166. numNzAEqua = numNzAEqua + numNzPhaseAEqua;
  167. if (numSubSocPath)
  168. {
  169. // 二阶锥个数
  170. numPhaseSubASoc = numMesh * numSubSocPath;// 各段二阶锥矩阵个数
  171. numSubASoc = numSubASoc + numPhaseSubASoc;// 总二阶锥个数
  172. // 二阶锥矩阵行数
  173. numRowPointASoc = cmscp_sum(dimSocPath, numSubSocPath);// 单个离散点处的二阶锥矩阵行数
  174. numRowPhaseASoc = numMesh * numRowPointASoc;// 各段二阶锥矩阵行数
  175. numRowASoc = numRowASoc + numMesh * numRowPhaseASoc;// 总二阶锥矩阵行数
  176. // 单段二阶锥矩阵的非零元个数
  177. idxStartRowSoc = numState + numNlPath;// 二阶锥矩阵在连续函数雅可比矩阵中的起始行
  178. idxEndRowSoc = numState + numNlPath + numRowPointASoc;// 二阶锥矩阵在连续函数雅可比矩阵中的结束行
  179. numNzPhaseASoc = numNzPhaseASoc + contStateNzPtrRow[idxEndRowSoc + 1] - contStateNzPtrRow[idxStartRowSoc + 1];
  180. numNzPhaseASoc = numNzPhaseASoc + contControlNzPtrRow[idxEndRowSoc + 1] - contControlNzPtrRow[idxStartRowSoc + 1];
  181. numNzPhaseASoc = numNzPhaseASoc + contParameterNzPtrRow[idxEndRowSoc + 1] - contParameterNzPtrRow[idxStartRowSoc + 1];
  182. numNzPhaseASoc = numMesh * numNzPhaseASoc;
  183. // 总二阶锥矩阵非零元个数
  184. numNzASoc = numNzASoc + numNzPhaseASoc;
  185. }
  186. //// 端点函数部分
  187. // 约束维度信息
  188. numNlEndp = dimension->phase[iphase]->endpoint->nl;
  189. numSubSocEndp = dimension->phase[iphase]->endpoint->soc->n;
  190. dimSocEndp = dimension->phase[iphase]->endpoint->soc->q;
  191. // 端点函数雅可比矩阵稀疏信息
  192. endpIniStateNzPtrRow = endpNzPos[iphase]->iniState->ptrRow;
  193. endpFinStateNzPtrRow = endpNzPos[iphase]->finState->ptrRow;
  194. endpIniTimeNzPtrRow = endpNzPos[iphase]->iniTime->ptrRow;
  195. endpFinTimeNzPtrRow = endpNzPos[iphase]->finTime->ptrRow;
  196. endpParameterNzPtrRow = endpNzPos[iphase]->parameter->ptrRow;
  197. // 不等式和等式部分
  198. numRowPhaseAIneq = 0;// 各段不等式约束的行数
  199. numRowPhaseAEqua = 0;// 各段等式约束的行数
  200. numNzPhaseAIneq = 0;// 各段不等式约束的非零元
  201. numNzPhaseAEqua = 0;// 各段等式约束的非零元
  202. for (iType = 0; iType < 4; iType++)
  203. {
  204. numType = endpConsType[iphase]->type[iType].num;
  205. idxType = endpConsType[iphase]->type[iType].idx;
  206. if (numType)
  207. {
  208. if (iType == 0)
  209. {
  210. numRowPhaseAIneq = numRowPhaseAIneq + 2 * numType;
  211. for (cnt = 0; cnt < numType; cnt++)
  212. {
  213. idxRow = idxType[cnt];
  214. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (endpIniStateNzPtrRow[idxRow + 1] - endpIniStateNzPtrRow[idxRow]);
  215. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (endpFinStateNzPtrRow[idxRow + 1] - endpFinStateNzPtrRow[idxRow]);
  216. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (endpIniTimeNzPtrRow[idxRow + 1] - endpIniTimeNzPtrRow[idxRow]);
  217. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (endpFinTimeNzPtrRow[idxRow + 1] - endpFinTimeNzPtrRow[idxRow]);
  218. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (endpParameterNzPtrRow[idxRow + 1] - endpParameterNzPtrRow[idxRow]);
  219. }
  220. }
  221. else if (iType == 1)
  222. {
  223. numRowPhaseAEqua = numRowPhaseAEqua + numType;
  224. for (cnt = 0; cnt < numType; cnt++)
  225. {
  226. idxRow = idxType[cnt];
  227. numNzPhaseAEqua = numNzPhaseAEqua + endpIniStateNzPtrRow[idxRow + 1] - endpIniStateNzPtrRow[idxRow];
  228. numNzPhaseAEqua = numNzPhaseAEqua + endpFinStateNzPtrRow[idxRow + 1] - endpFinStateNzPtrRow[idxRow];
  229. numNzPhaseAEqua = numNzPhaseAEqua + endpIniTimeNzPtrRow[idxRow + 1] - endpIniTimeNzPtrRow[idxRow];
  230. numNzPhaseAEqua = numNzPhaseAEqua + endpFinTimeNzPtrRow[idxRow + 1] - endpFinTimeNzPtrRow[idxRow];
  231. numNzPhaseAEqua = numNzPhaseAEqua + endpParameterNzPtrRow[idxRow + 1] - endpParameterNzPtrRow[idxRow];
  232. }
  233. }
  234. else
  235. {
  236. numRowPhaseAIneq = numRowPhaseAIneq + numType;
  237. for (cnt = 0; cnt < numType; cnt++)
  238. {
  239. idxRow = idxType[cnt];
  240. numNzPhaseAIneq = numNzPhaseAIneq + endpIniStateNzPtrRow[idxRow + 1] - endpIniStateNzPtrRow[idxRow];
  241. numNzPhaseAIneq = numNzPhaseAIneq + endpFinStateNzPtrRow[idxRow + 1] - endpFinStateNzPtrRow[idxRow];
  242. numNzPhaseAIneq = numNzPhaseAIneq + endpIniTimeNzPtrRow[idxRow + 1] - endpIniTimeNzPtrRow[idxRow];
  243. numNzPhaseAIneq = numNzPhaseAIneq + endpFinTimeNzPtrRow[idxRow + 1] - endpFinTimeNzPtrRow[idxRow];
  244. numNzPhaseAIneq = numNzPhaseAIneq + endpParameterNzPtrRow[idxRow + 1] - endpParameterNzPtrRow[idxRow];
  245. }
  246. }
  247. }
  248. }
  249. numRowAIneq = numRowAIneq + numRowPhaseAIneq;
  250. numRowAEqua = numRowAEqua + numRowPhaseAEqua;
  251. numNzAIneq = numNzAIneq + numNzPhaseAIneq;
  252. numNzAEqua = numNzAEqua + numNzPhaseAEqua;
  253. // 二阶锥约束部分
  254. if (numSubSocEndp)
  255. {
  256. // 二阶锥个数
  257. numSubASoc = numSubASoc + numSubSocEndp; // 总二阶锥个数, 每段就一个端点约束函数
  258. // 二阶锥矩阵行数
  259. numRowPhaseASoc = cmscp_sum(dimSocEndp, numSubSocEndp); // 各段二阶锥矩阵行数
  260. numRowASoc = numRowASoc + numRowPhaseASoc; // 总二阶锥矩阵行数
  261. // 单段矩阵的非零元个数
  262. idxStartRowSoc = numNlEndp; // 二阶锥矩阵在连续函数雅可比矩阵中的起始行
  263. idxEndRowSoc = numNlEndp + numRowPhaseASoc; // 二阶锥矩阵在连续函数雅可比矩阵中的结束行
  264. numNzPhaseASoc = 0;
  265. numNzPhaseASoc = numNzPhaseASoc + endpIniStateNzPtrRow[idxEndRowSoc] - endpIniStateNzPtrRow[idxStartRowSoc];
  266. numNzPhaseASoc = numNzPhaseASoc + endpFinStateNzPtrRow[idxEndRowSoc] - endpFinStateNzPtrRow[idxStartRowSoc];
  267. numNzPhaseASoc = numNzPhaseASoc + endpIniTimeNzPtrRow[idxEndRowSoc] - endpIniTimeNzPtrRow[idxStartRowSoc];
  268. numNzPhaseASoc = numNzPhaseASoc + endpFinTimeNzPtrRow[idxEndRowSoc] - endpFinTimeNzPtrRow[idxStartRowSoc];
  269. numNzPhaseASoc = numNzPhaseASoc + endpParameterNzPtrRow[idxEndRowSoc] - endpParameterNzPtrRow[idxStartRowSoc];
  270. // 总二阶锥矩阵非零元个数
  271. numNzASoc = numNzASoc + numNzPhaseASoc;
  272. }
  273. if (iphase < numPhase - 1)
  274. {
  275. // 约束维数信息
  276. numNlLink = dimension->phase[iphase]->linkage->nl;// 非线性部分维数
  277. numSubSocLink = dimension->phase[iphase]->linkage->soc->n;
  278. dimSocLink = dimension->phase[iphase]->linkage->soc->q;
  279. // 稀疏信息
  280. linkLeftStatefNzPtrRow = linkNzPos[iphase]->leftStatef->ptrRow;
  281. linkLeftTfNzPtrRow = linkNzPos[iphase]->leftTf->ptrRow;
  282. linkLeftParameterNzPtrRow = linkNzPos[iphase]->leftParameter->ptrRow;
  283. linkRightState0NzPtrRow = linkNzPos[iphase]->rightState0->ptrRow;
  284. linkRightT0NzPtrRow = linkNzPos[iphase]->rightT0->ptrRow;
  285. linkRightParameterNzPtrRow = linkNzPos[iphase]->rightParameter->ptrRow;
  286. // 统计各类非线性约束矩阵的行数
  287. numRowPhaseAIneq = 0;// 各段不等式约束的行数
  288. numRowPhaseAEqua = 0;// 各段等式约束的行数
  289. numNzPhaseAIneq = 0;// 各段不等式约束的非零元
  290. numNzPhaseAEqua = 0;// 各段等式约束的非零元
  291. for (iType = 0; iType < 4; iType++)
  292. {
  293. numType = linkConsType[iphase]->type[iType].num;
  294. idxType = linkConsType[iphase]->type[iType].idx;
  295. if (numType)
  296. {
  297. if (iType == 0)
  298. {
  299. numRowPhaseAIneq = numRowPhaseAIneq + 2 * numType;
  300. for (cnt = 0; cnt < numType; cnt++)
  301. {
  302. idxRow = idxType[cnt];
  303. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (linkLeftStatefNzPtrRow[idxRow + 1] - linkLeftStatefNzPtrRow[idxRow]);
  304. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (linkLeftTfNzPtrRow[idxRow + 1] - linkLeftTfNzPtrRow[idxRow]);
  305. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (linkLeftParameterNzPtrRow[idxRow + 1] - linkLeftParameterNzPtrRow[idxRow]);
  306. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (linkRightState0NzPtrRow[idxRow + 1] - linkRightState0NzPtrRow[idxRow]);
  307. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (linkRightT0NzPtrRow[idxRow + 1] - linkRightT0NzPtrRow[idxRow]);
  308. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (linkRightParameterNzPtrRow[idxRow + 1] - linkRightParameterNzPtrRow[idxRow]);
  309. }
  310. }
  311. else if (iType == 1)
  312. {
  313. numRowPhaseAEqua = numRowPhaseAEqua + numType;
  314. for (cnt = 0; cnt < numType; cnt++)
  315. {
  316. idxRow = idxType[cnt];
  317. numNzPhaseAEqua = numNzPhaseAEqua + linkLeftStatefNzPtrRow[idxRow + 1] - linkLeftStatefNzPtrRow[idxRow];
  318. numNzPhaseAEqua = numNzPhaseAEqua + linkLeftTfNzPtrRow[idxRow + 1] - linkLeftTfNzPtrRow[idxRow];
  319. numNzPhaseAEqua = numNzPhaseAEqua + linkLeftParameterNzPtrRow[idxRow + 1] - linkLeftParameterNzPtrRow[idxRow];
  320. numNzPhaseAEqua = numNzPhaseAEqua + linkRightState0NzPtrRow[idxRow + 1] - linkRightState0NzPtrRow[idxRow];
  321. numNzPhaseAEqua = numNzPhaseAEqua + linkRightT0NzPtrRow[idxRow + 1] - linkRightT0NzPtrRow[idxRow];
  322. numNzPhaseAEqua = numNzPhaseAEqua + linkRightParameterNzPtrRow[idxRow + 1] - linkRightParameterNzPtrRow[idxRow];
  323. }
  324. }
  325. else
  326. {
  327. numRowPhaseAIneq = numRowPhaseAIneq + numType;
  328. for (cnt = 0; cnt < numType; cnt++)
  329. {
  330. idxRow = idxType[cnt];
  331. numNzPhaseAIneq = numNzPhaseAIneq + linkLeftStatefNzPtrRow[idxRow + 1] - linkLeftStatefNzPtrRow[idxRow];
  332. numNzPhaseAIneq = numNzPhaseAIneq + linkLeftTfNzPtrRow[idxRow + 1] - linkLeftTfNzPtrRow[idxRow];
  333. numNzPhaseAIneq = numNzPhaseAIneq + linkLeftParameterNzPtrRow[idxRow + 1] - linkLeftParameterNzPtrRow[idxRow];
  334. numNzPhaseAIneq = numNzPhaseAIneq + linkRightState0NzPtrRow[idxRow + 1] - linkRightState0NzPtrRow[idxRow];
  335. numNzPhaseAIneq = numNzPhaseAIneq + linkRightT0NzPtrRow[idxRow + 1] - linkRightT0NzPtrRow[idxRow];
  336. numNzPhaseAIneq = numNzPhaseAIneq + linkRightParameterNzPtrRow[idxRow + 1] - linkRightParameterNzPtrRow[idxRow];
  337. }
  338. }
  339. }
  340. }
  341. numRowAIneq = numRowAIneq + numRowPhaseAIneq;
  342. numRowAEqua = numRowAEqua + numRowPhaseAEqua;
  343. numNzAIneq = numNzAIneq + numNzPhaseAIneq;
  344. numNzAEqua = numNzAEqua + numNzPhaseAEqua;
  345. if (numSubSocLink)
  346. {
  347. // 二阶锥个数
  348. numSubASoc = numSubASoc + numSubSocLink;// 总二阶锥个数, 每段就一个端点约束函数
  349. // 二阶锥矩阵行数
  350. numRowPhaseASoc = cmscp_sum(dimSocLink, numSubSocLink);// 各段二阶锥矩阵行数
  351. numRowASoc = numRowASoc + numRowPhaseASoc;// 总二阶锥矩阵行数
  352. // 单段矩阵的非零元个数
  353. idxStartRowSoc = numNlLink;// 二阶锥矩阵在连续函数雅可比矩阵中的起始行
  354. idxEndRowSoc = numNlLink + numRowPhaseASoc;// 二阶锥矩阵在连续函数雅可比矩阵中的结束行
  355. numNzPhaseASoc = 0;
  356. numNzPhaseASoc = numNzPhaseASoc + linkLeftStatefNzPtrRow[idxEndRowSoc] - linkLeftStatefNzPtrRow[idxStartRowSoc];
  357. numNzPhaseASoc = numNzPhaseASoc + linkLeftTfNzPtrRow[idxEndRowSoc] - linkLeftTfNzPtrRow[idxStartRowSoc];
  358. numNzPhaseASoc = numNzPhaseASoc + linkLeftParameterNzPtrRow[idxEndRowSoc] - linkLeftParameterNzPtrRow[idxStartRowSoc];
  359. numNzPhaseASoc = numNzPhaseASoc + linkRightState0NzPtrRow[idxEndRowSoc] - linkRightState0NzPtrRow[idxStartRowSoc];
  360. numNzPhaseASoc = numNzPhaseASoc + linkRightT0NzPtrRow[idxEndRowSoc] - linkRightT0NzPtrRow[idxStartRowSoc];
  361. numNzPhaseASoc = numNzPhaseASoc + linkRightParameterNzPtrRow[idxEndRowSoc] - linkRightParameterNzPtrRow[idxStartRowSoc];
  362. // 总二阶锥矩阵非零元个数
  363. numNzASoc = numNzASoc + numNzPhaseASoc;
  364. }
  365. }
  366. //// 上下边界约束
  367. // 各段不等式和等式约束的行数也等于非零元个数
  368. numRowPhaseAEqua = 0;
  369. numRowPhaseAIneq = 0;
  370. // 统计行数
  371. for (iType = 0; iType < 4; iType++)
  372. {
  373. // 初始时刻变量
  374. numType = boundIniTimeType[iphase]->type[iType].num;
  375. if (numType)
  376. {
  377. if (iType == 0)
  378. {
  379. numRowPhaseAIneq = numRowPhaseAIneq + 2 * numType;
  380. }
  381. else if (iType == 1)
  382. {
  383. numRowPhaseAEqua = numRowPhaseAEqua + numType;
  384. }
  385. else
  386. {
  387. numRowPhaseAIneq = numRowPhaseAIneq + numType;
  388. }
  389. }
  390. // 终端时刻变量
  391. numType = boundFinTimeType[iphase]->type[iType].num;
  392. if (numType)
  393. {
  394. if (iType == 0)
  395. {
  396. numRowPhaseAIneq = numRowPhaseAIneq + 2 * numType;
  397. }
  398. else if (iType == 1)
  399. {
  400. numRowPhaseAEqua = numRowPhaseAEqua + numType;
  401. }
  402. else
  403. {
  404. numRowPhaseAIneq = numRowPhaseAIneq + numType;
  405. }
  406. }
  407. // 初始状态变量
  408. numType = boundIniStateType[iphase]->type[iType].num;
  409. if (numType)
  410. {
  411. if (iType == 0)
  412. {
  413. numRowPhaseAIneq = numRowPhaseAIneq + 2 * numType;
  414. }
  415. else if (iType == 1)
  416. {
  417. numRowPhaseAEqua = numRowPhaseAEqua + numType;
  418. }
  419. else
  420. {
  421. numRowPhaseAIneq = numRowPhaseAIneq + numType;
  422. }
  423. }
  424. // 终端状态变量
  425. numType = boundFinStateType[iphase]->type[iType].num;
  426. if (numType)
  427. {
  428. if (iType == 0)
  429. {
  430. numRowPhaseAIneq = numRowPhaseAIneq + 2 * numType;
  431. }
  432. else if (iType == 1)
  433. {
  434. numRowPhaseAEqua = numRowPhaseAEqua + numType;
  435. }
  436. else
  437. {
  438. numRowPhaseAIneq = numRowPhaseAIneq + numType;
  439. }
  440. }
  441. // 过程状态变量
  442. numType = boundStateType[iphase]->type[iType].num;
  443. if (numType)
  444. {
  445. if (iType == 0)
  446. {
  447. numRowPhaseAIneq = numRowPhaseAIneq + 2 * (numMesh - 2) * numType;
  448. }
  449. else if (iType == 1)
  450. {
  451. numRowPhaseAEqua = numRowPhaseAEqua + (numMesh - 2) * numType;
  452. }
  453. else
  454. {
  455. numRowPhaseAIneq = numRowPhaseAIneq + (numMesh - 2) * numType;
  456. }
  457. }
  458. // 控制变量
  459. numType = boundControlType[iphase]->type[iType].num;
  460. if (numType)
  461. {
  462. if (iType == 0)
  463. {
  464. numRowPhaseAIneq = numRowPhaseAIneq + 2 * numMesh * numType;
  465. }
  466. else if (iType == 1)
  467. {
  468. numRowPhaseAEqua = numRowPhaseAEqua + numMesh * numType;
  469. }
  470. else
  471. {
  472. numRowPhaseAIneq = numRowPhaseAIneq + numMesh * numType;
  473. }
  474. }
  475. // 静态参数变量
  476. numType = boundParameterType[iphase]->type[iType].num;
  477. if (numType)
  478. {
  479. if (iType == 0)
  480. {
  481. numRowPhaseAIneq = numRowPhaseAIneq + 2 * numType;
  482. }
  483. else if (iType == 1)
  484. {
  485. numRowPhaseAEqua = numRowPhaseAEqua + numType;
  486. }
  487. else
  488. {
  489. numRowPhaseAIneq = numRowPhaseAIneq + numType;
  490. }
  491. }
  492. }
  493. numRowAIneq = numRowAIneq + numRowPhaseAIneq;
  494. numRowAEqua = numRowAEqua + numRowPhaseAEqua;
  495. numNzAIneq = numNzAIneq + numRowPhaseAIneq;
  496. numNzAEqua = numNzAEqua + numRowPhaseAEqua;
  497. //// 信赖域约束
  498. // 二阶锥约束个数
  499. // 状态:numMesh、控制:numMesh、初始时刻:1、终端时刻:1、静态参数:1
  500. numSubASoc = numSubASoc + numMesh * 2 + 3;
  501. // 二阶锥约束矩阵行数和非零元个数相等
  502. // 每个锥的维数比对应变量多1
  503. numRowPhaseASoc = numMesh * (numState + numControl + 2) + numParameter + 5;
  504. numRowASoc = numRowASoc + numRowPhaseASoc;
  505. numNzASoc = numNzASoc + numRowPhaseASoc;
  506. }
  507. // 事件约束
  508. for (iType = 0; iType < 4; iType++)
  509. {
  510. numType = eventConsType->type[iType].num;
  511. idxType = eventConsType->type[iType].idx;
  512. if (numType)
  513. {
  514. if (iType == 0)
  515. {
  516. for (cnt = 0; cnt < numType; cnt++)
  517. {
  518. // 统计非零元素个数
  519. // 第idxType(cnt)个event约束被分为numPhase段
  520. idxRow = idxType[cnt] + 1; // 加偏置1,因为稀疏信息是objEvent的,第一行是目标函数obj的
  521. for (iphase = 0; iphase < numPhase; iphase++)
  522. {
  523. // 稀疏信息
  524. objEventIniStateNzPos = objEventNzPos[iphase]->iniState->ptrRow;
  525. objEventFinStateNzPos = objEventNzPos[iphase]->finState->ptrRow;
  526. objEventIniTimeNzPos = objEventNzPos[iphase]->iniTime->ptrRow;
  527. objEventFinTimeNzPos = objEventNzPos[iphase]->finTime->ptrRow;
  528. objEventParameterNzPos = objEventNzPos[iphase]->parameter->ptrRow;
  529. // 每段非零元个数
  530. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (objEventIniStateNzPos[idxRow + 1] - objEventIniStateNzPos[idxRow]);
  531. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (objEventFinStateNzPos[idxRow + 1] - objEventFinStateNzPos[idxRow]);
  532. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (objEventIniTimeNzPos[idxRow + 1] - objEventIniTimeNzPos[idxRow]);
  533. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (objEventFinTimeNzPos[idxRow + 1] - objEventFinTimeNzPos[idxRow]);
  534. numNzPhaseAIneq = numNzPhaseAIneq + 2 * (objEventParameterNzPos[idxRow + 1] - objEventParameterNzPos[idxRow]);
  535. }
  536. numRowAIneq = numRowAIneq + 1;
  537. }
  538. }
  539. else if (iType == 1)
  540. {
  541. for (cnt = 0; cnt < numType; cnt++)
  542. {
  543. // 统计非零元素个数
  544. // 第idxType(cnt)个event约束被分为numPhase段
  545. idxRow = idxType[cnt] + 1; // 加偏置1,因为稀疏信息是objEvent的,第一行是目标函数obj的
  546. for (iphase = 0; iphase < numPhase; iphase++)
  547. {
  548. // 稀疏信息
  549. objEventIniStateNzPos = objEventNzPos[iphase]->iniState->ptrRow;
  550. objEventFinStateNzPos = objEventNzPos[iphase]->finState->ptrRow;
  551. objEventIniTimeNzPos = objEventNzPos[iphase]->iniTime->ptrRow;
  552. objEventFinTimeNzPos = objEventNzPos[iphase]->finTime->ptrRow;
  553. objEventParameterNzPos = objEventNzPos[iphase]->parameter->ptrRow;
  554. // 每段非零元个数
  555. numNzPhaseAEqua = numNzPhaseAEqua + objEventIniStateNzPos[idxRow + 1] - objEventIniStateNzPos[idxRow];
  556. numNzPhaseAEqua = numNzPhaseAEqua + objEventFinStateNzPos[idxRow + 1] - objEventFinStateNzPos[idxRow];
  557. numNzPhaseAEqua = numNzPhaseAEqua + objEventIniTimeNzPos[idxRow + 1] - objEventIniTimeNzPos[idxRow];
  558. numNzPhaseAEqua = numNzPhaseAEqua + objEventFinTimeNzPos[idxRow + 1] - objEventFinTimeNzPos[idxRow];
  559. numNzPhaseAEqua = numNzPhaseAEqua + objEventParameterNzPos[idxRow + 1] - objEventParameterNzPos[idxRow];
  560. }
  561. numRowAEqua = numRowAEqua + 1;
  562. }
  563. }
  564. else
  565. {
  566. for (cnt = 0; cnt < numType; cnt++)
  567. {
  568. // 统计非零元素个数
  569. // 第idxType(cnt)个event约束被分为numPhase段
  570. idxRow = idxType[cnt] + 1; // 加偏置1,因为稀疏信息是objEvent的,第一行是目标函数obj的
  571. for (iphase = 0; iphase < numPhase; iphase++)
  572. {
  573. // 稀疏信息
  574. objEventIniStateNzPos = objEventNzPos[iphase]->iniState->ptrRow;
  575. objEventFinStateNzPos = objEventNzPos[iphase]->finState->ptrRow;
  576. objEventIniTimeNzPos = objEventNzPos[iphase]->iniTime->ptrRow;
  577. objEventFinTimeNzPos = objEventNzPos[iphase]->finTime->ptrRow;
  578. objEventParameterNzPos = objEventNzPos[iphase]->parameter->ptrRow;
  579. // 每段非零元个数
  580. numNzPhaseAIneq = numNzPhaseAIneq + objEventIniStateNzPos[idxRow + 1] - objEventIniStateNzPos[idxRow];
  581. numNzPhaseAIneq = numNzPhaseAIneq + objEventFinStateNzPos[idxRow + 1] - objEventFinStateNzPos[idxRow];
  582. numNzPhaseAIneq = numNzPhaseAIneq + objEventIniTimeNzPos[idxRow + 1] - objEventIniTimeNzPos[idxRow];
  583. numNzPhaseAIneq = numNzPhaseAIneq + objEventFinTimeNzPos[idxRow + 1] - objEventFinTimeNzPos[idxRow];
  584. numNzPhaseAIneq = numNzPhaseAIneq + objEventParameterNzPos[idxRow + 1] - objEventParameterNzPos[idxRow];
  585. }
  586. numRowAIneq = numRowAIneq + 1;
  587. }
  588. }
  589. }
  590. }
  591. // 分配约束相关空间
  592. socpInfo = (cmscp_socpInfo*)malloc(sizeof(cmscp_socpInfo));
  593. socpInfo->bEqua = (double*)malloc(numRowAEqua * sizeof(double));
  594. socpInfo->bCone = (double*)malloc((numRowAIneq + numRowASoc) * sizeof(double));
  595. socpInfo->AEqua = (cmscp_crsSpmat*)malloc(sizeof(cmscp_crsSpmat));
  596. socpInfo->ACone = (cmscp_crsSpmat*)malloc(sizeof(cmscp_crsSpmat));
  597. socpInfo->AEqua->ptrRow = (int*)malloc((numRowAEqua + 1) * sizeof(int));
  598. socpInfo->AEqua->ptrRow[numRowAEqua] = numNzAEqua;
  599. socpInfo->AEqua->idxCol = (int*)malloc(numNzAEqua * sizeof(int));
  600. socpInfo->AEqua->v = (double*)malloc(numNzAEqua * sizeof(double));
  601. socpInfo->AEqua->m = numRowAEqua;
  602. socpInfo->AEqua->n = numOptVar;
  603. socpInfo->ACone->ptrRow = (int*)malloc((numRowAIneq + numRowASoc + 1) * sizeof(int));
  604. socpInfo->ACone->ptrRow[numRowAIneq + numRowASoc] = numNzAIneq + numNzASoc;
  605. socpInfo->ACone->idxCol = (int*)malloc((numNzAIneq + numNzASoc) * sizeof(int));
  606. socpInfo->ACone->v = (double*)malloc((numNzAIneq + numNzASoc) * sizeof(double));
  607. socpInfo->ACone->m = numRowAIneq + numRowASoc;
  608. socpInfo->ACone->n = numOptVar;
  609. socpInfo->AEqua1 = (cmscp_ccsSpmat*)malloc(sizeof(cmscp_ccsSpmat));
  610. socpInfo->ACone1 = (cmscp_ccsSpmat*)malloc(sizeof(cmscp_ccsSpmat));
  611. socpInfo->AEqua1->idxRow = (int*)malloc(numNzAEqua * sizeof(int));
  612. socpInfo->AEqua1->ptrCol = (int*)malloc((numOptVar+1) * sizeof(int));
  613. socpInfo->AEqua1->ptrCol[numOptVar] = numNzAEqua;
  614. socpInfo->AEqua1->v = (double*)malloc(numNzAEqua * sizeof(double));
  615. socpInfo->AEqua1->m = numRowAEqua;
  616. socpInfo->AEqua1->n = numOptVar;
  617. socpInfo->ACone1->idxRow = (int*)malloc((numNzAIneq + numNzASoc) * sizeof(int));
  618. socpInfo->ACone1->ptrCol = (int*)malloc( (numOptVar + 1) * sizeof(int));
  619. socpInfo->ACone1->ptrCol[numOptVar] = numNzAIneq + numNzASoc;
  620. socpInfo->ACone1->v = (double*)malloc((numNzAIneq + numNzASoc) * sizeof(double));
  621. socpInfo->ACone1->m = numRowAIneq + numRowASoc;
  622. socpInfo->ACone1->n = numOptVar;
  623. socpInfo->dimCone = (cmscp_dimCone*)malloc(sizeof(cmscp_dimCone));
  624. socpInfo->dimCone->l = numRowAIneq;
  625. socpInfo->dimCone->nsoc = numSubASoc;
  626. socpInfo->dimCone->q = (int*)malloc(numSubASoc * sizeof(int));
  627. socpInfo->numOptVar = numOptVar;
  628. socpInfo->numPhaseOptVar = numPhaseOptVar;
  629. socpInfo->c = c;
  630. socpInfo->cntNzAEqua = 0;
  631. socpInfo->cntRowAEqua = 0;
  632. socpInfo->cntNzAIneq = 0;
  633. socpInfo->cntRowAIneq = 0;
  634. socpInfo->cntNzASoc = numNzAIneq;
  635. socpInfo->cntRowASoc = numRowAIneq;
  636. socpInfo->cntSubASoc = 0;
  637. // 输出
  638. *socpInfo0 = socpInfo;
  639. }
  640. void socpInfoSparseObjEvent(cmscp_socpInfo** socpInfo0, cmscp_trajInfo** refTraj, cmscp_sparseInfo* sparseInfo, cmscp_consTypeInfo* consTypeInfo, cmscp_setup* setup)
  641. {
  642. // 函数功能:获取与目标函数和事件约束相关的二阶锥问题参数
  643. // 输入:
  644. // socpInfo0:无获目标函数和事件约束相关的二阶锥问题参数
  645. // refTraj:参考轨迹
  646. // consTypeInfo:约束类型信息
  647. // sparseInfo:稀疏(非零元位置)信息
  648. // setup:问题设置参数
  649. // 输出:
  650. // socpInfo0:有目标函数和事件约束相关的二阶锥问题参数
  651. // 假设函数的自变量按照[x, u, p]排序
  652. // 假设优化变量的排列顺序为
  653. // [{x1}, { u1 }, t01, tf1, p1, Tr1, .., { xi }, { ui }, t0i, tfi, pi, Tri, ..., { xN }, { uN }, t0N, tfN, pN, TrN];
  654. // 其中{ xi } = { xi_1,...,xi_j,...,xi_n }, xi_j表示第i段的第j个状态向量
  655. // { ui } = { ui_1,...,ui_j,...,ui_n }, xi_j表示第i段的第j个控制输入
  656. // t0i, tfi表示第i段初始和终端时刻
  657. // pi表示第i段参数变量
  658. // Tri表示软信赖域边界
  659. // 谢磊:2022 / 6 / 27编写
  660. // 变量声明
  661. int numPhase, numOptVar, * numPhaseOptVar, iphase, numMesh, numState, numControl, numParameter, numRowAEqua, numNzAEqua, numRowAIneq, numNzAIneq, numRowASoc, numNzASoc, numSubASoc, numNlPath, numSubSocPath, * dimSocPath, * contStateNzPtrRow, * contControlNzPtrRow, * contParameterNzPtrRow, numNzPhaseAEqua, numRowPhaseAIneq, numRowPhaseAEqua, numNzPhaseAIneq, iType, numType, * idxType, cnt, idxRow, numPhaseSubASoc, numRowPointASoc, numRowPhaseASoc, idxStartRowSoc, idxEndRowSoc, numNzPhaseASoc, numNlEndp, numSubSocEndp, * dimSocEndp, * endpIniStateNzPtrRow, * endpFinStateNzPtrRow, * endpIniTimeNzPtrRow, * endpFinTimeNzPtrRow, * endpParameterNzPtrRow, numNlLink, numSubSocLink, * dimSocLink, * linkLeftStatefNzPtrRow, * linkLeftTfNzPtrRow, * linkLeftParameterNzPtrRow, * linkRightState0NzPtrRow, * linkRightT0NzPtrRow, * linkRightParameterNzPtrRow, numNlEvent, numSubSocEvent, * dimSocEvent, idxStartRowSocEvent, * objEventIniStateNzPos, * objEventFinStateNzPos, * objEventIniTimeNzPos, * objEventFinTimeNzPos, * ptrRowAEqua, * idxColAEqua, * ptrRowACone, * idxColACone, cntNzAEqua, cntRowAEqua, cntNzAIneq, cntRowAIneq, cntNzASoc, cntRowASoc, cntSubASoc, idxOptVarPhaseStart, cntRowGrd, * iniStateNzPtrRow, * finStateNzPtrRow, * iniTimeNzPtrRow, * finTimeNzPtrRow, * parameterNzPtrRow, * iniStateNzIdxCol, * finStateNzIdxCol, * iniTimeNzIdxCol, * finTimeNzIdxCol, * parameterNzIdxCol, idxOptVarState0, idxOptVarStatef, idxOptVarT0, idxOptVarTf, idxOptVarParameter, idxOptVarTrust, j, nnzType1, i, i0, cntRowAIneqLower, numType1, idxGrdCol, p, numSocEvent, numRowEvent, numRowSocEvent;
  662. double* c, * valueAEqua, * bEqua, * valueACone, * bCone, w, * eventNlLower, * eventNlUpper, * refState0, * refStatef, refT0, refTf, * refParameter, temp, * eventGrd, * eventRef, *eventSocUpper;
  663. cmscp_dimension* dimension;
  664. cmscp_mesh** mesh;
  665. cmscp_bound* bounds;
  666. cmscp_trust* trust;
  667. cmscp_consType* consType;
  668. cmscp_nzPos_endp** objEventNzPos;
  669. cmscp_derInfo* objDerInfo;
  670. cmscp_derInfo* eventDerInfo;
  671. cmscp_socpInfo* socpInfo;
  672. cmscp_dimCone* dimCone;
  673. // 1. 基本参数
  674. dimension = setup->dimension;// 问题维数信息
  675. mesh = setup->mesh;// 网格信息
  676. numPhase = setup->numPhase;// 阶段数
  677. bounds = setup->bounds;// 边界信息
  678. trust = setup->trust;// 信赖域信息
  679. socpInfo = (*socpInfo0);
  680. numPhaseOptVar = socpInfo->numPhaseOptVar;// 每段优化变量个数
  681. objEventNzPos = sparseInfo->objEventNzPos;// 稀疏信息
  682. consType = consTypeInfo->event;// 约束类型信息
  683. // 二阶锥规划参数
  684. numOptVar = socpInfo->numOptVar;
  685. c = socpInfo->c;
  686. ptrRowAEqua = socpInfo->AEqua->ptrRow;
  687. idxColAEqua = socpInfo->AEqua->idxCol;
  688. valueAEqua = socpInfo->AEqua->v;
  689. bEqua = socpInfo->bEqua;
  690. ptrRowACone = socpInfo->ACone->ptrRow;
  691. idxColACone = socpInfo->ACone->idxCol;
  692. valueACone = socpInfo->ACone->v;
  693. bCone = socpInfo->bCone;
  694. dimCone = socpInfo->dimCone;
  695. // 计数变量
  696. cntNzAEqua = socpInfo->cntNzAEqua;
  697. cntRowAEqua = socpInfo->cntRowAEqua;
  698. cntNzAIneq = socpInfo->cntNzAIneq;
  699. cntRowAIneq = socpInfo->cntRowAIneq;
  700. cntNzASoc = socpInfo->cntNzASoc;
  701. cntRowASoc = socpInfo->cntRowASoc;
  702. cntSubASoc = socpInfo->cntSubASoc;
  703. //1.1. 梯度信息
  704. gradInfoSparseObjEvent(&objDerInfo, &eventDerInfo, sparseInfo, refTraj, setup);
  705. eventGrd = eventDerInfo->der;
  706. eventRef = eventDerInfo->ref;
  707. //// 2->目标函数
  708. floatVecFillin(c, numOptVar, 0);
  709. // 索引
  710. idxOptVarPhaseStart = 0; // 各段起始索引
  711. cntRowGrd = 0; // 目标函数梯度计数
  712. for (iphase = 0; iphase < numPhase; iphase++)
  713. {
  714. // 各段维数信息
  715. numMesh = mesh[iphase]->num;
  716. numState = dimension->phase[iphase]->state;
  717. numControl = dimension->phase[iphase]->control;
  718. numParameter = dimension->phase[iphase]->parameter;
  719. if (strcmp(setup->trust->model, "hard") == 0)
  720. {
  721. w = 0;
  722. }
  723. else if ((strcmp(setup->trust->model, "soft") == 0))
  724. {
  725. w = trust->phase[iphase]->softPenaWeight;
  726. }
  727. // 稀疏信息
  728. iniStateNzPtrRow = objEventNzPos[iphase]->iniState->ptrRow;
  729. finStateNzPtrRow = objEventNzPos[iphase]->finState->ptrRow;
  730. iniTimeNzPtrRow = objEventNzPos[iphase]->iniTime->ptrRow;
  731. finTimeNzPtrRow = objEventNzPos[iphase]->finTime->ptrRow;
  732. parameterNzPtrRow = objEventNzPos[iphase]->parameter->ptrRow;
  733. iniStateNzIdxCol = objEventNzPos[iphase]->iniState->idxCol;
  734. finStateNzIdxCol = objEventNzPos[iphase]->finState->idxCol;
  735. iniTimeNzIdxCol = objEventNzPos[iphase]->iniTime->idxCol;
  736. finTimeNzIdxCol = objEventNzPos[iphase]->finTime->idxCol;
  737. parameterNzIdxCol = objEventNzPos[iphase]->parameter->idxCol;
  738. // 每段各种所需部分的起始索引
  739. idxOptVarState0 = idxOptVarPhaseStart;
  740. idxOptVarStatef = idxOptVarPhaseStart + (numMesh - 1) * numState;
  741. idxOptVarT0 = idxOptVarPhaseStart + numMesh * (numState + numControl);
  742. idxOptVarTf = idxOptVarT0 + 1;
  743. idxOptVarParameter = idxOptVarPhaseStart + numMesh * (numState + numControl) + 2;
  744. idxOptVarTrust = idxOptVarParameter + numParameter;
  745. // x0 前面系数
  746. for (cnt = iniStateNzPtrRow[0]; cnt < iniStateNzPtrRow[1]; cnt++)
  747. {
  748. j = iniStateNzIdxCol[cnt];
  749. c[idxOptVarState0 + j] = objDerInfo->der[cntRowGrd + j];
  750. }
  751. cntRowGrd = cntRowGrd + numState;
  752. // xf 前面系数
  753. for (cnt = finStateNzPtrRow[0]; cnt < finStateNzPtrRow[1]; cnt++)
  754. {
  755. j = finStateNzIdxCol[cnt];
  756. c[idxOptVarStatef + j] = objDerInfo->der[cntRowGrd + j];
  757. }
  758. cntRowGrd = cntRowGrd + numState;
  759. // t0 前面系数
  760. for (cnt = iniTimeNzPtrRow[0]; cnt < iniTimeNzPtrRow[1]; cnt++)
  761. {
  762. j = iniTimeNzIdxCol[cnt];
  763. c[idxOptVarT0] = objDerInfo->der[cntRowGrd + j];
  764. }
  765. cntRowGrd = cntRowGrd + 1;
  766. // tf 前面系数
  767. for (cnt = finTimeNzPtrRow[0]; cnt < finTimeNzPtrRow[1]; cnt++)
  768. {
  769. j = finTimeNzIdxCol[cnt];
  770. c[idxOptVarTf] = objDerInfo->der[cntRowGrd + j];
  771. }
  772. cntRowGrd = cntRowGrd + 1;
  773. // p 前面系数
  774. for (cnt = parameterNzPtrRow[0]; cnt < parameterNzPtrRow[1]; cnt++)
  775. {
  776. j = parameterNzIdxCol[cnt];
  777. c[idxOptVarParameter + j] = objDerInfo->der[cntRowGrd + j];
  778. }
  779. cntRowGrd = cntRowGrd + numParameter;
  780. // Tr 前面系数
  781. c[idxOptVarTrust] = w;
  782. // 更新索引
  783. idxOptVarPhaseStart = idxOptVarPhaseStart + numPhaseOptVar[iphase];
  784. }
  785. //// 3-> 事件函数
  786. // 3->1 类型1:双不等式约束项
  787. // 上界: A * x0 + B * xf + C * t0 + D * tf + F * p <= b
  788. // 下界: - A * x0 - B * xf - C * t0 - D * tf - F * p <= b
  789. iType = 1;
  790. numType = consType->type[iType].num;
  791. idxType = consType->type[iType].idx;
  792. // 维数信息
  793. numNlEvent = dimension->event->nl;
  794. numSocEvent = dimension->event->soc->n;
  795. dimSocEvent = dimension->event->soc->q;
  796. if (numSocEvent)
  797. {
  798. numRowSocEvent = cmscp_sum(dimSocEvent, numSocEvent);
  799. numRowEvent = numNlEvent + numRowSocEvent;
  800. }
  801. else
  802. {
  803. numRowEvent = numNlEvent;
  804. }
  805. // 约束边界
  806. eventNlLower = bounds->event->nl->lower;
  807. eventNlUpper = bounds->event->nl->upper;
  808. eventSocUpper = bounds->event->soc;
  809. if (numType)
  810. {
  811. nnzType1 = getType1EventNnz(consType->type[iType], objEventNzPos, numPhase); // 第1类约束的一半非零元个数
  812. for (cnt = 0; cnt < numType; cnt++)
  813. {
  814. i = idxType[cnt]; // 在Event矩阵的行
  815. i0 = i + 1; // 在objEvent矩阵的行(Event第一行前加obj)
  816. cntRowAIneq = cntRowAIneq + 1;
  817. cntRowAIneqLower = cntRowAIneq + numType; // 下界部分的行位置
  818. idxOptVarPhaseStart = 0; // 各段起始索引
  819. idxGrdCol = 0; // 雅可比矩阵的列的计数器
  820. // 负左端常数项
  821. temp = -eventRef[i];
  822. // 每行首元素位置
  823. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  824. ptrRowACone[cntRowAIneq+ numType] = cntNzAIneq+ nnzType1;
  825. for (iphase = 0; iphase < numPhase; iphase++)
  826. {
  827. // 离散点个数和各种变量的维数
  828. numMesh = mesh[iphase]->num;
  829. numState = dimension->phase[iphase]->state;
  830. numControl = dimension->phase[iphase]->control;
  831. numParameter = dimension->phase[iphase]->parameter;
  832. // 稀疏信息
  833. iniStateNzPtrRow = objEventNzPos[iphase]->iniState->ptrRow;
  834. finStateNzPtrRow = objEventNzPos[iphase]->finState->ptrRow;
  835. iniTimeNzPtrRow = objEventNzPos[iphase]->iniTime->ptrRow;
  836. finTimeNzPtrRow = objEventNzPos[iphase]->finTime->ptrRow;
  837. parameterNzPtrRow = objEventNzPos[iphase]->parameter->ptrRow;
  838. iniStateNzIdxCol = objEventNzPos[iphase]->iniState->idxCol;
  839. finStateNzIdxCol = objEventNzPos[iphase]->finState->idxCol;
  840. parameterNzIdxCol = objEventNzPos[iphase]->parameter->idxCol;
  841. // 每段各种所需部分的起始索引
  842. idxOptVarState0 = idxOptVarPhaseStart;
  843. idxOptVarStatef = idxOptVarPhaseStart + (numMesh - 1) * numState;
  844. idxOptVarT0 = idxOptVarPhaseStart + numMesh * (numState + numControl);
  845. idxOptVarTf = idxOptVarT0 + 1;
  846. idxOptVarParameter = idxOptVarPhaseStart + numMesh * (numState + numControl) + 2;
  847. // 参考轨迹
  848. refState0 = refTraj[iphase]->state->v;
  849. refStatef = refTraj[iphase]->state->v + (numMesh - 1) * numState;
  850. refT0 = refTraj[iphase]->initialtime;
  851. refTf = refTraj[iphase]->finaltime;
  852. refParameter = refTraj[iphase]->parameter;
  853. // x0 前面系数
  854. for (p = iniStateNzPtrRow[i0]; p < iniStateNzPtrRow[i0 + 1]; p++)
  855. {
  856. j = iniStateNzIdxCol[p]; // 列坐标
  857. idxColACone[cntNzAIneq] = idxOptVarState0 + j;
  858. valueACone[cntNzAIneq] = eventGrd[i + (idxGrdCol + j) * numRowEvent];
  859. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  860. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  861. temp = temp + valueACone[cntNzAIneq] * refState0[j];
  862. cntNzAIneq = cntNzAIneq + 1;
  863. }
  864. idxGrdCol = idxGrdCol + numState;
  865. // xf 前面系数
  866. for (p = finStateNzPtrRow[i0]; p < finStateNzPtrRow[i0 + 1]; p++)
  867. {
  868. j = finStateNzIdxCol[p]; // 列
  869. idxColACone[cntNzAIneq] = idxOptVarStatef + j;
  870. valueACone[cntNzAIneq] = eventGrd[i + (idxGrdCol + j) * numRowEvent];
  871. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  872. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  873. temp = temp + valueACone[cntNzAIneq] * refStatef[j];
  874. cntNzAIneq = cntNzAIneq + 1;
  875. }
  876. idxGrdCol = idxGrdCol + numState;
  877. // t0 前面系数
  878. if (iniTimeNzPtrRow[i0] + 1 == iniTimeNzPtrRow[i0 + 1])
  879. {
  880. idxColACone[cntNzAIneq] = idxOptVarT0;
  881. valueACone[cntNzAIneq] = eventGrd[i + idxGrdCol * numRowEvent];
  882. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  883. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  884. temp = temp + valueACone[cntNzAIneq] * refT0;
  885. cntNzAIneq = cntNzAIneq + 1;
  886. }
  887. idxGrdCol = idxGrdCol + 1;
  888. // tf 前面系数
  889. if (finTimeNzPtrRow[i0] + 1 == finTimeNzPtrRow[i0 + 1])
  890. {
  891. idxColACone[cntNzAIneq] = idxOptVarTf;
  892. valueACone[cntNzAIneq] = eventGrd[i + idxGrdCol * numRowEvent];
  893. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  894. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  895. temp = temp + valueACone[cntNzAIneq] * refTf;
  896. cntNzAIneq = cntNzAIneq + 1;
  897. }
  898. idxGrdCol = idxGrdCol + 1;
  899. // p 前面系数
  900. for (p = parameterNzPtrRow[i0]; p < parameterNzPtrRow[i0 + 1]; p++)
  901. {
  902. j = parameterNzIdxCol[p]; // 列
  903. idxColACone[cntNzAIneq] = idxOptVarParameter + j;
  904. valueACone[cntNzAIneq] = eventGrd[i + (idxGrdCol + j) * numRowEvent];
  905. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  906. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  907. temp = temp + valueACone[cntNzAIneq] * refParameter[j];
  908. cntNzAIneq = cntNzAIneq + 1;
  909. }
  910. idxGrdCol = idxGrdCol + numParameter;
  911. // 更新索引
  912. idxOptVarPhaseStart = idxOptVarPhaseStart + numPhaseOptVar[iphase];
  913. }
  914. // 右端项
  915. bCone[cntRowAIneq] = temp + eventNlUpper[i]; // 上界
  916. bCone[numType + cntRowAIneq] = -temp - eventNlLower[i]; // 下界
  917. cntRowAIneq = cntRowAIneq + 1;
  918. }
  919. cntNzAIneq = cntNzAIneq + nnzType1;
  920. cntRowAIneq = cntRowAIneq + numType;
  921. }
  922. // 3->2 类型2:等式约束项
  923. // A * x0 + B * xf + C * t0 + D * tf + F * p == b
  924. iType = 1;
  925. numType = consType->type[iType].num;
  926. idxType = consType->type[iType].idx;
  927. if (numType)
  928. {
  929. for (cnt = 0; cnt < numType; cnt++)
  930. {
  931. i = idxType[cnt];
  932. i0 = i + 1;
  933. idxOptVarPhaseStart = 0;
  934. idxGrdCol = 0; // 雅可比矩阵的列的计数器
  935. // 负左端常数项
  936. temp = -eventRef[i];
  937. // 每行首元素位置
  938. ptrRowACone[cntRowAEqua] = cntNzAEqua;
  939. for (iphase = 0; iphase < numPhase; iphase++)
  940. {
  941. // 离散点个数和各种变量的维数
  942. numMesh = mesh[iphase]->num;
  943. numState = dimension->phase[iphase]->state;
  944. numControl = dimension->phase[iphase]->control;
  945. numParameter = dimension->phase[iphase]->parameter;
  946. // 稀疏信息
  947. iniStateNzPtrRow = objEventNzPos[iphase]->iniState->ptrRow;
  948. finStateNzPtrRow = objEventNzPos[iphase]->finState->ptrRow;
  949. iniTimeNzPtrRow = objEventNzPos[iphase]->iniTime->ptrRow;
  950. finTimeNzPtrRow = objEventNzPos[iphase]->finTime->ptrRow;
  951. parameterNzPtrRow = objEventNzPos[iphase]->parameter->ptrRow;
  952. iniStateNzIdxCol = objEventNzPos[iphase]->iniState->idxCol;
  953. finStateNzIdxCol = objEventNzPos[iphase]->finState->idxCol;
  954. parameterNzIdxCol = objEventNzPos[iphase]->parameter->idxCol;
  955. // 每段各种所需部分的起始索引
  956. idxOptVarState0 = idxOptVarPhaseStart;
  957. idxOptVarStatef = idxOptVarPhaseStart + (numMesh - 1) * numState;
  958. idxOptVarT0 = idxOptVarPhaseStart + numMesh * (numState + numControl);
  959. idxOptVarTf = idxOptVarT0 + 1;
  960. idxOptVarParameter = idxOptVarPhaseStart + numMesh * (numState + numControl) + 2;
  961. // 参考轨迹
  962. refState0 = refTraj[iphase]->state->v;
  963. refStatef = refTraj[iphase]->state->v + (numMesh - 1) * numState;
  964. refT0 = refTraj[iphase]->initialtime;
  965. refTf = refTraj[iphase]->finaltime;
  966. refParameter = refTraj[iphase]->parameter;
  967. // x0 前面系数
  968. for (p = iniStateNzPtrRow[i0]; p < iniStateNzPtrRow[i0 + 1]; p++)
  969. {
  970. j = iniStateNzIdxCol[p]; // 列坐标
  971. cntNzAEqua = cntNzAEqua + 1;
  972. idxColAEqua[cntNzAEqua] = idxOptVarState0 + j;
  973. valueACone[cntNzAEqua] = eventGrd[i + (idxGrdCol + j) * numRowEvent];
  974. temp = temp + valueACone[cntNzAEqua] * refState0[j];
  975. }
  976. idxGrdCol = idxGrdCol + numState;
  977. // xf 前面系数
  978. for (p = finStateNzPtrRow[i0]; p < finStateNzPtrRow[i0 + 1]; p++)
  979. {
  980. j = finStateNzIdxCol[p]; // 列
  981. idxColAEqua[cntNzAEqua] = idxOptVarStatef + j;
  982. valueAEqua[cntNzAEqua] = eventGrd[i + (idxGrdCol + j) * numRowEvent];
  983. temp = temp + valueAEqua[cntNzAEqua] * refStatef[j];
  984. cntNzAEqua = cntNzAEqua + 1;
  985. }
  986. idxGrdCol = idxGrdCol + numState;
  987. // t0 前面系数
  988. if (iniTimeNzPtrRow[i0] + 1 == iniTimeNzPtrRow[i0 + 1])
  989. {
  990. idxColAEqua[cntNzAEqua] = idxOptVarT0;
  991. valueAEqua[cntNzAEqua] = eventGrd[i + idxGrdCol * numRowEvent];
  992. temp = temp + valueAEqua[cntNzAEqua] * refT0;
  993. cntNzAEqua = cntNzAEqua + 1;
  994. }
  995. idxGrdCol = idxGrdCol + 1;
  996. // tf 前面系数
  997. if (finTimeNzPtrRow[i0] + 1 == finTimeNzPtrRow[i0 + 1])
  998. {
  999. idxColAEqua[cntNzAEqua] = idxOptVarTf;
  1000. valueAEqua[cntNzAEqua] = eventGrd[i + idxGrdCol * numRowEvent];
  1001. temp = temp + valueAEqua[cntNzAEqua] * refTf;
  1002. cntNzAEqua = cntNzAEqua + 1;
  1003. }
  1004. idxGrdCol = idxGrdCol + 1;
  1005. // p 前面系数
  1006. for (p = parameterNzPtrRow[i0]; p < parameterNzPtrRow[i0 + 1]; p++)
  1007. {
  1008. j = parameterNzIdxCol[p]; // 列
  1009. idxColAEqua[cntNzAEqua] = idxOptVarParameter + j;
  1010. valueAEqua[cntNzAEqua] = eventGrd[i + idxGrdCol * numRowEvent];
  1011. temp = temp + valueAEqua[cntNzAEqua] * refParameter[j];
  1012. cntNzAEqua = cntNzAEqua + 1;
  1013. }
  1014. idxGrdCol = idxGrdCol + numParameter;
  1015. // 更新索引
  1016. idxOptVarPhaseStart = idxOptVarPhaseStart + numPhaseOptVar[iphase];
  1017. }
  1018. // 右端项
  1019. bEqua[cntRowAEqua] = temp + eventNlUpper[i];
  1020. cntRowAEqua = cntRowAEqua + 1;
  1021. }
  1022. }
  1023. // 3->3 类型3:上边界约束项
  1024. // A * x0 + B * xf + C * t0 + D * tf + F * p == b
  1025. iType = 2;
  1026. numType = consType->type[iType].num;
  1027. idxType = consType->type[iType].idx;
  1028. if (numType)
  1029. {
  1030. for (cnt = 0; cnt < numType; cnt++)
  1031. {
  1032. i = idxType[cnt];
  1033. i0 = i + 1;
  1034. idxOptVarPhaseStart = 0;
  1035. idxGrdCol = 0; // 雅可比矩阵的列的计数器
  1036. // 负左端常数项
  1037. temp = -eventRef[i];
  1038. // 每行首元素位置
  1039. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  1040. for (iphase = 0; iphase < numPhase; iphase++)
  1041. {
  1042. // 离散点个数和各种变量的维数
  1043. numMesh = mesh[iphase]->num;
  1044. numState = dimension->phase[iphase]->state;
  1045. numControl = dimension->phase[iphase]->control;
  1046. numParameter = dimension->phase[iphase]->parameter;
  1047. // 稀疏信息
  1048. iniStateNzPtrRow = objEventNzPos[iphase]->iniState->ptrRow;
  1049. finStateNzPtrRow = objEventNzPos[iphase]->finState->ptrRow;
  1050. iniTimeNzPtrRow = objEventNzPos[iphase]->iniTime->ptrRow;
  1051. finTimeNzPtrRow = objEventNzPos[iphase]->finTime->ptrRow;
  1052. parameterNzPtrRow = objEventNzPos[iphase]->parameter->ptrRow;
  1053. iniStateNzIdxCol = objEventNzPos[iphase]->iniState->idxCol;
  1054. finStateNzIdxCol = objEventNzPos[iphase]->finState->idxCol;
  1055. parameterNzIdxCol = objEventNzPos[iphase]->parameter->idxCol;
  1056. // 每段各种所需部分的起始索引
  1057. idxOptVarState0 = idxOptVarPhaseStart;
  1058. idxOptVarStatef = idxOptVarPhaseStart + (numMesh - 1) * numState;
  1059. idxOptVarT0 = idxOptVarPhaseStart + numMesh * (numState + numControl);
  1060. idxOptVarTf = idxOptVarT0 + 1;
  1061. idxOptVarParameter = idxOptVarPhaseStart + numMesh * (numState + numControl) + 2;
  1062. // 参考轨迹
  1063. refState0 = refTraj[iphase]->state->v;
  1064. refStatef = refTraj[iphase]->state->v + (numMesh - 1) * numState;
  1065. refT0 = refTraj[iphase]->initialtime;
  1066. refTf = refTraj[iphase]->finaltime;
  1067. refParameter = refTraj[iphase]->parameter;
  1068. // x0 前面系数
  1069. for (p = iniStateNzPtrRow[i0]; p < iniStateNzPtrRow[i0 + 1]; p++)
  1070. {
  1071. j = iniStateNzIdxCol[p]; // 列坐标
  1072. idxColACone[cntNzAIneq] = idxOptVarState0 + j;
  1073. valueACone[cntNzAIneq] = eventGrd[i + idxGrdCol * numRowEvent];
  1074. temp = temp + valueACone[cntNzAIneq] * refState0[j];
  1075. cntNzAIneq = cntNzAIneq + 1;
  1076. }
  1077. idxGrdCol = idxGrdCol + numState;
  1078. // xf 前面系数
  1079. for (p = finStateNzPtrRow[i0]; p < finStateNzPtrRow[i0 + 1]; p++)
  1080. {
  1081. j = finStateNzIdxCol[p]; // 列
  1082. idxColACone[cntNzAIneq] = idxOptVarStatef + j;
  1083. valueACone[cntNzAIneq] = eventGrd[i + idxGrdCol * numRowEvent];
  1084. temp = temp + valueACone[cntNzAIneq] * refStatef[j];
  1085. cntNzAIneq = cntNzAIneq + 1;
  1086. }
  1087. idxGrdCol = idxGrdCol + numState;
  1088. // t0 前面系数
  1089. if (iniTimeNzPtrRow[i0] + 1 == iniTimeNzPtrRow[i0 + 1])
  1090. {
  1091. cntNzAIneq = cntNzAIneq + 1;
  1092. valueACone[cntNzAIneq] = eventGrd[i + idxGrdCol * numRowEvent];
  1093. temp = temp + valueACone[cntNzAIneq] * refT0;
  1094. idxColACone[cntNzAIneq] = idxOptVarT0;
  1095. }
  1096. idxGrdCol = idxGrdCol + 1;
  1097. // tf 前面系数
  1098. if (finTimeNzPtrRow[i0] + 1 == finTimeNzPtrRow[i0 + 1])
  1099. {
  1100. cntNzAIneq = cntNzAIneq + 1;
  1101. valueACone[cntNzAIneq] = eventGrd[i + idxGrdCol * numRowEvent];
  1102. temp = temp + valueACone[cntNzAIneq] * refTf;
  1103. idxColACone[cntNzAIneq] = idxOptVarTf;
  1104. }
  1105. idxGrdCol = idxGrdCol + 1;
  1106. // p 前面系数
  1107. for (p = parameterNzPtrRow[i0]; p < parameterNzPtrRow[i0 + 1]; p++)
  1108. {
  1109. j = parameterNzIdxCol[p]; // 列
  1110. idxColACone[cntNzAIneq] = idxOptVarParameter + j;
  1111. valueACone[cntNzAIneq] = eventGrd[i + idxGrdCol * numRowEvent];
  1112. temp = temp + valueACone[cntNzAIneq] * refParameter[j];
  1113. cntNzAIneq = cntNzAIneq + 1;
  1114. }
  1115. idxGrdCol = idxGrdCol + numParameter;
  1116. // 更新索引
  1117. idxOptVarPhaseStart = idxOptVarPhaseStart + numPhaseOptVar[iphase];
  1118. }
  1119. // 右端项
  1120. bEqua[cntRowAIneq] = temp + eventNlUpper[i];
  1121. cntRowAIneq = cntRowAIneq + 1;
  1122. }
  1123. }
  1124. // 3->4 类型4:下边界约束项
  1125. // A * x0 + B * xf + C * t0 + D * tf + F * p == b
  1126. iType = 3;
  1127. numType = consType->type[iType].num;
  1128. idxType = consType->type[iType].idx;
  1129. if (numType)
  1130. {
  1131. for (cnt = 0; cnt < numType; cnt++)
  1132. {
  1133. i = idxType[cnt];
  1134. i0 = i + 1;
  1135. idxOptVarPhaseStart = 0;
  1136. idxGrdCol = 0; // 雅可比矩阵的列的计数器
  1137. // 负左端常数项
  1138. temp = -eventRef[i];
  1139. // 每行首元素位置
  1140. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  1141. for (iphase = 0; iphase < numPhase; iphase++)
  1142. {
  1143. // 离散点个数和各种变量的维数
  1144. numMesh = mesh[iphase]->num;
  1145. numState = dimension->phase[iphase]->state;
  1146. numControl = dimension->phase[iphase]->control;
  1147. numParameter = dimension->phase[iphase]->parameter;
  1148. // 稀疏信息
  1149. iniStateNzPtrRow = objEventNzPos[iphase]->iniState->ptrRow;
  1150. finStateNzPtrRow = objEventNzPos[iphase]->finState->ptrRow;
  1151. iniTimeNzPtrRow = objEventNzPos[iphase]->iniTime->ptrRow;
  1152. finTimeNzPtrRow = objEventNzPos[iphase]->finTime->ptrRow;
  1153. parameterNzPtrRow = objEventNzPos[iphase]->parameter->ptrRow;
  1154. iniStateNzIdxCol = objEventNzPos[iphase]->iniState->idxCol;
  1155. finStateNzIdxCol = objEventNzPos[iphase]->finState->idxCol;
  1156. parameterNzIdxCol = objEventNzPos[iphase]->parameter->idxCol;
  1157. // 每段各种所需部分的起始索引
  1158. idxOptVarState0 = idxOptVarPhaseStart;
  1159. idxOptVarStatef = idxOptVarPhaseStart + (numMesh - 1) * numState;
  1160. idxOptVarT0 = idxOptVarPhaseStart + numMesh * (numState + numControl);
  1161. idxOptVarTf = idxOptVarT0 + 1;
  1162. idxOptVarParameter = idxOptVarPhaseStart + numMesh * (numState + numControl) + 2;
  1163. // 参考轨迹
  1164. refState0 = refTraj[iphase]->state->v;
  1165. refStatef = refTraj[iphase]->state->v + (numMesh - 1) * numState;
  1166. refT0 = refTraj[iphase]->initialtime;
  1167. refTf = refTraj[iphase]->finaltime;
  1168. refParameter = refTraj[iphase]->parameter;
  1169. // x0 前面系数
  1170. for (p = iniStateNzPtrRow[i0]; p < iniStateNzPtrRow[i0 + 1]; p++)
  1171. {
  1172. j = iniStateNzIdxCol[p]; // 列坐标
  1173. idxColACone[cntNzAIneq] = idxOptVarState0 + j;
  1174. valueACone[cntNzAIneq] = -eventGrd[i + idxGrdCol * numRowEvent];
  1175. temp = temp + valueACone[cntNzAIneq] * refState0[j];
  1176. cntNzAIneq = cntNzAIneq + 1;
  1177. }
  1178. idxGrdCol = idxGrdCol + numState;
  1179. // xf 前面系数
  1180. for (p = finStateNzPtrRow[i0]; p < finStateNzPtrRow[i0 + 1]; p++)
  1181. {
  1182. j = finStateNzIdxCol[p]; // 列
  1183. idxColACone[cntNzAIneq] = idxOptVarStatef + j;
  1184. valueACone[cntNzAIneq] = -eventGrd[i + idxGrdCol * numRowEvent];
  1185. temp = temp + valueACone[cntNzAIneq] * refStatef[j];
  1186. cntNzAIneq = cntNzAIneq + 1;
  1187. }
  1188. idxGrdCol = idxGrdCol + numState;
  1189. // t0 前面系数
  1190. if (iniTimeNzPtrRow[i0] + 1 == iniTimeNzPtrRow[i0 + 1])
  1191. {
  1192. cntNzAIneq = cntNzAIneq + 1;
  1193. idxColACone[cntNzAIneq] = idxOptVarT0;
  1194. valueACone[cntNzAIneq] = -eventGrd[i + idxGrdCol * numRowEvent];
  1195. temp = temp + valueACone[cntNzAIneq] * refT0;
  1196. }
  1197. idxGrdCol = idxGrdCol + 1;
  1198. // tf 前面系数
  1199. if (finTimeNzPtrRow[i0] + 1 == finTimeNzPtrRow[i0 + 1])
  1200. {
  1201. idxColACone[cntNzAIneq] = idxOptVarTf;
  1202. valueACone[cntNzAIneq] = -eventGrd[i + idxGrdCol * numRowEvent];
  1203. temp = temp + valueACone[cntNzAIneq] * refTf;
  1204. cntNzAIneq = cntNzAIneq + 1;
  1205. }
  1206. idxGrdCol = idxGrdCol + 1;
  1207. // p 前面系数
  1208. for (p = parameterNzPtrRow[i0]; p < parameterNzPtrRow[i0 + 1]; p++)
  1209. {
  1210. j = parameterNzIdxCol[p]; // 列
  1211. idxColACone[cntNzAIneq] = idxOptVarParameter + j;
  1212. valueACone[cntNzAIneq] = -eventGrd[i + idxGrdCol * numRowEvent];
  1213. temp = temp + valueACone[cntNzAIneq] * refParameter[j];
  1214. cntNzAIneq = cntNzAIneq + 1;
  1215. }
  1216. idxGrdCol = idxGrdCol + numParameter;
  1217. // 更新索引
  1218. idxOptVarPhaseStart = idxOptVarPhaseStart + numPhaseOptVar[iphase];
  1219. }
  1220. // 右端项
  1221. bEqua[cntRowAIneq] = temp + eventNlUpper[i];
  1222. cntRowAIneq = cntRowAIneq + 1;
  1223. }
  1224. }
  1225. // 3->5 二阶锥约束
  1226. // 事件约束维度信息
  1227. if (numSocEvent)
  1228. {
  1229. for (i = 0; i < numRowSocEvent; i++)
  1230. {
  1231. i0 = numNlEvent + 1 + i;
  1232. temp = -eventRef[numNlEvent + i];
  1233. // 每行首个元素的位置
  1234. ptrRowACone[cntRowASoc] = cntNzASoc;
  1235. idxOptVarPhaseStart = 0;// 各段起始索引
  1236. idxGrdCol = 0;// 雅可比矩阵的列的计数
  1237. for (iphase = 0; iphase < numPhase; iphase++)
  1238. {
  1239. // 离散点个数和各种变量的维数
  1240. numMesh = mesh[iphase]->num;
  1241. numState = dimension->phase[iphase]->state;
  1242. numControl = dimension->phase[iphase]->control;
  1243. numParameter = dimension->phase[iphase]->parameter;
  1244. // 稀疏信息
  1245. iniStateNzPtrRow = objEventNzPos[iphase]->iniState->ptrRow;
  1246. finStateNzPtrRow = objEventNzPos[iphase]->finState->ptrRow;
  1247. iniTimeNzPtrRow = objEventNzPos[iphase]->iniTime->ptrRow;
  1248. finTimeNzPtrRow = objEventNzPos[iphase]->finTime->ptrRow;
  1249. parameterNzPtrRow = objEventNzPos[iphase]->parameter->ptrRow;
  1250. iniStateNzIdxCol = objEventNzPos[iphase]->iniState->idxCol;
  1251. finStateNzIdxCol = objEventNzPos[iphase]->finState->idxCol;
  1252. parameterNzIdxCol = objEventNzPos[iphase]->parameter->idxCol;
  1253. // 每段各种所需部分的起始索引
  1254. idxOptVarState0 = idxOptVarPhaseStart;
  1255. idxOptVarStatef = idxOptVarPhaseStart + (numMesh - 1) * numState;
  1256. idxOptVarT0 = idxOptVarPhaseStart + numMesh * (numState + numControl);
  1257. idxOptVarTf = idxOptVarT0 + 1;
  1258. idxOptVarParameter = idxOptVarPhaseStart + numMesh * (numState + numControl) + 2;
  1259. // 参考轨迹
  1260. refState0 = refTraj[iphase]->state->v;
  1261. refStatef = refTraj[iphase]->state->v + (numMesh - 1) * numState;
  1262. refT0 = refTraj[iphase]->initialtime;
  1263. refTf = refTraj[iphase]->finaltime;
  1264. refParameter = refTraj[iphase]->parameter;
  1265. // x0 前面系数
  1266. for (p = iniStateNzPtrRow[i0]; p < iniStateNzPtrRow[i0 + 1]; p++)
  1267. {
  1268. j = iniStateNzIdxCol[p]; // 列
  1269. cntNzASoc = cntNzASoc + 1;
  1270. idxColACone[cntNzASoc] = idxOptVarState0 + j;
  1271. valueACone[cntNzASoc] = eventGrd[numNlEvent + i + (idxGrdCol + j) * numRowEvent];
  1272. temp = temp + valueACone[cntNzASoc] * refState0[j];
  1273. }
  1274. idxGrdCol = idxGrdCol + numState;
  1275. // xf 前面系数
  1276. for (p = finStateNzPtrRow[i0]; p < finStateNzPtrRow[i0 + 1]; p++)
  1277. {
  1278. j = finStateNzIdxCol[p]; // 列
  1279. cntNzASoc = cntNzASoc + 1;
  1280. idxColACone[cntNzASoc] = idxOptVarStatef + j;
  1281. valueACone[cntNzASoc] = eventGrd[numNlEvent + i + (idxGrdCol + j) * numRowEvent];
  1282. temp = temp + valueACone[cntNzASoc] * refStatef[j];
  1283. }
  1284. idxGrdCol = idxGrdCol + numState;
  1285. // t0 前面系数
  1286. if (iniTimeNzPtrRow[i0] + 1 == iniTimeNzPtrRow[i0 + 1])
  1287. {
  1288. cntNzASoc = cntNzASoc + 1;
  1289. idxColACone[cntNzASoc] = idxOptVarT0 + 1;
  1290. valueACone[cntNzASoc] = eventGrd[numNlEvent + i + (idxGrdCol) * numRowEvent];
  1291. temp = temp + valueACone[cntNzASoc] * refT0;
  1292. }
  1293. idxGrdCol = idxGrdCol + 1;
  1294. // tf 前面系数
  1295. if (finTimeNzPtrRow[i0] + 1 == finTimeNzPtrRow[i0 + 1])
  1296. {
  1297. cntNzASoc = cntNzASoc + 1;
  1298. idxColACone[cntNzASoc] = idxOptVarTf + 1;
  1299. valueACone[cntNzASoc] = eventGrd[numNlEvent + i + (idxGrdCol) * numRowEvent];
  1300. temp = temp + valueACone[cntNzASoc] * refTf;
  1301. }
  1302. idxGrdCol = idxGrdCol + 1;
  1303. // p 前面系数
  1304. for (p = parameterNzPtrRow[i0]; p < parameterNzPtrRow[i0 + 1]; p++)
  1305. {
  1306. j = parameterNzIdxCol[p]; // 列
  1307. cntNzASoc = cntNzASoc + 1;
  1308. idxColACone[cntNzASoc] = idxOptVarParameter + j;
  1309. valueACone[cntNzASoc] = eventGrd[numNlEvent + i + (idxGrdCol + j) * numRowEvent];
  1310. temp = temp + valueACone[cntNzASoc] * refParameter[j];
  1311. }
  1312. idxGrdCol = idxGrdCol + numParameter;
  1313. // 更新索引
  1314. idxOptVarPhaseStart = idxOptVarPhaseStart + numPhaseOptVar[iphase];
  1315. }
  1316. cntRowASoc = cntRowASoc + 1;
  1317. }
  1318. for (i = 0; i < numSocEvent; i++)
  1319. {
  1320. dimCone->q[cntSubASoc] = dimSocEvent[i];
  1321. cntSubASoc = cntSubASoc + 1;
  1322. }
  1323. }
  1324. if (objDerInfo->ref) { free(objDerInfo->ref); }
  1325. if (objDerInfo->der) { free(objDerInfo->der); }
  1326. if (objDerInfo) { free(objDerInfo); }
  1327. if (eventDerInfo->ref) { free(eventDerInfo->ref); }
  1328. if (eventDerInfo->der) { free(eventDerInfo->der); }
  1329. if (eventDerInfo) { free(eventDerInfo); }
  1330. }
  1331. void socpInfoSparseEndp(cmscp_socpInfo** socpInfo0, cmscp_trajInfo** refTraj, cmscp_sparseInfo* sparseInfo, cmscp_consTypeInfo* consTypeInfo, cmscp_setup* setup) {
  1332. // 函数功能:获取端点函数约束的二阶锥规划问题参数
  1333. // 输入:
  1334. // socpInfo0:无获端点函数约束相关的二阶锥问题参数
  1335. // refTraj:参考轨迹
  1336. // consTypeInfo:约束类型信息
  1337. // sparseInfo:稀疏(非零元位置)信息
  1338. // setup:问题设置参数
  1339. // 输出:
  1340. // socpInfo0:有端点函数约束相关的二阶锥问题参数
  1341. // 假设函数的自变量按照[x, u, p]排序
  1342. // 谢磊:2022 / 6 / 27编写
  1343. // 变量声明
  1344. int numPhase, * numPhaseOptVar, iphase, numMesh, numState, numControl, iType, numType, * idxType, cnt, numNlEndp, * dimSocEndp, * idxColAEqua, * ptrRowAEqua, * idxColACone, * ptrRowACone, cntNzAEqua, cntRowAEqua, cntNzAIneq, cntRowAIneq, cntNzASoc, cntRowASoc, cntSubASoc, idxOptVarPhaseStart, numSocEndp, numRowSocEndp, * iniStateNzPtrRow, * finStateNzPtrRow, * iniTimeNzPtrRow, * finTimeNzPtrRow, * parameterNzPtrRow, * iniStateNzIdxCol, * finStateNzIdxCol, * parameterNzIdxCol, idxEndpGrdColStatef, idxEndpGrdColT0, idxEndpGrdColTf, idxEndpGrdColParameter, idxOptVarState0, idxOptVarStatef, idxOptVarT0, idxOptVarTf, idxOptVarParameter, nnzType1, i, p, j, numEndpOutput, i0, numOptVar;
  1345. double* valueAEqua, * bEqua, * valueACone, * bCone, * endpNlLower, * endpNlUpper, * endpSocUpper, * refState0, * refStatef, refT0, refTf, * refParameter, * endpRef, * endpGrd, temp, *c;
  1346. cmscp_dimension* dimension;
  1347. cmscp_mesh** mesh;
  1348. cmscp_bound* bounds;
  1349. cmscp_trust* trust;
  1350. cmscp_consType** consType;
  1351. cmscp_type* consTypePhase;
  1352. cmscp_nzPos_endp** endpNzPos;
  1353. cmscp_derInfo** gradInfo;
  1354. cmscp_socpInfo* socpInfo;
  1355. cmscp_dimCone* dimCone;
  1356. // 1. 基本参数
  1357. dimension = setup->dimension;// 问题维数信息
  1358. mesh = setup->mesh;// 网格信息
  1359. numPhase = setup->numPhase;// 阶段数
  1360. bounds = setup->bounds;// 边界信息
  1361. trust = setup->trust;// 信赖域信息
  1362. socpInfo = (*socpInfo0);
  1363. numOptVar = socpInfo->numOptVar;
  1364. numPhaseOptVar = socpInfo->numPhaseOptVar;// 每段优化变量个数
  1365. endpNzPos = sparseInfo->endpNzPos;// 稀疏信息
  1366. consType = consTypeInfo->endp;// 约束类型信息
  1367. // 二阶锥规划参数
  1368. ptrRowAEqua = socpInfo->AEqua->ptrRow;
  1369. idxColAEqua = socpInfo->AEqua->idxCol;
  1370. valueAEqua = socpInfo->AEqua->v;
  1371. bEqua = socpInfo->bEqua;
  1372. ptrRowACone = socpInfo->ACone->ptrRow;
  1373. idxColACone = socpInfo->ACone->idxCol;
  1374. valueACone = socpInfo->ACone->v;
  1375. bCone = socpInfo->bCone;
  1376. dimCone = socpInfo->dimCone;
  1377. // 计数变量
  1378. cntNzAEqua = socpInfo->cntNzAEqua;
  1379. cntRowAEqua = socpInfo->cntRowAEqua;
  1380. cntNzAIneq = socpInfo->cntNzAIneq;
  1381. cntRowAIneq = socpInfo->cntRowAIneq;
  1382. cntNzASoc = socpInfo->cntNzASoc;
  1383. cntRowASoc = socpInfo->cntRowASoc;
  1384. cntSubASoc = socpInfo->cntSubASoc;
  1385. // 梯度信息
  1386. gradInfo = (cmscp_derInfo**)malloc(numPhase * sizeof(cmscp_derInfo));
  1387. gradInfoSparseEndp(gradInfo, sparseInfo, refTraj, setup);
  1388. //// 填充非零元素
  1389. // 优化变量中各段起始索引
  1390. idxOptVarPhaseStart = 0;
  1391. for (iphase = 0; iphase < numPhase; iphase++)
  1392. {
  1393. // 各段维数信息
  1394. numMesh = mesh[iphase]->num;
  1395. numState = dimension->phase[iphase]->state;
  1396. numControl = dimension->phase[iphase]->control;
  1397. numNlEndp = dimension->phase[iphase]->endpoint->nl;
  1398. numSocEndp = dimension->phase[iphase]->endpoint->soc->n;
  1399. dimSocEndp = dimension->phase[iphase]->endpoint->soc->q;
  1400. if (numSocEndp)
  1401. {
  1402. numRowSocEndp = cmscp_sum(dimSocEndp, numSocEndp);
  1403. }
  1404. else
  1405. {
  1406. numRowSocEndp = 0;
  1407. }
  1408. if (dimension->phase[iphase]->endpoint->soc->n)
  1409. {
  1410. numEndpOutput = numNlEndp + cmscp_sum(dimSocEndp, numSocEndp);
  1411. }
  1412. else
  1413. {
  1414. numEndpOutput = numNlEndp;
  1415. }
  1416. // 各段约束类型信息
  1417. consTypePhase = consType[iphase]->type;
  1418. // 各段稀疏信息
  1419. iniStateNzPtrRow = endpNzPos[iphase]->iniState->ptrRow;
  1420. finStateNzPtrRow = endpNzPos[iphase]->finState->ptrRow;
  1421. iniTimeNzPtrRow = endpNzPos[iphase]->iniTime->ptrRow;
  1422. finTimeNzPtrRow = endpNzPos[iphase]->finTime->ptrRow;
  1423. parameterNzPtrRow = endpNzPos[iphase]->parameter->ptrRow;
  1424. iniStateNzIdxCol = endpNzPos[iphase]->iniState->idxCol;
  1425. finStateNzIdxCol = endpNzPos[iphase]->finState->idxCol;
  1426. parameterNzIdxCol = endpNzPos[iphase]->parameter->idxCol;
  1427. // 各段约束上下界
  1428. endpNlLower = bounds->phase[iphase]->endpoint->nl->lower;
  1429. endpNlUpper = bounds->phase[iphase]->endpoint->nl->upper;
  1430. endpSocUpper = bounds->phase[iphase]->endpoint->soc;
  1431. // 各段参考轨迹
  1432. refState0 = refTraj[iphase]->state->v;
  1433. refStatef = refTraj[iphase]->state->v + (numMesh - 1) * numState;
  1434. refT0 = refTraj[iphase]->initialtime;
  1435. refTf = refTraj[iphase]->finaltime;
  1436. refParameter = refTraj[iphase]->parameter;
  1437. // 各段梯度信息
  1438. endpRef = gradInfo[iphase]->ref;
  1439. endpGrd = gradInfo[iphase]->der;
  1440. // 各段所需变量的起始索引
  1441. idxEndpGrdColStatef = numState;// EndpGrd矩阵中终端点状态的对应的列的起始索引
  1442. idxEndpGrdColT0 = 2 * numState;// EndpGrd矩阵中初始时刻的对应的列的起始索引
  1443. idxEndpGrdColTf = idxEndpGrdColT0 + 1;// EndpGrd矩阵中终端时刻的对应的列的起始索引
  1444. idxEndpGrdColParameter = idxEndpGrdColTf + 1;// EndpGrd矩阵中终端时刻的对应的列的起始索引
  1445. idxOptVarState0 = idxOptVarPhaseStart;// 优化变量中每段的初始点状态的索引
  1446. idxOptVarStatef = idxOptVarPhaseStart + (numMesh - 1) * numState;// 优化变量中每段的终端点状态的索引
  1447. idxOptVarT0 = idxOptVarPhaseStart + numMesh * (numState + numControl);// 优化变量中每段的初始点时刻的索引
  1448. idxOptVarTf = idxOptVarT0 + 1;// 优化变量中每段的终端点时刻的索引
  1449. idxOptVarParameter = idxOptVarTf + 1;// 优化变量中每段的静态参数的索引
  1450. //// 1 类型1:双不等式约束项
  1451. // 上界:A * x0 + B * xf + C * t0 + D * tf + F * p <= b
  1452. // 下界: - A * x0 - B * xf - C * t0 - D * tf - F * p <= b
  1453. iType = 0;
  1454. numType = consTypePhase[iType].num;
  1455. idxType = consTypePhase[iType].idx;
  1456. if (numType)
  1457. {
  1458. nnzType1 = getType1EndpNnz(consTypePhase[iType], endpNzPos[iphase]);// 一半非零元个数
  1459. for (cnt = 0; cnt < numType; cnt++)
  1460. {
  1461. i = idxType[cnt];
  1462. temp = -endpRef[i];
  1463. // 每行首个非零元的位置
  1464. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  1465. ptrRowACone[cntRowAIneq + numType] = cntNzAIneq + nnzType1;
  1466. // x0前面系数
  1467. for (p = iniStateNzPtrRow[i]; p < iniStateNzPtrRow[i + 1]; p++)
  1468. {
  1469. j = iniStateNzIdxCol[p];// 列
  1470. idxColACone[cntNzAIneq] = idxOptVarState0 + j;
  1471. valueACone[cntNzAIneq] = endpGrd[i+j * numEndpOutput];
  1472. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  1473. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  1474. temp = temp + valueACone[cntNzAIneq] * refState0[j];
  1475. cntNzAIneq = cntNzAIneq + 1;
  1476. }
  1477. // xf前面系数
  1478. for (p = finStateNzPtrRow[i]; p < finStateNzPtrRow[i + 1]; p++)
  1479. {
  1480. j = finStateNzIdxCol[p];// 列
  1481. idxColACone[cntNzAIneq] = idxOptVarStatef + j;
  1482. valueACone[cntNzAIneq] = endpGrd[i+(idxEndpGrdColStatef + j) * numEndpOutput];
  1483. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  1484. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  1485. temp = temp + valueACone[cntNzAIneq] * refStatef[j];
  1486. cntNzAIneq = cntNzAIneq + 1;
  1487. }
  1488. // t0前面系数
  1489. if (iniTimeNzPtrRow[i] + 1 == iniTimeNzPtrRow[i + 1])
  1490. {
  1491. idxColACone[cntNzAIneq] = idxOptVarT0;
  1492. valueACone[cntNzAIneq] = endpGrd[i+idxEndpGrdColT0 * numEndpOutput];
  1493. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  1494. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  1495. temp = temp + valueACone[cntNzAIneq] * refT0;
  1496. cntNzAIneq = cntNzAIneq + 1;
  1497. }
  1498. // tf前面系数
  1499. if (finTimeNzPtrRow[i] + 1 == finTimeNzPtrRow[i + 1]) {
  1500. idxColACone[cntNzAIneq] = idxOptVarTf;
  1501. valueACone[cntNzAIneq] = endpGrd[i+idxEndpGrdColTf * numEndpOutput];
  1502. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  1503. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  1504. temp = temp + valueACone[cntNzAIneq] * refTf;
  1505. cntNzAIneq = cntNzAIneq + 1;
  1506. }
  1507. // p前面系数
  1508. for (p = parameterNzPtrRow[i]; p < parameterNzPtrRow[i + 1]; p++)
  1509. {
  1510. j = parameterNzIdxCol[p];// 列
  1511. idxColACone[cntNzAIneq] = idxOptVarParameter + j;
  1512. valueACone[cntNzAIneq] = endpGrd[i+(idxEndpGrdColParameter + j) * numEndpOutput];
  1513. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  1514. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  1515. temp = temp + valueACone[cntNzAIneq] * refParameter[j];
  1516. cntNzAIneq = cntNzAIneq + 1;
  1517. }
  1518. bCone[cntRowAIneq] = temp + endpNlUpper[i];
  1519. bCone[cntRowAIneq + numType] = -temp - endpNlLower[i];
  1520. cntRowAIneq = cntRowAIneq + 1;
  1521. }
  1522. cntNzAIneq = cntNzAIneq + nnzType1;
  1523. cntRowAIneq = cntRowAIneq + numType;
  1524. }
  1525. //// 2 类型2:等式约束项,表达式:
  1526. // A * x0 + B * xf + C * t0 + D * tf + F * p == b
  1527. iType = 1;
  1528. numType = consTypePhase[iType].num;
  1529. idxType = consTypePhase[iType].idx;
  1530. if (numType)
  1531. {
  1532. for (cnt = 0; cnt < numType; cnt++)
  1533. {
  1534. i = idxType[cnt];
  1535. temp = -endpRef[i];
  1536. // 每行首个非零元的位置
  1537. ptrRowAEqua[cntRowAEqua] = cntNzAEqua;
  1538. // x0前面系数
  1539. for (p = iniStateNzPtrRow[i]; p < iniStateNzPtrRow[i + 1]; p++)
  1540. {
  1541. j = iniStateNzIdxCol[p];// 列
  1542. idxColAEqua[cntNzAEqua] = idxOptVarState0 + j;
  1543. valueAEqua[cntNzAEqua] = endpGrd[i+j * numEndpOutput];
  1544. temp = temp + valueAEqua[cntNzAEqua] * refState0[j];
  1545. cntNzAEqua = cntNzAEqua + 1;
  1546. }
  1547. // xf前面系数
  1548. for (p = finStateNzPtrRow[i]; p < finStateNzPtrRow[i + 1]; p++)
  1549. {
  1550. j = finStateNzIdxCol[p];// 列
  1551. idxColAEqua[cntNzAEqua] = idxOptVarStatef + j;
  1552. valueAEqua[cntNzAEqua] = endpGrd[i+(idxEndpGrdColStatef + j) * numEndpOutput];
  1553. temp = temp + valueAEqua[cntNzAEqua] * refStatef[j];
  1554. cntNzAEqua = cntNzAEqua + 1;
  1555. }
  1556. // t0前面系数
  1557. if (iniTimeNzPtrRow[i] + 1 == iniTimeNzPtrRow[i + 1])
  1558. {
  1559. idxColAEqua[cntNzAEqua] = idxOptVarT0;
  1560. valueAEqua[cntNzAEqua] = endpGrd[i+idxEndpGrdColT0 * numEndpOutput];
  1561. temp = temp + valueAEqua[cntNzAEqua] * refT0;
  1562. cntNzAEqua = cntNzAEqua + 1;
  1563. }
  1564. // tf前面系数
  1565. if (finTimeNzPtrRow[i] + 1 == finTimeNzPtrRow[i + 1])
  1566. {
  1567. idxColAEqua[cntNzAEqua] = idxOptVarTf;
  1568. valueAEqua[cntNzAEqua] = endpGrd[i+idxEndpGrdColTf * numEndpOutput];
  1569. temp = temp + valueAEqua[cntNzAEqua] * refTf;
  1570. cntNzAEqua = cntNzAEqua + 1;
  1571. }
  1572. // p前面系数
  1573. for (p = parameterNzPtrRow[i]; p < parameterNzPtrRow[i + 1]; p++)
  1574. {
  1575. j = parameterNzIdxCol[p];// 列
  1576. idxColAEqua[cntNzAEqua] = idxOptVarParameter + j;
  1577. valueAEqua[cntNzAEqua] = endpGrd[i+(idxEndpGrdColParameter + j) * numEndpOutput];
  1578. temp = temp + valueAEqua[cntNzAEqua] * refParameter[j];
  1579. cntNzAEqua = cntNzAEqua + 1;
  1580. }
  1581. bEqua[cntRowAEqua] = temp + endpNlUpper[i];
  1582. cntRowAEqua = cntRowAEqua + 1;
  1583. }
  1584. }
  1585. //// 类型3:上不等式约束项,表达式:
  1586. // A* x0 + B * xf + C * t0 + D * tf + F * p <= b
  1587. iType = 2;
  1588. numType = consTypePhase[iType].num;
  1589. idxType = consTypePhase[iType].idx;
  1590. if (numType)
  1591. {
  1592. for (cnt = 0; cnt < numType; cnt++)
  1593. {
  1594. i = idxType[cnt];
  1595. temp = -endpRef[i];
  1596. // 每行首个非零元的位置
  1597. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  1598. // x0前面系数
  1599. for (p = iniStateNzPtrRow[i]; p < iniStateNzPtrRow[i + 1]; p++)
  1600. {
  1601. j = iniStateNzIdxCol[p];// 列
  1602. idxColACone[cntNzAIneq] = idxOptVarState0 + j;
  1603. valueACone[cntNzAIneq] = endpGrd[i+j * numEndpOutput];
  1604. temp = temp + valueACone[cntNzAIneq] * refState0[j];
  1605. cntNzAIneq = cntNzAIneq + 1;
  1606. }
  1607. // xf前面系数
  1608. for (p = finStateNzPtrRow[i]; p < finStateNzPtrRow[i + 1]; p++)
  1609. {
  1610. j = finStateNzIdxCol[p];// 列
  1611. idxColACone[cntNzAIneq] = idxOptVarStatef + j;
  1612. valueACone[cntNzAIneq] = endpGrd[i+(idxEndpGrdColStatef + j) * numEndpOutput];
  1613. temp = temp + valueACone[cntNzAIneq] * refStatef[j];
  1614. cntNzAIneq = cntNzAIneq + 1;
  1615. }
  1616. // t0前面系数
  1617. if (iniTimeNzPtrRow[i] + 1 == iniTimeNzPtrRow[i + 1])
  1618. {
  1619. idxColACone[cntNzAIneq] = idxOptVarT0;
  1620. valueACone[cntNzAIneq] = endpGrd[i+idxEndpGrdColT0 * numEndpOutput];
  1621. temp = temp + valueACone[cntNzAIneq] * refT0;
  1622. cntNzAIneq = cntNzAIneq + 1;
  1623. }
  1624. // tf前面系数
  1625. if (finTimeNzPtrRow[i] + 1 == finTimeNzPtrRow[i + 1])
  1626. {
  1627. idxColACone[cntNzAIneq] = idxOptVarTf;
  1628. valueACone[cntNzAIneq] = endpGrd[i+idxEndpGrdColTf * numEndpOutput];
  1629. temp = temp + valueACone[cntNzAIneq] * refTf;
  1630. cntNzAIneq = cntNzAIneq + 1;
  1631. }
  1632. // p前面系数
  1633. for (p = parameterNzPtrRow[i]; p < parameterNzPtrRow[i + 1]; p++)
  1634. {
  1635. j = parameterNzIdxCol[p];// 列
  1636. idxColACone[cntNzAIneq] = idxOptVarParameter + j;
  1637. valueACone[cntNzAIneq] = endpGrd[i+(idxEndpGrdColParameter + j) * numEndpOutput];
  1638. temp = temp + valueACone[cntNzAIneq] * refParameter[j];
  1639. cntNzAIneq = cntNzAIneq + 1;
  1640. }
  1641. bCone[cntRowAIneq] = temp + endpNlUpper[i];
  1642. cntRowAIneq = cntRowAIneq + 1;
  1643. }
  1644. }
  1645. //// 类型4:下不等式约束项
  1646. // 表达式: - A * x0 - B * xf - C * t0 - D * tf - F * p <= -b
  1647. iType = 3;
  1648. numType = consTypePhase[iType].num;
  1649. idxType = consTypePhase[iType].idx;
  1650. if (numType)
  1651. {
  1652. for (cnt = 0; cnt < numType; cnt++)
  1653. {
  1654. i = idxType[cnt];
  1655. temp = endpRef[i];
  1656. // 每行首个非零元的位置
  1657. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  1658. // x0前面系数
  1659. for (p = iniStateNzPtrRow[i]; p < iniStateNzPtrRow[i + 1]; p++)
  1660. {
  1661. j = iniStateNzIdxCol[p];// 列
  1662. idxColACone[cntNzAIneq] = idxOptVarState0 + j;
  1663. valueACone[cntNzAIneq] = -endpGrd[i+j * numEndpOutput];
  1664. temp = temp + valueACone[cntNzAIneq] * refState0[j];
  1665. cntNzAIneq = cntNzAIneq + 1;
  1666. }
  1667. // xf前面系数
  1668. for (p = finStateNzPtrRow[i]; p < finStateNzPtrRow[i + 1]; p++)
  1669. {
  1670. j = finStateNzIdxCol[p];// 列
  1671. idxColACone[cntNzAIneq] = idxOptVarStatef + j;
  1672. valueACone[cntNzAIneq] = -endpGrd[i+(idxEndpGrdColStatef + j) * numEndpOutput];
  1673. temp = temp + valueACone[cntNzAIneq] * refStatef[j];
  1674. cntNzAIneq = cntNzAIneq + 1;
  1675. }
  1676. // t0前面系数
  1677. if (iniTimeNzPtrRow[i] + 1 == iniTimeNzPtrRow[i + 1])
  1678. {
  1679. idxColACone[cntNzAIneq] = idxOptVarT0;
  1680. valueACone[cntNzAIneq] = -endpGrd[i+idxEndpGrdColT0 * numEndpOutput];
  1681. temp = temp + valueACone[cntNzAIneq] * refT0;
  1682. cntNzAIneq = cntNzAIneq + 1;
  1683. }
  1684. // tf前面系数
  1685. if (finTimeNzPtrRow[i] + 1 == finTimeNzPtrRow[i + 1])
  1686. {
  1687. idxColACone[cntNzAIneq] = idxOptVarTf;
  1688. valueACone[cntNzAIneq] = -endpGrd[i+idxEndpGrdColTf * numEndpOutput];
  1689. temp = temp + valueACone[cntNzAIneq] * refTf;
  1690. cntNzAIneq = cntNzAIneq + 1;
  1691. }
  1692. // p前面系数
  1693. for (p = parameterNzPtrRow[i]; p < parameterNzPtrRow[i + 1]; p++)
  1694. {
  1695. j = parameterNzIdxCol[p];// 列
  1696. idxColACone[cntNzAIneq] = idxOptVarParameter + j;
  1697. valueACone[cntNzAIneq] = -endpGrd[i+(idxEndpGrdColParameter + j) * numEndpOutput];
  1698. temp = temp + valueACone[cntNzAIneq] * refParameter[j];
  1699. cntNzAIneq = cntNzAIneq + 1;
  1700. }
  1701. bCone[cntRowAIneq] = temp - endpNlLower[i];
  1702. cntRowAIneq = cntRowAIneq + 1;
  1703. }
  1704. }
  1705. if (numSocEndp)
  1706. {
  1707. for (i = 0; i < numRowSocEndp; i++)
  1708. {
  1709. i0 = numNlEndp + i;
  1710. temp = -endpRef[i0];
  1711. // 每行首个非零元的位置
  1712. ptrRowACone[cntRowASoc] = cntNzASoc;
  1713. // x0前面系数
  1714. for (p = iniStateNzPtrRow[i0]; p < iniStateNzPtrRow[i0 + 1]; p++)
  1715. {
  1716. j = iniStateNzIdxCol[p];// 列
  1717. idxColACone[cntNzASoc] = idxOptVarState0 + j;
  1718. valueACone[cntNzASoc] = endpGrd[i0+j * numEndpOutput];
  1719. temp = temp + valueACone[cntNzASoc] * refState0[j];
  1720. cntNzASoc = cntNzASoc + 1;
  1721. }
  1722. // xf前面系数
  1723. for (p = finStateNzPtrRow[i0]; p < finStateNzPtrRow[i0 + 1]; p++)
  1724. {
  1725. j = finStateNzIdxCol[p];// 列
  1726. idxColACone[cntNzASoc] = idxOptVarStatef + j;
  1727. valueACone[cntNzASoc] = endpGrd[i0+(idxEndpGrdColStatef + j) * numEndpOutput];
  1728. temp = temp + valueACone[cntNzASoc] * refStatef[j];
  1729. cntNzASoc = cntNzASoc + 1;
  1730. }
  1731. // t0前面系数
  1732. if (iniTimeNzPtrRow[i0] + 1 == iniTimeNzPtrRow[i0 + 1])
  1733. {
  1734. idxColACone[cntNzASoc] = idxOptVarT0;
  1735. valueACone[cntNzASoc] = endpGrd[i0+idxEndpGrdColT0 * numEndpOutput];
  1736. temp = temp + valueACone[cntNzASoc] * refT0;
  1737. cntNzASoc = cntNzASoc + 1;
  1738. }
  1739. // tf前面系数
  1740. if (finTimeNzPtrRow[i0] + 1 == finTimeNzPtrRow[i0 + 1])
  1741. {
  1742. idxColACone[cntNzASoc] = idxOptVarTf;
  1743. valueACone[cntNzASoc] = endpGrd[i0+idxEndpGrdColTf * numEndpOutput];
  1744. temp = temp + valueACone[cntNzASoc] * refTf;
  1745. cntNzASoc = cntNzASoc + 1;
  1746. }
  1747. // p前面系数
  1748. for (p = parameterNzPtrRow[i0]; p < parameterNzPtrRow[i0 + 1]; p++)
  1749. {
  1750. j = parameterNzIdxCol[p];// 列
  1751. idxColACone[cntNzASoc] = idxOptVarParameter + j;
  1752. valueACone[cntNzASoc] = endpGrd[i0+(idxEndpGrdColParameter + j) * numEndpOutput];
  1753. temp = temp + valueACone[cntNzASoc] * refParameter[j];
  1754. cntNzASoc = cntNzASoc + 1;
  1755. }
  1756. bCone[cntRowASoc] = temp + endpSocUpper[i];
  1757. cntRowASoc = cntRowASoc + 1;
  1758. }
  1759. for (i = 0; i < numSocEndp; i++)
  1760. {
  1761. dimCone->q[cntSubASoc] = dimSocEndp[i];
  1762. cntSubASoc = cntSubASoc + 1;
  1763. }
  1764. }
  1765. idxOptVarPhaseStart = idxOptVarPhaseStart + numPhaseOptVar[iphase];
  1766. free(gradInfo[iphase]->ref);
  1767. free(gradInfo[iphase]->der);
  1768. free(gradInfo[iphase]);
  1769. }
  1770. socpInfo->cntNzAEqua = cntNzAEqua;
  1771. socpInfo->cntRowAEqua = cntRowAEqua;
  1772. socpInfo->cntNzAIneq = cntNzAIneq;
  1773. socpInfo->cntRowAIneq = cntRowAIneq;
  1774. socpInfo->cntNzASoc = cntNzASoc;
  1775. socpInfo->cntRowASoc = cntRowASoc;
  1776. socpInfo->cntSubASoc = cntSubASoc;
  1777. free(gradInfo);
  1778. }
  1779. void socpInfoSparseCont(cmscp_socpInfo** socpInfo0, cmscp_trajInfo** refTraj, cmscp_sparseInfo* sparseInfo, cmscp_consTypeInfo* consTypeInfo, cmscp_setup* setup)
  1780. {
  1781. // 函数功能:获取连续函数约束的二阶锥规划问题参数
  1782. // 输入:
  1783. // socpInfo:二阶锥信息结构体
  1784. // refTraj:参考轨迹信息
  1785. // consTypeInfo:约束类型信息
  1786. // sparseInfo:稀疏(非零元位置)信息
  1787. // setup:问题设置参数
  1788. // 输出:
  1789. // socpInfo:二阶锥信息结构体
  1790. // 谢磊:2022 / 4 / 18编写
  1791. // 变量声明
  1792. int numPhase, * numPhaseOptVar, iphase, numMesh, numState, numControl, cnt, * idxColAEqua, * ptrRowAEqua, * idxColACone, * ptrRowACone, cntNzAEqua, cntRowAEqua, cntNzAIneq, cntRowAIneq, cntNzASoc, cntRowASoc, cntSubASoc, idxOptVarPhaseStart, * parameterNzPtrRow, * parameterNzIdxCol, idxOptVarT0, idxOptVarTf, idxOptVarParameter, nnzType1, i, p, j, i0, numParameter, numNlPath, numSocPath, * dimSocPath, numRowSocPath, numRowPath, * stateNzPtrRow, * controlNzPtrRow, * stateNzIdxCol, * controlNzIdxCol, * stateNzIdxRow, * controlNzIdxRow, * parameterNzIdxRow, * stateNzPtrCol, * controlNzPtrCol, * parameterNzPtrCol, numType1, numType2, numType3, numType4, * idxType1, * idxType2, * idxType3, * idxType4, idxPathGrdColControl, idxPathGrdColParameter, idxDynGrdColControl, idxDynGrdColParameter, idxOptVarStateK, idxOptVarControlK, k, idxDynGrd, idxOptVarStateKm1, idxOptVarControlKm1, numContInput, idxPathGrd;;
  1793. double* valueAEqua, * bEqua, * valueACone, * bCone, refT0, refTf, * refParameter, temp, * refState, * refControl, * meshNode, * refStateK, * refControlK, * pathNlLower, * pathNlUpper, * pathSocUpper, * dynRefK, * dynGrdK, * pathRefK, * pathGrdK, * dynRefKm1, * dynGrdKm1, * pathRefKm1, * pathGrdKm1, a1, a2, * refStateKm1, * refControlKm1, * tempPtr;
  1794. cmscp_dimension* dimension;
  1795. cmscp_mesh** mesh;
  1796. cmscp_bound* bounds;
  1797. cmscp_consType** consType;
  1798. cmscp_nzPos_cont** contNzPos;
  1799. cmscp_derInfo** dynGradInfo;
  1800. cmscp_derInfo** pathGradInfo;
  1801. cmscp_socpInfo* socpInfo;
  1802. cmscp_dimCone* dimCone;
  1803. // 分配空间
  1804. dynGradInfo = (cmscp_derInfo**)malloc(sizeof(cmscp_derInfo*));
  1805. dynGradInfo[0] = (cmscp_derInfo*)malloc(sizeof(cmscp_derInfo));
  1806. pathGradInfo = (cmscp_derInfo**)malloc(sizeof(cmscp_derInfo*));
  1807. pathGradInfo[0] = (cmscp_derInfo*)malloc(sizeof(cmscp_derInfo));
  1808. // //基本参数
  1809. dimension = setup->dimension;// 问题维数信息
  1810. mesh = setup->mesh;// 网格信息
  1811. bounds = setup->bounds;// 边界信息
  1812. numPhase = setup->numPhase;// 阶段数
  1813. socpInfo = *socpInfo0;
  1814. numPhaseOptVar = socpInfo->numPhaseOptVar;// 每段优化变量个数
  1815. contNzPos = sparseInfo->contNzPos;// 稀疏信息
  1816. consType = consTypeInfo->path;// 约束类型信息
  1817. // 二阶锥规划参数
  1818. ptrRowAEqua = socpInfo->AEqua->ptrRow;
  1819. idxColAEqua = socpInfo->AEqua->idxCol;
  1820. valueAEqua = socpInfo->AEqua->v;
  1821. bEqua = socpInfo->bEqua;
  1822. ptrRowACone = socpInfo->ACone->ptrRow;
  1823. idxColACone = socpInfo->ACone->idxCol;
  1824. valueACone = socpInfo->ACone->v;
  1825. bCone = socpInfo->bCone;
  1826. dimCone = socpInfo->dimCone;
  1827. // 计数变量
  1828. cntNzAEqua = socpInfo->cntNzAEqua;
  1829. cntRowAEqua = socpInfo->cntRowAEqua;
  1830. cntNzAIneq = socpInfo->cntNzAIneq;
  1831. cntRowAIneq = socpInfo->cntRowAIneq;
  1832. cntNzASoc = socpInfo->cntNzASoc;
  1833. cntRowASoc = socpInfo->cntRowASoc;
  1834. cntSubASoc = socpInfo->cntSubASoc;
  1835. // 优化变量中各段起始索引
  1836. idxOptVarPhaseStart = 0;
  1837. for (iphase = 0; iphase < numPhase; iphase++)
  1838. {
  1839. // 维数
  1840. numMesh = mesh[iphase]->num;
  1841. numState = dimension->phase[iphase]->state;
  1842. numControl = dimension->phase[iphase]->control;
  1843. numParameter = dimension->phase[iphase]->parameter;
  1844. numNlPath = dimension->phase[iphase]->path->nl;
  1845. numSocPath = dimension->phase[iphase]->path->soc->n;
  1846. dimSocPath = dimension->phase[iphase]->path->soc->q;
  1847. if (numSocPath)
  1848. {
  1849. numRowSocPath = cmscp_sum(dimSocPath, numSocPath);
  1850. }
  1851. else
  1852. {
  1853. numRowSocPath = 0;
  1854. }
  1855. numRowPath = numNlPath + numRowSocPath;
  1856. // 约束上下界简写
  1857. pathNlLower = bounds->phase[iphase]->path->nl->lower;
  1858. pathNlUpper = bounds->phase[iphase]->path->nl->upper;
  1859. pathSocUpper = bounds->phase[iphase]->path->soc;
  1860. // 参考轨迹
  1861. refState = refTraj[iphase]->state->v;
  1862. refControl = refTraj[iphase]->control->v;
  1863. refParameter = refTraj[iphase]->parameter;
  1864. refT0 = refTraj[iphase]->initialtime;
  1865. refTf = refTraj[iphase]->finaltime;
  1866. // 稀疏信息
  1867. stateNzPtrRow = contNzPos[iphase]->state->ptrRow;
  1868. controlNzPtrRow = contNzPos[iphase]->control->ptrRow;
  1869. parameterNzPtrRow = contNzPos[iphase]->parameter->ptrRow;
  1870. stateNzIdxCol = contNzPos[iphase]->state->idxCol;
  1871. controlNzIdxCol = contNzPos[iphase]->control->idxCol;
  1872. parameterNzIdxCol = contNzPos[iphase]->parameter->idxCol;
  1873. stateNzIdxRow = contNzPos[iphase]->state->idxRow;
  1874. controlNzIdxRow = contNzPos[iphase]->control->idxRow;
  1875. parameterNzIdxRow = contNzPos[iphase]->parameter->idxRow;
  1876. stateNzPtrCol = contNzPos[iphase]->state->ptrCol;
  1877. controlNzPtrCol = contNzPos[iphase]->control->ptrCol;
  1878. parameterNzPtrCol = contNzPos[iphase]->parameter->ptrCol;
  1879. // 约束类型简写
  1880. numType1 = consType[iphase]->type[0].num;
  1881. numType2 = consType[iphase]->type[1].num;
  1882. numType3 = consType[iphase]->type[2].num;
  1883. numType4 = consType[iphase]->type[3].num;
  1884. idxType1 = consType[iphase]->type[0].idx;
  1885. idxType2 = consType[iphase]->type[1].idx;
  1886. idxType3 = consType[iphase]->type[2].idx;
  1887. idxType4 = consType[iphase]->type[3].idx;
  1888. nnzType1 = getType1PathNnz(consType[iphase]->type[0], contNzPos[iphase], numState); // Type1约束的一半非零元个数
  1889. //离散节点
  1890. meshNode = mesh[iphase]->node;
  1891. // 索引
  1892. idxPathGrdColControl = numRowPath * numState;
  1893. idxPathGrdColParameter = numRowPath * (numState + numControl);
  1894. idxDynGrdColControl = numState * numState;
  1895. idxDynGrdColParameter = numState * (numState + numControl);
  1896. idxOptVarT0 = idxOptVarPhaseStart + numMesh * (numState + numControl);
  1897. idxOptVarTf = idxOptVarT0 + 1;
  1898. idxOptVarParameter = idxOptVarTf + 1;
  1899. idxOptVarStateK = idxOptVarPhaseStart;
  1900. idxOptVarControlK = idxOptVarPhaseStart + numMesh * numState;
  1901. // 导数信息空间
  1902. numContInput = numState + numControl + numParameter;
  1903. dynRefKm1 = (double*)malloc(numState * sizeof(double));
  1904. dynGrdKm1 = (double*)malloc(numState * numContInput * sizeof(double));
  1905. pathRefKm1 = (double*)malloc(numRowPath * sizeof(double));
  1906. pathGrdKm1 = (double*)malloc(numRowPath * numContInput * sizeof(double));
  1907. dynRefK = (double*)malloc(numState * sizeof(double));
  1908. dynGrdK = (double*)malloc(numState * numContInput * sizeof(double));
  1909. pathRefK = (double*)malloc(numRowPath * sizeof(double));
  1910. pathGrdK = (double*)malloc(numRowPath * numContInput * sizeof(double));
  1911. for (k = 0; k < numMesh; k++)
  1912. {
  1913. refStateK = refState + k * numState;
  1914. refControlK = refControl + k * numControl;
  1915. refStateKm1 = refState + (k - 1) * numState;
  1916. refControlKm1 = refControl + (k - 1) * numControl;
  1917. // 梯度信息
  1918. dynGradInfo[0]->ref = dynRefK;
  1919. dynGradInfo[0]->der = dynGrdK;
  1920. pathGradInfo[0]->ref = pathRefK;
  1921. pathGradInfo[0]->der = pathGrdK;
  1922. gradInfoSparseCont(dynGradInfo, pathGradInfo, refStateK, refControlK,
  1923. refParameter, stateNzIdxRow, controlNzIdxRow,
  1924. parameterNzIdxRow, stateNzPtrCol, controlNzPtrCol,
  1925. parameterNzPtrCol, numState,
  1926. numControl, numParameter, numRowPath,
  1927. iphase, setup);
  1928. if (k > 0) {
  1929. // 常数项
  1930. a1 = 0.5 * (meshNode[k] - meshNode[k - 1]);
  1931. a2 = a1 * (refTf - refT0);
  1932. for (i = 0; i < numState; i++) { // 行
  1933. ptrRowAEqua[cntRowAEqua] = cntNzAEqua;
  1934. temp = 0;
  1935. // xK前面系数
  1936. for (p = stateNzPtrRow[i]; p < stateNzPtrRow[i + 1]; p++)
  1937. {
  1938. j = stateNzIdxCol[p];// 列
  1939. idxDynGrd = j * numState + i;// 第(i, j)个元素
  1940. idxColAEqua[cntNzAEqua] = idxOptVarStateKm1 + j;
  1941. valueAEqua[cntNzAEqua] = a2 * dynGrdKm1[idxDynGrd];
  1942. temp = temp + valueAEqua[cntNzAEqua] * refStateKm1[j];
  1943. if (i == j)
  1944. {
  1945. valueAEqua[cntNzAEqua] = valueAEqua[cntNzAEqua] + 1;
  1946. }
  1947. cntNzAEqua = cntNzAEqua + 1;
  1948. }
  1949. // xKp1前面系数
  1950. for (p = stateNzPtrRow[i]; p < stateNzPtrRow[i + 1]; p++)
  1951. {
  1952. j = stateNzIdxCol[p];// 列
  1953. idxDynGrd = j * numState + i;// 第(i, j)个元素
  1954. idxColAEqua[cntNzAEqua] = idxOptVarStateK + j;
  1955. valueAEqua[cntNzAEqua] = a2 * dynGrdK[idxDynGrd];
  1956. temp = temp + valueAEqua[cntNzAEqua] * refStateK[j];
  1957. if (i == j)
  1958. {
  1959. valueAEqua[cntNzAEqua] = valueAEqua[cntNzAEqua] - 1;
  1960. }
  1961. cntNzAEqua = cntNzAEqua + 1;
  1962. }
  1963. // uK前面系数
  1964. for (p = controlNzPtrRow[i]; p < controlNzPtrRow[i + 1]; p++)
  1965. {
  1966. j = controlNzIdxCol[p];// 列
  1967. idxDynGrd = idxDynGrdColControl + j * numState + i;// 第(i, j)个元素
  1968. idxColAEqua[cntNzAEqua] = idxOptVarControlKm1 + j;
  1969. valueAEqua[cntNzAEqua] = a2 * dynGrdKm1[idxDynGrd];
  1970. temp = temp + valueAEqua[cntNzAEqua] * refControlKm1[j];
  1971. cntNzAEqua = cntNzAEqua + 1;
  1972. }
  1973. // uKp1前面系数
  1974. for (p = controlNzPtrRow[i]; p < controlNzPtrRow[i + 1]; p++)
  1975. {
  1976. j = controlNzIdxCol[p];// 列
  1977. idxDynGrd = idxDynGrdColControl + j * numState + i;// 第(i, j)个元素
  1978. idxColAEqua[cntNzAEqua] = idxOptVarControlK + j;
  1979. valueAEqua[cntNzAEqua] = a2 * dynGrdK[idxDynGrd];
  1980. temp = temp + valueAEqua[cntNzAEqua] * refControlK[j];
  1981. cntNzAEqua = cntNzAEqua + 1;
  1982. }
  1983. // t0前面系数
  1984. idxColAEqua[cntNzAEqua] = idxOptVarT0;
  1985. valueAEqua[cntNzAEqua] = -a1 * (dynRefKm1[i] + dynRefK[i]);
  1986. cntNzAEqua = cntNzAEqua + 1;
  1987. // tf前面系数
  1988. idxColAEqua[cntNzAEqua] = idxOptVarTf;
  1989. valueAEqua[cntNzAEqua] = -valueAEqua[cntNzAEqua - 1];
  1990. cntNzAEqua = cntNzAEqua + 1;
  1991. // p前面系数
  1992. for (p = parameterNzPtrRow[i]; p < parameterNzPtrRow[i + 1]; p++)
  1993. {
  1994. j = parameterNzIdxCol[p];// 列
  1995. idxDynGrd = idxDynGrdColParameter + j * numState + i;// 第(i, j)个元素
  1996. idxColAEqua[cntNzAEqua] = idxOptVarParameter + j;
  1997. valueAEqua[cntNzAEqua] = a2 * (dynGrdKm1[idxDynGrd] + dynGrdK[idxDynGrd]);
  1998. temp = temp + valueAEqua[cntNzAEqua] * refParameter[j];
  1999. cntNzAEqua = cntNzAEqua + 1;
  2000. }
  2001. bEqua[cntRowAEqua] = temp;
  2002. cntRowAEqua = cntRowAEqua + 1;
  2003. }
  2004. }
  2005. //// 路径约束的系数和右端项
  2006. // 类型1:双不等式约束项
  2007. // 上界: Ak* xK + BK * uK + CK * p <= bK
  2008. // 下界: - Ak * xK - BK * uK - CK * p <= -bK
  2009. if (numType1)
  2010. {
  2011. for (cnt = 0; cnt < numType1; cnt++)
  2012. {
  2013. i = idxType1[cnt];// 在路径约束函数中的行位置
  2014. i0 = i + numState;// 在连续约束函数中的行位置
  2015. temp = -pathRefK[i];
  2016. // 每行首个非零元的位置
  2017. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  2018. ptrRowACone[cntRowAIneq + numType1] = cntNzAIneq + nnzType1;
  2019. // xK前面系数矩阵
  2020. for (p = stateNzPtrRow[i0]; p < stateNzPtrRow[i0 + 1]; p++)
  2021. {
  2022. j = stateNzIdxCol[p];// 列
  2023. idxPathGrd = j * numRowPath + i;// 第(i, j)个元素
  2024. idxColACone[cntNzAIneq] = idxOptVarStateK + j;
  2025. valueACone[cntNzAIneq] = pathGrdK[idxPathGrd];
  2026. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  2027. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  2028. temp = temp + valueACone[cntNzAIneq] * refStateK[j];
  2029. cntNzAIneq = cntNzAIneq + 1;
  2030. }
  2031. // uK前面系数矩阵
  2032. for (p = controlNzPtrRow[i0]; p < controlNzPtrRow[i0 + 1]; p++)
  2033. {
  2034. j = controlNzIdxCol[p];// 列
  2035. idxPathGrd = idxPathGrdColControl + j * numRowPath + i;// 第(i, j)个元素
  2036. idxColACone[cntNzAIneq] = idxOptVarControlK + j;
  2037. valueACone[cntNzAIneq] = pathGrdK[idxPathGrd];
  2038. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  2039. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  2040. temp = temp + valueACone[cntNzAIneq] * refControlK[j];
  2041. cntNzAIneq = cntNzAIneq + 1;
  2042. }
  2043. // p前面系数矩阵
  2044. for (p = parameterNzPtrRow[i0]; p < parameterNzPtrRow[i0 + 1]; p++) {
  2045. j = parameterNzIdxCol[p];// 列
  2046. idxPathGrd = idxPathGrdColParameter + j * numRowPath + i;// 第(i, j)个元素
  2047. idxColACone[cntNzAIneq] = idxOptVarParameter + j;
  2048. valueACone[cntNzAIneq] = pathGrdK[idxPathGrd];
  2049. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  2050. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  2051. temp = temp + valueACone[cntNzAIneq] * refParameter[j];
  2052. cntNzAIneq = cntNzAIneq + 1;
  2053. }
  2054. // 右端项
  2055. bCone[cntRowAIneq] = temp + pathNlUpper[i]; // 上界
  2056. bCone[numType1 + cntRowAIneq] = -temp - pathNlLower[i]; // 下界
  2057. cntRowAIneq = cntRowAIneq + 1;
  2058. }
  2059. cntNzAIneq = cntNzAIneq + nnzType1;
  2060. cntRowAIneq = cntRowAIneq + numType1;
  2061. }
  2062. // 等式约束项,表达式:
  2063. // Ak* xK + BK * uK + CK * p == bK
  2064. if (numType2)
  2065. {
  2066. for (cnt = 0; cnt < numType2; cnt++)
  2067. {
  2068. i = idxType2[cnt];
  2069. i0 = i + numState;
  2070. temp = -pathRefK[i];
  2071. ptrRowAEqua[cntRowAEqua] = cntNzAEqua;
  2072. // xK前面系数
  2073. for (p = stateNzPtrRow[i0]; p < stateNzPtrRow[i0 + 1]; p++) {
  2074. j = stateNzIdxCol[p];// 列
  2075. idxPathGrd = j * numRowPath + i;// 第(i, j)个元素
  2076. idxColAEqua[cntNzAEqua] = idxOptVarStateK + j;
  2077. valueAEqua[cntNzAEqua] = pathGrdK[idxPathGrd];
  2078. temp = temp + valueAEqua[cntNzAEqua] * refStateK[j];
  2079. cntNzAEqua = cntNzAEqua + 1;
  2080. }
  2081. // uK前面系数
  2082. for (p = controlNzPtrRow[i0]; p < controlNzPtrRow[i0 + 1]; p++)
  2083. {
  2084. j = controlNzIdxCol[p];// 列
  2085. idxPathGrd = idxPathGrdColControl + j * numRowPath + i;// 第(i, j)个元素
  2086. idxColAEqua[cntNzAEqua] = idxOptVarControlK + j;
  2087. valueAEqua[cntNzAEqua] = pathGrdK[idxPathGrd];
  2088. temp = temp + valueAEqua[cntNzAEqua] * refControlK[j];
  2089. cntNzAEqua = cntNzAEqua + 1;
  2090. }
  2091. // p前面系数
  2092. for (p = parameterNzPtrRow[i0]; p < parameterNzPtrRow[i0 + 1]; p++)
  2093. {
  2094. j = parameterNzIdxCol[p];// 列
  2095. idxPathGrd = idxPathGrdColParameter + j * numRowPath + i;// 第(i, j)个元素
  2096. idxColAEqua[cntNzAEqua] = idxOptVarParameter + j;
  2097. valueAEqua[cntNzAEqua] = pathGrdK[idxPathGrd];
  2098. temp = temp + valueAEqua[cntNzAEqua] * refParameter[j];
  2099. cntNzAEqua = cntNzAEqua + 1;
  2100. }
  2101. bEqua[cntRowAEqua] = temp + pathNlUpper[i];
  2102. cntRowAEqua = cntRowAEqua + 1;
  2103. }
  2104. }
  2105. // 上单边不等式约束项,表达式:
  2106. // Ak* xK + BK * uK + CK * p <= bK
  2107. if (numType3)
  2108. {
  2109. for (cnt = 0; cnt < numType3; cnt++)
  2110. {
  2111. i = idxType3[cnt];
  2112. i0 = i + numState;
  2113. temp = -pathRefK[i];
  2114. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  2115. // xK前面系数
  2116. for (p = stateNzPtrRow[i0]; p < stateNzPtrRow[i0 + 1]; p++)
  2117. {
  2118. j = stateNzIdxCol[p];// 列
  2119. idxPathGrd = j * numRowPath + i;// 第(i, j)个元素
  2120. idxColACone[cntNzAIneq] = idxOptVarStateK + j;
  2121. valueACone[cntNzAIneq] = pathGrdK[idxPathGrd];
  2122. temp = temp + valueACone[cntNzAIneq] * refStateK[j];
  2123. cntNzAIneq = cntNzAIneq + 1;
  2124. }
  2125. // uK前面系数
  2126. for (p = controlNzPtrRow[i0]; p < controlNzPtrRow[i0 + 1]; p++)
  2127. {
  2128. j = controlNzIdxCol[p];// 列
  2129. idxPathGrd = idxPathGrdColControl + j * numRowPath + i;// 第(i, j)个元素
  2130. idxColACone[cntNzAIneq] = idxOptVarControlK + j;
  2131. valueACone[cntNzAIneq] = pathGrdK[idxPathGrd];
  2132. temp = temp + valueACone[cntNzAIneq] * refControlK[j];
  2133. cntNzAIneq = cntNzAIneq + 1;
  2134. }
  2135. // p前面系数
  2136. for (p = parameterNzPtrRow[i0]; p < parameterNzPtrRow[i0 + 1]; p++)
  2137. {
  2138. j = parameterNzIdxCol[p];// 列
  2139. idxPathGrd = idxPathGrdColParameter + j * numRowPath + i;// 第(i, j)个元素
  2140. idxColACone[cntNzAIneq] = idxOptVarParameter + j;
  2141. valueACone[cntNzAIneq] = pathGrdK[idxPathGrd];
  2142. temp = temp + valueACone[cntNzAIneq] * refParameter[j];
  2143. cntNzAIneq = cntNzAIneq + 1;
  2144. }
  2145. bCone[cntRowAIneq] = temp + pathNlUpper[i];
  2146. cntRowAIneq = cntRowAIneq + 1;
  2147. }
  2148. }
  2149. // 下单边不等式约束项,表达式:
  2150. // -Ak * xK - BK * uK - CK * p <= -bK
  2151. if (numType4) {
  2152. for (cnt = 0; cnt < numType4; cnt++)
  2153. {
  2154. i = idxType4[cnt];
  2155. i0 = i + numState;
  2156. temp = pathRefK[i];
  2157. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  2158. // xK前面系数
  2159. for (p = stateNzPtrRow[i0]; p < stateNzPtrRow[i0 + 1]; p++)
  2160. {
  2161. j = stateNzIdxCol[p];// 列
  2162. idxPathGrd = j * numRowPath + i;// 第(i, j)个元素
  2163. idxColACone[cntNzAIneq] = idxOptVarStateK + j;
  2164. valueACone[cntNzAIneq] = -pathGrdK[idxPathGrd];
  2165. temp = temp + valueACone[cntNzAIneq] * refStateK[j];
  2166. cntNzAIneq = cntNzAIneq + 1;
  2167. }
  2168. // uK前面系数
  2169. for (p = controlNzPtrRow[i0]; p < controlNzPtrRow[i0 + 1]; p++)
  2170. {
  2171. j = controlNzIdxCol[p];// 列
  2172. idxPathGrd = idxPathGrdColControl + j * numRowPath + i;// 第(i, j)个元素
  2173. idxColACone[cntNzAIneq] = idxOptVarControlK + j;
  2174. valueACone[cntNzAIneq] = -pathGrdK[idxPathGrd];
  2175. temp = temp + valueACone[cntNzAIneq] * refControlK[j];
  2176. cntNzAIneq = cntNzAIneq + 1;
  2177. }
  2178. // p前面系数
  2179. for (p = parameterNzPtrRow[i0]; p < parameterNzPtrRow[i0 + 1]; p++)
  2180. {
  2181. j = parameterNzIdxCol[p];// 列
  2182. idxPathGrd = idxPathGrdColParameter + j * numRowPath + i;// 第(i, j)个元素
  2183. idxColACone[cntNzAIneq] = idxOptVarParameter + j;
  2184. valueACone[cntNzAIneq] = -pathGrdK[idxPathGrd];
  2185. temp = temp + valueACone[cntNzAIneq] * refParameter[j];
  2186. cntNzAIneq = cntNzAIneq + 1;
  2187. }
  2188. bCone[cntRowAIneq] = temp - pathNlLower[i];
  2189. cntRowAIneq = cntRowAIneq + 1;
  2190. }
  2191. }
  2192. if (numSocPath)
  2193. {
  2194. for (i = 0; i < numRowSocPath; i++)
  2195. {
  2196. i0 = numState + numNlPath + i;
  2197. temp = -pathRefK[numNlPath + i];
  2198. ptrRowACone[cntRowASoc] = cntNzASoc;
  2199. // xK前面系数
  2200. for (p = stateNzPtrRow[i0]; p < stateNzPtrRow[i0 + 1]; p++)
  2201. {
  2202. j = stateNzIdxCol[p];// 列
  2203. idxPathGrd = j * numRowPath + numNlPath + i;// 第(i, j)个元素
  2204. idxColACone[cntNzASoc] = idxOptVarStateK + j;
  2205. valueACone[cntNzASoc] = pathGrdK[idxPathGrd];
  2206. temp = temp + valueACone[cntNzASoc] * refStateK[j];
  2207. cntNzASoc = cntNzASoc + 1;
  2208. }
  2209. // uK前面系数
  2210. for (p = controlNzPtrRow[i0]; p < controlNzPtrRow[i0 + 1]; p++)
  2211. {
  2212. j = controlNzIdxCol[p];// 列
  2213. idxPathGrd = idxPathGrdColControl + j * numRowPath + numNlPath + i;// 第(i, j)个元素
  2214. idxColACone[cntNzASoc] = idxOptVarControlK + j;
  2215. valueACone[cntNzASoc] = pathGrdK[idxPathGrd];
  2216. temp = temp + valueACone[cntNzASoc] * refControlK[j];
  2217. cntNzASoc = cntNzASoc + 1;
  2218. }
  2219. // p前面系数
  2220. for (p = parameterNzPtrRow[i0]; p < parameterNzPtrRow[i0 + 1]; p++)
  2221. {
  2222. j = parameterNzIdxCol[p];// 列
  2223. idxPathGrd = idxPathGrdColParameter + j * numRowPath + numNlPath + i;// 第(i, j)个元素
  2224. idxColACone[cntNzASoc] = idxOptVarParameter + j;
  2225. valueACone[cntNzASoc] = pathGrdK[idxPathGrd];
  2226. temp = temp + valueACone[cntNzASoc] * refParameter[j];
  2227. cntNzASoc = cntNzASoc + 1;
  2228. }
  2229. bCone[cntRowASoc] = temp + pathSocUpper[i];
  2230. cntRowASoc = cntRowASoc + 1;
  2231. }
  2232. for (i = 0; i < numSocPath; i++)
  2233. {
  2234. cntSubASoc = cntSubASoc + 1;
  2235. dimCone->q[cntSubASoc] = dimSocPath[i];
  2236. }
  2237. }
  2238. // dynRefKm1和dynRefK空间互换,实现的功能为dynRefKm1=dynRefK,且不需要另外创建空间;
  2239. tempPtr = dynRefKm1; dynRefKm1 = dynRefK; dynRefK = tempPtr;
  2240. tempPtr = dynGrdKm1; dynGrdKm1 = dynGrdK; dynGrdK = tempPtr;
  2241. tempPtr = pathRefKm1; pathRefKm1 = pathRefK; pathRefK = tempPtr;
  2242. tempPtr = pathGrdKm1; pathGrdKm1 = pathGrdK; pathGrdK = tempPtr;
  2243. // 更新点索引
  2244. idxOptVarStateKm1 = idxOptVarStateK;
  2245. idxOptVarStateK = idxOptVarStateK + numState;
  2246. idxOptVarControlKm1 = idxOptVarControlK;
  2247. idxOptVarControlK = idxOptVarControlK + numControl;
  2248. }
  2249. if (dynRefK) { free(dynRefKm1); }
  2250. if (dynGrdK) { free(dynGrdKm1); }
  2251. if (pathRefK) { free(pathRefKm1); }
  2252. if (pathGrdK) { free(pathGrdKm1); }
  2253. if (dynRefK) { free(dynRefK); }
  2254. if (dynGrdK) { free(dynGrdK); }
  2255. if (pathRefK) { free(pathRefK); }
  2256. if (pathGrdK) { free(pathGrdK); }
  2257. // 更新段索引
  2258. idxOptVarPhaseStart = idxOptVarPhaseStart + numPhaseOptVar[iphase];
  2259. }
  2260. free(dynGradInfo[0]);
  2261. free(dynGradInfo);
  2262. free(pathGradInfo[0]);
  2263. free(pathGradInfo);
  2264. socpInfo->cntNzAEqua = cntNzAEqua;
  2265. socpInfo->cntRowAEqua = cntRowAEqua;
  2266. socpInfo->cntNzAIneq = cntNzAIneq;
  2267. socpInfo->cntRowAIneq = cntRowAIneq;
  2268. socpInfo->cntNzASoc = cntNzASoc;
  2269. socpInfo->cntRowASoc = cntRowASoc;
  2270. socpInfo->cntSubASoc = cntSubASoc;
  2271. }
  2272. void socpInfoSparseLink(cmscp_socpInfo** socpInfo0, cmscp_trajInfo** refTraj, cmscp_sparseInfo* sparseInfo, cmscp_consTypeInfo* consTypeInfo, cmscp_setup* setup)
  2273. {
  2274. // 函数功能:获取衔接函数约束的二阶锥规划问题参数
  2275. // 输入:
  2276. // socpInfo:二阶锥信息结构体
  2277. // refTraj:参考轨迹信息
  2278. // consTypeInfo:约束类型信息
  2279. // sparseInfo:稀疏(非零元位置)信息
  2280. // setup:问题设置参数
  2281. // 输出:
  2282. // socpInfo:二阶锥信息结构体
  2283. // 谢磊:2022 / 4 / 18编写
  2284. // //基本参数
  2285. int numPhase, * numPhaseOptVar, iphase, cnt, numNlEndp, * idxColAEqua, * ptrRowAEqua, * idxColACone, * ptrRowACone, cntNzAEqua, cntRowAEqua, cntNzAIneq, cntRowAIneq, cntNzASoc, cntRowASoc, cntSubASoc, nnzType1, i, p, j, i0, numType1, numType2, numType3, numType4, * idxType1, * idxType2, * idxType3, * idxType4, idxOptVarLeftPhaseStart, idxOptVarRightPhaseStart, numLeftMesh, numRightMesh, numLeftStatef, numLeftControl, numRightState, numRightControl, numLeftParameter, numNlLink, numSocLink, * dimSocLink, * leftStatefPtrRow, * leftTfPtrRow, * leftParameterPtrRow, * rightState0PtrRow, * rightT0PtrRow, * rightParameterPtrRow, * leftStatefIdxCol, * leftParameterIdxCol, * rightState0IdxCol, * rightParameterIdxCol, idxLinkGrdColLeftTf, idxLinkGrdColLeftParameter, idxLinkGrdColRightState0, idxLinkGrdColRightT0, idxLinkGrdColRightParameter, idxOptVarLeftStatef, idxOptVarLeftTf, idxOptVarLeftParameter, idxOptVarRightState0, idxOptVarRightT0, idxOptVarRightParameter,
  2286. numLinkInput, numLinkOutput;
  2287. double* valueAEqua, * bEqua, * valueACone, * bCone, temp, * nlLower, * nlUpper, * linkSocUpper, * refLeftStatef, refLeftTf, * refLeftParameter, * refRightState0, refRightT0, * refRightParameter, * linkRef, * linkGrd;
  2288. cmscp_dimension* dimension;
  2289. cmscp_mesh** mesh;
  2290. cmscp_bound* bounds;
  2291. cmscp_consType** consType;
  2292. cmscp_type* consTypePhase;
  2293. cmscp_nzPos_link** linkNzPos;
  2294. cmscp_derInfo** gradInfo;
  2295. cmscp_socpInfo* socpInfo;
  2296. cmscp_dimCone* dimCone;
  2297. dimension = setup->dimension; // 问题维数信息
  2298. mesh = setup->mesh; // 网格信息
  2299. bounds = setup->bounds; // 上下边界信息
  2300. numPhase = setup->numPhase; // 阶段数
  2301. socpInfo = *socpInfo0;
  2302. numPhaseOptVar = socpInfo->numPhaseOptVar; // 每段优化变量个数
  2303. linkNzPos = sparseInfo->linkNzPos; // 稀疏信息
  2304. consType = consTypeInfo->link; // 约束类型
  2305. // 二阶锥规划参数
  2306. ptrRowAEqua = socpInfo->AEqua->ptrRow;
  2307. idxColAEqua = socpInfo->AEqua->idxCol;
  2308. valueAEqua = socpInfo->AEqua->v;
  2309. bEqua = socpInfo->bEqua;
  2310. ptrRowACone = socpInfo->ACone->ptrRow;
  2311. idxColACone = socpInfo->ACone->idxCol;
  2312. valueACone = socpInfo->ACone->v;
  2313. bCone = socpInfo->bCone;
  2314. dimCone = socpInfo->dimCone;
  2315. // 计数变量
  2316. cntNzAEqua = socpInfo->cntNzAEqua;
  2317. cntRowAEqua = socpInfo->cntRowAEqua;
  2318. cntNzAIneq = socpInfo->cntNzAIneq;
  2319. cntRowAIneq = socpInfo->cntRowAIneq;
  2320. cntNzASoc = socpInfo->cntNzASoc;
  2321. cntRowASoc = socpInfo->cntRowASoc;
  2322. cntSubASoc = socpInfo->cntSubASoc;
  2323. // 梯度信息
  2324. gradInfo = (cmscp_derInfo**)malloc(numPhase * sizeof(cmscp_derInfo));
  2325. gradInfoSparseLink(gradInfo, sparseInfo, refTraj, setup);
  2326. //// 填充非零元素
  2327. // 索引
  2328. idxOptVarLeftPhaseStart = 0; // 各段起始索引
  2329. idxOptVarRightPhaseStart = numPhaseOptVar[0]; // 各段起始索引
  2330. for (iphase = 0; iphase < numPhase - 1; iphase++)
  2331. {
  2332. // 维数
  2333. numLeftMesh = mesh[iphase]->num;
  2334. numRightMesh = mesh[iphase + 1]->num;
  2335. numLeftStatef = dimension->phase[iphase]->state;
  2336. numLeftControl = dimension->phase[iphase]->control;
  2337. numRightState = dimension->phase[iphase + 1]->state;
  2338. numRightControl = dimension->phase[iphase + 1]->control;
  2339. numLeftParameter = dimension->phase[iphase]->parameter;
  2340. numNlLink = dimension->phase[iphase]->linkage->nl;
  2341. numSocLink = dimension->phase[iphase]->linkage->soc->n;
  2342. dimSocLink = dimension->phase[iphase]->linkage->soc->q;
  2343. numLinkInput = numLeftStatef + numRightState + numRightState + numLeftParameter + 2;
  2344. if (dimension->phase[iphase]->linkage->soc->n)
  2345. {
  2346. numLinkOutput = dimension->phase[iphase]->linkage->nl + cmscp_sum(dimension->phase[iphase]->linkage->soc->q, dimension->phase[iphase]->linkage->soc->n);
  2347. }
  2348. else
  2349. {
  2350. numLinkOutput = dimension->phase[iphase]->linkage->nl;
  2351. }
  2352. // 稀疏信息
  2353. leftStatefPtrRow = linkNzPos[iphase]->leftStatef->ptrRow;
  2354. leftTfPtrRow = linkNzPos[iphase]->leftTf->ptrRow;
  2355. leftParameterPtrRow = linkNzPos[iphase]->leftParameter->ptrRow;
  2356. rightState0PtrRow = linkNzPos[iphase]->rightState0->ptrRow;
  2357. rightT0PtrRow = linkNzPos[iphase]->rightT0->ptrRow;
  2358. rightParameterPtrRow = linkNzPos[iphase]->rightParameter->ptrRow;
  2359. leftStatefIdxCol = linkNzPos[iphase]->leftStatef->idxCol;
  2360. leftParameterIdxCol = linkNzPos[iphase]->leftParameter->idxCol;
  2361. rightState0IdxCol = linkNzPos[iphase]->rightState0->idxCol;
  2362. rightParameterIdxCol = linkNzPos[iphase]->rightParameter->idxCol;
  2363. // 约束类型简写
  2364. numType1 = consType[iphase]->type[0].num;
  2365. numType2 = consType[iphase]->type[1].num;
  2366. numType3 = consType[iphase]->type[2].num;
  2367. numType4 = consType[iphase]->type[3].num;
  2368. idxType1 = consType[iphase]->type[0].idx;
  2369. idxType2 = consType[iphase]->type[1].idx;
  2370. idxType3 = consType[iphase]->type[2].idx;
  2371. idxType4 = consType[iphase]->type[3].idx;
  2372. // 第1类约束的一半非零元个数
  2373. nnzType1 = getType1LinkNnz(consType[iphase]->type[0], linkNzPos[iphase]);
  2374. // 上下界
  2375. nlLower = bounds->phase[iphase]->linkage->nl->lower;
  2376. nlUpper = bounds->phase[iphase]->linkage->nl->upper;
  2377. linkSocUpper = bounds->phase[iphase]->linkage->soc;
  2378. // 参考轨迹
  2379. refLeftStatef = refTraj[iphase]->state->v + (numLeftMesh - 1) * numLeftStatef;
  2380. refLeftTf = refTraj[iphase]->finaltime;
  2381. refLeftParameter = refTraj[iphase]->parameter;
  2382. refRightState0 = refTraj[iphase + 1]->state->v;
  2383. refRightT0 = refTraj[iphase + 1]->initialtime;
  2384. refRightParameter = refTraj[iphase + 1]->parameter;
  2385. // 梯度信息
  2386. linkRef = gradInfo[iphase]->ref;
  2387. linkGrd = gradInfo[iphase]->der;
  2388. // 索引
  2389. idxLinkGrdColLeftTf = numLeftStatef;
  2390. idxLinkGrdColLeftParameter = idxLinkGrdColLeftTf + 1;
  2391. idxLinkGrdColRightState0 = idxLinkGrdColLeftParameter + numLeftParameter;
  2392. idxLinkGrdColRightT0 = idxLinkGrdColRightState0 + numRightState;
  2393. idxLinkGrdColRightParameter = idxLinkGrdColRightT0 + 1;
  2394. idxOptVarLeftStatef = idxOptVarLeftPhaseStart + (numLeftMesh - 1) * numLeftStatef;
  2395. idxOptVarLeftTf = idxOptVarLeftPhaseStart + numLeftMesh * (numLeftStatef + numLeftControl) + 1;
  2396. idxOptVarLeftParameter = idxOptVarLeftTf + 1;
  2397. idxOptVarRightState0 = idxOptVarRightPhaseStart;
  2398. idxOptVarRightT0 = idxOptVarRightPhaseStart + numRightMesh * (numRightState + numRightControl);
  2399. idxOptVarRightParameter = idxOptVarRightT0 + 2;
  2400. // 双不等式约束项,表达式:
  2401. if (numType1)
  2402. {
  2403. // 上界:A * xfl + B * tfl + C * pl + D * x0r + E * t0r + F * pr <= b
  2404. for (cnt = 0; cnt < numType1; cnt++) {
  2405. i = idxType1[cnt];
  2406. temp = -linkRef[i];
  2407. // 每行首个非零元的位置
  2408. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  2409. ptrRowACone[cntRowAIneq + numType1] = cntNzAIneq + nnzType1;
  2410. // 左侧xf前面系数
  2411. for (p = leftStatefPtrRow[i]; p < leftStatefPtrRow[i + 1]; p++)
  2412. {
  2413. j = leftStatefIdxCol[p]; // 列
  2414. idxColACone[cntNzAIneq] = idxOptVarLeftStatef + j;
  2415. valueACone[cntNzAIneq] = linkGrd[i + j* numLinkOutput];
  2416. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  2417. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  2418. temp = temp + valueACone[cntNzAIneq] * refLeftStatef[j];
  2419. cntNzAIneq = cntNzAIneq + 1;
  2420. }
  2421. // 左侧tf前面系数
  2422. if (leftTfPtrRow[i] + 1 == leftTfPtrRow[i + 1])
  2423. {
  2424. idxColACone[cntNzAIneq] = idxOptVarLeftTf;
  2425. valueACone[cntNzAIneq] = linkGrd[i + idxLinkGrdColLeftTf * numLinkOutput];
  2426. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  2427. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  2428. temp = temp + valueACone[cntNzAIneq] * refLeftTf;
  2429. cntNzAIneq = cntNzAIneq + 1;
  2430. }
  2431. // 左侧p前面系数
  2432. for (p = leftParameterPtrRow[i]; p < leftParameterPtrRow[i + 1]; p++)
  2433. {
  2434. j = leftParameterIdxCol[p]; // 列
  2435. idxColACone[cntNzAIneq] = idxOptVarLeftParameter + j;
  2436. valueACone[cntNzAIneq] = linkGrd[i + (idxLinkGrdColLeftParameter + j) * numLinkOutput];
  2437. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  2438. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  2439. temp = temp + valueACone[cntNzAIneq] * refLeftParameter[j];
  2440. cntNzAIneq = cntNzAIneq + 1;
  2441. }
  2442. // 右侧x0前面系数
  2443. for (p = rightState0PtrRow[i]; p < rightState0PtrRow[i + 1]; p++)
  2444. {
  2445. j = rightState0IdxCol[p]; // 列
  2446. idxColACone[cntNzAIneq] = idxOptVarRightState0 + j;
  2447. valueACone[cntNzAIneq] = linkGrd[i + (idxLinkGrdColRightState0 + j) * numLinkOutput];
  2448. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  2449. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  2450. temp = temp + valueACone[cntNzAIneq] * refRightState0[j];
  2451. cntNzAIneq = cntNzAIneq + 1;
  2452. }
  2453. // 右侧t0前面系数
  2454. if (rightT0PtrRow[i] + 1 == rightT0PtrRow[i + 1])
  2455. {
  2456. idxColACone[cntNzAIneq] = idxOptVarRightT0;
  2457. valueACone[cntNzAIneq] = linkGrd[i + idxLinkGrdColRightT0 * numLinkOutput];
  2458. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  2459. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  2460. temp = temp + valueACone[cntNzAIneq] * refRightT0;
  2461. cntNzAIneq = cntNzAIneq + 1;
  2462. }
  2463. // 右侧p前面系数
  2464. for (p = rightParameterPtrRow[i]; p < rightParameterPtrRow[i + 1]; p++) {
  2465. j = rightParameterIdxCol[p]; // 列
  2466. idxColACone[cntNzAIneq] = idxOptVarRightParameter + j;
  2467. valueACone[cntNzAIneq] = linkGrd[i + (idxLinkGrdColRightParameter + j) * numLinkOutput];
  2468. idxColACone[cntNzAIneq + nnzType1] = idxColACone[cntNzAIneq];
  2469. valueACone[cntNzAIneq + nnzType1] = -valueACone[cntNzAIneq];
  2470. temp = temp + valueACone[cntNzAIneq] * refRightParameter[j];
  2471. cntNzAIneq = cntNzAIneq + 1;
  2472. }
  2473. bCone[cntRowAIneq] = temp + nlUpper[i];
  2474. bCone[cntRowAIneq] = -temp - nlLower[i];
  2475. cntRowAIneq = cntRowAIneq + 1;
  2476. }
  2477. }
  2478. // 等式约束项,表达式:
  2479. // A* xfl + B * tfl + C * pl + D * x0r + E * t0r + F * pr == b
  2480. if (numType2)
  2481. {
  2482. for (cnt = 0; cnt < numType2; cnt++) {
  2483. i = idxType2[cnt];
  2484. temp = -linkRef[i];
  2485. // 每行首个非零元的位置
  2486. ptrRowAEqua[cntRowAEqua] = cntNzAEqua;
  2487. // 左侧xf前面系数
  2488. for (p = leftStatefPtrRow[i]; p < leftStatefPtrRow[i + 1]; p++)
  2489. {
  2490. j = leftStatefIdxCol[p]; // 列
  2491. idxColAEqua[cntNzAEqua] = idxOptVarLeftStatef + j;
  2492. valueAEqua[cntNzAEqua] = linkGrd[i + j * numLinkOutput];
  2493. temp = temp + valueAEqua[cntNzAEqua] * refLeftStatef[j];
  2494. cntNzAEqua = cntNzAEqua + 1;
  2495. }
  2496. // 左侧tf前面系数
  2497. if (leftTfPtrRow[i] + 1 == leftTfPtrRow[i + 1])
  2498. {
  2499. idxColAEqua[cntNzAEqua] = idxOptVarLeftTf;
  2500. valueAEqua[cntNzAEqua] = linkGrd[i + (idxLinkGrdColLeftTf) * numLinkOutput];
  2501. temp = temp + valueAEqua[cntNzAEqua] * refLeftTf;
  2502. cntNzAEqua = cntNzAEqua + 1;
  2503. }
  2504. // 左侧p前面系数
  2505. for (p = leftParameterPtrRow[i]; p < leftParameterPtrRow[i + 1]; p++)
  2506. {
  2507. j = leftParameterIdxCol[p]; // 列
  2508. idxColAEqua[cntNzAEqua] = idxOptVarLeftParameter + j;
  2509. valueAEqua[cntNzAEqua] = linkGrd[i + (idxLinkGrdColLeftParameter + j) * numLinkOutput];
  2510. temp = temp + valueAEqua[cntNzAEqua] * refLeftParameter[j];
  2511. cntNzAEqua = cntNzAEqua + 1;
  2512. }
  2513. // 右侧x0前面系数
  2514. for (p = rightState0PtrRow[i]; p < rightState0PtrRow[i + 1]; p++) {
  2515. j = rightState0IdxCol[p]; // 列
  2516. idxColAEqua[cntNzAEqua] = idxOptVarRightState0 + j;
  2517. valueAEqua[cntNzAEqua] = linkGrd[i + (idxLinkGrdColRightState0 + j) * numLinkOutput];
  2518. temp = temp + valueAEqua[cntNzAEqua] * refRightState0[j];
  2519. cntNzAEqua = cntNzAEqua + 1;
  2520. }
  2521. // 右侧t0前面系数
  2522. if (rightT0PtrRow[i] + 1 == rightT0PtrRow[i + 1]) {
  2523. idxColAEqua[cntNzAEqua] = idxOptVarRightT0;
  2524. valueAEqua[cntNzAEqua] = linkGrd[i + idxLinkGrdColRightT0 * numLinkOutput];
  2525. temp = temp + valueAEqua[cntNzAEqua] * refRightT0;
  2526. cntNzAEqua = cntNzAEqua + 1;
  2527. }
  2528. // 右侧p前面系数
  2529. for (p = rightParameterPtrRow[i]; p < rightParameterPtrRow[i + 1]; p++)
  2530. {
  2531. j = rightParameterIdxCol[p]; // 列
  2532. idxColAEqua[cntNzAEqua] = idxOptVarRightParameter + j;
  2533. valueAEqua[cntNzAEqua] = linkGrd[i + (idxLinkGrdColRightParameter + j) * numLinkOutput];
  2534. temp = temp + valueAEqua[cntNzAEqua] * refRightParameter[j];
  2535. cntNzAEqua = cntNzAEqua + 1;
  2536. }
  2537. bEqua[cntRowAEqua] = temp + nlUpper[i];
  2538. cntRowAEqua = cntRowAEqua + 1;
  2539. }
  2540. }
  2541. // 上不等式约束项,表达式:
  2542. // A* xfl + B * tfl + C * pl + D * x0r + E * t0r + F * pr <= b
  2543. if (numType3)
  2544. {
  2545. for (cnt = 0; cnt < numType3; cnt++) {
  2546. i = idxType3[cnt];
  2547. temp = -linkRef[i];
  2548. // 每行首个非零元的位置
  2549. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  2550. // 左侧xf前面系数
  2551. for (p = leftStatefPtrRow[i]; p = leftStatefPtrRow[i + 1]; p++) {
  2552. j = leftStatefIdxCol[p]; // 列
  2553. cntNzAIneq = cntNzAIneq + 1;
  2554. idxColACone[cntNzAIneq] = idxOptVarLeftStatef + j;
  2555. valueACone[cntNzAIneq] = linkGrd[i + j * numLinkOutput];
  2556. temp = temp + valueACone[cntNzAIneq] * refLeftStatef[j];
  2557. }
  2558. // 左侧tf前面系数
  2559. if (leftTfPtrRow[i] + 1 == leftTfPtrRow[i + 1])
  2560. {
  2561. cntNzAIneq = cntNzAIneq + 1;
  2562. idxColACone[cntNzAIneq] = idxOptVarLeftTf;
  2563. valueACone[cntNzAIneq] = linkGrd[i + idxLinkGrdColLeftTf * numLinkOutput];
  2564. temp = temp + valueACone[cntNzAIneq] * refLeftTf;
  2565. }
  2566. // 左侧p前面系数
  2567. for (p = leftParameterPtrRow[i]; p < leftParameterPtrRow[i + 1]; p++) {
  2568. j = leftParameterIdxCol[p]; // 列
  2569. cntNzAIneq = cntNzAIneq + 1;
  2570. idxColACone[cntNzAIneq] = idxOptVarLeftParameter + j;
  2571. valueACone[cntNzAIneq] = linkGrd[i + (idxLinkGrdColLeftParameter + j) * numLinkOutput];
  2572. temp = temp + valueACone[cntNzAIneq] * refLeftParameter[j];
  2573. }
  2574. // 右侧x0前面系数
  2575. for (p = rightState0PtrRow[i]; p < rightState0PtrRow[i + 1]; p++) {
  2576. j = rightState0IdxCol[p]; // 列
  2577. cntNzAIneq = cntNzAIneq + 1;
  2578. idxColACone[cntNzAIneq] = idxOptVarRightState0 + j;
  2579. valueACone[cntNzAIneq] = linkGrd[i + (idxLinkGrdColRightState0 + j) * numLinkOutput];
  2580. temp = temp + valueACone[cntNzAIneq] * refRightState0[j];
  2581. }
  2582. // 右侧t0前面系数
  2583. if (rightT0PtrRow[i] + 1 == rightT0PtrRow[i + 1])
  2584. {
  2585. cntNzAIneq = cntNzAIneq + 1;
  2586. idxColACone[cntNzAIneq] = idxOptVarRightT0 + 1;
  2587. valueACone[cntNzAIneq] = linkGrd[i + idxLinkGrdColRightT0 * numLinkOutput];
  2588. temp = temp + valueACone[cntNzAIneq] * refRightT0;
  2589. }
  2590. // 右侧p前面系数
  2591. for (p = rightParameterPtrRow[i]; p < rightParameterPtrRow[i + 1]; p++)
  2592. {
  2593. j = rightParameterIdxCol[p]; // 列
  2594. cntNzAIneq = cntNzAIneq + 1;
  2595. idxColACone[cntNzAIneq] = idxOptVarRightParameter + j;
  2596. valueACone[cntNzAIneq] = linkGrd[i + (idxLinkGrdColRightParameter + j) * numLinkOutput];
  2597. temp = temp + valueACone[cntNzAIneq] * refRightParameter[j];
  2598. }
  2599. bCone[cntRowAIneq] = temp + nlUpper[i];
  2600. cntRowAIneq = cntRowAIneq + 1;
  2601. }
  2602. }
  2603. // 下不等式约束项,表达式:
  2604. // -A * xfl - B * tfl - C * pl - D * x0r - E * t0r - F * pr <= -b
  2605. if (numType4)
  2606. {
  2607. for (cnt = 0; cnt < numType4; cnt++)
  2608. {
  2609. i = idxType4[cnt];
  2610. temp = linkRef[i];
  2611. // 每行首个非零元的位置
  2612. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  2613. // 左侧xf前面系数
  2614. for (p = leftStatefPtrRow[i]; p < leftStatefPtrRow[i + 1]; p++)
  2615. {
  2616. j = leftStatefIdxCol[p]; // 列
  2617. idxColACone[cntNzAIneq] = idxOptVarLeftStatef + j;
  2618. valueACone[cntNzAIneq] = -linkGrd[i + j * numLinkOutput];
  2619. temp = temp + valueACone[cntNzAIneq] * refLeftStatef[j];
  2620. cntNzAIneq = cntNzAIneq + 1;
  2621. }
  2622. // 左侧tf前面系数
  2623. if (leftTfPtrRow[i] + 1 == leftTfPtrRow[i + 1])
  2624. {
  2625. idxColACone[cntNzAIneq] = idxOptVarLeftTf;
  2626. valueACone[cntNzAIneq] = -linkGrd[i + idxLinkGrdColLeftTf * numLinkOutput];
  2627. temp = temp + valueACone[cntNzAIneq] * refLeftTf;
  2628. cntNzAIneq = cntNzAIneq + 1;
  2629. }
  2630. // 左侧p前面系数
  2631. for (p = leftParameterPtrRow[i]; p < leftParameterPtrRow[i + 1]; p++)
  2632. {
  2633. j = leftParameterIdxCol[p]; // 列
  2634. idxColACone[cntNzAIneq] = idxOptVarLeftParameter + j;
  2635. valueACone[cntNzAIneq] = -linkGrd[i + (idxLinkGrdColLeftParameter + j) * numLinkOutput];
  2636. temp = temp + valueACone[cntNzAIneq] * refLeftParameter[j];
  2637. cntNzAIneq = cntNzAIneq + 1;
  2638. }
  2639. // 右侧x0前面系数
  2640. for (p = rightState0PtrRow[i]; p < rightState0PtrRow[i + 1]; p++)
  2641. {
  2642. j = rightState0IdxCol[p]; // 列
  2643. idxColACone[cntNzAIneq] = idxOptVarRightState0 + j;
  2644. valueACone[cntNzAIneq] = -linkGrd[i + (idxLinkGrdColRightState0 + j) * numLinkOutput];
  2645. temp = temp + valueACone[cntNzAIneq] * refRightState0[j];
  2646. cntNzAIneq = cntNzAIneq + 1;
  2647. }
  2648. // 右侧t0前面系数
  2649. if (rightT0PtrRow[i] + 1 == rightT0PtrRow[i + 1])
  2650. {
  2651. idxColACone[cntNzAIneq] = idxOptVarRightT0;
  2652. valueACone[cntNzAIneq] = -linkGrd[i + idxLinkGrdColRightT0 * numLinkOutput];
  2653. temp = temp + valueACone[cntNzAIneq] * refRightT0;
  2654. cntNzAIneq = cntNzAIneq + 1;
  2655. }
  2656. // 右侧p前面系数
  2657. for (p = rightParameterPtrRow[i]; p < rightParameterPtrRow[i + 1]; p++)
  2658. {
  2659. j = rightParameterIdxCol[p]; // 列
  2660. idxColACone[cntNzAIneq] = idxOptVarRightParameter + j;
  2661. valueACone[cntNzAIneq] = -linkGrd[i + (idxLinkGrdColRightParameter + j) * numLinkOutput];
  2662. temp = temp + valueACone[cntNzAIneq] * refRightParameter[j];
  2663. cntNzAIneq = cntNzAIneq + 1;
  2664. }
  2665. bCone[cntRowAIneq] = temp - nlLower[i];
  2666. cntRowAIneq = cntRowAIneq + 1;
  2667. }
  2668. }
  2669. if (numSocLink)
  2670. {
  2671. for (i = 0; i < cmscp_sum(dimSocLink, numSocLink); i++)
  2672. {
  2673. cntRowASoc = cntRowASoc + 1;
  2674. i0 = i + numNlLink;
  2675. temp = -linkRef[i0];
  2676. ptrRowACone[cntRowASoc] = cntNzASoc;
  2677. // 左侧xf前面系数
  2678. for (p = leftStatefPtrRow[i0]; p < leftStatefPtrRow[i0 + 1]; p++)
  2679. {
  2680. j = leftStatefIdxCol[p]; // 列
  2681. idxColACone[cntNzASoc] = idxOptVarLeftStatef + j;
  2682. valueACone[cntNzASoc] = linkGrd[i0 + j * numLinkOutput];
  2683. temp = temp + valueACone[cntNzASoc] * refLeftStatef[j];
  2684. cntNzASoc = cntNzASoc + 1;
  2685. }
  2686. // 左侧tf前面系数
  2687. if (leftTfPtrRow[i0] + 1 == leftTfPtrRow[i0 + 1])
  2688. {
  2689. idxColACone[cntNzASoc] = idxOptVarLeftTf;
  2690. valueACone[cntNzASoc] = linkGrd[i0 + idxLinkGrdColLeftTf * numLinkOutput];
  2691. temp = temp + valueACone[cntNzASoc] * refLeftTf;
  2692. cntNzASoc = cntNzASoc + 1;
  2693. }
  2694. // 左侧p前面系数
  2695. for (p = leftParameterPtrRow[i0]; p < leftParameterPtrRow[i0 + 1]; p++)
  2696. {
  2697. j = leftParameterIdxCol[p]; // 列
  2698. idxColACone[cntNzASoc] = idxOptVarLeftParameter + j;
  2699. valueACone[cntNzASoc] = linkGrd[i0 + (idxLinkGrdColLeftParameter + j) * numLinkOutput];
  2700. temp = temp + valueACone[cntNzASoc] * refLeftParameter[j];
  2701. cntNzASoc = cntNzASoc + 1;
  2702. }
  2703. // 右侧x0前面系数
  2704. for (p = rightState0PtrRow[i0]; p < rightState0PtrRow[i0 + 1]; p++)
  2705. {
  2706. j = rightState0IdxCol[p]; // 列
  2707. idxColACone[cntNzASoc] = idxOptVarRightState0 + j;
  2708. valueACone[cntNzASoc] = linkGrd[i0 + (idxLinkGrdColRightState0 + j) * numLinkOutput];
  2709. temp = temp + valueACone[cntNzASoc] * refRightState0[j];
  2710. cntNzASoc = cntNzASoc + 1;
  2711. }
  2712. // 右侧t0前面系数
  2713. if (rightT0PtrRow[i0] + 1 == rightT0PtrRow[i0 + 1])
  2714. {
  2715. idxColACone[cntNzASoc] = idxOptVarRightT0 + 1;
  2716. valueACone[cntNzASoc] = linkGrd[i0 + idxLinkGrdColRightT0 * numLinkOutput];
  2717. temp = temp + valueACone[cntNzASoc] * refRightT0;
  2718. cntNzASoc = cntNzASoc + 1;
  2719. }
  2720. // 右侧p前面系数
  2721. for (p = rightParameterPtrRow[i0]; p < rightParameterPtrRow[i0 + 1]; p++)
  2722. {
  2723. j = rightParameterIdxCol[p]; // 列
  2724. idxColACone[cntNzASoc] = idxOptVarRightParameter + j;
  2725. valueACone[cntNzASoc] = linkGrd[i0 + (idxLinkGrdColRightParameter + j) * numLinkOutput];
  2726. temp = temp + valueACone[cntNzASoc] * refRightParameter[j];
  2727. cntNzASoc = cntNzASoc + 1;
  2728. }
  2729. bCone[cntRowASoc] = temp + linkSocUpper[i];
  2730. cntRowAIneq = cntRowAIneq + 1;
  2731. }
  2732. for (i = 0; i < numSocLink; i++)
  2733. {
  2734. cntSubASoc = cntSubASoc + 1;
  2735. dimCone->q[cntSubASoc] = dimSocLink[i];
  2736. }
  2737. }
  2738. idxOptVarLeftPhaseStart = idxOptVarRightPhaseStart;
  2739. idxOptVarRightPhaseStart = idxOptVarRightPhaseStart + numPhaseOptVar[iphase + 1];
  2740. }
  2741. for ( iphase = 0; iphase < numPhase-1; iphase++)
  2742. {
  2743. free(gradInfo[iphase]->ref);
  2744. free(gradInfo[iphase]->der);
  2745. free(gradInfo[iphase]);
  2746. }
  2747. free(gradInfo);
  2748. socpInfo->cntNzAEqua = cntNzAEqua;
  2749. socpInfo->cntRowAEqua = cntRowAEqua;
  2750. socpInfo->cntNzAIneq = cntNzAIneq;
  2751. socpInfo->cntRowAIneq = cntRowAIneq;
  2752. socpInfo->cntNzASoc = cntNzASoc;
  2753. socpInfo->cntRowASoc = cntRowASoc;
  2754. socpInfo->cntSubASoc = cntSubASoc;
  2755. }
  2756. void socpInfoBound(cmscp_socpInfo** socpInfo0, cmscp_consTypeInfo* consTypeInfo, cmscp_setup* setup) {
  2757. // socpInfo: 已有的二阶锥信息
  2758. // refTraj : 参考轨迹信息
  2759. // gradInfo : 梯度信息
  2760. // setup : 设置信息
  2761. int numPhase, * numPhaseOptVar, iphase, numMesh, numState, numControl, cnt, * idxColAEqua, * ptrRowAEqua, * idxColACone, * ptrRowACone, cntNzAEqua, cntRowAEqua, cntNzAIneq, cntRowAIneq, cntNzASoc, cntRowASoc, idxOptVarPhaseStart, idxOptVarState0, idxOptVarStatef, idxOptVarT0, idxOptVarTf, idxOptVarParameter, i, idxOptVarStateK, idxOptVarControlK, k, numOptVar, numIniTimeType1, numIniTimeType2, numIniTimeType3, numIniTimeType4, * idxIniTimeType1, * idxIniTimeType2, * idxIniTimeType3, * idxIniTimeType4, numFinTimeType1, numFinTimeType2, numFinTimeType3, numFinTimeType4, * idxFinTimeType1, * idxFinTimeType2, * idxFinTimeType3, * idxFinTimeType4, numIniStateType1, numIniStateType2, numIniStateType3, numIniStateType4, * idxIniStateType1, * idxIniStateType2, * idxIniStateType3, * idxIniStateType4, numFinStateType1, numFinStateType2, numFinStateType3, numFinStateType4, * idxFinStateType1, * idxFinStateType2, * idxFinStateType3, * idxFinStateType4, numStateType1, numStateType2, numStateType3, numStateType4, * idxStateType1, * idxStateType2, * idxStateType3, * idxStateType4, numControlType1, numControlType2, numControlType3, numControlType4, * idxControlType1, * idxControlType2, * idxControlType3, * idxControlType4, numParameterType1, numParameterType2, numParameterType3, numParameterType4, * idxParameterType1, * idxParameterType2, * idxParameterType3, * idxParameterType4;
  2762. double* valueAEqua, * bEqua, * valueACone, * bCone, iniTimeLower, iniTimeUpper, finTimeLower, finTimeUpper, * iniStateLower, * iniStateUpper, * finStateLower, * finStateUpper, * stateLower, * stateUpper, * controlLower, * controlUpper, * parameterLower, * parameterUpper;
  2763. cmscp_dimension* dimension;
  2764. cmscp_mesh** mesh;
  2765. cmscp_bound* bounds;
  2766. cmscp_consType_bound* consType;
  2767. cmscp_socpInfo* socpInfo;
  2768. cmscp_dimCone* dimCone;
  2769. cmscp_consType** iniTimeType, ** finTimeType, ** iniStateType, ** finStateType, ** stateType, ** controlType, ** parameterType;
  2770. // 1 基本参数
  2771. dimension = setup->dimension;// 问题维数信息
  2772. mesh = setup->mesh;// 网格信息
  2773. bounds = setup->bounds;// 上下边界信息
  2774. numPhase = setup->numPhase;// 阶段数
  2775. socpInfo = *socpInfo0;
  2776. numOptVar = socpInfo->numOptVar;// 总优化变量个数
  2777. numPhaseOptVar = socpInfo->numPhaseOptVar;// 每段优化变量个数
  2778. consType = consTypeInfo->bound;// 约束类型
  2779. iniTimeType = consType->iniTime;
  2780. finTimeType = consType->finTime;
  2781. iniStateType = consType->iniState;
  2782. finStateType = consType->finState;
  2783. stateType = consType->state;
  2784. controlType = consType->control;
  2785. parameterType = consType->parameter;
  2786. // 二阶锥规划参数
  2787. ptrRowAEqua = socpInfo->AEqua->ptrRow;
  2788. idxColAEqua = socpInfo->AEqua->idxCol;
  2789. valueAEqua = socpInfo->AEqua->v;
  2790. bEqua = socpInfo->bEqua;
  2791. ptrRowACone = socpInfo->ACone->ptrRow;
  2792. idxColACone = socpInfo->ACone->idxCol;
  2793. valueACone = socpInfo->ACone->v;
  2794. bCone = socpInfo->bCone;
  2795. dimCone = socpInfo->dimCone;
  2796. // 计数变量
  2797. cntNzAEqua = socpInfo->cntNzAEqua;
  2798. cntRowAEqua = socpInfo->cntRowAEqua;
  2799. cntNzAIneq = socpInfo->cntNzAIneq;
  2800. cntRowAIneq = socpInfo->cntRowAIneq;
  2801. cntNzASoc = socpInfo->cntNzASoc;
  2802. cntRowASoc = socpInfo->cntRowASoc;
  2803. // 2 填充非零元素
  2804. // 各段起始变量索引
  2805. idxOptVarPhaseStart = 0;
  2806. for (iphase = 0; iphase < numPhase; iphase++) {
  2807. // 维数
  2808. numMesh = mesh[iphase]->num;
  2809. numState = dimension->phase[iphase]->state;
  2810. numControl = dimension->phase[iphase]->control;
  2811. // 初始时刻约束类型简写
  2812. numIniTimeType1 = iniTimeType[iphase]->type[0].num;
  2813. numIniTimeType2 = iniTimeType[iphase]->type[1].num;
  2814. numIniTimeType3 = iniTimeType[iphase]->type[2].num;
  2815. numIniTimeType4 = iniTimeType[iphase]->type[3].num;
  2816. idxIniTimeType1 = iniTimeType[iphase]->type[0].idx;
  2817. idxIniTimeType2 = iniTimeType[iphase]->type[1].idx;
  2818. idxIniTimeType3 = iniTimeType[iphase]->type[2].idx;
  2819. idxIniTimeType4 = iniTimeType[iphase]->type[3].idx;
  2820. // 终端时刻约束类型简写
  2821. numFinTimeType1 = finTimeType[iphase]->type[0].num;
  2822. numFinTimeType2 = finTimeType[iphase]->type[1].num;
  2823. numFinTimeType3 = finTimeType[iphase]->type[2].num;
  2824. numFinTimeType4 = finTimeType[iphase]->type[3].num;
  2825. idxFinTimeType1 = finTimeType[iphase]->type[0].idx;
  2826. idxFinTimeType2 = finTimeType[iphase]->type[1].idx;
  2827. idxFinTimeType3 = finTimeType[iphase]->type[2].idx;
  2828. idxFinTimeType4 = finTimeType[iphase]->type[3].idx;
  2829. // 初始状态约束类型简写
  2830. numIniStateType1 = iniStateType[iphase]->type[0].num;
  2831. numIniStateType2 = iniStateType[iphase]->type[1].num;
  2832. numIniStateType3 = iniStateType[iphase]->type[2].num;
  2833. numIniStateType4 = iniStateType[iphase]->type[3].num;
  2834. idxIniStateType1 = iniStateType[iphase]->type[0].idx;
  2835. idxIniStateType2 = iniStateType[iphase]->type[1].idx;
  2836. idxIniStateType3 = iniStateType[iphase]->type[2].idx;
  2837. idxIniStateType4 = iniStateType[iphase]->type[3].idx;
  2838. // 终端状态约束类型简写
  2839. numFinStateType1 = finStateType[iphase]->type[0].num;
  2840. numFinStateType2 = finStateType[iphase]->type[1].num;
  2841. numFinStateType3 = finStateType[iphase]->type[2].num;
  2842. numFinStateType4 = finStateType[iphase]->type[3].num;
  2843. idxFinStateType1 = finStateType[iphase]->type[0].idx;
  2844. idxFinStateType2 = finStateType[iphase]->type[1].idx;
  2845. idxFinStateType3 = finStateType[iphase]->type[2].idx;
  2846. idxFinStateType4 = finStateType[iphase]->type[3].idx;
  2847. // 状态约束类型简写
  2848. numStateType1 = stateType[iphase]->type[0].num;
  2849. numStateType2 = stateType[iphase]->type[1].num;
  2850. numStateType3 = stateType[iphase]->type[2].num;
  2851. numStateType4 = stateType[iphase]->type[3].num;
  2852. idxStateType1 = stateType[iphase]->type[0].idx;
  2853. idxStateType2 = stateType[iphase]->type[1].idx;
  2854. idxStateType3 = stateType[iphase]->type[2].idx;
  2855. idxStateType4 = stateType[iphase]->type[3].idx;
  2856. // 控制约束类型简写
  2857. numControlType1 = controlType[iphase]->type[0].num;
  2858. numControlType2 = controlType[iphase]->type[1].num;
  2859. numControlType3 = controlType[iphase]->type[2].num;
  2860. numControlType4 = controlType[iphase]->type[3].num;
  2861. idxControlType1 = controlType[iphase]->type[0].idx;
  2862. idxControlType2 = controlType[iphase]->type[1].idx;
  2863. idxControlType3 = controlType[iphase]->type[2].idx;
  2864. idxControlType4 = controlType[iphase]->type[3].idx;
  2865. // 静态变量约束类型简写
  2866. numParameterType1 = parameterType[iphase]->type[0].num;
  2867. numParameterType2 = parameterType[iphase]->type[1].num;
  2868. numParameterType3 = parameterType[iphase]->type[2].num;
  2869. numParameterType4 = parameterType[iphase]->type[3].num;
  2870. idxParameterType1 = parameterType[iphase]->type[0].idx;
  2871. idxParameterType2 = parameterType[iphase]->type[1].idx;
  2872. idxParameterType3 = parameterType[iphase]->type[2].idx;
  2873. idxParameterType4 = parameterType[iphase]->type[3].idx;
  2874. // 初始和终端时刻约束边界简写
  2875. iniTimeLower = bounds->phase[iphase]->initialtime->lower;
  2876. iniTimeUpper = bounds->phase[iphase]->initialtime->upper;
  2877. finTimeLower = bounds->phase[iphase]->finaltime->lower;
  2878. finTimeUpper = bounds->phase[iphase]->finaltime->upper;
  2879. // 初始、终端和过程状态约束边界简写
  2880. iniStateLower = bounds->phase[iphase]->initialstate->lower;
  2881. iniStateUpper = bounds->phase[iphase]->initialstate->upper;
  2882. finStateLower = bounds->phase[iphase]->finalstate->lower;
  2883. finStateUpper = bounds->phase[iphase]->finalstate->upper;
  2884. stateLower = bounds->phase[iphase]->state->lower;
  2885. stateUpper = bounds->phase[iphase]->state->upper;
  2886. // 控制约束边界简写
  2887. controlLower = bounds->phase[iphase]->control->lower;
  2888. controlUpper = bounds->phase[iphase]->control->upper;
  2889. // 静态变量约束边界简写
  2890. parameterLower = bounds->phase[iphase]->parameter->lower;
  2891. parameterUpper = bounds->phase[iphase]->parameter->upper;
  2892. // 各个变量索引
  2893. idxOptVarT0 = idxOptVarPhaseStart + numMesh * (numState + numControl);
  2894. idxOptVarTf = idxOptVarT0 + 1;
  2895. idxOptVarParameter = idxOptVarTf + 1;
  2896. // 初始状态变量
  2897. idxOptVarState0 = idxOptVarPhaseStart;
  2898. if (numIniStateType1) {
  2899. for (cnt = 0; cnt < numIniStateType1; cnt++) {
  2900. i = idxIniStateType1[cnt];
  2901. // xK <= bUpper
  2902. bCone[cntRowAIneq] = iniStateUpper[i];
  2903. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  2904. idxColACone[cntNzAIneq] = idxOptVarState0 + i;
  2905. valueACone[cntNzAIneq] = 1.0;
  2906. cntRowAIneq = cntRowAIneq + 1;
  2907. cntNzAIneq = cntNzAIneq + 1;
  2908. // -xK <= -bLower
  2909. bCone[cntRowAIneq] = -iniStateLower[i];
  2910. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  2911. idxColACone[cntNzAIneq] = idxOptVarState0 + i;
  2912. valueACone[cntNzAIneq] = -1.0;
  2913. cntRowAIneq = cntRowAIneq + 1;
  2914. cntNzAIneq = cntNzAIneq + 1;
  2915. }
  2916. }
  2917. if (numIniStateType2) {
  2918. for (cnt = 0; cnt < numIniStateType2; cnt++) {
  2919. i = idxIniStateType2[cnt];
  2920. // xK == bUpper
  2921. bEqua[cntRowAEqua] = iniStateUpper[i];
  2922. ptrRowAEqua[cntRowAEqua] = cntNzAEqua;
  2923. idxColAEqua[cntNzAEqua] = idxOptVarState0 + i;
  2924. valueAEqua[cntNzAEqua] = 1;
  2925. cntRowAEqua = cntRowAEqua + 1;
  2926. cntNzAEqua = cntNzAEqua + 1;
  2927. }
  2928. }
  2929. if (numIniStateType3) {
  2930. for (cnt = 0; cnt < numIniStateType3; cnt++) {
  2931. i = idxIniStateType3[cnt];
  2932. // xK <= bUpper
  2933. bCone[cntRowAIneq] = iniStateUpper[i];
  2934. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  2935. idxColACone[cntNzAIneq] = idxOptVarState0 + i;
  2936. valueACone[cntNzAIneq] = 1.0;
  2937. cntRowAIneq = cntRowAIneq + 1;
  2938. cntNzAIneq = cntNzAIneq + 1;
  2939. }
  2940. }
  2941. if (numIniStateType4) {
  2942. for (cnt = 0; cnt < numIniStateType4; cnt++) {
  2943. i = idxIniStateType4[cnt];
  2944. // -xK <= -bLower
  2945. bCone[cntRowAIneq] = -iniStateLower[i];
  2946. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  2947. idxColACone[cntNzAIneq] = idxOptVarState0 + i;
  2948. valueACone[cntNzAIneq] = -1.0;
  2949. cntRowAIneq = cntRowAIneq + 1;
  2950. cntNzAIneq = cntNzAIneq + 1;
  2951. }
  2952. }
  2953. // 过程状态
  2954. idxOptVarStateK = idxOptVarPhaseStart + numState;
  2955. if (numStateType1 != 0) {
  2956. for (k = 1; k < numMesh - 1; k++) {
  2957. for (cnt = 0; cnt < numStateType1; cnt++) {
  2958. i = idxStateType1[cnt];
  2959. // xK <= bUpper
  2960. bCone[cntRowAIneq] = stateUpper[i];
  2961. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  2962. idxColACone[cntNzAIneq] = idxOptVarStateK + i;
  2963. valueACone[cntNzAIneq] = 1.0;
  2964. cntRowAIneq = cntRowAIneq + 1;
  2965. cntNzAIneq = cntNzAIneq + 1;
  2966. // -xK <= -bLower
  2967. bCone[cntRowAIneq] = -stateLower[i];
  2968. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  2969. idxColACone[cntNzAIneq] = idxOptVarStateK + i;
  2970. valueACone[cntNzAIneq] = -1.0;
  2971. cntRowAIneq = cntRowAIneq + 1;
  2972. cntNzAIneq = cntNzAIneq + 1;
  2973. }
  2974. idxOptVarStateK = idxOptVarStateK + numState;
  2975. }
  2976. }
  2977. idxOptVarStateK = idxOptVarPhaseStart + numState;
  2978. if (numStateType2 != 0) {
  2979. for (k = 1; k < numMesh - 1; k++) {
  2980. for (cnt = 0; cnt < numStateType2; cnt++) {
  2981. i = idxStateType2[cnt];
  2982. // xK == bUpper
  2983. bEqua[cntRowAEqua] = stateUpper[i];
  2984. ptrRowAEqua[cntRowAEqua] = cntNzAEqua;
  2985. idxColAEqua[cntNzAEqua] = idxOptVarStateK + i;
  2986. valueAEqua[cntNzAEqua] = 1;
  2987. cntRowAEqua = cntRowAEqua + 1;
  2988. cntNzAEqua = cntNzAEqua + 1;
  2989. }
  2990. idxOptVarStateK = idxOptVarStateK + numState;
  2991. }
  2992. }
  2993. idxOptVarStateK = idxOptVarPhaseStart + numState;
  2994. if (numStateType3 != 0) {
  2995. for (k = 1; k < numMesh - 1; k++) {
  2996. for (cnt = 0; cnt < numStateType3; cnt++) {
  2997. i = idxStateType3[cnt];
  2998. // xK <= bUpper
  2999. bCone[cntRowAIneq] = stateUpper[i];
  3000. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3001. idxColACone[cntNzAIneq] = idxOptVarStateK + i;
  3002. valueACone[cntNzAIneq] = 1.0;
  3003. cntRowAIneq = cntRowAIneq + 1;
  3004. cntNzAIneq = cntNzAIneq + 1;
  3005. }
  3006. idxOptVarStateK = idxOptVarStateK + numState;
  3007. }
  3008. }
  3009. idxOptVarStateK = idxOptVarPhaseStart + numState;
  3010. if (numStateType4 != 0) {
  3011. for (k = 1; k < numMesh - 1; k++) {
  3012. for (cnt = 0; cnt < numStateType4; cnt++) {
  3013. i = idxStateType4[cnt];
  3014. // -xK <= -bLower
  3015. bCone[cntRowAIneq] = -stateLower[i];
  3016. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3017. idxColACone[cntNzAIneq] = idxOptVarStateK + i;
  3018. valueACone[cntNzAIneq] = -1.0;
  3019. cntRowAIneq = cntRowAIneq + 1;
  3020. cntNzAIneq = cntNzAIneq + 1;
  3021. }
  3022. idxOptVarStateK = idxOptVarStateK + numState;
  3023. }
  3024. }
  3025. // 终端状态
  3026. idxOptVarStatef = idxOptVarPhaseStart + (numMesh - 1) * numState;
  3027. if (numFinStateType1 != 0) {
  3028. for (cnt = 0; cnt < numFinStateType1; cnt++) {
  3029. i = idxFinStateType1[cnt];
  3030. // xK <= bUpper
  3031. bCone[cntRowAIneq] = finStateUpper[i];
  3032. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3033. idxColACone[cntNzAIneq] = idxOptVarStatef + i;
  3034. valueACone[cntNzAIneq] = 1.0;
  3035. cntRowAIneq = cntRowAIneq + 1;
  3036. cntNzAIneq = cntNzAIneq + 1;
  3037. // -xK <= -bLower
  3038. bCone[cntRowAIneq] = -finStateLower[i];
  3039. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3040. idxColACone[cntNzAIneq] = idxOptVarStatef + i;
  3041. valueACone[cntNzAIneq] = -1.0;
  3042. cntRowAIneq = cntRowAIneq + 1;
  3043. cntNzAIneq = cntNzAIneq + 1;
  3044. }
  3045. }
  3046. if (numFinStateType2 != 0) {
  3047. for (cnt = 0; cnt < numFinStateType2; cnt++) {
  3048. i = idxFinStateType2[cnt];
  3049. // xK == bUpper
  3050. bEqua[cntRowAEqua] = finStateUpper[i];
  3051. ptrRowAEqua[cntRowAEqua] = cntNzAEqua;
  3052. idxColAEqua[cntNzAEqua] = idxOptVarStatef + i;
  3053. valueAEqua[cntNzAEqua] = 1;
  3054. cntRowAEqua = cntRowAEqua + 1;
  3055. cntNzAEqua = cntNzAEqua + 1;
  3056. }
  3057. }
  3058. if (numFinStateType3 != 0) {
  3059. for (cnt = 0; cnt < numFinStateType3; cnt++) {
  3060. i = idxFinStateType3[cnt];
  3061. // xK <= bUpper
  3062. bCone[cntRowAIneq] = finStateUpper[i];
  3063. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3064. idxColACone[cntNzAIneq] = idxOptVarStatef + i;
  3065. valueACone[cntNzAIneq] = 1.0;
  3066. cntRowAIneq = cntRowAIneq + 1;
  3067. cntNzAIneq = cntNzAIneq + 1;
  3068. }
  3069. }
  3070. if (numFinStateType4 != 0) {
  3071. for (cnt = 0; cnt < numFinStateType4; cnt++) {
  3072. i = idxFinStateType4[cnt];
  3073. // -xK <= -bLower
  3074. bCone[cntRowAIneq] = -finStateLower[i];
  3075. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3076. idxColACone[cntNzAIneq] = idxOptVarStatef + i;
  3077. valueACone[cntNzAIneq] = -1.0;
  3078. cntRowAIneq = cntRowAIneq + 1;
  3079. cntNzAIneq = cntNzAIneq + 1;
  3080. }
  3081. }
  3082. // 控制变量
  3083. idxOptVarControlK = idxOptVarPhaseStart + numMesh * numState;
  3084. if (numControlType1 != 0) {
  3085. for (k = 0; k < numMesh; k++) {
  3086. for (cnt = 0; cnt < numControlType1; cnt++) {
  3087. i = idxControlType1[cnt];
  3088. // xK <= bUpper
  3089. bCone[cntRowAIneq] = controlUpper[i];
  3090. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3091. idxColACone[cntNzAIneq] = idxOptVarControlK + i;
  3092. valueACone[cntNzAIneq] = 1.0;
  3093. cntRowAIneq = cntRowAIneq + 1;
  3094. cntNzAIneq = cntNzAIneq + 1;
  3095. // -xK <= -bLower
  3096. bCone[cntRowAIneq] = -controlLower[i];
  3097. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3098. idxColACone[cntNzAIneq] = idxOptVarControlK + i;
  3099. valueACone[cntNzAIneq] = -1.0;
  3100. cntRowAIneq = cntRowAIneq + 1;
  3101. cntNzAIneq = cntNzAIneq + 1;
  3102. }
  3103. idxOptVarControlK = idxOptVarControlK + numControl;
  3104. }
  3105. }
  3106. idxOptVarControlK = idxOptVarPhaseStart + numMesh * numState;
  3107. if (numControlType2 != 0) {
  3108. for (k = 0; k < numMesh; k++) {
  3109. for (cnt = 0; cnt < numControlType2; cnt++) {
  3110. i = idxControlType2[cnt];
  3111. // xK == bUpper
  3112. bEqua[cntRowAEqua] = controlUpper[i];
  3113. ptrRowAEqua[cntRowAEqua] = cntNzAEqua;
  3114. idxColAEqua[cntNzAEqua] = idxOptVarControlK + i;
  3115. valueAEqua[cntNzAEqua] = 1;
  3116. cntRowAEqua = cntRowAEqua + 1;
  3117. cntNzAEqua = cntNzAEqua + 1;
  3118. }
  3119. idxOptVarControlK = idxOptVarControlK + numControl;
  3120. }
  3121. }
  3122. idxOptVarControlK = idxOptVarPhaseStart + numMesh * numState;
  3123. if (numControlType3 != 0) {
  3124. for (k = 0; k < numMesh; k++) {
  3125. for (cnt = 0; cnt < numControlType3; cnt++) {
  3126. i = idxControlType3[cnt];
  3127. // xK <= bUpper
  3128. bCone[cntRowAIneq] = controlUpper[i];
  3129. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3130. idxColACone[cntNzAIneq] = idxOptVarControlK + i;
  3131. valueACone[cntNzAIneq] = 1.0;
  3132. cntRowAIneq = cntRowAIneq + 1;
  3133. cntNzAIneq = cntNzAIneq + 1;
  3134. }
  3135. idxOptVarControlK = idxOptVarControlK + numControl;
  3136. }
  3137. }
  3138. idxOptVarControlK = idxOptVarPhaseStart + numMesh * numState;
  3139. if (numControlType4 != 0) {
  3140. for (k = 0; k < numMesh; k++) {
  3141. for (cnt = 0; cnt < numControlType4; cnt++) {
  3142. i = idxControlType4[cnt];
  3143. // -xK <= -bLower
  3144. bCone[cntRowAIneq] = -controlLower[i];
  3145. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3146. idxColACone[cntNzAIneq] = idxOptVarControlK + i;
  3147. valueACone[cntNzAIneq] = -1.0;
  3148. cntRowAIneq = cntRowAIneq + 1;
  3149. cntNzAIneq = cntNzAIneq + 1;
  3150. }
  3151. idxOptVarControlK = idxOptVarControlK + numControl;
  3152. }
  3153. }
  3154. // 初始时刻
  3155. if (numIniTimeType1 != 0) {
  3156. for (cnt = 0; cnt < numIniTimeType1; cnt++) {
  3157. i = idxIniTimeType1[cnt];
  3158. // t0 <= bUpper
  3159. bCone[cntRowAIneq] = iniTimeUpper;
  3160. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3161. idxColACone[cntNzAIneq] = idxOptVarT0 + i;
  3162. valueACone[cntNzAIneq] = 1.0;
  3163. cntRowAIneq = cntRowAIneq + 1;
  3164. cntNzAIneq = cntNzAIneq + 1;
  3165. // -t0 <= -bLower
  3166. bCone[cntRowAIneq] = -iniTimeLower;
  3167. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3168. idxColACone[cntNzAIneq] = idxOptVarT0 + i;
  3169. valueACone[cntNzAIneq] = -1.0;
  3170. cntRowAIneq = cntRowAIneq + 1;
  3171. cntNzAIneq = cntNzAIneq + 1;
  3172. }
  3173. }
  3174. if (numIniTimeType2 != 0) {
  3175. for (cnt = 0; cnt < numIniTimeType2; cnt++) {
  3176. i = idxIniTimeType2[cnt];
  3177. // t0 == bUpper
  3178. bEqua[cntRowAEqua] = iniTimeUpper;
  3179. ptrRowAEqua[cntRowAEqua] = cntNzAEqua;
  3180. idxColAEqua[cntNzAEqua] = idxOptVarT0 + i;
  3181. valueAEqua[cntNzAEqua] = 1;
  3182. cntRowAEqua = cntRowAEqua + 1;
  3183. cntNzAEqua = cntNzAEqua + 1;
  3184. }
  3185. }
  3186. if (numIniTimeType3 != 0) {
  3187. for (cnt = 0; cnt < numIniTimeType3; cnt++) {
  3188. i = idxIniTimeType3[cnt];
  3189. // t0 <= bUpper
  3190. bCone[cntRowAIneq] = iniTimeUpper;
  3191. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3192. idxColACone[cntNzAIneq] = idxOptVarT0 + i;
  3193. valueACone[cntNzAIneq] = 1.0;
  3194. cntRowAIneq = cntRowAIneq + 1;
  3195. cntNzAIneq = cntNzAIneq + 1;
  3196. }
  3197. }
  3198. if (numIniTimeType4 != 0) {
  3199. for (cnt = 0; cnt < numIniTimeType4; cnt++) {
  3200. i = idxIniTimeType4[cnt];
  3201. // -t0 <= -bLower
  3202. bCone[cntRowAIneq] = -iniTimeLower;
  3203. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3204. idxColACone[cntNzAIneq] = idxOptVarT0 + i;
  3205. valueACone[cntNzAIneq] = -1.0;
  3206. cntRowAIneq = cntRowAIneq + 1;
  3207. cntNzAIneq = cntNzAIneq + 1;
  3208. }
  3209. }
  3210. // 终端时刻
  3211. if (numFinTimeType1 != 0) {
  3212. for (cnt = 0; cnt < numFinTimeType1; cnt++) {
  3213. i = idxFinTimeType1[cnt];
  3214. // tf <= bUpper
  3215. bCone[cntRowAIneq] = finTimeUpper;
  3216. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3217. idxColACone[cntNzAIneq] = idxOptVarTf + i;
  3218. valueACone[cntNzAIneq] = 1.0;
  3219. cntRowAIneq = cntRowAIneq + 1;
  3220. cntNzAIneq = cntNzAIneq + 1;
  3221. // -tf <= -bLower
  3222. bCone[cntRowAIneq] = -finTimeLower;
  3223. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3224. idxColACone[cntNzAIneq] = idxOptVarTf + i;
  3225. valueACone[cntNzAIneq] = -1.0;
  3226. cntRowAIneq = cntRowAIneq + 1;
  3227. cntNzAIneq = cntNzAIneq + 1;
  3228. }
  3229. }
  3230. if (numFinTimeType2 != 0) {
  3231. for (cnt = 0; cnt < numFinTimeType2; cnt++) {
  3232. i = idxFinTimeType2[cnt];
  3233. // tf == bUpper
  3234. bEqua[cntRowAEqua] = finTimeUpper;
  3235. ptrRowAEqua[cntRowAEqua] = cntNzAEqua;
  3236. idxColAEqua[cntNzAEqua] = idxOptVarTf + i;
  3237. valueAEqua[cntNzAEqua] = 1;
  3238. cntRowAEqua = cntRowAEqua + 1;
  3239. cntNzAEqua = cntNzAEqua + 1;
  3240. }
  3241. }
  3242. if (numFinTimeType3 != 0) {
  3243. for (cnt = 0; cnt < numFinTimeType3; cnt++) {
  3244. i = idxFinTimeType3[cnt];
  3245. // tf <= bUpper
  3246. bCone[cntRowAIneq] = finTimeUpper;
  3247. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3248. idxColACone[cntNzAIneq] = idxOptVarTf + i;
  3249. valueACone[cntNzAIneq] = 1.0;
  3250. cntRowAIneq = cntRowAIneq + 1;
  3251. cntNzAIneq = cntNzAIneq + 1;
  3252. }
  3253. }
  3254. if (numFinTimeType4 != 0) {
  3255. for (cnt = 0; cnt < numFinTimeType4; cnt++) {
  3256. i = idxFinTimeType4[cnt];
  3257. // -tf <= -bLower
  3258. bCone[cntRowAIneq] = -finTimeLower;
  3259. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3260. idxColACone[cntNzAIneq] = idxOptVarTf + i;
  3261. valueACone[cntNzAIneq] = -1.0;
  3262. cntRowAIneq = cntRowAIneq + 1;
  3263. cntNzAIneq = cntNzAIneq + 1;
  3264. }
  3265. }
  3266. // 静态参数变量
  3267. if (numParameterType1 != 0) {
  3268. for (cnt = 0; cnt < numParameterType1; cnt++) {
  3269. i = idxParameterType1[cnt];
  3270. // p <= bUpper
  3271. bCone[cntRowAIneq] = parameterUpper[i];
  3272. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3273. idxColACone[cntNzAIneq] = idxOptVarParameter + i;
  3274. valueACone[cntNzAIneq] = 1.0;
  3275. cntRowAIneq = cntRowAIneq + 1;
  3276. cntNzAIneq = cntNzAIneq + 1;
  3277. // -p <= -bLower
  3278. bCone[cntRowAIneq] = -parameterLower[i];
  3279. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3280. idxColACone[cntNzAIneq] = idxOptVarParameter + i;
  3281. valueACone[cntNzAIneq] = -1.0;
  3282. cntRowAIneq = cntRowAIneq + 1;
  3283. cntNzAIneq = cntNzAIneq + 1;
  3284. }
  3285. }
  3286. if (numParameterType2 != 0) {
  3287. for (cnt = 0; cnt < numParameterType2; cnt++) {
  3288. i = idxParameterType2[cnt];
  3289. // p == bUpper
  3290. bEqua[cntRowAEqua] = parameterUpper[i];
  3291. ptrRowAEqua[cntRowAEqua] = cntNzAEqua;
  3292. idxColAEqua[cntNzAEqua] = idxOptVarParameter + i;
  3293. valueAEqua[cntNzAEqua] = 1;
  3294. cntRowAEqua = cntRowAEqua + 1;
  3295. cntNzAEqua = cntNzAEqua + 1;
  3296. }
  3297. }
  3298. if (numParameterType3 != 0) {
  3299. for (cnt = 0; cnt < numParameterType3; cnt++) {
  3300. i = idxParameterType3[cnt];
  3301. // p <= bUpper
  3302. bCone[cntRowAIneq] = parameterUpper[i];
  3303. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3304. idxColACone[cntNzAIneq] = idxOptVarParameter + i;
  3305. valueACone[cntNzAIneq] = 1.0;
  3306. cntRowAIneq = cntRowAIneq + 1;
  3307. cntNzAIneq = cntNzAIneq + 1;
  3308. }
  3309. }
  3310. if (numParameterType4 != 0) {
  3311. for (cnt = 0; cnt < numParameterType4; cnt++) {
  3312. i = idxParameterType4[cnt];
  3313. // -p <= -bLower
  3314. bCone[cntRowAIneq] = -parameterLower[i];
  3315. ptrRowACone[cntRowAIneq] = cntNzAIneq;
  3316. idxColACone[cntNzAIneq] = idxOptVarParameter + i;
  3317. valueACone[cntNzAIneq] = -1.0;
  3318. cntRowAIneq = cntRowAIneq + 1;
  3319. cntNzAIneq = cntNzAIneq + 1;
  3320. }
  3321. }
  3322. idxOptVarPhaseStart = idxOptVarPhaseStart + numPhaseOptVar[iphase];
  3323. }
  3324. socpInfo->cntNzAEqua = cntNzAEqua;
  3325. socpInfo->cntRowAEqua = cntRowAEqua;
  3326. socpInfo->cntNzAIneq = cntNzAIneq;
  3327. socpInfo->cntRowAIneq = cntRowAIneq;
  3328. socpInfo->cntNzASoc = cntNzASoc;
  3329. socpInfo->cntRowASoc = cntRowASoc;
  3330. }
  3331. void socpInfoTrust(cmscp_socpInfo** socpInfo0, cmscp_trajInfo** refTraj, cmscp_setup* setup)
  3332. {
  3333. // socpInfo: 已有的二阶锥信息
  3334. // refTraj : 参考轨迹信息
  3335. // gradInfo : 梯度信息
  3336. // setup : 设置信息
  3337. //
  3338. int numPhase, * numPhaseOptVar, iphase, numMesh, numState, numControl, * idxColAEqua, * ptrRowAEqua, * idxColACone, * ptrRowACone, cntNzAEqua, cntRowAEqua, cntNzAIneq, cntRowAIneq, cntNzASoc, cntRowASoc, cntSubASoc, idxOptVarPhaseStart, idxOptVarT0, idxOptVarTf, idxOptVarParameter, p, j, numParameter, k, idxStateK, idxControlK, idxOptVarTrust;
  3339. double* valueAEqua, * bEqua, * valueACone, * bCone, refT0, refTf, * refParameter, * refState, * refControl, a1, a2, weightIniTime, weightFinTime, * weightState, * weightControl, * weightParameter;
  3340. cmscp_dimension* dimension;
  3341. cmscp_mesh** mesh;
  3342. cmscp_trust* trust;
  3343. cmscp_socpInfo* socpInfo;
  3344. cmscp_dimCone* dimCone;
  3345. // //基本参数
  3346. dimension = setup->dimension;// 问题维数信息
  3347. mesh = setup->mesh;// 网格信息
  3348. numPhase = setup->numPhase;// 阶段数
  3349. socpInfo = *socpInfo0;
  3350. numPhaseOptVar = socpInfo->numPhaseOptVar;// 每段优化变量个数
  3351. trust = setup->trust;// 信赖域信息
  3352. // 二阶锥规划问题参数
  3353. // 二阶锥规划参数
  3354. ptrRowAEqua = socpInfo->AEqua->ptrRow;
  3355. idxColAEqua = socpInfo->AEqua->idxCol;
  3356. valueAEqua = socpInfo->AEqua->v;
  3357. bEqua = socpInfo->bEqua;
  3358. ptrRowACone = socpInfo->ACone->ptrRow;
  3359. idxColACone = socpInfo->ACone->idxCol;
  3360. valueACone = socpInfo->ACone->v;
  3361. bCone = socpInfo->bCone;
  3362. dimCone = socpInfo->dimCone;
  3363. // 计数变量
  3364. cntNzAEqua = socpInfo->cntNzAEqua;
  3365. cntRowAEqua = socpInfo->cntRowAEqua;
  3366. cntNzAIneq = socpInfo->cntNzAIneq;
  3367. cntRowAIneq = socpInfo->cntRowAIneq;
  3368. cntNzASoc = socpInfo->cntNzASoc;
  3369. cntRowASoc = socpInfo->cntRowASoc;
  3370. cntSubASoc = socpInfo->cntSubASoc;
  3371. //// 填充信赖域约束非零元
  3372. // 硬信赖域:
  3373. // [0] <= _K[delta]
  3374. // [x][x_r]
  3375. // 或者软信赖域:
  3376. // [-delta] <= _K[0]
  3377. // [x][x_r]
  3378. // 每段起始点索引
  3379. idxOptVarPhaseStart = 0;
  3380. for (iphase = 0; iphase < numPhase; iphase++) {
  3381. numMesh = mesh[iphase]->num;
  3382. numState = dimension->phase[iphase]->state;
  3383. numControl = dimension->phase[iphase]->control;
  3384. numParameter = dimension->phase[iphase]->parameter;
  3385. refState = refTraj[iphase]->state->v;
  3386. refControl = refTraj[iphase]->control->v;
  3387. refT0 = refTraj[iphase]->initialtime;
  3388. refTf = refTraj[iphase]->finaltime;
  3389. refParameter = refTraj[iphase]->parameter;
  3390. if (strcmp(trust->model, "hard")==0) {
  3391. a1 = 0;
  3392. a2 = trust->phase[iphase]->hardRadius;
  3393. }
  3394. else if (strcmp(trust->model, "soft")==0) {
  3395. a1 = -1;
  3396. a2 = 0;
  3397. }
  3398. weightIniTime = trust->phase[iphase]->weight->initialtime;
  3399. weightFinTime = trust->phase[iphase]->weight->finaltime;
  3400. weightState = trust->phase[iphase]->weight->state;
  3401. weightControl = trust->phase[iphase]->weight->control;
  3402. weightParameter = trust->phase[iphase]->weight->parameter;
  3403. // 索引
  3404. idxStateK = idxOptVarPhaseStart;
  3405. idxControlK = idxOptVarPhaseStart + numMesh * numState;
  3406. idxOptVarT0 = idxOptVarPhaseStart + numMesh * (numState + numControl);
  3407. idxOptVarTf = idxOptVarT0 + 1;
  3408. idxOptVarParameter = idxOptVarTf + 1;
  3409. idxOptVarTrust = idxOptVarParameter + numParameter;
  3410. for (k = 0; k < numMesh; k++) {
  3411. // 状态变量
  3412. ptrRowACone[cntRowASoc] = cntNzASoc;
  3413. idxColACone[cntNzASoc] = idxOptVarTrust;
  3414. valueACone[cntNzASoc] = a1;
  3415. bCone[cntRowASoc] = a2;
  3416. cntRowASoc = cntRowASoc + 1;
  3417. cntNzASoc = cntNzASoc + 1;
  3418. for (j = 0; j < numState; j++) {
  3419. ptrRowACone[cntRowASoc] = cntNzASoc;
  3420. idxColACone[cntNzASoc] = idxStateK + j;
  3421. valueACone[cntNzASoc] = weightState[j];
  3422. bCone[cntRowASoc] = weightState[j] * refState[j + k * numState];
  3423. cntRowASoc = cntRowASoc + 1;
  3424. cntNzASoc = cntNzASoc + 1;
  3425. }
  3426. dimCone->q[cntSubASoc] = numState + 1;
  3427. cntSubASoc = cntSubASoc + 1;
  3428. // 控制变量
  3429. ptrRowACone[cntRowASoc] = cntNzASoc;
  3430. idxColACone[cntNzASoc] = idxOptVarTrust;
  3431. valueACone[cntNzASoc] = a1;
  3432. bCone[cntRowASoc] = a2;
  3433. cntRowASoc = cntRowASoc + 1;
  3434. cntNzASoc = cntNzASoc + 1;
  3435. for (j = 0; j < numControl; j++) {
  3436. ptrRowACone[cntRowASoc] = cntNzASoc;
  3437. idxColACone[cntNzASoc] = idxControlK + j;
  3438. valueACone[cntNzASoc] = weightControl[j];
  3439. bCone[cntRowASoc] = weightControl[j] * refControl[j + k* numControl];
  3440. cntRowASoc = cntRowASoc + 1;
  3441. cntNzASoc = cntNzASoc + 1;
  3442. }
  3443. dimCone->q[cntSubASoc] = numControl + 1;
  3444. cntSubASoc = cntSubASoc + 1;
  3445. idxStateK = idxStateK + numState;
  3446. idxControlK = idxControlK + numControl;
  3447. }
  3448. // 初始时刻
  3449. ptrRowACone[cntRowASoc] = cntNzASoc;
  3450. idxColACone[cntNzASoc] = idxOptVarTrust;
  3451. valueACone[cntNzASoc] = a1;
  3452. bCone[cntRowASoc] = a2;
  3453. cntRowASoc = cntRowASoc + 1;
  3454. cntNzASoc = cntNzASoc + 1;
  3455. ptrRowACone[cntRowASoc] = cntNzASoc;
  3456. idxColACone[cntNzASoc] = idxOptVarT0;
  3457. valueACone[cntNzASoc] = weightIniTime;
  3458. bCone[cntRowASoc] = weightIniTime * refT0;
  3459. dimCone->q[cntSubASoc] = 2;
  3460. cntRowASoc = cntRowASoc + 1;
  3461. cntNzASoc = cntNzASoc + 1;
  3462. cntSubASoc = cntSubASoc + 1;
  3463. // 终端时刻
  3464. ptrRowACone[cntRowASoc] = cntNzASoc;
  3465. idxColACone[cntNzASoc] = idxOptVarTrust;
  3466. valueACone[cntNzASoc] = a1;
  3467. bCone[cntRowASoc] = a2;
  3468. cntRowASoc = cntRowASoc + 1;
  3469. cntNzASoc = cntNzASoc + 1;
  3470. ptrRowACone[cntRowASoc] = cntNzASoc;
  3471. idxColACone[cntNzASoc] = idxOptVarTf;
  3472. valueACone[cntNzASoc] = weightFinTime;
  3473. bCone[cntRowASoc] = weightFinTime * refTf;
  3474. dimCone->q[cntSubASoc] = 2;
  3475. cntSubASoc = cntSubASoc + 1;
  3476. cntRowASoc = cntRowASoc + 1;
  3477. cntNzASoc = cntNzASoc + 1;
  3478. // 参数变量
  3479. ptrRowACone[cntRowASoc] = cntNzASoc;
  3480. idxColACone[cntNzASoc] = idxOptVarTrust;
  3481. valueACone[cntNzASoc] = a1;
  3482. bCone[cntRowASoc] = a2;
  3483. cntRowASoc = cntRowASoc + 1;
  3484. cntNzASoc = cntNzASoc + 1;
  3485. for (j = 0; j < numParameter; j++) {
  3486. ptrRowACone[cntRowASoc] = cntNzASoc;
  3487. idxColACone[cntNzASoc] = idxOptVarParameter + j;
  3488. valueACone[cntNzASoc] = weightParameter[j];
  3489. bCone[cntRowASoc] = weightParameter[j] * refParameter[j];
  3490. cntRowASoc = cntRowASoc + 1;
  3491. cntNzASoc = cntNzASoc + 1;
  3492. }
  3493. dimCone->q[cntSubASoc] = numParameter + 1;
  3494. cntSubASoc = cntSubASoc + 1;
  3495. idxOptVarPhaseStart = idxOptVarPhaseStart + numPhaseOptVar[iphase];
  3496. }
  3497. socpInfo->cntNzAEqua = cntNzAEqua;
  3498. socpInfo->cntRowAEqua = cntRowAEqua;
  3499. socpInfo->cntNzAIneq = cntNzAIneq;
  3500. socpInfo->cntRowAIneq = cntRowAIneq;
  3501. socpInfo->cntNzASoc = cntNzASoc;
  3502. socpInfo->cntRowASoc = cntRowASoc;
  3503. socpInfo->cntSubASoc = cntSubASoc;
  3504. }
  3505. void getSocpInfo(cmscp_socpInfo** socpInfo0, cmscp_trajInfo** refTraj, cmscp_sparseInfo* sparseInfo, cmscp_consTypeInfo* consTypeInfo, cmscp_setup* setup)
  3506. {
  3507. // 函数功能:获取二阶锥规划问题的参数
  3508. // 输入:
  3509. // refTraj:参考轨迹信息
  3510. // sparseInfo:稀疏(非零元位置)信息
  3511. // consTypeInfo:约束类型信息
  3512. // setup:问题设置参数
  3513. // 输出:
  3514. // socpInfo:存储有连续函数稀疏信息的结构体指针
  3515. // 谢磊:2022 / 6 / 27编写
  3516. cmscp_socpInfo* socpInfo;
  3517. // 1. 获取二阶锥规划问题的维度信息和申请空间
  3518. socpInfoSparseDim(socpInfo0, consTypeInfo, sparseInfo, setup);
  3519. socpInfo = *socpInfo0;
  3520. // 2. 获取二阶锥规划问题的目标函数
  3521. socpInfoSparseObjEvent(socpInfo0, refTraj, sparseInfo, consTypeInfo, setup);
  3522. // 3. 获取二阶锥规划问题的端点函数
  3523. socpInfoSparseEndp(socpInfo0, refTraj, sparseInfo, consTypeInfo, setup);
  3524. // 3. 获取二阶锥规划问题的连续函数
  3525. socpInfoSparseCont(socpInfo0, refTraj, sparseInfo, consTypeInfo, setup);
  3526. // 4. 获取二阶锥规划问题的衔接函数
  3527. socpInfoSparseLink(socpInfo0, refTraj, sparseInfo, consTypeInfo, setup);
  3528. // 5. 获取二阶锥规划问题的边界函数
  3529. socpInfoBound(socpInfo0, consTypeInfo, setup);
  3530. // 6. 获取二阶锥规划问题的信赖域函数
  3531. socpInfoTrust(socpInfo0, refTraj, setup);
  3532. // 7. 数据格式转化
  3533. floatRcsToCcs(socpInfo->AEqua->ptrRow, socpInfo->AEqua->idxCol, socpInfo->AEqua->v,
  3534. socpInfo->AEqua->m, socpInfo->AEqua->n, socpInfo->AEqua1->idxRow, socpInfo->AEqua1->ptrCol, socpInfo->AEqua1->v);
  3535. floatRcsToCcs(socpInfo->ACone->ptrRow, socpInfo->ACone->idxCol, socpInfo->ACone->v,
  3536. socpInfo->ACone->m, socpInfo->ACone->n, socpInfo->ACone1->idxRow, socpInfo->ACone1->ptrCol, socpInfo->ACone1->v);
  3537. /* 打印二阶锥信息 */
  3538. printIntVector(socpInfo->ACone->ptrRow, socpInfo->ACone->m + 1, "result\\AConePtrRow.txt");
  3539. printIntVector(socpInfo->ACone->idxCol, socpInfo->ACone->ptrRow[socpInfo->ACone->m], "result\\AConeIdxCol.txt");
  3540. printFloatVector(socpInfo->ACone->v, socpInfo->ACone->ptrRow[socpInfo->ACone->m], "result\\AConeValue.txt");
  3541. printIntVector(socpInfo->AEqua->ptrRow, socpInfo->AEqua->m + 1, "result\\AEquaPtrRow.txt");
  3542. printIntVector(socpInfo->AEqua->idxCol, socpInfo->AEqua->ptrRow[socpInfo->AEqua->m], "result\\AEquaIdxCol.txt");
  3543. printFloatVector(socpInfo->AEqua->v, socpInfo->AEqua->ptrRow[socpInfo->AEqua->m], "result\\AEquaValue.txt");
  3544. printFloatVector(socpInfo->bEqua, socpInfo->AEqua->m, "result\\bEqua.txt");
  3545. printFloatVector(socpInfo->bCone, socpInfo->ACone->m, "result\\bCone.txt");
  3546. printIntVector(socpInfo->dimCone->q, socpInfo->dimCone->nsoc, "result\\dimQ.txt");
  3547. printFloatVector(socpInfo->c, socpInfo->numOptVar, "result\\c.txt");
  3548. // 8 释放行压缩格式部分数据的空间
  3549. freeSocpInfo(&socpInfo, setup, 0);
  3550. }