help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-glpk] [Fwd: Problem with GLPK when trying to find value functi


From: Andrew Makhorin
Subject: Re: [Help-glpk] [Fwd: Problem with GLPK when trying to find value functions]
Date: Thu, 26 Jul 2012 22:09:06 +0400

> Thanks for your reply. Here is additional information about the
> machine:
> proc: Intel(R) Core(TM)2 CPU T7400  @ 2.16GHz;
> cpu MHz : 2167.000
> cache size : 4096 KB
> Here is the code:
> double SolveLP (int **A, int *c, int *rhs, int numcols, int numrows)
> {
>   double z; // value function
>   int ne = numcols*numrows;; // total # of nonzero elements
>   int k = 1;
>   int *ia = new int[ne];
>   int *ja = new int[ne];
>   double *ar = new double[ne];
> 
> 
>   glp_prob *LP;
>   LP = glp_create_prob();
> 
> 
>   // Read parametrs
>  
>  glp_set_obj_dir(LP, GLP_MAX);
>  
>   // setting righthand side (beta)
>   glp_add_rows(LP, numrows);
>   for (int i=1; i<=numrows; ++i)
>     glp_set_row_bnds(LP, i, GLP_UP, 0.0, rhs[i-1]);
>   
>   // setting x-vector bounds and obj. function coefficients
>   glp_add_cols(LP, numcols);
>   for (int j=1; j<=numcols; ++j){
>     glp_set_col_bnds(LP, j, GLP_LO, 0.0, 0.0);
>     glp_set_obj_coef(LP, j, c[j-1]);
>   }
> 
> 
>   // read constraint matrix
> 
> 
>   for (int i=1; i<=numrows; ++i)
>     for (int j=1; j<=numcols; ++j){
>       ia[k] = i;
>       ja[k] = j;
>       ar[k] = double(A[i-1][j-1]);
>       ++k;
>     }
>   
>   glp_load_matrix(LP, ne, ia, ja, ar);
>  
>   // Run simplex
>   glp_simplex(LP, NULL);
> 
> 
>   // Get obj. value
>   z = glp_get_obj_val(LP);
>    
>   glp_erase_prob(LP); // work with erase problem
>   //glp_delete_prob(LP); // doesn't work in loop
>   
>   return z;
>   }
> 


You need either reserve 0-th elements by declaring

  int *ia = new int[1+ne];
  int *ja = new int[1+ne];
  double *ar = new double[1+ne];

or initialize k to 0 rather than 1 and load the constraint matrix with
the following call:

  glp_load_matrix(LP, ne, ia-1, ja-1, ar-1);

Please consult the glpk reference manual for more details about passing
arrays to glpk api routines.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]