summaryrefslogtreecommitdiff
path: root/build/Bonmin/include/coin/BonTNLPSolver.hpp
blob: 195fbad68ebf343412766abb174da9f7da5b0f91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// (C) Copyright International Business Machines (IBM) 2006, 2007
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// Authors :
// Pierre Bonami, IBM
//
// Date : 26/09/2006


#ifndef TNLPSolver_H
#define TNLPSolver_H
#include "IpTNLP.hpp"
#include "BonTMINLP2TNLP.hpp"

//Some declarations
#include "IpOptionsList.hpp"
#include "CoinWarmStart.hpp"
#include "BonRegisteredOptions.hpp"
#include "CoinTime.hpp"
namespace Bonmin  {
/** This is a generic class for calling an NLP solver to solve a TNLP.
    A TNLPSolver is able to solve and resolve a problem, it has some options (stored
    with Ipopt OptionList structure and registeredOptions) it produces some statistics (in SolveStatisctics and sometimes some errorCodes.
*/
class TNLPSolver: public Ipopt::ReferencedObject{
 public:

  enum ReturnStatus /** Standard return statuses for a solver*/{
    iterationLimit = -3/** Solver reached iteration limit. */,
    timeLimit = 5/** Solver reached iteration limit. */,
    doesNotConverge = -8/** Algorithm does not converge.*/,
    computationError = -2/** Some error was made in the computations. */,
    notEnoughFreedom = -1/** not enough degrees of freedom.*/,
    illDefinedProblem = -4/** The solver finds that the problem is not well defined. */,
    illegalOption =-5/** An option is not valid. */,
    externalException =-6/** Some unrecovered exception occured in an external tool used by the solver. */,
    exception =-7/** Some unrocevered exception */,
    solvedOptimal = 1/** Problem solved to an optimal solution.*/,
    solvedOptimalTol =2/** Problem solved to "acceptable level of tolerance. */,
    provenInfeasible =3/** Infeasibility Proven. */,
    unbounded = 4/** Problem is unbounded.*/,
    numReturnCodes/**Fake member to know size*/
  };



//#############################################################################

  /** We will throw this error when a problem is not solved.
      Eventually store the error code from solver*/
  class UnsolvedError
  {
  public:
    /** Constructor */
    UnsolvedError(int errorNum = -10000, 
                  Ipopt::SmartPtr<TMINLP2TNLP> model = NULL,
                  std::string name="")
    :
     errorNum_(errorNum),
     model_(model),
     name_(name)
    {if(name_=="") 
{
#ifndef NDEBUG
	std::cerr<<"FIXME"<<std::endl;
#endif
}}
    /** Print error message.*/
    void printError(std::ostream & os);
    /** Get the string corresponding to error.*/
    virtual const std::string& errorName() const = 0;
    /** Return the name of the solver. */
    virtual const std::string& solverName() const = 0;
    /** Return error number. */
    int errorNum() const{
    return errorNum_;}
    /** destructor. */
    virtual ~UnsolvedError(){}
    /** write files with differences between input model and
        this one */
    void writeDiffFiles(const std::string prefix=std::string()) const;
  private:
    /** Error code (solver dependent). */
    int errorNum_;

    /** model_ on which error occured*/
    Ipopt::SmartPtr< TMINLP2TNLP > model_;

    /** name of the model on which error occured. */
    std::string name_;
  }
  ;

  virtual UnsolvedError * newUnsolvedError(int num,
					   Ipopt::SmartPtr<TMINLP2TNLP> problem,
					   std::string name) = 0;
 


  /// default Constructor
   TNLPSolver();

  ///Constructor with options initialization
TNLPSolver(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions,
           Ipopt::SmartPtr<Ipopt::OptionsList> options,
           Ipopt::SmartPtr<Ipopt::Journalist> journalist,
           const std::string & prefix);

  ///virtual copy constructor
  virtual Ipopt::SmartPtr<TNLPSolver> clone() = 0;

   /// Virtual destructor
   virtual ~TNLPSolver();

