Exemple #1
0
/* Set the right hand side vector */
void __declspec(dllexport) WINAPI _set_rh_vec(lprec *lp, double *rh)
 {
  if (lp != NULL) {
   freebuferror();
   set_rh_vec(lp, rh);
  }
 }
Exemple #2
0
// check, if v is a positive integer combination of the vectors in gens
bool is_spanned_by(const VecSparse& v, const std::set<VecSparse>& gens) {
#ifdef LPSOLVE_OPT
  // std::cout << "Check: is " << v << "spanned by " << gens << "?...";

  if(gens.empty()) {
    return false;
  }

  if(v.empty()) {
    return true;
  }

  if(gens.find(v) != gens.end()) {
    //std::cout << "trivially spanned!"<<std::endl;
    return true;
  }

  lprec *lp;
  unsigned int N = gens.size();
  // dim =  number of variables that "gens" is talking about (altogether)

  //collect the variables of all the sparse vectors (v and gens) and number them
  unsigned int num_vars = 1; //we start counting at 1, since the 0-th row will be the values of the objective function
  std::map<VarPtr, unsigned int> vars;

  for(auto &pair : v) {
    if(vars.find(pair.first) == vars.end()) {
      vars[pair.first] = num_vars;
      num_vars++;
    }
  }

  for(auto &g : gens) {
    for(auto &pair : g) {
      if(vars.find(pair.first) == vars.end()) {
        vars[pair.first] = num_vars;
        num_vars++;
      }
    }
  }

  unsigned int dim = num_vars-1;
  lp = make_lp(dim, N); //first row = objective function (will be zeros in our case)!

  if(lp==NULL) {
    std::cerr << "ERROR: could not create LP (make_lp) of size " << dim+1 << "," << N << std::endl;
    return false;
  }

  set_verbose(lp, IMPORTANT);

  int colno = 1;
  // set the columns of the LP to the generators
  for (auto &g : gens) {
    int num_entries = g.size();

    //std::cout << "num_entries: " << num_entries << std::endl;

    REAL *sparsecolumn = new REAL[num_entries]; //non-zero-vals
    int *rowno = new int[num_entries]; //numbers of the non-zero rows

    int idx = 0;
    //assemble the columns and write them to the LP
    for(auto &pair : g) {
        sparsecolumn[idx] = pair.second;
        rowno[idx] = vars[pair.first];
        idx++;
    }
    set_columnex(lp, colno, num_entries, sparsecolumn, rowno);
    colno++;

    delete[] sparsecolumn;
    delete[] rowno;
  }

  // set the rhs to v
  REAL *rhs = new REAL[dim+1];
  for(auto pair : v) {
    rhs[vars[pair.first]] = pair.second;
  }
  set_rh_vec(lp, rhs);
  delete[] rhs;

  // set all variables to be integers and >=0
  for(int i=1; i<=N; i++){
    set_int(lp, i, TRUE);
    // set_lowbo(lp, i, 0); // the default should be 0 anyways
  }

  for(int i=1; i<=dim; i++){
      set_constr_type(lp, i, EQ);
  }


  set_maxim(lp);

  //print_lp(lp);

  int ret = solve(lp);

  if(ret == 0) {
    // std::cout << "yes!" << std::endl;
    //feasible solution found
/*    REAL *row = new REAL[N];
    get_variables(lp, row);
    for(int j = 0; j < N; j++)
      printf("%s: %f\n", get_col_name(lp, j + 1), row[j]);

    delete[] row;
*/
    return true;
  }
  else if(ret == 2) {
    // std::cout << "no!" << std::endl;
    // LP is infeasible
    return false;
  }
  else {
    std::cerr << "ERROR: lp_solve returned: " << ret << std::endl;
    return false;
  }
#else
  return false;
#endif

}