Scheduler
glpkImplementation.cpp
Go to the documentation of this file.
1 
10 #include "glpkImplementation.h"
11 
12 #include <stdexcept>
13 extern "C"
14 {
15  #include <glpk.h>
16 }
17 
18 
19 using namespace Mdp;
20 
21 double GlpkImplementation::solve(std::vector<double>& columns,
22  std::vector<double> coeffs,
23  std::vector<std::vector<double>> eqCoeffs,
24  std::vector<double> eqValue,
25  std::vector<std::vector<double>> ineqCoeffs,
26  std::vector<double> ineqValue)
27 {
28  double objFunc;
29 
30  size_t nbCol = columns.size();
31  size_t nbEqConst = eqCoeffs.size();
32  size_t nbIneqConst = ineqCoeffs.size();
33  /* see 1.3 in [3]*/
34  glp_prob *lp;
35  const size_t nbRow = nbEqConst + nbIneqConst;
36  const size_t constrSize = nbCol*nbRow;
37  int *ia = new int[1 + constrSize];
38  int *ja = new int[1 + constrSize];
39  double *ar = new double[1 + constrSize];
40  lp = glp_create_prob();
41  glp_set_prob_name(lp, "MDP solver");
42  glp_set_obj_dir(lp, GLP_MIN);
43  glp_add_rows(lp, nbRow);
44  for (size_t i = 0; i < nbEqConst; i++)
45  {
46  glp_set_row_name(lp, i+1, ""); //is this even necessary?
47  glp_set_row_bnds(lp, i+1, GLP_FX, eqValue[i], eqValue[i]);
48  }
49  for (size_t i = 0; i < nbIneqConst; i++)
50  {
51  glp_set_row_name(lp, i+1+nbEqConst, ""); //is this even necessary?
52  glp_set_row_bnds(lp, i+1+nbEqConst, GLP_UP, 0.0, ineqValue[i]);
53  }
54  glp_add_cols(lp, nbCol);
55  for (size_t i = 0; i < nbCol; i++)
56  {
57  glp_set_col_name(lp, i+1, ""); //is this even necessary??
58  glp_set_col_bnds(lp, i+1, GLP_LO, 0.0, 0.0);
59  /*the rho are greater than 0, see [1].
60  specifying this here simplifies the code, with respect to having to declare new constraints,
61  but makes the code non-reusable.*/
62  glp_set_obj_coef(lp, i+1, coeffs[i]);
63  }
64 
65 
66  int counter = 1;
67  for (size_t i = 1; i <= nbEqConst; i++)
68  {
69  for (size_t j = 1; j <= nbCol; j++)
70  {
71  ia[counter] = i;
72  ja[counter] = j;
73  ar[counter] = eqCoeffs[i-1][j-1];
74  counter++;
75  }
76  }
77  for (size_t i = nbEqConst+1; i <= nbRow; i++)
78 
79  {
80  for (size_t j = 1; j <= nbCol; j++)
81  {
82  ia[counter] = i;
83  ja[counter] = j;
84  ar[counter] = ineqCoeffs[i-1-nbEqConst][j-1];
85  counter++;
86  }
87  }
88  glp_load_matrix(lp, constrSize, ia, ja, ar);
89 
90 
91  switch (algorithm)
92  {
93  case LpAlgo::simplex:
94  objFunc = simplexSolve(lp, columns);
95  break;
96  case LpAlgo::interiorPoint:
97  objFunc = interiorPointSolve(lp, columns);
98  break;
99  default:
100  throw std::runtime_error("Unknown LP algorithm");
101  break;
102  }
103 
104  glp_delete_prob(lp);
105 
106  delete ia;
107  delete ja;
108  delete ar;
109  return objFunc;
110 
111 
112 
113 }
114 
115 
116 double GlpkImplementation::simplexSolve(glp_prob *lp, std::vector<double>& columns)
117 {
118  glp_simplex(lp, NULL);
119  for (size_t i = 0; i < columns.size(); i++)
120  {
121  columns[i] = glp_get_col_prim(lp, i+1);
122  }
123  return glp_get_obj_val(lp);
124 }
125 
126 
127 double GlpkImplementation::interiorPointSolve(glp_prob *lp, std::vector<double>& columns)
128 {
129  glp_interior(lp, NULL);
130  for (size_t i = 0; i < columns.size(); i++)
131  {
132  columns[i] = glp_ipt_col_prim(lp, i+1);
133  }
134  return glp_ipt_obj_val(lp);
135 }
double solve(std::vector< double > &variables, std::vector< double > coeffs, std::vector< std::vector< double >> eqCoeffs, std::vector< double > eqValue, std::vector< std::vector< double >> ineqCoeffs, std::vector< double > ineqValue)
Definition: action.h:18