BonAmplInterface.cpp
Go to the documentation of this file.
1 // (C) Copyright International Business Machines Corporation and
2 // Carnegie Mellon University 2004, 2007
3 //
4 // All Rights Reserved.
5 // This code is published under the Eclipse Public License.
6 //
7 // Authors :
8 // Pierre Bonami, Carnegie Mellon University,
9 // Andreas Waechter, International Business Machines Corporation
10 //
11 // Date : 12/01/2004
12 
13 #include "BonminConfig.h"
14 
15 #include "BonAmplInterface.hpp"
16 #include "BonIpoptSolver.hpp"
17 #include "BonColReader.hpp"
18 #ifdef COIN_HAS_FILTERSQP
19 # include "BonFilterSolver.hpp"
20 #endif
21 #include <string>
22 #include <sstream>
23 
24 #include "BonTNLP2FPNLP.hpp"
25 
26 namespace Bonmin
27 {
30  OsiTMINLPInterface(), amplTminlp_(NULL)
31  {}
32 
35  OsiTMINLPInterface(other), amplTminlp_(NULL)
36  {
37  amplTminlp_ = dynamic_cast<Bonmin::AmplTMINLP *> (GetRawPtr(tminlp_));
38  }
40  OsiSolverInterface *
41  AmplInterface::clone(bool CopyData )
42  {
43  return new AmplInterface(*this);
44  }
45 
48  {
49  amplTminlp_ = NULL;
50  }
51 
52 
56  std::string* nl_file_content /* = NULL*/
57  )
58  {
59  if (!IsValid(app_)) {
60  createApplication(roptions, options, journalist, "bonmin.");
61  }
62  // set the default options... expect_infeasible, etc...
63  if (!IsValid(tminlp_)) {
64  amplTminlp_ = new AmplTMINLP(Ipopt::ConstPtr(app_->journalist()), app_->roptions(), app_->options(), argv,
65  NULL, appName() , nl_file_content);
66  tminlp_ = GetRawPtr(amplTminlp_);
67  }
68  else {
69  AmplTMINLP * amplTMINLP = dynamic_cast<AmplTMINLP *> (GetRawPtr(tminlp_));
70  if (amplTMINLP) {
71  AmplTMINLP * newAmpl = amplTMINLP->createEmpty();
72  newAmpl->Initialize(ConstPtr(app_->journalist()), app_->roptions(), app_->options(), argv,
73  NULL, appName() , nl_file_content);
74  amplTminlp_ = newAmpl;
75  tminlp_ = GetRawPtr(amplTminlp_);
76  }
77  else {
78  amplTminlp_ = new AmplTMINLP(ConstPtr(app_->journalist()), app_->roptions(), app_->options(), argv,
79  NULL, appName() , nl_file_content);
80  tminlp_ = GetRawPtr(amplTminlp_);
81  }
82  }
83  problem_ = new TMINLP2TNLP(tminlp_);
84  feasibilityProblem_ = new TNLP2FPNLP
86  if(feasibility_mode_){
87  problem_to_optimize_ = GetRawPtr(feasibilityProblem_);
88  }
89  else {
90  problem_to_optimize_ = GetRawPtr(problem_);
91  }
92 
93  int numcols = getNumCols();
94  if (obj_)
95  delete [] obj_;
96  obj_ = new double[numcols];
97  CoinFillN(obj_,numcols,1.);
98  setStrParam(OsiProbName, std::string(argv[1]));
99  extractInterfaceParams();
100  hasBeenOptimized_ = false;
101  //Read columns and row names if they exists
102  readNames();
103  }
104 
105  void
107 {}
108 
109 
110  void
112  {
113  std::string probName;
114  getStrParam(OsiProbName, probName);
115  NamesReader colRead(probName, ".col");
116  if (colRead.readFile()) {
117  OsiNameVec colNames;
118  colRead.copyNames(colNames);
119  setColNames(colNames, 0, static_cast<int>(colNames.size()), 0);
120  }
121 
122  NamesReader rowRead(probName, ".row");
123  if (rowRead.readFile()) {
124  OsiNameVec rowNames;
125  rowRead.copyNames(rowNames);
126  setRowNames(rowNames, 0, static_cast<int>(rowNames.size()), 0);
127  }
128 
129 
130  }
131 
132 } /* end namespace Bonmin. */
virtual void Initialize(const Ipopt::SmartPtr< const Ipopt::Journalist > &jnlst, const Ipopt::SmartPtr< Bonmin::RegisteredOptions > roptions, const Ipopt::SmartPtr< Ipopt::OptionsList > options, char **&argv, Ipopt::AmplSuffixHandler *suffix_handler=NULL, const std::string &appName="bonmin", std::string *nl_file_content=NULL)
Constructor.
void copyNames(OsiSolverInterface::OsiNameVec &Names)
Copy the names to Names.
virtual ~AmplInterface()
Destructor.
virtual OsiSolverInterface * clone(bool CopyData=true)
Clone.
This is class provides an Osi interface for a Mixed Integer Linear Program expressed as a TMINLP (so ...
bool IsValid(const OSSmartPtr< U > &smart_ptr)
Definition: OSSmartPtr.hpp:465
void readNames()
Read variables and row names in .col and .row files.
bool readFile()
Reads the .col file.
virtual int getNumCols() const
Get number of columns.
Class for providing an Osi interface to Ipopt with an ampl nl file as input.
bool getStrParam(OsiStrParam key, std::string &value) const
virtual void readAmplNlFile(char **&argv, Ipopt::SmartPtr< Bonmin::RegisteredOptions > roptions, Ipopt::SmartPtr< Ipopt::OptionsList > options, Ipopt::SmartPtr< Ipopt::Journalist > journalist, std::string *nl_file_content=NULL)
OSSmartPtr< const U > ConstPtr(const OSSmartPtr< U > &smart_ptr)
Definition: OSSmartPtr.hpp:458
bool setStrParam(OsiStrParam key, const std::string &value)
U * GetRawPtr(const OSSmartPtr< U > &smart_ptr)
Definition: OSSmartPtr.hpp:452
A class for reading a .col or .row file containing name for variables and constraints (usually ampl g...
virtual AmplTMINLP * createEmpty()
Constructor.
AmplInterface()
Default constructor.
Ipopt::SmartPtr< Bonmin::AmplTMINLP > amplTminlp_
TMINLP problem (the original problem usually an AmplTMINLP).
Ampl MINLP Interface.
virtual void setAppDefaultOptions(Ipopt::SmartPtr< Ipopt::OptionsList > Options)
To set some application specific defaults.
This is an adapter class that converts a TMINLP to a TNLP to be solved by Ipopt.
This is an adapter class to convert an NLP to a Feasibility Pump NLP by changing the objective functi...