Jacb_struct_Re.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. 函数功能:
  3. 雅克比矩阵依赖关系重构,根据上一代的雅可比矩阵依赖关系,生成下一代。
  4. 输入:
  5. Jacb_struct_info:雅克比基础结构信息[结构体]
  6. MeshGrid_nextstep:下一步的网格信息[矩阵]
  7. length_state:状态量长度
  8. length_control:控制量长度
  9. length_path:路径约束维度
  10. length_event:事件约束维度
  11. length_integral:积分量维度
  12. 输出:
  13. Jacb_struct1:新网格下的雅克比矩阵依赖关系矩阵[稀疏矩阵]
  14. 时间: matlab版 李兆亭,2020/07/13
  15. 时间: C++版 唐湘佶,2020/10/21
  16. */
  17. #include <iostream>
  18. #include <armadillo>
  19. #include <time.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include "GPM.h"
  23. int Jacb_struct_Re(Jacb_struct_info0 Jacb_struct_info, mat MeshGrid_nextstep, int length_state, int length_control,
  24. int length_path, int length_event, int length_integral, int length_parameter, sp_mat* Jacb_struct1) {
  25. if (MeshGrid_nextstep.is_empty()) {
  26. (*Jacb_struct1).reset(); // 若为空,表明问题已解决,无需再次赋值
  27. return 1;
  28. }
  29. vec Nk, N_col;
  30. int Col_total, z_len, interval_num;
  31. Nk = MeshGrid_nextstep.col(0); // 每个间隔内的阶数[列向量]
  32. N_col = Nk + 1;
  33. Col_total = round(sum(N_col)); // 总配点数
  34. z_len = (Col_total + 1) * length_state + Col_total * length_control + 2; // 自变量数目
  35. interval_num = Nk.n_rows; // 间隔数目
  36. ////////////////////////////////////////1、缺陷矩阵对于自变量Z的相关性////////////////////////////////////
  37. int dD_dS_L = 1; // 默认为1
  38. mat dD_dS_R = Jacb_struct_info.dD_dS_R;
  39. mat dD_dU = Jacb_struct_info.dD_dU;
  40. urowvec dD_dt = Jacb_struct_info.dD_dt;
  41. // 1.1缺陷-Z雅可比矩阵初始化
  42. int rs, cs, re, ce;
  43. int row_num = 0;
  44. int column_num = 0;
  45. int Defect_length = Col_total * length_state;
  46. sp_mat dD_dZ(Defect_length, z_len);
  47. // 1.2缺陷矩阵的左侧拟合微分对于状态的微分计算
  48. int col_num;
  49. for (int D_disnum = 0; D_disnum < length_state; D_disnum++) {
  50. for (int int_num = 0; int_num < interval_num; int_num++) {
  51. col_num = N_col(int_num);
  52. dD_dZ.rows(row_num, (row_num - 1 + col_num)).cols(column_num, (column_num + col_num)) = ones(col_num, col_num + 1);
  53. row_num = row_num + col_num;
  54. column_num = column_num + col_num;
  55. }
  56. column_num += 1;
  57. }
  58. // 1.3缺陷矩阵的右侧动力学微分对于状态的微分计算
  59. for (int y_disnum = 0; y_disnum < length_state; y_disnum++) {
  60. for (int D_disnum = 0; D_disnum < length_state; D_disnum++) {
  61. if (dD_dS_R(D_disnum, y_disnum) != 0) {
  62. rs = D_disnum * Col_total;
  63. cs = y_disnum * (Col_total + 1); // LGR插值点比配点多1
  64. re = (D_disnum + 1) * Col_total - 1;
  65. ce = (y_disnum + 1) * (Col_total + 1) - 2; // LGR插值点比配点多1
  66. dD_dZ.rows(rs, re).cols(cs, ce) = dD_dZ.rows(rs, re).cols(cs, ce) + diagmat(ones(Col_total, 1) * dD_dS_R(D_disnum, y_disnum));
  67. }
  68. }
  69. }
  70. // 1.4缺陷矩阵对于控制量的微分
  71. for (int u_disnum = 0; u_disnum < length_control; u_disnum++) {
  72. for (int D_disnum = 0; D_disnum < length_state; D_disnum++) {
  73. if (dD_dU(D_disnum, u_disnum) != 0) {
  74. rs = D_disnum * Col_total;
  75. cs = column_num + u_disnum * Col_total; // LGR配点
  76. re = (D_disnum + 1) * Col_total - 1;
  77. ce = column_num - 1 + (u_disnum + 1) * Col_total; // LGR配点
  78. dD_dZ.rows(rs, re).cols(cs, ce) = diagmat(ones(Col_total, 1) * dD_dU(D_disnum, u_disnum));
  79. }
  80. }
  81. }
  82. column_num = ce + 1;
  83. // 1.5缺陷矩阵对于时间的微分
  84. if (sum(dD_dt) == 0) {
  85. dD_dZ.cols(column_num + 0, column_num + 1).fill(0);
  86. }
  87. else {
  88. dD_dZ.cols(column_num + 0, column_num + 1).fill(1);
  89. }
  90. dD_dZ = spones(dD_dZ);
  91. //////////////////////////////2、路径约束对于自变量Z的相关性////////////////////////////////////////////
  92. mat dP_dS = Jacb_struct_info.dP_dS;
  93. mat dP_dU = Jacb_struct_info.dP_dU;
  94. int column_num_p;
  95. int Path_length = Col_total * length_path;
  96. sp_mat dP_dZ(Path_length, z_len);
  97. if (length_path == 0) {
  98. dP_dZ.reset();
  99. }
  100. else {
  101. // 2.1路径约束 - Z雅克比矩阵初始化
  102. // 2.2路径约束对于状态的微分
  103. for (int Y_disnum = 0; Y_disnum < length_state; Y_disnum++) {
  104. for (int C_disnum = 0; C_disnum < length_path; C_disnum++) {
  105. if (dP_dS(C_disnum, Y_disnum) != 0) {
  106. rs = C_disnum * Col_total;
  107. cs = Y_disnum * (Col_total + 1); // LGR插值点比配点多1
  108. re = (C_disnum + 1) * Col_total - 1;
  109. ce = (Y_disnum + 1) * (Col_total + 1) - 2; // LGR插值点比配点多1
  110. dP_dZ.rows(rs, re).cols(cs, ce) = diagmat(ones(Col_total, 1) * dP_dS(C_disnum, Y_disnum));
  111. }
  112. }
  113. }
  114. column_num_p = length_state * (Col_total + 1) - 1;
  115. // 2.3路径约束对于控制的微分
  116. for (int U_disnum = 0; U_disnum < length_control; U_disnum++) {
  117. for (int C_disnum = 0; C_disnum < length_path; C_disnum++) {
  118. if (dP_dU(C_disnum, U_disnum) != 0) {
  119. rs = C_disnum * Col_total;
  120. cs = column_num_p + 1 + U_disnum * Col_total; // LGR插值点比配点多1
  121. re = (C_disnum + 1) * Col_total - 1;
  122. ce = column_num_p + (U_disnum + 1) * Col_total; // LGR插值点比配点多1
  123. dP_dZ.rows(rs, re).cols(cs, ce) = diagmat(ones(Col_total, 1) * dP_dU(C_disnum, U_disnum));
  124. }
  125. }
  126. }
  127. // 2.4路径约束对于端点时间的微分(按照约定,路径约束不显含时间,微分为0)
  128. }
  129. //////////////////////////////////////////3、积分约束对于自变量Z的相关性//////////////////////////////////////////////////
  130. mat dI_dS = Jacb_struct_info.dI_dS;
  131. mat dI_dU = Jacb_struct_info.dI_dU;
  132. sp_mat dI_dZ(length_integral, z_len);
  133. if (length_integral == 0) {
  134. dI_dZ.reset();
  135. }
  136. else {
  137. // 3.1积分约束 - Z雅克比矩阵初始化
  138. // 3.2积分约束对于状态量的微分
  139. int row_repeat0 = 1;
  140. int col_repeat0 = Col_total + 1;
  141. dI_dZ.rows(0, length_integral - 1).cols(0, ((Col_total + 1) * length_state - 1)) = repelem(dI_dS, row_repeat0, col_repeat0);
  142. for (int s_i = 0; s_i < length_state; s_i++) {
  143. dI_dZ.rows(0, length_integral - 1).col((s_i + 1) * (Col_total + 1) - 1) = zeros(length_integral, 1);
  144. }
  145. // 3.3积分约束对于控制量的微分
  146. col_repeat0 = Col_total;
  147. int cs1 = (Col_total + 1) * length_state;
  148. int ce1 = Col_total * length_control + (Col_total + 1) * length_state - 1;
  149. dI_dZ.rows(0, length_integral - 1).cols(cs1, ce1) = repelem(dI_dU, row_repeat0, col_repeat0);
  150. // 2.4积分约束对于时间的相关性(由于是积分过程,则必然相关)
  151. int cs2 = Col_total * length_control + (Col_total + 1) * length_state;
  152. int ce2 = cs2 + 1;
  153. dI_dZ.rows(0, length_integral - 1).cols(cs2, ce2).fill(1);
  154. }
  155. /////////////////4、事件约束对于自变量Z的相关性////////////////////////////////////
  156. mat dE_dS = Jacb_struct_info.dE_dS;
  157. sp_mat dE_dZ(length_event, z_len);
  158. if (length_event == 0) {
  159. dE_dZ.reset();
  160. }
  161. else {
  162. // 4.1事件约束 - Z雅克比矩阵初始化
  163. // 4.2事件约束对于状态量的微分
  164. for (int i = 0; i < length_state; i++) {
  165. dE_dZ.col(i * (Col_total + 1)) = dE_dS.col(2 * i);
  166. dE_dZ.col((i + 1) * (Col_total + 1) - 1) = dE_dS.col(2 * i + 1);
  167. }
  168. // 4.3事件约束对于控制量的微分(根据约定,无关)
  169. // 4.4事件约束对于时间的微分(根据约定,无关)
  170. }
  171. /////////////////5、计算约束对于待优化参数的相关性////////////////////////////////////
  172. int length_F0 = Col_total*length_state + Col_total*length_path + length_integral + length_event; // NLP约束数目
  173. sp_mat dF_dp; dF_dp.reset();
  174. if (length_parameter != 0) {
  175. umat dD_dp = Jacb_struct_info.dD_dp;
  176. umat dP_dp = Jacb_struct_info.dP_dp;
  177. sp_mat dI_dp = Jacb_struct_info.dI_dp;
  178. mat dE_dp = Jacb_struct_info.dE_dp;
  179. //dD_dp.print("dD_dp");
  180. //dP_dp.print("dP_dp");
  181. //dI_dp.print("dI_dp");
  182. //dE_dp.print("dE_dp");
  183. dF_dp = sp_mat(length_F0, length_parameter);
  184. for (int i = 1; i <= length_state; i++) {
  185. int ns = 1 + (i - 1)*Col_total;
  186. int ne = i*Col_total;
  187. dF_dp.rows(ns - 1, ne - 1) = conv_to<mat>::from(repmat(dD_dp.row(i - 1), Col_total, 1));
  188. }
  189. for (int i = 1; i <= length_path; i++) {
  190. int ns = Col_total*length_state + 1 + (i - 1)*Col_total;
  191. int ne = Col_total*length_state + i*Col_total;
  192. dF_dp.rows(ns - 1, ne - 1) = conv_to<mat>::from(repmat(dP_dp.row(i - 1), Col_total, 1));
  193. }
  194. if (length_integral != 0) {
  195. for (int i = 1; i <= length_integral; i++){
  196. int n = Col_total*length_state + Col_total*length_path + i;
  197. dF_dp.row(n - 1) = dI_dp.row(i - 1);
  198. }
  199. }
  200. if (length_event != 0) {
  201. for (int i = 1; i <= length_event; i++) {
  202. int n = Col_total*length_state + Col_total*length_path + length_integral + i;
  203. dF_dp.row(n - 1) = dE_dp.row(i - 1);
  204. }
  205. }
  206. }
  207. *Jacb_struct1 = join_rows(join_cols(dD_dZ, dP_dZ, dI_dZ, dE_dZ), dF_dp);
  208. return 1;
  209. }