source: branches/devel/Bonmin/src/BonminAmplInterface/BonAmplInterface.cpp @ 62

Last change on this file since 62 was 62, checked in by pbonami, 13 years ago

astyled the devel branch

  • Property svn:eol-style set to native
  • Property svn:keywords set to "Author Date Id Revision"
File size: 3.9 KB
Line 
1#include "BonAmplInterface.hpp"
2#include <string>
3#include <sstream>
4
5
6namespace Bonmin
7{
8  /** Default constructor */
9  AmplInterface::AmplInterface(): IpoptInterface(), amplTminlp_(NULL)
10  {}
11
12  /** Constructor with inputed ampl command line (reads model from nl file)*/
13  AmplInterface::AmplInterface(char **& amplArgs)
14      :
15      IpoptInterface(),
16      amplTminlp_(NULL)
17  {
18    readAmplNlFile(amplArgs, NULL, NULL);
19  }
20
21  /** Copy constructor */
22  AmplInterface::AmplInterface(const AmplInterface &other):
23      IpoptInterface(other), amplTminlp_(NULL)
24  {
25    amplTminlp_ = dynamic_cast<Bonmin::AmplTMINLP *> (GetRawPtr(tminlp_));
26  }
27/// Clone
28  AmplInterface *
29  AmplInterface::clone(bool CopyData )
30  {
31    return new AmplInterface(*this);
32  }
33
34///Destructor
35  AmplInterface::~AmplInterface()
36  {
37    amplTminlp_ = NULL;
38  }
39
40  /** Read an ampl . nl file from the given filename */
41  void
42  AmplInterface::readAmplNlFile(char**& filename,
43      std::string* ipopt_file_content,
44      std::string* nl_file_content)
45  {
46
47
48
49    app_ = new IpoptSolver;
50
51    SmartPtr<RegisteredOptions> roptions = app_->RegOptions();
52    register_ALL_options(roptions);
53
54    // Call initalize to open output
55    app_->Initialize("");
56    // Read the bonmin.opt input file
57    if (ipopt_file_content == NULL) {
58      app_->Initialize("bonmin.opt");
59    }
60    else {
61      std::stringstream ss(ipopt_file_content->c_str());
62      app_->Initialize(ss);
63    }
64
65
66    // set the default options... expect_infeasible, etc...
67    IpoptSolver * ipopt = dynamic_cast<IpoptSolver *> (GetRawPtr(app_));
68    if (!IsValid(tminlp_)) {
69      amplTminlp_ = new AmplTMINLP(ConstPtr(ipopt->getIpoptApp().Jnlst()), app_->Options(), filename,
70          NULL, appName() , nl_file_content);
71      tminlp_ = GetRawPtr(amplTminlp_);
72    }
73    else {
74      AmplTMINLP * amplTMINLP = dynamic_cast<AmplTMINLP *> (GetRawPtr(tminlp_));
75      if (amplTMINLP) {
76        AmplTMINLP * newAmpl = amplTMINLP->createEmpty();
77        newAmpl->Initialize(ConstPtr(ipopt->getIpoptApp().Jnlst()), app_->Options(), filename,
78            NULL, appName() , nl_file_content);
79        amplTminlp_ = newAmpl;
80        tminlp_ = GetRawPtr(amplTminlp_);
81      }
82      else {
83        amplTminlp_ = new AmplTMINLP(ConstPtr(ipopt->getIpoptApp().Jnlst()), app_->Options(), filename,
84            NULL, appName() , nl_file_content);
85        tminlp_ = GetRawPtr(amplTminlp_);
86      }
87    }
88    problem_ = new TMINLP2TNLP(tminlp_);//, *app_->Options());
89
90    bool print_options_documentation;
91    app_->Options()->GetBoolValue("print_options_documentation",
92        print_options_documentation, "");
93    if (print_options_documentation) {
94      std::list<std::string> categories;
95      categories.push_back("bonmin branch-and-bound options");
96      categories.push_back("bonmin options for robustness");
97      categories.push_back("bonmin options for non-convex problems");
98      categories.push_back("bonmin options : B-Hyb specific options");
99//    roptions->OutputLatexOptionDocumentation2(*app_->Jnlst(),categories);
100      roptions->OutputOptionDocumentation(*(ipopt->getIpoptApp().Jnlst()),categories);
101    }
102
103    int numcols = getNumCols();
104    if (obj_)
105      delete [] obj_;
106    obj_ = new double[numcols];
107    CoinFillN(obj_,numcols,1.);
108    setStrParam(OsiProbName, std::string(filename[1]));
109    extractInterfaceParams();
110    hasBeenOptimized_ = false;
111    feasibilityProblem_ = new TNLP2FPNLP
112        (Ipopt::SmartPtr<TNLP>(Ipopt::GetRawPtr(problem_)));
113  }
114
115  /** write ampl solution file */
116  void
117  AmplInterface::writeAmplSolFile(std::string message,const double * primalSol,const double * dualSol)
118  {
119    TMINLP * tminlp = GetRawPtr(tminlp_);
120    AmplTMINLP * ampl_tminlp = dynamic_cast<AmplTMINLP *> (tminlp);
121    if (ampl_tminlp)
122      ampl_tminlp->write_solution(message,primalSol,dualSol);
123    else
124      std::cerr<<"Errot can not write .sol file for non ampl problem"<<std::endl;
125  }
126
127}
Note: See TracBrowser for help on using the repository browser.