123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #include "IpIpoptApplication.hpp"
- #include "IpSolveStatistics.hpp"
- #include "program.hpp"
- #include <iostream>
- #include <time.h>
- #include <armadillo>
- #include "GPM.h"
- using namespace Ipopt;
- using namespace arma;
- int ipopt_apply(
- /* input */
- int var_num, int constraint_num, int nnz_jac, int nnz_h, vec x00, vec Fmin0, vec Fmax0, vec Zmin0, vec Zmax0,
- auxdata1 auxdata00, mat W_phase00, mat deta_t_in_col00, int Col_total00, int length_state00, int length_control00,
- Grad_struct_info0 Grad_struct_info00, int length_integral00, double eps00, mat D_phase00, uvec first_col00, int length_path00,
- 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,
- int length_parameter00, Jacb_struct_info0 Jacb_struct_info00, char *deri_supplier00, int length_F000, NLP1 NLP,
- int(*Dynamics_ptr00)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
- int(*Objective_ptr00)(input0* input, double* output, mat* event),
- vec* z_ans, vec* zl_temp, vec* zu_temp, vec* lambda_temp, int* iter_num, double *cpu_time
- ){
- // 相关参量赋值
- get_var_num(var_num);
- get_constraint_num(constraint_num);
- get_nnz_jac(nnz_jac);
- get_nnz_h(nnz_h);
- get_x0(x00);
- get_bounds(Fmin0, Fmax0, Zmin0, Zmax0);
- get_data(auxdata00, W_phase00, deta_t_in_col00, Col_total00, length_state00, length_control00,
- Grad_struct_info00, length_integral00, eps00, D_phase00, first_col00, length_path00,
- length_event00, Jacb_struct00, D_phasei0, N_Col00, I_Z_struct00, interval_num00, length_Z00,
- length_parameter00, Jacb_struct_info00, deri_supplier00, length_F000, Dynamics_ptr00, Objective_ptr00);
- // 创建nlp的实例
- SmartPtr<TNLP> mynlp = new Program();
- // 创建IpoptApplication的实例
- //
- // 使用Factory,因为这使我们可以使用Ipopt Windows DLL编译此示例
- SmartPtr<IpoptApplication> app = IpoptApplicationFactory();
- // IPOPT选项设置
- app->Options()->SetStringValue("mu_strategy", NLP.ipoptoptions.mu_strategy);
- app->Options()->SetStringValue("linear_solver", NLP.ipoptoptions.linear_solver); // 求解器,ma27、ma57、ma77、ma86、ma97、pardiso、wsmp、mumps
- // Convergence
- app->Options()->SetNumericValue("tol", NLP.ipoptoptions.tolerance); // 算法的收敛容限
- app->Options()->SetIntegerValue("max_iter", 3000); // 最大迭代次数
- #ifndef LUOYC20220604
- app->Options()->SetNumericValue("max_cpu_time", 200000); // 最大CPU迭代时间
- #else
- app->Options()->SetNumericValue("max_cpu_time", 1); // 最大CPU迭代时间
- #endif
- app->Options()->SetNumericValue("acceptable_tol", 1e-5); // 相对可接受的收敛误差
- app->Options()->SetIntegerValue("acceptable_iter", 10); // 相对可接受的迭代次数
- //app->Options()->SetNumericValue("acceptable_obj_change_tol", 1e-3); // 目标函数的相对变化(按 Max(1,|f(x)|)缩放)小于这个值,则终止
- // NLP Scaling
- // app->Options()->SetStringValue("nlp_scaling_method", "gradient-based"); // 问题的缩放方法
- // NLP
- app->Options()->SetNumericValue("nlp_lower_bound_inf", -1e19); // 小于或等于此值的任何界限都将被视为-inf
- app->Options()->SetNumericValue("nlp_upper_bound_inf", 1e19); // 大于或等于此值的任何界限都将被视为inf
- // output
- app->Options()->SetIntegerValue("print_level", 0); // 控制台的显示等级,0~12
- //app->Options()->SetStringValue("output_file", "ipopt_result.out"); // 设置输出文件
- app->Options()->SetStringValue("print_user_options", "no"); // 打印用户设置的所有选项的列表
- app->Options()->SetStringValue("print_options_documentation", "no"); // 打印所有可用算法选项
- app->Options()->SetStringValue("print_timing_statistics", "no"); // 打印所选任务的CPU使用情况
- //app->Options()->SetStringValue("option_file_name", "ipopt_option"); // 指定选项文件的名称
- //app->Options()->SetStringValue("inf_pr_output", "internal"); // 选择“internal”打印出该公式的约束违反。 // 使用“original”打印原始NLP中的真正约束违反
- //app->Options()->SetIntegerValue("print_frequency_iter", 1); // 打印汇总迭代输出行的迭代频率
- //
- app->Options()->SetStringValue("hessian_approximation", NLP.ipoptoptions.hessian_approximation); // 打印汇总迭代输出行的迭代频率
-
- ApplicationReturnStatus status;
-
- // 初始化
- status = app->Initialize();
- if (status != Solve_Succeeded) {
- std::cout << std::endl << std::endl << "*** 初 始 化 错 误 ! ***" << std::endl;
- return (int) status;
- }
-
- // 进行优化
- status = app->OptimizeTNLP(mynlp);
- Status_Display(status);
- int a = status;
- *z_ans = Program::x;
- *zl_temp = Program::z_L;
- *zu_temp = Program::z_U;
- *lambda_temp = Program::lambda;
- *cpu_time = app->Statistics()->TotalCPUTime();
- Index iter_count = app->Statistics()->IterationCount();
- *iter_num = iter_count;
- /*
- if (a == -13 || a == -199) {
- *cpu_time = 0;
- Index iter_count = 0;
- *iter_num = iter_count;
- }
- else {
- *cpu_time = app->Statistics()->TotalCPUTime();
- Index iter_count = app->Statistics()->IterationCount();
- *iter_num = iter_count;
- }
- */
- return (int) status;
- }
|