예제 #1
0
파일: glpcpx.c 프로젝트: emersonxsu/glpk
static char *row_name(struct csa *csa, int i, char rname[255+1])
{     /* construct symbolic name of i-th row (constraint) */
      const char *name;
      if (i == 0)
         name = glp_get_obj_name(csa->P);
      else
         name = glp_get_row_name(csa->P, i);
      if (name == NULL) goto fake;
      strcpy(rname, name);
      adjust_name(rname);
      if (check_name(rname)) goto fake;
      return rname;
fake: if (i == 0)
         strcpy(rname, "obj");
      else
         sprintf(rname, "r_%d", i);
      return rname;
}
static void
update_quality (struct GAS_MLP_Handle *mlp, struct ATS_Address * address)
{
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating quality metrics for peer `%s'\n",
      GNUNET_i2s (&address->peer));

  GNUNET_assert (NULL != address);
  GNUNET_assert (NULL != address->mlp_information);
  GNUNET_assert (NULL != address->ats);

  struct MLP_information *mlpi = address->mlp_information;
  struct GNUNET_ATS_Information *ats = address->ats;
  GNUNET_assert (mlpi != NULL);

  int c;
  for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
  {
    int index = mlp_lookup_ats(address, mlp->q[c]);

    if (index == GNUNET_SYSERR)
      continue;

    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' value `%s': %f\n",
        GNUNET_i2s (&address->peer),
        mlp_ats_to_string(mlp->q[c]),
        (double) ats[index].value);

    int i = mlpi->q_avg_i[c];
    double * qp = mlpi->q[c];
    qp[i] = (double) ats[index].value;

    int t;
    for (t = 0; t < MLP_AVERAGING_QUEUE_LENGTH; t++)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': `%s' queue[%u]: %f\n",
        GNUNET_i2s (&address->peer),
        mlp_ats_to_string(mlp->q[c]),
        t,
        qp[t]);
    }

    if (mlpi->q_avg_i[c] + 1 < (MLP_AVERAGING_QUEUE_LENGTH))
      mlpi->q_avg_i[c] ++;
    else
      mlpi->q_avg_i[c] = 0;


    int c2;
    int c3;
    double avg = 0.0;
    switch (mlp->q[c])
    {
      case GNUNET_ATS_QUALITY_NET_DELAY:
        c3 = 0;
        for (c2 = 0; c2 < MLP_AVERAGING_QUEUE_LENGTH; c2++)
        {
          if (mlpi->q[c][c2] != -1)
          {
            double * t2 = mlpi->q[c] ;
            avg += t2[c2];
            c3 ++;
          }
        }
        if ((c3 > 0) && (avg > 0))
          /* avg = 1 / ((q[0] + ... + q[l]) /c3) => c3 / avg*/
          mlpi->q_averaged[c] = (double) c3 / avg;
        else
          mlpi->q_averaged[c] = 0.0;

        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': `%s' average sum: %f, average: %f, weight: %f\n",
          GNUNET_i2s (&address->peer),
          mlp_ats_to_string(mlp->q[c]),
          avg,
          avg / (double) c3,
          mlpi->q_averaged[c]);

        break;
      case GNUNET_ATS_QUALITY_NET_DISTANCE:
        c3 = 0;
        for (c2 = 0; c2 < MLP_AVERAGING_QUEUE_LENGTH; c2++)
        {
          if (mlpi->q[c][c2] != -1)
          {
            double * t2 = mlpi->q[c] ;
            avg += t2[c2];
            c3 ++;
          }
        }
        if ((c3 > 0) && (avg > 0))
          /* avg = 1 / ((q[0] + ... + q[l]) /c3) => c3 / avg*/
          mlpi->q_averaged[c] = (double) c3 / avg;
        else
          mlpi->q_averaged[c] = 0.0;

        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s': `%s' average sum: %f, average: %f, weight: %f\n",
          GNUNET_i2s (&address->peer),
          mlp_ats_to_string(mlp->q[c]),
          avg,
          avg / (double) c3,
          mlpi->q_averaged[c]);

        break;
      default:
        break;
    }

    if ((mlpi->c_b != 0) && (mlpi->r_q[c] != 0))
    {

      /* Get current number of columns */
      int found = GNUNET_NO;
      int cols = glp_get_num_cols(mlp->prob);
      int *ind = GNUNET_malloc (cols * sizeof (int) + 1);
      double *val = GNUNET_malloc (cols * sizeof (double) + 1);

      /* Get the matrix row of quality */
      int length = glp_get_mat_row(mlp->prob, mlp->r_q[c], ind, val);
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "cols %i, length %i c_b %i\n", cols, length, mlpi->c_b);
      int c4;
      /* Get the index if matrix row of quality */
      for (c4 = 1; c4 <= length; c4++ )
      {
        if (mlpi->c_b == ind[c4])
        {
          /* Update the value */
          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating quality `%s' column `%s' row `%s' : %f -> %f\n",
              mlp_ats_to_string(mlp->q[c]),
              glp_get_col_name (mlp->prob, ind[c4]),
              glp_get_row_name (mlp->prob, mlp->r_q[c]),
              val[c4],
              mlpi->q_averaged[c]);
          val[c4] = mlpi->q_averaged[c];
          found = GNUNET_YES;
          break;
        }
      }

      if (found == GNUNET_NO)
        {

          ind[length+1] = mlpi->c_b;
          val[length+1] = mlpi->q_averaged[c];
          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%i ind[%i] val[%i]:  %i %f\n", length+1,  length+1, length+1, mlpi->c_b, mlpi->q_averaged[c]);
          glp_set_mat_row (mlp->prob, mlpi->r_q[c], length+1, ind, val);
        }
      else
        {
        /* Get the index if matrix row of quality */
        glp_set_mat_row (mlp->prob, mlpi->r_q[c], length, ind, val);
        }

      GNUNET_free (ind);
      GNUNET_free (val);
    }
  }
}
예제 #3
0
int glp_print_ranges(glp_prob *P, int len, const int list[],
                     int flags, const char *fname)
{   /* print sensitivity analysis report */
    glp_file *fp = NULL;
    GLPROW *row;
    GLPCOL *col;
    int m, n, pass, k, t, numb, type, stat, var1, var2, count, page,
        ret;
    double lb, ub, slack, coef, prim, dual, value1, value2, coef1,
           coef2, obj1, obj2;
    const char *name, *limit;
    char buf[13+1];
    /* sanity checks */
    if (P == NULL || P->magic != GLP_PROB_MAGIC)
        xerror("glp_print_ranges: P = %p; invalid problem object\n",
               P);
    m = P->m, n = P->n;
    if (len < 0)
        xerror("glp_print_ranges: len = %d; invalid list length\n",
               len);
    if (len > 0)
    {   if (list == NULL)
            xerror("glp_print_ranges: list = %p: invalid parameter\n",
                   list);
        for (t = 1; t <= len; t++)
        {   k = list[t];
            if (!(1 <= k && k <= m+n))
                xerror("glp_print_ranges: list[%d] = %d; row/column numb"
                       "er out of range\n", t, k);
        }
    }
    if (flags != 0)
        xerror("glp_print_ranges: flags = %d; invalid parameter\n",
               flags);
    if (fname == NULL)
        xerror("glp_print_ranges: fname = %p; invalid parameter\n",
               fname);
    if (glp_get_status(P) != GLP_OPT)
    {   xprintf("glp_print_ranges: optimal basic solution required\n");
        ret = 1;
        goto done;
    }
    if (!glp_bf_exists(P))
    {   xprintf("glp_print_ranges: basis factorization required\n");
        ret = 2;
        goto done;
    }
    /* start reporting */
    xprintf("Write sensitivity analysis report to '%s'...\n", fname);
    fp = glp_open(fname, "w");
    if (fp == NULL)
    {   xprintf("Unable to create '%s' - %s\n", fname, get_err_msg());
        ret = 3;
        goto done;
    }
    page = count = 0;
    for (pass = 1; pass <= 2; pass++)
        for (t = 1; t <= (len == 0 ? m+n : len); t++)
        {   if (t == 1) count = 0;
            k = (len == 0 ? t : list[t]);
            if (pass == 1 && k > m || pass == 2 && k <= m)
                continue;
            if (count == 0)
            {   xfprintf(fp, "GLPK %-4s - SENSITIVITY ANALYSIS REPORT%73sPa"
                         "ge%4d\n", glp_version(), "", ++page);
                xfprintf(fp, "\n");
                xfprintf(fp, "%-12s%s\n", "Problem:",
                         P->name == NULL ? "" : P->name);
                xfprintf(fp, "%-12s%s%s%.10g (%s)\n", "Objective:",
                         P->obj == NULL ? "" : P->obj,
                         P->obj == NULL ? "" : " = ", P->obj_val,
                         P->dir == GLP_MIN ? "MINimum" :
                         P->dir == GLP_MAX ? "MAXimum" : "???");
                xfprintf(fp, "\n");
                xfprintf(fp, "%6s %-12s %2s %13s %13s %13s  %13s %13s %13s "
                         "%s\n", "No.", pass == 1 ? "Row name" : "Column name",
                         "St", "Activity", pass == 1 ? "Slack" : "Obj coef",
                         "Lower bound", "Activity", "Obj coef", "Obj value at",
                         "Limiting");
                xfprintf(fp, "%6s %-12s %2s %13s %13s %13s  %13s %13s %13s "
                         "%s\n", "", "", "", "", "Marginal", "Upper bound",
                         "range", "range", "break point", "variable");
                xfprintf(fp, "------ ------------ -- ------------- --------"
                         "----- -------------  ------------- ------------- ------"
                         "------- ------------\n");
            }
            if (pass == 1)
            {   numb = k;
                xassert(1 <= numb && numb <= m);
                row = P->row[numb];
                name = row->name;
                type = row->type;
                lb = glp_get_row_lb(P, numb);
                ub = glp_get_row_ub(P, numb);
                coef = 0.0;
                stat = row->stat;
                prim = row->prim;
                if (type == GLP_FR)
                    slack = - prim;
                else if (type == GLP_LO)
                    slack = lb - prim;
                else if (type == GLP_UP || type == GLP_DB || type == GLP_FX)
                    slack = ub - prim;
                dual = row->dual;
            }
            else
            {   numb = k - m;
                xassert(1 <= numb && numb <= n);
                col = P->col[numb];
                name = col->name;
                lb = glp_get_col_lb(P, numb);
                ub = glp_get_col_ub(P, numb);
                coef = col->coef;
                stat = col->stat;
                prim = col->prim;
                slack = 0.0;
                dual = col->dual;
            }
            if (stat != GLP_BS)
            {   glp_analyze_bound(P, k, &value1, &var1, &value2, &var2);
                if (stat == GLP_NF)
                    coef1 = coef2 = coef;
                else if (stat == GLP_NS)
                    coef1 = -DBL_MAX, coef2 = +DBL_MAX;
                else if (stat == GLP_NL && P->dir == GLP_MIN ||
                         stat == GLP_NU && P->dir == GLP_MAX)
                    coef1 = coef - dual, coef2 = +DBL_MAX;
                else
                    coef1 = -DBL_MAX, coef2 = coef - dual;
                if (value1 == -DBL_MAX)
                {   if (dual < -1e-9)
                        obj1 = +DBL_MAX;
                    else if (dual > +1e-9)
                        obj1 = -DBL_MAX;
                    else
                        obj1 = P->obj_val;
                }
                else
                    obj1 = P->obj_val + dual * (value1 - prim);
                if (value2 == +DBL_MAX)
                {   if (dual < -1e-9)
                        obj2 = -DBL_MAX;
                    else if (dual > +1e-9)
                        obj2 = +DBL_MAX;
                    else
                        obj2 = P->obj_val;
                }
                else
                    obj2 = P->obj_val + dual * (value2 - prim);
            }
            else
            {   glp_analyze_coef(P, k, &coef1, &var1, &value1, &coef2,
                                 &var2, &value2);
                if (coef1 == -DBL_MAX)
                {   if (prim < -1e-9)
                        obj1 = +DBL_MAX;
                    else if (prim > +1e-9)
                        obj1 = -DBL_MAX;
                    else
                        obj1 = P->obj_val;
                }
                else
                    obj1 = P->obj_val + (coef1 - coef) * prim;
                if (coef2 == +DBL_MAX)
                {   if (prim < -1e-9)
                        obj2 = -DBL_MAX;
                    else if (prim > +1e-9)
                        obj2 = +DBL_MAX;
                    else
                        obj2 = P->obj_val;
                }
                else
                    obj2 = P->obj_val + (coef2 - coef) * prim;
            }
            /*** first line ***/
            /* row/column number */
            xfprintf(fp, "%6d", numb);
            /* row/column name */
            xfprintf(fp, " %-12.12s", name == NULL ? "" : name);
            if (name != NULL && strlen(name) > 12)
                xfprintf(fp, "%s\n%6s %12s", name+12, "", "");
            /* row/column status */
            xfprintf(fp, " %2s",
                     stat == GLP_BS ? "BS" : stat == GLP_NL ? "NL" :
                     stat == GLP_NU ? "NU" : stat == GLP_NF ? "NF" :
                     stat == GLP_NS ? "NS" : "??");
            /* row/column activity */
            xfprintf(fp, " %s", format(buf, prim));
            /* row slack, column objective coefficient */
            xfprintf(fp, " %s", format(buf, k <= m ? slack : coef));
            /* row/column lower bound */
            xfprintf(fp, " %s", format(buf, lb));
            /* row/column activity range */
            xfprintf(fp, "  %s", format(buf, value1));
            /* row/column objective coefficient range */
            xfprintf(fp, " %s", format(buf, coef1));
            /* objective value at break point */
            xfprintf(fp, " %s", format(buf, obj1));
            /* limiting variable name */
            if (var1 != 0)
            {   if (var1 <= m)
                    limit = glp_get_row_name(P, var1);
                else
                    limit = glp_get_col_name(P, var1 - m);
                if (limit != NULL)
                    xfprintf(fp, " %s", limit);
            }
            xfprintf(fp, "\n");
            /*** second line ***/
            xfprintf(fp, "%6s %-12s %2s %13s", "", "", "", "");
            /* row/column reduced cost */
            xfprintf(fp, " %s", format(buf, dual));
            /* row/column upper bound */
            xfprintf(fp, " %s", format(buf, ub));
            /* row/column activity range */
            xfprintf(fp, "  %s", format(buf, value2));
            /* row/column objective coefficient range */
            xfprintf(fp, " %s", format(buf, coef2));
            /* objective value at break point */
            xfprintf(fp, " %s", format(buf, obj2));
            /* limiting variable name */
            if (var2 != 0)
            {   if (var2 <= m)
                    limit = glp_get_row_name(P, var2);
                else
                    limit = glp_get_col_name(P, var2 - m);
                if (limit != NULL)
                    xfprintf(fp, " %s", limit);
            }
            xfprintf(fp, "\n");
            xfprintf(fp, "\n");
            /* print 10 items per page */
            count = (count + 1) % 10;
        }
    xfprintf(fp, "End of report\n");
#if 0 /* FIXME */
    xfflush(fp);
#endif
    if (glp_ioerr(fp))
    {   xprintf("Write error on '%s' - %s\n", fname, get_err_msg());
        ret = 4;
        goto done;
    }
    ret = 0;
done:
    if (fp != NULL) glp_close(fp);
    return ret;
}
예제 #4
0
const char *lpx_get_row_name(LPX *lp, int i)
{     /* retrieve row name */
      return glp_get_row_name(lp, i);
}
예제 #5
0
const char *c_glp_get_row_name (glp_prob *lp, int i){
  	return glp_get_row_name (lp, i);
}
예제 #6
0
// retrieve all missing values of LP/MILP
void Rglpk_retrieve_MP_from_file (char **file, int *type,
				  int *lp_n_constraints,
				  int *lp_n_objective_vars,
				  double *lp_objective_coefficients,
				  int *lp_constraint_matrix_i,
				  int *lp_constraint_matrix_j,
				  double *lp_constraint_matrix_values,
				  int *lp_direction_of_constraints,
				  double *lp_right_hand_side,
				  double *lp_left_hand_side,
				  int *lp_objective_var_is_integer,
				  int *lp_objective_var_is_binary,
				  int *lp_bounds_type,
				  double *lp_bounds_lower,
				  double *lp_bounds_upper,
				  int *lp_ignore_first_row,
				  int *lp_verbosity,
				  char **lp_constraint_names,
				  char **lp_objective_vars_names
				  ) {
  extern glp_prob *lp;
  glp_tran *tran;
  const char *str; 
  
  int i, j, lp_column_kind, tmp;
  int ind_offset, status;

  // Turn on/off Terminal Output
  if (*lp_verbosity==1)
    glp_term_out(GLP_ON);
  else
    glp_term_out(GLP_OFF);

  // create problem object
  if (lp)
    glp_delete_prob(lp);
  lp = glp_create_prob();

  // read file -> gets stored as an GLPK problem object 'lp'
  // which file type do we have?
  switch (*type){
  case 1: 
    // Fixed (ancient) MPS Format, param argument currently NULL
    status = glp_read_mps(lp, GLP_MPS_DECK, NULL, *file);
    break;
  case 2:
    // Free (modern) MPS format, param argument currently NULL
    status = glp_read_mps(lp, GLP_MPS_FILE, NULL, *file);
    break;
  case 3:
    // CPLEX LP Format
    status = glp_read_lp(lp, NULL, *file);
    break;
  case 4:
    // MATHPROG Format (based on lpx_read_model function)
    tran = glp_mpl_alloc_wksp();

    status = glp_mpl_read_model(tran, *file, 0);

    if (!status) {
        status = glp_mpl_generate(tran, NULL);
        if (!status) {
            glp_mpl_build_prob(tran, lp);
        }
    }
    glp_mpl_free_wksp(tran);
    break;    
  } 

  // if file read successfully glp_read_* returns zero
  if ( status != 0 ) {
    glp_delete_prob(lp);
    lp = NULL;
    error("Reading file %c failed.", *file);
  }
  
  if(*lp_verbosity==1)
    Rprintf("Retrieve column specific data ...\n");

  if(glp_get_num_cols(lp) != *lp_n_objective_vars) {
    glp_delete_prob(lp);
    lp = NULL;
    error("The number of columns is not as specified");
  }

  // retrieve column specific data (values, bounds and type)
  for (i = 0; i < *lp_n_objective_vars; i++) {
    lp_objective_coefficients[i] = glp_get_obj_coef(lp, i+1);
    
    // Note that str must not be freed befor we have returned
    // from the .C call in R! 
    str = glp_get_col_name(lp, i+1);    
    if (str){
      lp_objective_vars_names[i] = (char *) str;
    }
    
    lp_bounds_type[i]            = glp_get_col_type(lp, i+1);
    lp_bounds_lower[i]           = glp_get_col_lb  (lp, i+1);
    lp_bounds_upper[i]           = glp_get_col_ub  (lp, i+1);
    lp_column_kind               = glp_get_col_kind(lp, i+1);
    // set to TRUE if objective variable is integer or binary  
    switch (lp_column_kind){
    case GLP_IV: 
      lp_objective_var_is_integer[i] = 1;
      break;
    case GLP_BV:
      lp_objective_var_is_binary[i] = 1;
      break;
    }
  }
  
  ind_offset = 0;

  if(*lp_verbosity==1)
    Rprintf("Retrieve row specific data ...\n");

  if(glp_get_num_rows(lp) != *lp_n_constraints) {
    glp_delete_prob(lp);
    lp = NULL;
    error("The number of rows is not as specified");
  }

  // retrieve row specific data (right hand side, direction of constraints)
  for (i = *lp_ignore_first_row; i < *lp_n_constraints; i++) {
    lp_direction_of_constraints[i] = glp_get_row_type(lp, i+1);
    
    str = glp_get_row_name(lp, i + 1);
    if (str) { 
      lp_constraint_names[i] = (char *) str;
    }
    
    // the right hand side. Note we don't allow for double bounded or
    // free auxiliary variables 
    if( lp_direction_of_constraints[i] == GLP_LO )
      lp_right_hand_side[i] = glp_get_row_lb(lp, i+1);
    if( lp_direction_of_constraints[i] == GLP_UP )
      lp_right_hand_side[i] = glp_get_row_ub(lp, i+1);
    if( lp_direction_of_constraints[i] == GLP_FX )
      lp_right_hand_side[i] = glp_get_row_lb(lp, i+1);
    if( lp_direction_of_constraints[i] == GLP_DB  ){
      lp_right_hand_side[i] = glp_get_row_ub(lp, i+1);
      lp_left_hand_side[i] =  glp_get_row_lb(lp, i+1);
    }

    tmp = glp_get_mat_row(lp, i+1, &lp_constraint_matrix_j[ind_offset-1],
			           &lp_constraint_matrix_values[ind_offset-1]);
    if (tmp > 0)
      for (j = 0; j < tmp; j++)
	lp_constraint_matrix_i[ind_offset+j] = i+1;
	ind_offset += tmp; 
  }
  
  if(*lp_verbosity==1)
    Rprintf("Done.\n");

}