solve_problem.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright (C) 2005, 2009 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: solve_problem.cpp 2005 2011-06-06 12:55:16Z stefan $
  6. //
  7. // Authors: Andreas Waechter IBM 2004-11-05
  8. #include "IpIpoptApplication.hpp"
  9. #include "RegisteredTNLP.hpp"
  10. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #else
  13. #include "configall_system.h"
  14. #endif
  15. #ifdef HAVE_CSTDIO
  16. # include <cstdio>
  17. #else
  18. # ifdef HAVE_STDIO_H
  19. # include <stdio.h>
  20. # else
  21. # error "don't have header file for stdio"
  22. # endif
  23. #endif
  24. #ifdef HAVE_CSTDLIB
  25. # include <cstdlib>
  26. #else
  27. # ifdef HAVE_STDLIB_H
  28. # include <stdlib.h>
  29. # else
  30. # error "don't have header file for stdlib"
  31. # endif
  32. #endif
  33. #ifdef HAVE_CSTRING
  34. # include <cstring>
  35. #else
  36. # ifdef HAVE_STRING_H
  37. # include <string.h>
  38. # else
  39. # error "don't have header file for string"
  40. # endif
  41. #endif
  42. #include <iostream>
  43. //**********************************************************************
  44. // Stuff for benchmarking
  45. // #define TIME_LIMIT
  46. #ifdef TIME_LIMIT
  47. #include <unistd.h>
  48. #include <pthread.h>
  49. extern "C" void* killer_thread(void* arg)
  50. {
  51. int runtime = *reinterpret_cast<int *>(arg);
  52. if (runtime <= 0) {
  53. printf("Invalid argument for run time (%d)\n", runtime);
  54. exit(-100);
  55. }
  56. printf("Limiting wall clock time to %d seconds.\n", runtime);
  57. sleep(runtime);
  58. printf("EXIT: Exceeding wall clock time limit of %d seconds.\n", runtime);
  59. exit(-999);
  60. return NULL;
  61. }
  62. #endif
  63. //**********************************************************************
  64. using namespace Ipopt;
  65. using namespace std;
  66. // This could probably be done more elegant and automatically, but I
  67. // can't get it to work right now. For now, list explicitly the
  68. // problems we want to include:
  69. #include "LuksanVlcek1.hpp"
  70. REGISTER_TNLP(LuksanVlcek1(0,0), LukVlE1)
  71. REGISTER_TNLP(LuksanVlcek1(-1.,0.), LukVlI1)
  72. #include "LuksanVlcek2.hpp"
  73. REGISTER_TNLP(LuksanVlcek2(0,0), LukVlE2)
  74. REGISTER_TNLP(LuksanVlcek2(-1.,0.), LukVlI2)
  75. #include "LuksanVlcek3.hpp"
  76. REGISTER_TNLP(LuksanVlcek3(0,0), LukVlE3)
  77. REGISTER_TNLP(LuksanVlcek3(-1.,0.), LukVlI3)
  78. #include "LuksanVlcek4.hpp"
  79. REGISTER_TNLP(LuksanVlcek4(0,0), LukVlE4)
  80. REGISTER_TNLP(LuksanVlcek4(-1.,0.), LukVlI4)
  81. #include "LuksanVlcek5.hpp"
  82. REGISTER_TNLP(LuksanVlcek5(0,0), LukVlE5)
  83. REGISTER_TNLP(LuksanVlcek5(-1.,0.), LukVlI5)
  84. #include "LuksanVlcek6.hpp"
  85. REGISTER_TNLP(LuksanVlcek6(0,0), LukVlE6)
  86. REGISTER_TNLP(LuksanVlcek6(-1.,0.), LukVlI6)
  87. #include "LuksanVlcek7.hpp"
  88. REGISTER_TNLP(LuksanVlcek7(0,0), LukVlE7)
  89. REGISTER_TNLP(LuksanVlcek7(-1.,0.), LukVlI7)
  90. #include "MittelmannBndryCntrlDiri.hpp"
  91. REGISTER_TNLP(MittelmannBndryCntrlDiri1, MBndryCntrl1)
  92. REGISTER_TNLP(MittelmannBndryCntrlDiri2, MBndryCntrl2)
  93. REGISTER_TNLP(MittelmannBndryCntrlDiri3, MBndryCntrl3)
  94. REGISTER_TNLP(MittelmannBndryCntrlDiri4, MBndryCntrl4)
  95. #include "MittelmannBndryCntrlDiri3D.hpp"
  96. REGISTER_TNLP(MittelmannBndryCntrlDiri3D, MBndryCntrl_3D)
  97. #include "MittelmannBndryCntrlDiri3D_27.hpp"
  98. REGISTER_TNLP(MittelmannBndryCntrlDiri3D_27, MBndryCntrl_3D_27)
  99. REGISTER_TNLP(MittelmannBndryCntrlDiri3D_27BT, MBndryCntrl_3D_27BT)
  100. #include "MittelmannBndryCntrlDiri3Dsin.hpp"
  101. REGISTER_TNLP(MittelmannBndryCntrlDiri3Dsin, MBndryCntrl_3Dsin)
  102. #include "MittelmannBndryCntrlNeum.hpp"
  103. REGISTER_TNLP(MittelmannBndryCntrlNeum1, MBndryCntrl5)
  104. REGISTER_TNLP(MittelmannBndryCntrlNeum2, MBndryCntrl6)
  105. REGISTER_TNLP(MittelmannBndryCntrlNeum3, MBndryCntrl7)
  106. REGISTER_TNLP(MittelmannBndryCntrlNeum4, MBndryCntrl8)
  107. #include "MittelmannDistCntrlDiri.hpp"
  108. REGISTER_TNLP(MittelmannDistCntrlDiri1, MDistCntrl1)
  109. REGISTER_TNLP(MittelmannDistCntrlDiri2, MDistCntrl2)
  110. REGISTER_TNLP(MittelmannDistCntrlDiri3, MDistCntrl3)
  111. REGISTER_TNLP(MittelmannDistCntrlDiri3a, MDistCntrl3a)
  112. #include "MittelmannDistCntrlNeumA.hpp"
  113. REGISTER_TNLP(MittelmannDistCntrlNeumA1, MDistCntrl4)
  114. REGISTER_TNLP(MittelmannDistCntrlNeumA2, MDistCntrl5)
  115. REGISTER_TNLP(MittelmannDistCntrlNeumA3, MDistCntrl6a)
  116. #include "MittelmannDistCntrlNeumB.hpp"
  117. REGISTER_TNLP(MittelmannDistCntrlNeumB1, MDistCntrl4a)
  118. REGISTER_TNLP(MittelmannDistCntrlNeumB2, MDistCntrl5a)
  119. REGISTER_TNLP(MittelmannDistCntrlNeumB3, MDistCntrl6)
  120. #include "MittelmannParaCntrl.hpp"
  121. REGISTER_TNLP(MittelmannParaCntrlBase<MittelmannParaCntrl5_1>, MPara5_1)
  122. REGISTER_TNLP(MittelmannParaCntrlBase<MittelmannParaCntrl5_2_1>, MPara5_2_1)
  123. REGISTER_TNLP(MittelmannParaCntrlBase<MittelmannParaCntrl5_2_2>, MPara5_2_2)
  124. REGISTER_TNLP(MittelmannParaCntrlBase<MittelmannParaCntrl5_2_3>, MPara5_2_3)
  125. static void print_problems()
  126. {
  127. printf("\nList of all registered problems:\n\n");
  128. RegisteredTNLPs::PrintRegisteredProblems();
  129. }
  130. int main00(int argv, char* argc[])
  131. {
  132. #ifndef LUOYC20220601
  133. SmartPtr<RegisteredTNLP> tnlp;
  134. // tnlp 要解决的问题
  135. tnlp = RegisteredTNLPs::GetTNLP("LukVlE1");
  136. // N 问题的规模
  137. Index N = 3;
  138. #else
  139. if (argv==2 && !strcmp(argc[1],"list")) {
  140. print_problems();
  141. return 0;
  142. }
  143. #ifdef TIME_LIMIT
  144. if (argv==4) {
  145. int runtime = atoi(argc[3]);
  146. pthread_t thread;
  147. pthread_create(&thread, NULL, killer_thread, &runtime);
  148. }
  149. else
  150. #endif
  151. if (argv!=3 && argv!=1) {
  152. printf("Usage: %s (this will ask for problem name)\n", argc[0]);
  153. printf(" %s ProblemName N\n", argc[0]);
  154. printf(" where N is a positive parameter determining problem size\n");
  155. printf(" %s list\n", argc[0]);
  156. printf(" to list all registered problems.\n");
  157. return -1;
  158. }
  159. SmartPtr<RegisteredTNLP> tnlp;
  160. Index N;
  161. if (argv!=1) {
  162. // Create an instance of your nlp...
  163. tnlp = RegisteredTNLPs::GetTNLP(argc[1]);
  164. if (!IsValid(tnlp)) {
  165. printf("Problem with name \"%s\" not known.\n", argc[1]);
  166. print_problems();
  167. return -2;
  168. }
  169. N = atoi(argc[2]);
  170. }
  171. else {
  172. bool done = false;
  173. while (!done) {
  174. string inputword;
  175. cout << "Enter problem name (or \"list\" for all available names):\n";
  176. cin >> inputword;
  177. if (inputword=="list") {
  178. print_problems();
  179. }
  180. else {
  181. tnlp = RegisteredTNLPs::GetTNLP(inputword.c_str());
  182. if (!IsValid(tnlp)) {
  183. printf("Problem with name \"%s\" not known.\n", inputword.c_str());
  184. }
  185. else {
  186. done = true;
  187. }
  188. }
  189. }
  190. cout << "Enter problem size:\n";
  191. cin >> N;
  192. }
  193. #endif
  194. if (N <= 0) {
  195. printf("Given problem size is invalid.\n");
  196. return -3;
  197. }
  198. bool retval = tnlp->InitializeProblem(N);
  199. if (!retval) {
  200. printf("Cannot initialize problem. Abort.\n");
  201. return -4;
  202. }
  203. // Create an instance of the IpoptApplication
  204. // We are using the factory, since this allows us to compile this
  205. // example with an Ipopt Windows DLL
  206. SmartPtr<IpoptApplication> app = IpoptApplicationFactory();
  207. ApplicationReturnStatus status;
  208. status = app->Initialize();
  209. if (status != Solve_Succeeded) {
  210. printf("\n\n*** Error during initialization!\n");
  211. return (int) status;
  212. }
  213. // Set option to use internal scaling
  214. // DOES NOT WORK FOR VLUKL* PROBLEMS:
  215. // app->Options()->SetStringValueIfUnset("nlp_scaling_method", "user-scaling");
  216. status = app->OptimizeTNLP(GetRawPtr(tnlp));
  217. return (int) status;
  218. }