GUROBISolver.hh 6.5 KB
Newer Older
David Bommes's avatar
David Bommes committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//=============================================================================
//
//  CLASS GUROBISolver
//
//=============================================================================


#ifndef COMISO_GUROBISOLVER_HH
#define COMISO_GUROBISOLVER_HH


//== COMPILE-TIME PACKAGE REQUIREMENTS ========================================
#include <CoMISo/Config/config.hh>
#if COMISO_GUROBI_AVAILABLE

//== INCLUDES =================================================================

#include <CoMISo/Config/CoMISoDefines.hh>
#include <vector>
David Bommes's avatar
David Bommes committed
20
#include <string>
David Bommes's avatar
David Bommes committed
21 22 23
#include "NProblemInterface.hh"
#include "NConstraintInterface.hh"
#include "VariableType.hh"
David Bommes's avatar
David Bommes committed
24
#include "GurobiHelper.hh"
David Bommes's avatar
David Bommes committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

#include <gurobi_c++.h>

//== FORWARDDECLARATIONS ======================================================

//== NAMESPACES ===============================================================

namespace COMISO {

//== CLASS DEFINITION =========================================================

	      

/** \class NewtonSolver GUROBISolver.hh

    Brief Description.
  
    A more elaborate description follows.
*/
class COMISODLLEXPORT GUROBISolver
{
public:

David Bommes's avatar
David Bommes committed
48 49
  /// Default constructor
  GUROBISolver();
David Bommes's avatar
David Bommes committed
50 51 52 53 54 55 56
 
  /// Destructor
  ~GUROBISolver() {}

  // ********** SOLVE **************** //
  bool solve(NProblemInterface*                  _problem,                // problem instance
             std::vector<NConstraintInterface*>& _constraints,            // linear constraints
57
             std::vector<PairIndexVtype>&        _discrete_constraints,   // discrete constraints
David Bommes's avatar
David Bommes committed
58 59
             const double                        _time_limit = 60     ); // time limit in seconds

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  bool solve(NProblemInterface*                  _problem,                // problem instance
             std::vector<NConstraintInterface*>& _constraints,            // linear constraints
             const double                        _time_limit = 60     ) // time limit in seconds
  {
    std::vector<PairIndexVtype> dc; return solve(_problem, _constraints, dc, _time_limit);
  }

  // same as previous but more control over stopping criteria
  // the optimizer runs in two phases
  // phase 1) stops if solution with a MIP gap lower than _gap0 is found or _time_limit0 is reached
  // phase 2) starts only if in phase 1 no solution with a MIP gap lower than _gap1 was found and
  //          uses _gap1 and _time_limit2 as parameters
  bool solve_two_phase(NProblemInterface*                  _problem,                // problem instance
             std::vector<NConstraintInterface*>& _constraints,            // linear constraints
             std::vector<PairIndexVtype>&        _discrete_constraints,   // discrete constraints
             const double                        _time_limit0 = 60, // time limit phase 1 in seconds
             const double                        _gap0 = 0.001,     // MIP gap phase 1
             const double                        _time_limit1 = 120, // time limit phase 2 in seconds
             const double                        _gap1 = 0.2 );       // MIP gap phase 2
Marcel Campen's avatar
Marcel Campen committed
79 80 81 82 83 84 85 86 87
  
  bool solve_two_phase(NProblemInterface*                  _problem,                // problem instance
                       std::vector<NConstraintInterface*>& _constraints,            // linear constraints
                       std::vector<PairIndexVtype>&        _discrete_constraints,   // discrete constraints
                       const double                        _time_limit0, // time limit phase 1 in seconds
                       const double                        _gap0 ,     // MIP gap phase 1
                       const double                        _time_limit1, // time limit phase 2 in seconds
                       const double                        _gap1,  // MIP gap phase 2
                       double&                             _final_gap);  //return final gap
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


  // optimization with additional lazy constraints that are only added iteratively to the problem if not satisfied
  bool solve(NProblemInterface*                        _problem,
                    const std::vector<NConstraintInterface*>& _constraints,
                    const std::vector<NConstraintInterface*>& _lazy_constraints,
                    std::vector<PairIndexVtype>&              _discrete_constraints,   // discrete constraints
                    const double                              _almost_infeasible = 0.5,
                    const int                                 _max_passes        = 5,
                    const double                              _time_limit = 60,
                    const bool                                _silent = false);


  // same as above with additional lazy constraints that are only added iteratively to the problem if not satisfied
  bool solve(NProblemInterface*                        _problem,
                    const std::vector<NConstraintInterface*>& _constraints,
                    const std::vector<NConstraintInterface*>& _lazy_constraints,
                    const double                              _almost_infeasible = 0.5,
                    const int                                 _max_passes        = 5,
                    const double                              _time_limit = 60,
                    const bool                                _silent = false)
  {
    std::vector<PairIndexVtype> dc; return solve(_problem, _constraints, _lazy_constraints, dc, _almost_infeasible, _max_passes, _time_limit, _silent);
  }


David Bommes's avatar
David Bommes committed
114 115 116 117
  void set_problem_output_path    ( const std::string &_problem_output_path);
  void set_problem_env_output_path( const std::string &_problem_env_output_path);
  void set_solution_input_path    ( const std::string &_solution_input_path);

David Bommes's avatar
David Bommes committed
118 119 120 121 122 123 124 125 126
protected:
  double* P(std::vector<double>& _v)
  {
    if( !_v.empty())
      return ((double*)&_v[0]);
    else
      return 0;
  }

127 128 129
private:
  void add_constraint_to_model(COMISO::NConstraintInterface* _constraint, GRBModel& _model, std::vector<GRBVar>& _vars, double * _x);

David Bommes's avatar
David Bommes committed
130 131
private:

David Bommes's avatar
David Bommes committed
132 133 134 135 136
  // filenames for exporting/importing gurobi solutions
  // if string is empty nothing is imported or exported
  std::string problem_output_path_;
  std::string problem_env_output_path_;
  std::string solution_input_path_;
David Bommes's avatar
David Bommes committed
137 138 139 140 141 142 143 144 145 146 147 148 149
};



//=============================================================================
} // namespace COMISO

//=============================================================================
#endif // COMISO_GUROBI_AVAILABLE
//=============================================================================
#endif // ACG_GUROBISOLVER_HH defined
//=============================================================================