FinalStateEstimate.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. % 函数功能:
  3. % 评估IPOPT求解器的求解结果,并据此进行网格的细化
  4. % 输入:
  5. % Y_phase_ans:状态量矩阵
  6. % U_phase_ans:控制量矩阵
  7. % tf:最大时间
  8. % t0_ans:新的最小时间
  9. % tf_ans:新的最大时间
  10. % interval_num:间隔数目
  11. % 输出:
  12. % B_phase:阶段内的边界约束矩阵
  13. % 编写:
  14. %
  15. 2021/08/16
  16. */
  17. #include <iostream>
  18. #include <armadillo>
  19. #include <time.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include "GPM.h"
  23. using namespace std;
  24. using namespace arma;
  25. int FinalStateEstimate(mat U_ans, mat Y_ans, mat MeshGrid, double t0_ans, double tf_ans, uvec N, char *method, vec time_ans,
  26. int interval_num, mat Ts1_Te1_ans, auxdata1 auxdata, int length_state, int length_control,char *interp_method,
  27. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  28. mat* FSE) {
  29. // 升阶后的时间
  30. uvec N_Col, N_interp, last_col;
  31. Method2Nk(method, N, &N_Col); // 新的配置点数目
  32. int Col_total = sum(N_Col);
  33. N_Col2interp(method, N_Col, &N_interp); // 配置点数目到插值点数目的转换
  34. last_col = ones<uvec>(interval_num);
  35. for (int i = 1; i <= interval_num; i++) {
  36. int temp1 = sum(N_interp.rows(0, i - 1));
  37. last_col(i - 1) = temp1 - i;
  38. }
  39. vec Time_interval_ans;
  40. Time2Newtime(Ts1_Te1_ans, MeshGrid, tf_ans, t0_ans, t0_ans, tf_ans, interval_num, Col_total, last_col, &Time_interval_ans); // 得到列向量
  41. vec time_ans_new = zeros(Col_total + 1, 1);
  42. time_ans_new.rows(0, Col_total - 1) = Time_interval_ans;
  43. time_ans_new(size(time_ans_new)(0) - 1) = tf_ans;
  44. mat U_average(Col_total + 1, length_control, fill::zeros); vec U_average_vec;
  45. for (int i = 1; i <= length_control; i++ ) {
  46. vec U_temp = U_ans.col(i - 1);
  47. interp1(time_ans, U_temp, time_ans_new, U_average_vec, interp_method);
  48. U_average.col(i - 1) = U_average_vec;
  49. }
  50. int n_length = size(U_average)(0);
  51. //////////////////////考虑积分的伪谱法修改////////////////////////
  52. // 计算由每个起点积分到终点时,终点的差值。
  53. (*FSE) = zeros(interval_num, Y_ans.n_cols);
  54. for (int i = 1; i <= interval_num; i++) {
  55. uvec last_col1;
  56. if (i == 1) {
  57. last_col1 = last_col.rows(i - 1, last_col.n_elem - 1);
  58. }
  59. else {
  60. last_col1 = last_col.rows(i - 1, last_col.n_elem - 1) - last_col(i - 2);
  61. }
  62. vec time_ans1 = zeros(last_col1(last_col1.n_elem - 1) + 1);
  63. vec time_ans1_temp = time_ans1.rows(0, last_col1(last_col1.n_elem - 1));
  64. Time2Newtime(Ts1_Te1_ans.rows(i - 1, Ts1_Te1_ans.n_rows - 1), MeshGrid.rows(i - 1, MeshGrid.n_rows - 1),
  65. tf_ans, t0_ans, t0_ans, tf_ans, interval_num - i + 1, sum(N_Col.rows(i - 1, N_Col.n_elem - 1)),
  66. last_col1, &time_ans1_temp);
  67. time_ans1.rows(0, last_col1(last_col1.n_elem - 1) - 1) = time_ans1_temp;
  68. time_ans1(last_col1(last_col1.n_elem - 1)) = tf_ans;
  69. vec t_interp; t_interp.reset();
  70. for (int i2 = 1; i2 <= last_col1(last_col1.n_elem - 1); i2++) {
  71. vec t_interp0 = linspace(time_ans1(i2 - 1), time_ans1(i2), 12);
  72. t_interp0 = t_interp0.rows(0, t_interp0.n_elem - 2); // 删去最后一行
  73. t_interp = join_cols(t_interp, t_interp0);
  74. }
  75. vec tf_ans_vec(1); tf_ans_vec(0) = tf_ans;
  76. t_interp = join_cols(t_interp, tf_ans_vec);
  77. mat U_average1(size(t_interp)(0), length_control, fill::zeros);
  78. for (int i = 1; i <= length_control; i++) {
  79. vec U_temp = U_ans.col(i - 1);
  80. interp1(time_ans, U_temp, t_interp, U_average_vec, interp_method);
  81. U_average1.col(i - 1) = U_average_vec;
  82. }
  83. //interp1(time_ans, U_ans, t_interp, U_average, interp_method);
  84. n_length = size(U_average1)(0);
  85. // 梯形欧拉积分
  86. mat state_int = zeros(n_length, Y_ans.n_cols);
  87. state_int.row(0) = Y_ans.row(0);
  88. for (int i_num = 1; i_num <= n_length - 1; i_num++) {
  89. mat dy, dy1, integrand, path;
  90. mat state_int_temp = state_int.row(i_num - 1);
  91. mat U_average_temp = U_average1.row(i_num - 1);
  92. (*Dynamics_ptr)(&state_int_temp, &U_average_temp, auxdata, &dy, &integrand, &path);
  93. state_int.row(i_num - 1) = state_int_temp; U_average1.row(i_num - 1) = U_average_temp;
  94. mat state_temp = state_int.row(i_num - 1) + (t_interp(i_num) - t_interp(i_num - 1))*dy;
  95. (*Dynamics_ptr)(&state_temp, &U_average_temp, auxdata, &dy1, &integrand, &path);
  96. state_int.row(i_num) = state_int.row(i_num - 1) + (t_interp(i_num) - t_interp(i_num - 1))*(dy1 + dy) / 2;
  97. }
  98. (*FSE).row(i - 1) = state_int.row(state_int.n_rows - 1);
  99. }
  100. return 1;
  101. }