cpp_example.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (C) 2004, 2009 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: cpp_example.cpp 2005 2011-06-06 12:55:16Z stefan $
  6. //
  7. // Authors: Carl Laird, Andreas Waechter IBM 2004-11-05
  8. #include "IpIpoptApplication.hpp"
  9. #include "IpSolveStatistics.hpp"
  10. #include "MyNLP.hpp"
  11. #include <iostream>
  12. using namespace Ipopt;
  13. int main(int argv, char* argc[])
  14. {
  15. // Create an instance of your nlp...
  16. SmartPtr<TNLP> mynlp = new MyNLP();
  17. // Create an instance of the IpoptApplication
  18. //
  19. // We are using the factory, since this allows us to compile this
  20. // example with an Ipopt Windows DLL
  21. SmartPtr<IpoptApplication> app = IpoptApplicationFactory();
  22. // Initialize the IpoptApplication and process the options
  23. ApplicationReturnStatus status;
  24. status = app->Initialize();
  25. if (status != Solve_Succeeded) {
  26. std::cout << std::endl << std::endl << "*** Error during initialization!" << std::endl;
  27. return (int) status;
  28. }
  29. status = app->OptimizeTNLP(mynlp);
  30. if (status == Solve_Succeeded) {
  31. // Retrieve some statistics about the solve
  32. Index iter_count = app->Statistics()->IterationCount();
  33. std::cout << std::endl << std::endl << "*** The problem solved in " << iter_count << " iterations!" << std::endl;
  34. Number final_obj = app->Statistics()->FinalObjective();
  35. std::cout << std::endl << std::endl << "*** The final value of the objective function is " << final_obj << '.' << std::endl;
  36. }
  37. return (int) status;
  38. }