GUROBISolver.hh 6.24 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
24
25
26
#include "NProblemInterface.hh"
#include "NConstraintInterface.hh"
#include "VariableType.hh"

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

27

David Bommes's avatar
David Bommes committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//== NAMESPACES ===============================================================

namespace COMISO {

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

	      

/** \class NewtonSolver GUROBISolver.hh

    Brief Description.
  
    A more elaborate description follows.
*/
class COMISODLLEXPORT GUROBISolver
{
public:
  // ********** SOLVE **************** //
  bool solve(NProblemInterface*                  _problem,                // problem instance
             std::vector<NConstraintInterface*>& _constraints,            // linear constraints
48
             std::vector<PairIndexVtype>&        _discrete_constraints,   // discrete constraints
David Bommes's avatar
David Bommes committed
49
50
             const double                        _time_limit = 60     ); // time limit in seconds

51
52
53
54
  bool solve(NProblemInterface*                  _problem,                // problem instance
             std::vector<NConstraintInterface*>& _constraints,            // linear constraints
             const double                        _time_limit = 60     ) // time limit in seconds
  {
55
56
    std::vector<PairIndexVtype> dc;
    return solve(_problem, _constraints, dc, _time_limit);
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  }

  // 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
71
72
73
74
75
76
77
78
79
  
  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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101


  // 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)
  {
102
103
    std::vector<PairIndexVtype> dc;
    return solve(_problem, _constraints, _lazy_constraints, dc, _almost_infeasible, _max_passes, _time_limit, _silent);
104
105
106
  }


David Bommes's avatar
David Bommes committed
107
108
109
110
  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
111
112
113
114
115
116
117
118
119
120
121
protected:
  double* P(std::vector<double>& _v)
  {
    if( !_v.empty())
      return ((double*)&_v[0]);
    else
      return 0;
  }

private:

David Bommes's avatar
David Bommes committed
122
123
124
125
126
  // 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
127
128
129
130
131
132
133
134
135
136
137
138
139
};



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

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