ipopt_apply.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "IpIpoptApplication.hpp"
  2. #include "IpSolveStatistics.hpp"
  3. #include "program.hpp"
  4. #include <iostream>
  5. #include <time.h>
  6. #include <armadillo>
  7. #include "GPM.h"
  8. using namespace Ipopt;
  9. using namespace arma;
  10. int ipopt_apply(
  11. /* input */
  12. int var_num, int constraint_num, int nnz_jac, int nnz_h, vec x00, vec Fmin0, vec Fmax0, vec Zmin0, vec Zmax0,
  13. auxdata1 auxdata00, mat W_phase00, mat deta_t_in_col00, int Col_total00, int length_state00, int length_control00,
  14. Grad_struct_info0 Grad_struct_info00, int length_integral00, double eps00, mat D_phase00, uvec first_col00, int length_path00,
  15. int length_event00, sp_mat Jacb_struct00, field<mat> D_phasei0, uvec N_Col00, sp_mat I_Z_struct00, int interval_num00, int length_Z00,
  16. int length_parameter00, Jacb_struct_info0 Jacb_struct_info00, char *deri_supplier00, int length_F000, NLP1 NLP,
  17. int(*Dynamics_ptr00)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  18. int(*Objective_ptr00)(input0* input, double* output, mat* event),
  19. vec* z_ans, vec* zl_temp, vec* zu_temp, vec* lambda_temp, int* iter_num, double *cpu_time
  20. ){
  21. // 相关参量赋值
  22. get_var_num(var_num);
  23. get_constraint_num(constraint_num);
  24. get_nnz_jac(nnz_jac);
  25. get_nnz_h(nnz_h);
  26. get_x0(x00);
  27. get_bounds(Fmin0, Fmax0, Zmin0, Zmax0);
  28. get_data(auxdata00, W_phase00, deta_t_in_col00, Col_total00, length_state00, length_control00,
  29. Grad_struct_info00, length_integral00, eps00, D_phase00, first_col00, length_path00,
  30. length_event00, Jacb_struct00, D_phasei0, N_Col00, I_Z_struct00, interval_num00, length_Z00,
  31. length_parameter00, Jacb_struct_info00, deri_supplier00, length_F000, Dynamics_ptr00, Objective_ptr00);
  32. // 创建nlp的实例
  33. SmartPtr<TNLP> mynlp = new Program();
  34. // 创建IpoptApplication的实例
  35. //
  36. // 使用Factory,因为这使我们可以使用Ipopt Windows DLL编译此示例
  37. SmartPtr<IpoptApplication> app = IpoptApplicationFactory();
  38. // IPOPT选项设置
  39. app->Options()->SetStringValue("mu_strategy", NLP.ipoptoptions.mu_strategy);
  40. app->Options()->SetStringValue("linear_solver", NLP.ipoptoptions.linear_solver); // 求解器,ma27、ma57、ma77、ma86、ma97、pardiso、wsmp、mumps
  41. // Convergence
  42. app->Options()->SetNumericValue("tol", NLP.ipoptoptions.tolerance); // 算法的收敛容限
  43. app->Options()->SetIntegerValue("max_iter", 3000); // 最大迭代次数
  44. #ifndef LUOYC20220604
  45. app->Options()->SetNumericValue("max_cpu_time", 200000); // 最大CPU迭代时间
  46. #else
  47. app->Options()->SetNumericValue("max_cpu_time", 1); // 最大CPU迭代时间
  48. #endif
  49. app->Options()->SetNumericValue("acceptable_tol", 1e-5); // 相对可接受的收敛误差
  50. app->Options()->SetIntegerValue("acceptable_iter", 10); // 相对可接受的迭代次数
  51. //app->Options()->SetNumericValue("acceptable_obj_change_tol", 1e-3); // 目标函数的相对变化(按 Max(1,|f(x)|)缩放)小于这个值,则终止
  52. // NLP Scaling
  53. // app->Options()->SetStringValue("nlp_scaling_method", "gradient-based"); // 问题的缩放方法
  54. // NLP
  55. app->Options()->SetNumericValue("nlp_lower_bound_inf", -1e19); // 小于或等于此值的任何界限都将被视为-inf
  56. app->Options()->SetNumericValue("nlp_upper_bound_inf", 1e19); // 大于或等于此值的任何界限都将被视为inf
  57. // output
  58. app->Options()->SetIntegerValue("print_level", 0); // 控制台的显示等级,0~12
  59. //app->Options()->SetStringValue("output_file", "ipopt_result.out"); // 设置输出文件
  60. app->Options()->SetStringValue("print_user_options", "no"); // 打印用户设置的所有选项的列表
  61. app->Options()->SetStringValue("print_options_documentation", "no"); // 打印所有可用算法选项
  62. app->Options()->SetStringValue("print_timing_statistics", "no"); // 打印所选任务的CPU使用情况
  63. //app->Options()->SetStringValue("option_file_name", "ipopt_option"); // 指定选项文件的名称
  64. //app->Options()->SetStringValue("inf_pr_output", "internal"); // 选择“internal”打印出该公式的约束违反。 // 使用“original”打印原始NLP中的真正约束违反
  65. //app->Options()->SetIntegerValue("print_frequency_iter", 1); // 打印汇总迭代输出行的迭代频率
  66. //
  67. app->Options()->SetStringValue("hessian_approximation", NLP.ipoptoptions.hessian_approximation); // 打印汇总迭代输出行的迭代频率
  68. ApplicationReturnStatus status;
  69. // 初始化
  70. status = app->Initialize();
  71. if (status != Solve_Succeeded) {
  72. std::cout << std::endl << std::endl << "*** 初 始 化 错 误 ! ***" << std::endl;
  73. return (int) status;
  74. }
  75. // 进行优化
  76. status = app->OptimizeTNLP(mynlp);
  77. Status_Display(status);
  78. int a = status;
  79. *z_ans = Program::x;
  80. *zl_temp = Program::z_L;
  81. *zu_temp = Program::z_U;
  82. *lambda_temp = Program::lambda;
  83. *cpu_time = app->Statistics()->TotalCPUTime();
  84. Index iter_count = app->Statistics()->IterationCount();
  85. *iter_num = iter_count;
  86. /*
  87. if (a == -13 || a == -199) {
  88. *cpu_time = 0;
  89. Index iter_count = 0;
  90. *iter_num = iter_count;
  91. }
  92. else {
  93. *cpu_time = app->Statistics()->TotalCPUTime();
  94. Index iter_count = app->Statistics()->IterationCount();
  95. *iter_num = iter_count;
  96. }
  97. */
  98. return (int) status;
  99. }