Istruct.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. % 函数功能:
  3. % 判断积分矩阵对于自变量Z的依赖关系
  4. % 输入:
  5. % Z0:自变量
  6. % Z_num:状态、控制、积分、时间的起止序号
  7. % Dynamics:动力学方程函数句柄
  8. % Objective: 性能指标函数句柄
  9. % Scal:归一化标识
  10. % Sca_UY:状态控制量归一化系数
  11. % Sca_t:时间归一化系数
  12. % deta_t_in_col:配点所对应时间段大小
  13. % Col_total:总配点数目
  14. % auxdata:其他参数
  15. % W_phase:积分权重矩阵
  16. % length_state:状态长度
  17. % length_control:控制量长度
  18. % eps0:求导步长
  19. % 输出:
  20. % I_Z_struct0:积分依赖性向量
  21. % 编写:
  22. % 李兆亭,2020/07/03
  23. % C++ : 李兆亭,2020/09/23
  24. */
  25. #include <iostream>
  26. #include <armadillo>
  27. #include <time.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "GPM.h"
  31. using namespace std;
  32. using namespace arma;
  33. int Istruct(vec Z1, int Col_total, int length_state, int length_control, int length_integral, mat W_phase, auxdata1 auxdata,
  34. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  35. vec* I) {
  36. // 变量Z的离解
  37. int num0 = size(Z1)(0);
  38. double tf = Z1(num0 - 1);
  39. double t0 = Z1(num0 - 2);
  40. int total_state = (Col_total + 1)*length_state;
  41. int total_control = Col_total*length_control;
  42. mat Y_phase_col = Z1.rows(0, total_state - 1);
  43. mat U_phase_col = Z1.rows(total_state, (total_state + total_control - 1));
  44. Y_phase_col = reshape(Y_phase_col, Col_total + 1, length_state); //状态量矩阵
  45. U_phase_col = reshape(U_phase_col, Col_total, length_control); //控制量矩阵
  46. mat Y_phase_col1 = Y_phase_col.rows(0, Col_total - 1);
  47. // 积分约束矩阵
  48. mat dState, I_phase, path;
  49. #ifdef LUOYC20220701
  50. printf("this is Istruct:\n");
  51. #endif
  52. #ifdef LUOYC20220701
  53. printf("Istruct Y_phase_col1 = :\n");
  54. for (int j = 0; j < 36; j++)
  55. {
  56. printf("%lf ", *(Y_phase_col1.mem + j));
  57. }
  58. printf("\n");
  59. printf("Istruct U_phase_col = :\n");
  60. for (int j = 0; j < 12; j++)
  61. {
  62. printf("%lf ", *(U_phase_col.mem + j));
  63. }
  64. printf("\n");
  65. #endif
  66. (*Dynamics_ptr)(&Y_phase_col1, &U_phase_col, auxdata, &dState, &I_phase, &path);
  67. #ifdef LUOYC20220701
  68. printf("Istruct Path_phase = :\n");
  69. for (int j = 0; j < 18; j++)
  70. {
  71. printf("%lf ", *(path.mem + j));
  72. }
  73. printf("\n");
  74. #endif
  75. vec Q_integral_phase1 = ((tf - t0)*W_phase*I_phase).t();
  76. *I = Q_integral_phase1;
  77. return 1;
  78. }