Hermes  0.9.5-beta
Hierarchical Distributed I/O Buffering System
linprog.h
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Distributed under BSD 3-Clause license. *
3  * Copyright by The HDF Group. *
4  * Copyright by the Illinois Institute of Technology. *
5  * All rights reserved. *
6  * *
7  * This file is part of Hermes. The full Hermes copyright notice, including *
8  * terms governing use, modification, and redistribution, is contained in *
9  * the COPYING file, which can be found at the top directory. If you do not *
10  * have access to the file, you may request a copy from help@hdfgroup.org. *
11  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12 
13 #ifndef HERMES_SRC_DPE_LINPROG_H_
14 #define HERMES_SRC_DPE_LINPROG_H_
15 
16 #include <glpk.h>
17 #include <math.h>
18 
19 #include <string>
20 #include <vector>
21 
22 namespace hermes {
23 
27 struct Array2DIdx {
28  int nrow_;
29  int ncol_;
32  Array2DIdx(int nrow, int ncol) : nrow_(nrow), ncol_(ncol) {}
33 
35  int Get(int i, int j) { return i * nrow_ + j; }
36 
38  int Begin(int i) { return Get(i, 0); }
39 
41  int End(int i) { return Get(i, ncol_); }
42 };
43 
48 const size_t kDefaultCoeffs = 1000 + 1;
49 
54  private:
55  int num_vars_;
57  glp_prob *lp_;
58  std::vector<int> ia_;
59  std::vector<int> ja_;
60  std::vector<double> ar_;
61  size_t cur_constraint_;
62  size_t result_;
64  public:
66  explicit LinearProgram(const char *name) {
67  lp_ = glp_create_prob();
68  glp_set_prob_name(lp_, name);
69  cur_constraint_ = 0;
70  }
71 
74  glp_delete_prob(lp_);
75  glp_free_env();
76  }
77 
79  void DefineDimension(int num_vars, int num_constraints) {
80  // NOTE(llogan): GLPK requires arrays start from "1" instead of "0"
81  glp_add_rows(lp_, num_constraints);
82  glp_add_cols(lp_, num_vars);
83  ia_.reserve(kDefaultCoeffs);
84  ia_.emplace_back(0);
85  ja_.reserve(kDefaultCoeffs);
86  ja_.emplace_back(0);
87  ar_.reserve(kDefaultCoeffs);
88  ar_.emplace_back(0.0);
89  num_vars_ = num_vars;
90  num_constraints_ = num_constraints;
91  }
92 
94  void AddConstraint(const std::string &base_name, int op_type, double lb,
95  double ub) {
96  cur_constraint_ += 1;
97  std::string name = base_name + std::to_string(cur_constraint_);
98  glp_set_row_name(lp_, cur_constraint_, name.c_str());
99  glp_set_row_bnds(lp_, cur_constraint_, op_type, lb, ub);
100  }
101 
103  void SetConstraintCoeff(int var, double val) {
104  var += 1;
105  ia_.emplace_back(cur_constraint_);
106  ja_.emplace_back(var);
107  ar_.emplace_back(val);
108  }
109 
111  void SetVariableBounds(const std::string &base_name, int var, int op_type,
112  double lb, double ub) {
113  var += 1;
114  std::string name = base_name + std::to_string(var);
115  glp_set_col_name(lp_, var, name.c_str());
116  glp_set_col_bnds(lp_, var, op_type, lb, ub);
117  }
118 
120  void SetObjective(int objective) { glp_set_obj_dir(lp_, objective); }
121 
123  void SetObjectiveCoeff(int var, double val) {
124  var += 1;
125  glp_set_obj_coef(lp_, var, val);
126  }
127 
129  void Solve() {
130  glp_load_matrix(lp_, ia_.size() - 1, ia_.data(), ja_.data(), ar_.data());
131  glp_smcp parm;
132  glp_init_smcp(&parm);
133  parm.msg_lev = GLP_MSG_OFF;
134  glp_simplex(lp_, &parm);
135  result_ = glp_get_status(lp_);
136  }
137 
139  bool IsOptimal() { return result_ == GLP_OPT; }
140 
142  double GetSolution() { return glp_get_obj_val(lp_); }
143 
145  double GetVariable(int var) {
146  var += 1;
147  return glp_get_col_prim(lp_, var);
148  }
149 
151  std::vector<double> ToVector() {
152  std::vector<double> v;
153  v.reserve(num_vars_);
154  for (int var = 0; var < num_vars_; ++var) {
155  v.emplace_back(GetVariable(var));
156  }
157  return v;
158  }
159 };
160 
161 } // namespace hermes
162 
163 #endif // HERMES_SRC_DPE_LINPROG_H_
Definition: linprog.h:53
double GetVariable(int var)
Definition: linprog.h:145
LinearProgram(const char *name)
Definition: linprog.h:66
~LinearProgram()
Definition: linprog.h:73
void SetVariableBounds(const std::string &base_name, int var, int op_type, double lb, double ub)
Definition: linprog.h:111
void SetObjective(int objective)
Definition: linprog.h:120
double GetSolution()
Definition: linprog.h:142
int num_vars_
Definition: linprog.h:55
glp_prob * lp_
Definition: linprog.h:57
std::vector< double > ar_
Definition: linprog.h:60
void Solve()
Definition: linprog.h:129
std::vector< double > ToVector()
Definition: linprog.h:151
std::vector< int > ia_
Definition: linprog.h:58
size_t cur_constraint_
Definition: linprog.h:61
void SetConstraintCoeff(int var, double val)
Definition: linprog.h:103
size_t result_
Definition: linprog.h:62
std::vector< int > ja_
Definition: linprog.h:59
int num_constraints_
Definition: linprog.h:56
void AddConstraint(const std::string &base_name, int op_type, double lb, double ub)
Definition: linprog.h:94
void SetObjectiveCoeff(int var, double val)
Definition: linprog.h:123
bool IsOptimal()
Definition: linprog.h:139
void DefineDimension(int num_vars, int num_constraints)
Definition: linprog.h:79
Definition: adapter_utils.cc:35
const size_t kDefaultCoeffs
Definition: linprog.h:48
Definition: linprog.h:27
int Get(int i, int j)
Definition: linprog.h:35
int End(int i)
Definition: linprog.h:41
int nrow_
Definition: linprog.h:28
int ncol_
Definition: linprog.h:29
int Begin(int i)
Definition: linprog.h:38
Array2DIdx(int nrow, int ncol)
Definition: linprog.h:32