   /** Initialize the TNLPSolver (read options from params_file)
   */
   virtual bool Initialize(std::string params_file) = 0;

   /** Initialize the TNLPSolver (read options from istream is)
   */
   virtual bool Initialize(std::istream& is) = 0;

   /** @name Solve methods */
   //@{
   /// Solves a problem expresses as a TNLP 
   virtual ReturnStatus OptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp) = 0;

   /// Resolves a problem expresses as a TNLP 
   virtual ReturnStatus ReOptimizeTNLP(const Ipopt::SmartPtr<Ipopt::TNLP> & tnlp) = 0;

  /// Set the warm start in the solver
  virtual bool setWarmStart(const CoinWarmStart * warm, 
                            Ipopt::SmartPtr<TMINLP2TNLP> tnlp) = 0;

/// Get warm start used in last optimization
  virtual CoinWarmStart * getUsedWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const = 0;

  /// Get the warm start form the solver
  virtual CoinWarmStart * getWarmStart(Ipopt::SmartPtr<TMINLP2TNLP> tnlp) const = 0;

  virtual CoinWarmStart * getEmptyWarmStart() const = 0;

  /** Check that warm start object is valid.*/
  virtual bool warmStartIsValid(const CoinWarmStart * ws) const = 0;  

  /// Enable the warm start options in the solver
  virtual void enableWarmStart() = 0;

  /// Disable the warm start options in the solver
  virtual void disableWarmStart() = 0;
   //@}

  ///Get a pointer to a journalist
  Ipopt::SmartPtr<Ipopt::Journalist> journalist(){
    return journalist_;}

   ///Get a pointer to RegisteredOptions (generally used to add new ones)
   Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions(){
     return roptions_;}

   /// Get the options (for getting their values).
   Ipopt::SmartPtr<const Ipopt::OptionsList> options() const {
     return ConstPtr(options_);}

   /// Get the options (for getting and setting their values).
   Ipopt::SmartPtr<Ipopt::OptionsList> options() {
     return options_;}

  /// Get the prefix
  const char * prefix(){
    return prefix_.c_str();
  }
   /// Register this solver options into passed roptions
static void RegisterOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions){}

   /// Get the CpuTime of the last optimization.
   virtual double CPUTime() = 0;

   /// Get the iteration count of the last optimization.
   virtual int IterationCount() = 0;


  /// turn off all output from the solver 
  virtual void setOutputToDefault() = 0 ;
  /// turn on all output from the solver
  virtual void forceSolverOutput(int log_level) = 0;
  /// Get the solver name
  virtual std::string & solverName() = 0;

    /** Say if an optimization status for a problem which failed is recoverable
        (problem may be solvable).*/
  bool isRecoverable(ReturnStatus &r);

  /** Setup for a global time limit for solver.*/
  void setup_global_time_limit(double time_limit){
    time_limit_ = time_limit + 5;
    start_time_ = CoinCpuTime();
  }

  /** Say if return status is an error.*/
  bool isError(ReturnStatus &r){
    return r < 0;}
  /** Error code (solver specific).*/
virtual int errorCode() const = 0;
protected:
   /** Determine if problem is of dimension zero and if it is check if solution
       is feasible.*/
   bool zeroDimension(const Ipopt::SmartPtr<Ipopt::TNLP> &tnlp, 
		     ReturnStatus &optimization_status);

   /** Initializes options and journalist.*/
   void initializeOptionsAndJournalist();

    /** Storage of Journalist for output */
    Ipopt::SmartPtr<Ipopt::Journalist> journalist_;
    
    /** List of Options */
    Ipopt::SmartPtr<Ipopt::OptionsList> options_;
    
    /** Registered Options */
    Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions_;
   
    /** Prefix to use for reading bonmin's options.*/
   std::string prefix_;
   /** Global start time.*/
   double start_time_;

   /** Global time limit.*/
   double time_limit_;

   /** To record default log level.*/
   int default_log_level_;
  /// Copy Constructor
  TNLPSolver(const TNLPSolver & other);

};
}
#endif