Пример #1
0
void lp_print_basis(LP* lp) {
  int  i, var;
  mpq_t q;

  mpq_init(q);
  putchar('\n');
  for (i = 0; i < lp->rows; i ++) {
    var = lp->basis_column[i];
    printf("%s: ", lp->var_name[var]);
    vector_get_element(&q, lp->x, var);
    print_rational_as_float(q, 12);
    printf("\t(");
    if (lp->lower.is_valid[var])
      print_rational_as_float(lp->lower.bound[var], 12);
    else
      printf("  ---  ");
    printf("\t");
    if (lp->upper.is_valid[var])
      print_rational_as_float(lp->upper.bound[var], 12);
    else
      printf("  ---  ");
    printf(")");
    putchar('\n');
  }
  putchar('\n');
  mpq_clear(q);
}
Пример #2
0
int
main(int argc, char **argv)
{
  MPI_Init(&argc, &argv);
  libmrc_params_init(argc, argv);

  struct vector *vec = vector_create(MPI_COMM_WORLD);
  vector_set_from_options(vec);
  vector_setup(vec);
  vector_view(vec);

  int nr_elements;
  vector_get_param_int(vec, "nr_elements", &nr_elements);

  for (int i = 0; i < nr_elements; i++) {
    vector_set_element(vec, i, 2. * i);
  }

  for (int i = 0; i < nr_elements; i++) {
    assert(vector_get_element(vec, i) == 2. * i);
  }

  vector_destroy(vec);

  MPI_Finalize();
}
void vector_print(vec * v){
    int i;
    double val;
    for(i=0; i<v->nrows; i++){
        val = vector_get_element(v, i);
        printf("%f\n", val);
    }
}
/* A = A - u*v where u is a column vec and v is a row vec */
void matrix_sub_column_times_row_vector(mat *A, vec *u, vec *v){
    int i,j;
    #pragma omp parallel for shared(A,u,v) private(j)
    for(i=0; i<(A->nrows); i++){
        for(j=0; j<(A->ncols); j++){
            matrix_set_element(A,i,j,matrix_get_element(A,i,j) - vector_get_element(u,i)*vector_get_element(v,j));
        }
    }
}
/* put vector row_vec as row i of a matrix */
void matrix_set_row(mat *M, int i, vec *row_vec){
    int j;
    #pragma omp parallel shared(row_vec,M,i) private(j) 
    {
    #pragma omp parallel for
    for(j=0; j<M->ncols; j++){ 
        matrix_set_element(M,i,j,vector_get_element(row_vec,j));
    }
    }
}
/* set column of matrix to vector */
void matrix_set_col(mat *M, int j, vec *column_vec){
    int i;
    #pragma omp parallel shared(column_vec,M,j) private(i) 
    {
    #pragma omp for
    for(i=0; i<M->nrows; i++){
        matrix_set_element(M,i,j,vector_get_element(column_vec,i));
    }
    }
}
Пример #7
0
void get_valid_rhs(mpq_t* q, LP* lp, int i) {
  mpq_t  r;
  int  j, var;

  mpq_init(r);

  vector_get_element(q, lp->b, i);

  for (j = 0; j < lp->A->row[i]->nonzeros; j ++) {
    var = lp->A->row[i]->i[j];
    if (is_const_var(lp, var))
      continue;
    vector_get_element(&r, lp->x, var);
    mympq_mul(r, r, *matrix_get_element_ptr(lp->A, i, var));
    mympq_sub(*q, *q, r);
  }

  mpq_clear(r);
}
Пример #8
0
void lp_set_row_range(LP* lp, int row, mpq_t q) {
  mpq_t q2;
  int   var;

  mpq_init(q2);

  if(lp->row_name != NULL) {
    char buf[LP_NAME_LEN_MAX];
    buf[0] = '#'; buf[1] = 'R'; buf[2] = 0;
    strncat(buf, lp->row_name[row], LP_NAME_LEN_MAX-3);
    var = lp_add_var(lp, buf);
  }
  else
    var = lp_add_var(lp, NULL);
  lp_add_slackref(lp, row+1);
  vector_get_element(&q2, lp->b, row);
  lp->upper.is_valid[var] = TRUE;
  mpq_init(lp->upper.bound[var]);
  if (lp->row_equality[row] == LP_EQUALITY_GE) {
    if (mpq_sgn(q) < 0)
      mpq_neg(q, q);
    mpq_set(lp->lower.bound[var], q2);
    mympq_add(q2, q2, q);
    mpq_set(lp->upper.bound[var], q2);
  } else if (lp->row_equality[row] == LP_EQUALITY_LE) {
    if (mpq_sgn(q) < 0)
      mpq_neg(q, q);
    mpq_set(lp->upper.bound[var], q2);
    mympq_sub(q2, q2, q);
    mpq_set(lp->lower.bound[var], q2);
  } else if (mpq_sgn(q) > 0) {
    mpq_set(lp->lower.bound[var], q2);
    mympq_add(q2, q2, q);
    mpq_set(lp->upper.bound[var], q2);        
  } else {
    mpq_set(lp->upper.bound[var], q2);
    mympq_add(q2, q2, q);
    mpq_set(lp->lower.bound[var], q2);
  }
  lp_set_row_equality(lp, row, LP_EQUALITY_EQ);
  lp_set_coefficient(lp, mympq_minus_one, row, var);
  vector_delete_element(lp->b, row);

  mpq_clear(q2);
}
Пример #9
0
void eta_file_ftran(eta_file* ef, EXLPvector* b, EXLPvector* x, EXLPvector* w) {
  /* Bx = b  を解く. */
  eta_matrix* U;
  int  i;
  mpq_t  q;

  mpq_init(q);

  U = my_malloc(sizeof(eta_matrix));
  vector_copy(x, b);

  for (i = 0; i < ef->U->columns; i ++) {
    vector_swap_elements(x, i, ef->P[i]);
    eta_matrix_mul_vector(ef->L[i], x);
  }
  for (i = 0; i < ef->s; i ++) {
    if (ef->Ls[i]->row == ef->Ls[i]->column) {
      vector_mul_element(x, ef->Ls[i]->value, ef->Ls[i]->row);
    } else {
      vector_get_element(&q, x, ef->Ls[i]->column);
      if (mpq_sgn(q)==0)continue;
      mympq_mul(q, q, ef->Ls[i]->value);
      vector_add_element(x, q, ef->Ls[i]->row);
    }
  }

  vector_permutate_inv(ef->Q, x);
  if (w != NULL)
    vector_copy(w, x);
  //if (w!=NULL)printf("(%d ", w->nonzeros),fflush(stdout);

  for (i = ef->U->columns-1; i >= 0; i --) {
    U->eta_column = i;
    U->eta_vector = ef->U->column[i];
    eta_matrix_solve_Ex_is_equal_to_v(U, x);
  }

//fprintf(stderr, "%d ",x->i[0]);
  vector_permutate_inv(ef->R, x);
  //printf(" %d)", x->nonzeros);fflush(stdout);

  mpq_clear(q);
  free(U);
}
Пример #10
0
void eta_file_btran(eta_file* ef, EXLPvector* c, EXLPvector* y) {
  /* yB = c  を解く. */
  eta_matrix* U;
  mpq_t  q;
  int  i;

  U = my_malloc(sizeof(eta_matrix));
  vector_copy(y, c);
  mpq_init(q);

  vector_permutate(ef->R, y);

  for (i = 0; i < ef->U->columns; i ++) {
    U->eta_column = i;
    U->eta_vector = ef->U->column[i];
    eta_matrix_solve_yE_is_equal_to_v(U, y);
  }

  vector_permutate(ef->Q, y);

  for (i = ef->s-1; i >= 0; i --) {
    if (ef->Ls[i]->row == ef->Ls[i]->column) {
      vector_mul_element(y, ef->Ls[i]->value, ef->Ls[i]->row);
    } else {
      vector_get_element(&q, y, ef->Ls[i]->row);
      mympq_mul(q, q, ef->Ls[i]->value);
      vector_add_element(y, q, ef->Ls[i]->column);
    }
  }
  for (i = ef->U->columns-1; i >= 0; i --) {
    if (ef->L[i]->eta_vector->nonzeros == 1) {
      vector_mul_element(y, ef->L[i]->eta_vector->value[0], i);
    } else {
      vector_inner_product(&q, y, ef->L[i]->eta_vector);
      vector_set_element(y, q, i);
    }
    vector_swap_elements(y, i, ef->P[i]);
  }

  mpq_clear(q);
  free(U);
}
/* output = input[inds] */
void fill_vector_from_row_list(vec *input, vec *inds, vec *output){
    int i,col_num;
    for(i=0; i<(input->nrows); i++){
        vector_set_element(output,i,vector_get_element(input,vector_get_element(inds,i)));
    }
}
Пример #12
0
void lp_add_artificials(LP* lp) {
  mpq_t  q1, q2, q3, best;
  char  buf[LP_NAME_LEN_MAX];
  int  i, j, k, l, m, x, best_var;

  mpq_init(q1);
  mpq_init(q2);
  mpq_init(q3);
  mpq_init(best);
  vector_zero_clear(lp->c);
  vector_zero_clear(lp->cb);

  x = lp_select_basis(lp);

  for (i = 0; i < lp->rows; i ++) {
    if (lp->basis_column[i] != -1)
      continue;
    get_valid_rhs(&q1, lp, i);
    if (mpq_sgn(q1) < 0) {
      matrix_rev_row_sgn(lp->A, i);
      vector_rev_element_sgn(lp->b, i);
      mpq_neg(q1, q1);
    }
    best_var = -1;
    for (m = 0; m < lp->A->row[i]->nonzeros; m ++) {
      j = lp->A->row[i]->i[m];
      if (!is_normal_var(lp, j) ||
          lp->is_basis[j] ||
          lp->A->column[j]->nonzeros == 0)
        continue;
      if (vector_get_first_nonzero_i(lp->A->column[j]) != i)
        continue;
      if (mpq_sgn(*vector_get_element_ptr(lp->x, j)))
        continue;
      mympq_div(q2, q1, *vector_get_element_ptr(lp->A->column[j], i));
      if ((lp->upper.is_valid[j] && mpq_cmp(lp->upper.bound[j], q2) < 0) ||
          (lp->lower.is_valid[j] && mpq_cmp(lp->lower.bound[j], q2) > 0))
        continue;
      if (mpq_sgn(q2)) {
        l = 0;
        for (k = 0; k < lp->A->column[j]->nonzeros; k ++) {
          if (lp->basis_column[lp->A->column[j]->i[k]] == -1 ||
              lp->A->column[j]->i[k] == i)
            continue;
          l = 1;
          break;
        }
        if (l)
          continue;
      }
      vector_get_element(&q3, lp->c_back, j);
      mympq_mul(q3, q3, q2);
      //if (!lp->maximize)
      //  mpq_neg(q3, q3);
      if (best_var >= 0 && mpq_cmp(best, q3) >= 0)
        continue;
      best_var = j;
      mpq_set(best, q3); 
    }
    if (best_var < 0)
      continue;
    x ++;
    j = best_var;
    mympq_div(q2, q1, *vector_get_element_ptr(lp->A->column[j], vector_get_first_nonzero_i(lp->A->column[j])));
    lp->is_basis[j] = TRUE;
    lp->basis_column[i] = j;
    vector_set_element(lp->x,  q2, j);
    vector_set_element(lp->xb, q2, i);
  }
  matrix_resize(lp->A, lp->rows, lp->vars + lp->rows - x);

  for (i = 0; i < lp->rows; i ++) {
    if (lp->basis_column[i] >= 0)
      continue;
    get_valid_rhs(&q1, lp, i);
    if(lp->var_name == NULL)
      j = lp_add_var_without_A(lp, NULL);
    else {
      strncpy(buf, "#A", LP_NAME_LEN_MAX);
      strncat(buf, lp->row_name[i], LP_NAME_LEN_MAX);
      j = lp_add_var_without_A(lp, buf);
    }
    lp_add_slackref(lp, -(i+1));
    lp->var_type[j] = LP_VAR_TYPE_ARTIFICIAL;
    lp->basis_column[i] = j;
    lp->is_basis[j] = TRUE;
    lp_set_coefficient(lp, mympq_one, i, j);
    vector_set_element(lp->x,  q1, j);
    vector_set_element(lp->xb, q1, i);
    vector_set_element(lp->c,  mympq_minus_one, j);
    vector_set_element(lp->cb, mympq_minus_one, i);
  }
#if 0
  lp->vars = MIN(lp->vars, lp->A->columns);
#endif

  mpq_clear(q1);
  mpq_clear(q2);
  mpq_clear(q3);
  mpq_clear(best);
}