Intput_Initial.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include <iostream>
  2. #include <armadillo>
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include "GPM.h"
  7. using namespace std;
  8. using namespace arma;
  9. int Intput_Initial(input0* input) {
  10. // 归一化向量
  11. double g = 9.80665;
  12. double Re = 6371110;
  13. double tc = sqrt(Re / g);
  14. double rc = Re;
  15. double vc = sqrt(Re*g);
  16. // 常值向量
  17. double pi = 3.141592653;
  18. double GMe = 1.0;
  19. double Sref = 0.4839;
  20. double M0 = 907;
  21. double C1_v = 2e-8;
  22. double Rd_v = 0.006;
  23. double n_v = 0.5;
  24. double m_v = 3.15;
  25. // 数据初始化
  26. int length_state = 6;
  27. int length_control = 2;
  28. int length_parameter = 0;
  29. int length_path = 3;
  30. // 猜测值
  31. vec guess_time; guess_time << 0 << 300.0 / tc << endr;
  32. mat guess_state(2, length_state, fill::zeros);
  33. mat guess_control(2, length_control, fill::zeros);
  34. mat guess_path(2, length_path, fill::zeros);
  35. rowvec guess_integral;
  36. // 算法参数设置
  37. char name[] = "RLV";
  38. char mesh_method[] = "normal"; // 网格细化方法的名字,字符串表示
  39. double mesh_tolerance = 1e-5; // 网格细化精度的允许值
  40. int mesh_maxiterration = 3; // 网格细化最大迭代次数
  41. int mesh_colpointsmin = 2; // 网格细化产生新interval下的最小配点数目
  42. int mesh_colpointsmax = 19; // 网格细化产生新interval下的最大配点数目
  43. double mesh_splitmult = 1.2; // 比例因子,确定创建新网格的间隔数目
  44. double mesh_curveratio = 2; // 网格间隔中解的最大平均曲率比的阈值,以确定是否增加新间隔
  45. int mesh_inter_num = 4; // 段p内的间隔数目, 整数
  46. vec mesh_colpoints; mesh_colpoints << 2 << 2 <<2 << 2 << endr; // 段p内的配置点数目,整数数组,长度与间隔数目相等
  47. vec mesh_i_proportion = 2.0 / 4.0 * ones(4, 1); // 初始网格划分的比例(当前为均等划分,和为2)
  48. double Scal = 0;
  49. char interp_method[] = "linear";
  50. int print_level = 0;
  51. char hessian_approximation[] = "limited-memory";
  52. char mu_strategy[] = "adaptive";
  53. char linear_solver[] = "ma57";
  54. double tol = 1e-7;
  55. int max_iter = 3000;
  56. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. // 1. input.setup结构体所有参数
  58. // 1.1 name
  59. setup0 setup = (*input).setup;
  60. setup.name = "Trajectory Optimization";
  61. // 1.2 functions
  62. //setup.functions.continous = "temp";
  63. //setup.functions.endpoint = "temp";
  64. // 状态变量长度
  65. setup.state_length = length_state;
  66. // 控制变量长度
  67. setup.control_length = length_control;
  68. // 待优化参数长度
  69. setup.parameter_length = length_parameter;
  70. // 1.3 bounds
  71. bounds1 bounds = setup.bounds;
  72. bounds.phase.initialtime.lower = 0; // 初始时间下限
  73. bounds.phase.initialtime.upper = 0; // 初始时间上限
  74. bounds.phase.finaltime.lower = 0; // 默认终端时间下限
  75. bounds.phase.finaltime.upper = 0; // 默认终端时间上限
  76. bounds.phase.state.lower.reset(); // 默认状态下限
  77. bounds.phase.state.upper.reset(); // 默认状态上限
  78. bounds.phase.initialstate.lower.reset(); // 默认初始状态下限
  79. bounds.phase.initialstate.upper.reset(); // 默认初始状态上限
  80. bounds.phase.finalstate.lower.reset(); // 默认终端状态下限
  81. bounds.phase.finalstate.upper.reset(); // 默认终端状态上限
  82. bounds.phase.control.lower.reset(); // 默认控制下限
  83. bounds.phase.control.upper.reset(); // 默认控制上限
  84. bounds.phase.path.lower.reset(); // 默认路径约束下限
  85. bounds.phase.path.upper.reset(); // 默认路径约束上限
  86. bounds.phase.integral.lower.reset(); // 默认积分量下限
  87. bounds.phase.integral.upper.reset(); // 默认积分量上限
  88. bounds.phase.eventgroup.lower.reset(); // 默认段间链接约束
  89. bounds.phase.eventgroup.upper.reset();
  90. bounds.phase.event.lower.reset();
  91. bounds.phase.event.upper.reset();
  92. bounds.phase.parameter.lower.reset(); // 待优化参数下限
  93. bounds.phase.parameter.upper.reset(); // 待优化参数上限
  94. setup.bounds = bounds;
  95. // 1.4 auxdata
  96. setup.auxdata.GMe = GMe;
  97. setup.auxdata.Sref = Sref;
  98. setup.auxdata.M0 = M0;
  99. setup.auxdata.C1_v = C1_v; // 与热流密度相关参数
  100. setup.auxdata.Rd_v = Rd_v; // 与热流密度相关参数
  101. setup.auxdata.n_v = n_v; // 与热流密度相关参数
  102. setup.auxdata.m_v = m_v;
  103. setup.auxdata.parameter.reset(); // 待优化参数
  104. // 1.5 derivatives
  105. setup.derivatives.supplier = "BD"; // 此项可用的选项包括:“CD”,“FD”,“BD”,“CSD”
  106. setup.derivatives.derivativelevel = 1; // 目前只使用一阶导数
  107. setup.derivatives.dependencies = "full"; //
  108. setup.derivatives.number = 2; // v阶导数有全变分,(v-1)阶导数可微
  109. // 1.6 scales
  110. setup.scales = Scal;
  111. // 1.7 method
  112. setup.method = "RPM";
  113. // 1.8 mesh
  114. setup.mesh.method = mesh_method; // 网格细化方法的名字,字符串表示
  115. setup.mesh.tolerance = mesh_tolerance; // 网格细化精度的允许值
  116. setup.mesh.maxiterration = mesh_maxiterration; // 网格细化最大迭代次数
  117. setup.mesh.colpointsmin = mesh_colpointsmin; // 网格细化产生新interval下的最小插值点数目
  118. setup.mesh.colpointsmax = mesh_colpointsmax; // 网格细化产生新interval下的最大插值点数目
  119. setup.mesh.splitmult = mesh_splitmult; // 比例因子,确定创建新网格的间隔数目
  120. setup.mesh.curveratio = mesh_curveratio; // 网格间隔中解的最大平均曲率比的阈值,以确定是否增加新间隔
  121. setup.mesh.phase.inter_num = mesh_inter_num; // 段p内的间隔数目, 整数
  122. setup.mesh.phase.colpoints = mesh_colpoints; // 段p内的配置点数目,整数数组,长度与间隔数目相等
  123. setup.mesh.phase.i_proportion = mesh_i_proportion; // 初始网格划分的比例
  124. setup.mesh.refinement.Conservative_coefficient = 0.5; // p-h-v网格细化方法的保守系数
  125. // 1.9 guess
  126. // 为充分采用用户经验,这里输入即为用户经验值。
  127. setup.guess.phase.time = guess_time;
  128. setup.guess.phase.state = guess_state;
  129. setup.guess.phase.control = guess_control;
  130. setup.guess.phase.integral = guess_integral;
  131. setup.guess.phase.interval_points << 1 << endr;
  132. // 1.10 NLP
  133. setup.NLP.solver = "ipopt";
  134. setup.NLP.ipoptoptions.print_level = print_level;
  135. setup.NLP.ipoptoptions.hessian_approximation = hessian_approximation;
  136. setup.NLP.ipoptoptions.mu_strategy = mu_strategy;
  137. setup.NLP.ipoptoptions.linear_solver = linear_solver;
  138. setup.NLP.ipoptoptions.tolerance = tol;
  139. setup.NLP.ipoptoptions.maxiterration = max_iter;
  140. // 1.11 插值方式
  141. setup.interp.method = "linear";
  142. // 1.12 feature
  143. setup.feature.FSE = 0;
  144. // 1.13 几种测试模式
  145. setup.testmode = 0; // 正常模式;
  146. // setup.testmode = 1; // 测试单个间隔、阶数不断递增时的收敛情况;
  147. // 组合为input结构体
  148. setup.warmstart = 0;
  149. (*input).setup = setup;
  150. ///////////////////////////////////////////////////////////////////
  151. return 1;
  152. }