Ejemplo n.º 1
0
void glpk_wrapper::set_domain(box const & b) {
    assert(!b.is_empty());
    changed = true;
    domain = b;
    for (unsigned int i = 0; i < b.size(); i++) {
        auto interval = b[i];
        double lb = interval.lb();
        double ub = interval.ub();
        if (lb == NEG_INFINITY) {
            if (ub == POS_INFINITY) {
                glp_set_col_bnds(lp, i+1, GLP_FR, lb, ub);
            } else {
                glp_set_col_bnds(lp, i+1, GLP_UP, lb, ub);
            }
        } else {
            if (ub == POS_INFINITY) {
                glp_set_col_bnds(lp, i+1, GLP_LO, lb, ub);
            } else {
                if (lb == ub) {
                    glp_set_col_bnds(lp, i+1, GLP_FX, lb, ub);
                } else {
                    glp_set_col_bnds(lp, i+1, GLP_DB, lb, ub);
                }
            }
        }
    }
}
Ejemplo n.º 2
0
int main(void)
{
  glp_prob *mip = glp_create_prob();
  glp_set_prob_name(mip, "sample");
  glp_set_obj_dir(mip, GLP_MAX);

  // 拘束条件
  // 具体的な関数は後で
  glp_add_rows(mip, 3); // 拘束条件の数
  glp_set_row_name(mip, 1, "c1"); glp_set_row_bnds(mip, 1, GLP_DB, 0.0, 20.0);
  glp_set_row_name(mip, 2, "c2"); glp_set_row_bnds(mip, 2, GLP_DB, 0.0, 30.0);
  glp_set_row_name(mip, 3, "c3"); glp_set_row_bnds(mip, 3, GLP_FX, 0.0, 0);

  // 変数
  // 変数そのものにかかる拘束は、拘束条件ではなくてこちらで管理
  glp_add_cols(mip, 4); // 変数の数
  glp_set_col_name(mip, 1, "x1");
  glp_set_col_bnds(mip, 1, GLP_DB, 0.0, 40.0); glp_set_obj_coef(mip, 1, 1.0);
  glp_set_col_name(mip, 2, "x2");
  glp_set_col_bnds(mip, 2, GLP_LO, 0.0, 0.0); glp_set_obj_coef(mip, 2, 2.0);
  glp_set_col_name(mip, 3, "x3");
  glp_set_col_bnds(mip, 3, GLP_LO, 0.0, 0.0); glp_set_obj_coef(mip, 3, 3.0);
  glp_set_col_kind(mip, 3, GLP_IV); // 整数値としての宣言
  glp_set_col_name(mip, 4, "x4");
  glp_set_col_bnds(mip, 4, GLP_DB, 2.0, 3.0); glp_set_obj_coef(mip, 4, 1.0);
  glp_set_col_kind(mip, 4, GLP_IV); // 整数値としての宣言

  int ia[1+9], ja[1+9];
  double ar[1+9];
  ia[1]=1,ja[1]=1,ar[1]=-1;   // a[1,1] = -1
  ia[2]=1,ja[2]=2,ar[2]=1;    // a[1,2] = 1
  ia[3]=1,ja[3]=3,ar[3]=1;    // a[1,3] = 1
  ia[4]=1,ja[4]=4,ar[4]=10;   // a[1,4] = 10
  ia[5]=2,ja[5]=1,ar[5]=1;    // a[2,1] = 1
  ia[6]=2,ja[6]=2,ar[6]=-3;   // a[2,2] = -3
  ia[7]=2,ja[7]=3,ar[7]=1;    // a[2,3] = 1
  ia[8]=3,ja[8]=2,ar[8]=1;    // a[3,2] = 1
  ia[9]=3,ja[9]=4,ar[9]=-3.5; // a[3,4] = -3.5
  glp_load_matrix(mip, 9, ia, ja, ar);

  glp_iocp parm;
  glp_init_iocp(&parm);
  parm.presolve = GLP_ON;
  int err = glp_intopt(mip, &parm);

  double z = glp_mip_obj_val(mip);
  double x1 = glp_mip_col_val(mip, 1);
  double x2 = glp_mip_col_val(mip, 2);
  double x3 = glp_mip_col_val(mip, 3);
  double x4 = glp_mip_col_val(mip, 4);
  printf("\nz = %g; x1 = %g; x2 = %g; x3 = %g, x4 = %g\n", z, x1, x2, x3, x4);
  // z = 122.5; x1 = 40; x2 = 10.5; x3 = 19.5, x4 = 3

  glp_delete_prob(mip);
  return 0;
}
void NADA() {
     glp_prob *lp;
     lp = glp_create_prob();
     glp_add_cols(lp, 3);
     glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 3.0); // 0 <= x1
     glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 2.0); // 0 <= x2
     glp_set_col_bnds(lp, 3, GLP_LO, 0.0, 0.0); // 0 <= x3

     glp_set_obj_dir(lp, GLP_MAX); //max
     glp_set_obj_coef(lp, 1, -3.0); // -3x1
     glp_set_obj_coef(lp, 2, 4.0); // +4x2
     glp_set_obj_coef(lp, 3, 11.0); // +11x3
     // max -3x1 + 4x2 + 11 x3.
     
     int indCol[123];
     double val[123];

     glp_add_rows(lp, 1);
     indCol[1] = 1; val[1] = 10; // 10*x1
     indCol[2] = 2; val[2] = 3; // 3*x2
     glp_set_row_bnds(lp, 1, GLP_UP, 0.0, 15.0);// <=15
     glp_set_mat_row(lp, 1, 2, indCol, val);//  10 x1 + 3 x2 <= 15

     glp_add_rows(lp, 1);
     indCol[1] = 3; val[1] = 9; // 9*x3
     indCol[2] = 1; val[2] = 7; // 7*x1
     glp_set_row_bnds(lp, 2, GLP_UP, 0.0, 38.0);// <=38
     glp_set_mat_row(lp, 2, 2, indCol, val);//  7x1+9x2<=38

     glp_add_rows(lp, 1);
     indCol[1] = 3; val[1] = 15; // 15*x3
     glp_set_row_bnds(lp, 3, GLP_LO, 0.0, 25.0);// >=25
     glp_set_mat_row(lp, 3, 1, indCol, val);//  15x3 >=25
     
     glp_set_col_kind(lp, 1, GLP_IV);// X1 EH INTEIRO
     glp_set_col_kind(lp, 2, GLP_IV);// X2 EH INTEIRO
     glp_set_col_kind(lp, 3, GLP_IV);// X3 EH INTEIRO
     
     glp_intopt(lp, NULL); // acha solucao com restricao de integralidade
//     glp_simplex(lp, NULL);

//     printf("Solucao Otima: %.3f\n", glp_get_obj_val(lp));     
//     printf("X1: %.3f\n", glp_get_col_prim(lp, 1));     
//     printf("X2: %.3f\n", glp_get_col_prim(lp, 2));     
//     printf("X3: %.3f\n", glp_get_col_prim(lp, 3));

     printf("Solucao Otima: %.3f\n", glp_mip_obj_val(lp));     
     printf("X1: %.3f\n", glp_mip_col_val(lp, 1));     
     printf("X2: %.3f\n", glp_mip_col_val(lp, 2));     
     printf("X3: %.3f\n", glp_mip_col_val(lp, 3));
          
//     for (int est = 1; est <= nEstradas; ++est) {
//          glp_set_col_bnds(lp, est, GLP_LO, 0.0, 0.0);
//     }
}
Ejemplo n.º 4
0
int main(void) {

	glp_prob *lp;
	int ia[1+1000], ja[1+1000];
	double ar[1+1000], z, x1, x2, x3;

	s1: lp = glp_create_prob();
	s2: glp_set_prob_name(lp, "sample");
	s3: glp_set_obj_dir(lp, GLP_MAX);
	s4: glp_add_rows(lp, 3);
	s5: glp_set_row_name(lp, 1, "p");
	s6: glp_set_row_bnds(lp, 1, GLP_UP, 0.0, 100.0);
	s7: glp_set_row_name(lp, 2, "q");
	s8: glp_set_row_bnds(lp, 2, GLP_UP, 0.0, 600.0);
	s9: glp_set_row_name(lp, 3, "r");
	s10: glp_set_row_bnds(lp, 3, GLP_UP, 0.0, 300.0);
	s11: glp_add_cols(lp, 3);
	s12: glp_set_col_name(lp, 1, "x1");
	s13: glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 0.0);
	s14: glp_set_obj_coef(lp, 1, 10.0);
	s15: glp_set_col_name(lp, 2, "x2");
	s16: glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 0.0);
	s17: glp_set_obj_coef(lp, 2, 6.0);
	s18: glp_set_col_name(lp, 3, "x3");
	s19: glp_set_col_bnds(lp, 3, GLP_LO, 0.0, 0.0);
	s20: glp_set_obj_coef(lp, 3, 4.0);

	s21: ia[1] = 1, ja[1] = 1, ar[1] = 1.0; /* a[1,1] = 1 */
	s22: ia[2] = 1, ja[2] = 2, ar[2] = 1.0; /* a[1,2] = 1 */
	s23: ia[3] = 1, ja[3] = 3, ar[3] = 1.0; /* a[1,3] = 1 */
	s24: ia[4] = 2, ja[4] = 1, ar[4] = 10.0; /* a[2,1] = 10 */
	s25: ia[5] = 3, ja[5] = 1, ar[5] = 2.0; /* a[3,1] = 2 */
	s26: ia[6] = 2, ja[6] = 2, ar[6] = 4.0; /* a[2,2] = 4 */
	s27: ia[7] = 3, ja[7] = 2, ar[7] = 2.0; /* a[3,2] = 2 */
	s28: ia[8] = 2, ja[8] = 3, ar[8] = 5.0; /* a[2,3] = 5 */
	s29: ia[9] = 3, ja[9] = 3, ar[9] = 6.0; /* a[3,3] = 6 */

	s30: glp_load_matrix(lp, 9, ia, ja, ar);
	s31: glp_simplex(lp, NULL);
	s32: z = glp_get_obj_val(lp);

	s33: x1 = glp_get_col_prim(lp, 1);
	s34: x2 = glp_get_col_prim(lp, 2);
	s35: x3 = glp_get_col_prim(lp, 3);

	s36: printf("\nz = %g; x1 = %g; x2 = %g; x3 = %g\n", z, x1, x2, x3);

	s37: glp_delete_prob(lp);
	
return 0;
}
glp_prob * montarModeloInicial() {
     glp_prob *lp;
     lp = glp_create_prob();//cria problema
     
     glp_add_cols(lp, nEstradas); // cria uma variavel por estrada
     for(int est = 1; est <= nEstradas; ++est) { // para cada estrada
          glp_set_col_bnds(lp, est, GLP_DB, 0.0, 1.0); // estrada entre 0 e 1
          glp_set_col_kind(lp, est, GLP_BV); // estrada binaria
     }
     
     glp_set_obj_dir(lp, GLP_MIN); //MIN
     for(int est=1; est <= nEstradas; ++est) { // para cada estrada
          glp_set_obj_coef(lp, est, estradas[est].dist);
           // custo da estrada eh coeficiente da objetivo
     }
     
     for (int cid = 1; cid <= nCidades; ++cid) {  //para cada cidade
          int indCol[123];
          double val[123];
          int nCoef = 0;
          for (int est = 1; est < nEstradas; ++est) { //para cada estrada
              Estrada estrada = estradas[est]; 
              if (estrada.ori == cid || estrada.dest == cid) {
                               //se cidade toca estrada est
                 indCol[nCoef + 1] = est; // a est-esima estrada
                 val[nCoef + 1] = 1.0; // com coeficiente 1 na linha
                 nCoef++; //incremente numero de coeficiente
              }
          }
          glp_add_rows(lp, 1);
          glp_set_mat_row(lp, cid, nCoef, indCol, val); // adiciona coeficientes da linha
          glp_set_row_bnds(lp, cid, GLP_DB, 2.0, 2.0); // restringe linha = 2.
     }
}
Ejemplo n.º 6
0
/* Maps the order of planes in solution to a list of constraints &ij */
void mapSolution(glp_prob * Prob, int solution[]) {
	int i,j;
	char buf[AUXSIZE];
	
	int uij;
	for( i = 0 ; i < n ; ++i )
		for( j = i+1 ; j < n ; ++j ) {
			sprintf(buf,"u%i,%i",solution[i],solution[j]);
			if( (uij = glp_find_col(Prob, buf)) ) {
                glp_set_col_bnds(Prob, uij, GLP_FX, 1, 1);
                glp_set_col_kind(Prob, uij, GLP_CV);
                sprintf(buf,"u%i,%i",solution[j],solution[i]);
				glp_set_col_bnds(Prob, uij=glp_find_col(Prob, buf), GLP_FX, 0, 0);
                glp_set_col_kind(Prob, uij, GLP_CV);
			}
		}
}
Ejemplo n.º 7
0
void NUMlinprog_addVariable (NUMlinprog me, double lowerBound, double upperBound, double coeff) {
	glp_add_cols (my linearProgram, 1);
	glp_set_col_bnds (my linearProgram, ++ my numberOfVariables,
		lowerBound == NUMundefined ? ( upperBound == NUMundefined ? GLP_FR : GLP_UP ) :
		upperBound == NUMundefined ? GLP_LO :
		lowerBound == upperBound ? GLP_FX : GLP_DB, lowerBound, upperBound);
	glp_set_obj_coef (my linearProgram, my ivar, coeff);
}
Ejemplo n.º 8
0
 int make_variable(double coef, int lo, int hi)
 {
   int col = glp_add_cols(ip_, 1);
   glp_set_col_bnds(ip_, col, GLP_DB, lo, hi);
   glp_set_col_kind(ip_, col, GLP_IV);
   glp_set_obj_coef(ip_, col, coef);
   return col;
 }
Ejemplo n.º 9
0
 int make_variable(double coef)
 {
   int col = glp_add_cols(ip_, 1);
   glp_set_col_bnds(ip_, col, GLP_DB, 0, 1);
   glp_set_col_kind(ip_, col, GLP_BV);
   glp_set_obj_coef(ip_, col, coef);
   return col;
 }
Ejemplo n.º 10
0
int glp_asnprob_lp(glp_prob *P, int form, glp_graph *G, int names,
                   int v_set, int a_cost)
{   glp_vertex *v;
    glp_arc *a;
    int i, j, ret, ind[1+2];
    double cost, val[1+2];
    if (!(form == GLP_ASN_MIN || form == GLP_ASN_MAX ||
            form == GLP_ASN_MMP))
        xerror("glp_asnprob_lp: form = %d; invalid parameter\n",
               form);
    if (!(names == GLP_ON || names == GLP_OFF))
        xerror("glp_asnprob_lp: names = %d; invalid parameter\n",
               names);
    if (v_set >= 0 && v_set > G->v_size - (int)sizeof(int))
        xerror("glp_asnprob_lp: v_set = %d; invalid offset\n",
               v_set);
    if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
        xerror("glp_asnprob_lp: a_cost = %d; invalid offset\n",
               a_cost);
    ret = glp_check_asnprob(G, v_set);
    if (ret != 0) goto done;
    glp_erase_prob(P);
    if (names) glp_set_prob_name(P, G->name);
    glp_set_obj_dir(P, form == GLP_ASN_MIN ? GLP_MIN : GLP_MAX);
    if (G->nv > 0) glp_add_rows(P, G->nv);
    for (i = 1; i <= G->nv; i++)
    {   v = G->v[i];
        if (names) glp_set_row_name(P, i, v->name);
        glp_set_row_bnds(P, i, form == GLP_ASN_MMP ? GLP_UP : GLP_FX,
                         1.0, 1.0);
    }
    if (G->na > 0) glp_add_cols(P, G->na);
    for (i = 1, j = 0; i <= G->nv; i++)
    {   v = G->v[i];
        for (a = v->out; a != NULL; a = a->t_next)
        {   j++;
            if (names)
            {   char name[50+1];
                sprintf(name, "x[%d,%d]", a->tail->i, a->head->i);
                xassert(strlen(name) < sizeof(name));
                glp_set_col_name(P, j, name);
            }
            ind[1] = a->tail->i, val[1] = +1.0;
            ind[2] = a->head->i, val[2] = +1.0;
            glp_set_mat_col(P, j, 2, ind, val);
            glp_set_col_bnds(P, j, GLP_DB, 0.0, 1.0);
            if (a_cost >= 0)
                memcpy(&cost, (char *)a->data + a_cost, sizeof(double));
            else
                cost = 1.0;
            glp_set_obj_coef(P, j, cost);
        }
    }
    xassert(j == G->na);
done:
    return ret;
}
/**
 * Add columns for all addresses
 *
 * @param cls GAS_MLP_Handle
 * @param key Hashcode
 * @param value ATS_Address
 *
 * @return GNUNET_OK to continue
 */
static int
create_columns_it (void *cls, const GNUNET_HashCode * key, void *value)
{
  struct GAS_MLP_Handle *mlp = cls;
  struct ATS_Address *address = value;
  struct MLP_information *mlpi;
  unsigned int col;
  char *name;

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

  /* Add bandwidth column */
  col = glp_add_cols (mlp->prob, 2);
  mlpi->c_b = col;
  mlpi->c_n = col + 1;


  GNUNET_asprintf (&name, "b_%s_%s", GNUNET_i2s (&address->peer), address->plugin);
  glp_set_col_name (mlp->prob, mlpi->c_b , name);
  GNUNET_free (name);
  /* Lower bound == 0 */
  glp_set_col_bnds (mlp->prob, mlpi->c_b , GLP_LO, 0.0, 0.0);
  /* Continuous value*/
  glp_set_col_kind (mlp->prob, mlpi->c_b , GLP_CV);
  /* Objective function coefficient == 0 */
  glp_set_obj_coef (mlp->prob, mlpi->c_b , 0);


  /* Add usage column */
  GNUNET_asprintf (&name, "n_%s_%s", GNUNET_i2s (&address->peer), address->plugin);
  glp_set_col_name (mlp->prob, mlpi->c_n, name);
  GNUNET_free (name);
  /* Limit value : 0 <= value <= 1 */
  glp_set_col_bnds (mlp->prob, mlpi->c_n, GLP_DB, 0.0, 1.0);
  /* Integer value*/
  glp_set_col_kind (mlp->prob, mlpi->c_n, GLP_IV);
  /* Objective function coefficient == 0 */
  glp_set_obj_coef (mlp->prob, mlpi->c_n, 0);

  return GNUNET_OK;
}
Ejemplo n.º 12
0
/* Swap the arrival of two adjacent planes */
void swapConstraint(glp_prob * Prob, int i, int solution[], int back) {
	int t,j;
	static char firstTime = 1;
	static int u[MAXSIZE][MAXSIZE];
	char buf[AUXSIZE];
	
	if( firstTime ) {
		firstTime = 0;
		for( t = 0 ; t < n ; ++t )
		    for( j = i+1 ; j < n ; ++j ) {
            	sprintf(buf,"u%i,%i",t,j);
            	u[t][j] = glp_find_col(Prob, buf);
            	sprintf(buf,"u%i,%i",j,t);
            	u[j][t] = glp_find_col(Prob, buf);
			}
	}
	
	if( u[solution[i]][solution[i+1]] ) {
    	glp_set_col_bnds(Prob, u[solution[i]][solution[i+1]], GLP_FX, back, 0);
    	glp_set_col_bnds(Prob, u[solution[i+1]][solution[i]], GLP_FX, !back, 0);
	}
}
Ejemplo n.º 13
0
vector<int> LinearProblem::ElasticFilter() const {
  LinearProblem tmp(*this);

  int realCols = glp_get_num_cols(tmp.lp_);
  for (int i = realCols; i > 0; --i) { // set old coefs to zero
    glp_set_obj_coef(tmp.lp_, i,  0);
  }

  int elasticCols = glp_get_num_rows(tmp.lp_);
  glp_add_cols(tmp.lp_, elasticCols);
  for (int i = 1; i <= elasticCols; ++i) {
    int indices[MAX_VARS];
    double values[MAX_VARS];
    int nonZeros = glp_get_mat_row(tmp.lp_, i, indices, values);

    indices[nonZeros + 1] = realCols + i;
    values[nonZeros + 1] = 1.0;

    glp_set_mat_row(tmp.lp_, i, nonZeros + 1, indices, values);
    glp_set_obj_coef(tmp.lp_, realCols + i,  1);
    glp_set_col_bnds(tmp.lp_, realCols + i, GLP_UP, 0.0, 0.0);
  }

  vector<int> suspects;
  glp_std_basis(tmp.lp_);
  int status = tmp.Solve();
  while ((status != GLP_INFEAS) && (status != GLP_NOFEAS)) {
    for (int i = 1; i <= elasticCols; ++i) {
      if (glp_get_col_prim(tmp.lp_, realCols + i) < 0) {
        suspects.push_back(i);
        glp_set_col_bnds(tmp.lp_, realCols + i, GLP_FX, 0.0, 0.0);
      }
    }
    status = tmp.Solve();
  }

  return suspects;
}
Ejemplo n.º 14
0
void B2GlpkHasher::add_trace_vars(const B2TraceCoeffs &trace_coeffs)
{
	const B2HashMap<B2Trace, unsigned int> &trace_vals = _trace_set.trace_vals();
	glp_add_cols(_lp, trace_vals.size());
	for(B2HashMap<B2Trace, unsigned int>::const_iterator trace_it = trace_vals.begin(); trace_it != trace_vals.end(); ++trace_it)
	{
		B2Trace trace = trace_it->first;
		unsigned int trace_var_idx = trace_it->second;
		glp_set_col_kind(_lp, trace_var_idx, GLP_BV);
		glp_set_col_name(_lp, trace_var_idx, B2TraceName(trace).name());
		glp_set_col_bnds(_lp, trace_var_idx, GLP_DB, 0.0, 1.0);
		glp_set_obj_coef(_lp, trace_var_idx, trace_coeffs[trace]);
	};
};
Ejemplo n.º 15
0
int GLPKLoadVariables(MFAVariable* InVariable, bool RelaxIntegerVariables,bool UseTightBounds) {
	if (GLPKModel == NULL) {
		FErrorFile() << "Could not add variable because GLPK object does not exist." << endl;
		FlushErrorFile();
		return FAIL;
	}

	int NumColumns = glp_get_num_cols(GLPKModel);

	if (InVariable->Index >= NumColumns) {
		glp_add_cols(GLPKModel, 1);
		string Name = GetMFAVariableName(InVariable);
		char* Temp = new char[Name.length()+1];
		strcpy(Temp,Name.data());
		glp_set_col_name(GLPKModel,InVariable->Index+1,Temp);
	}


	double LowerBound = InVariable->LowerBound;
	double UpperBound = InVariable->UpperBound;
	if (UseTightBounds) {
		LowerBound = InVariable->Min;
		UpperBound = InVariable->Max;
	}
	if (LowerBound != UpperBound) {
		glp_set_col_bnds(GLPKModel, InVariable->Index+1, GLP_DB, InVariable->LowerBound, InVariable->UpperBound);
	} else {
		glp_set_col_bnds(GLPKModel, InVariable->Index+1, GLP_FX, InVariable->LowerBound, InVariable->UpperBound);
	}

	if (InVariable->Binary && !RelaxIntegerVariables) {
		//glp_set_class(GLPKModel, GLP_MIP); There is no equivalent in the new API
		glp_set_col_kind(GLPKModel, InVariable->Index+1,GLP_IV);
	}

	return SUCCESS;
}
Ejemplo n.º 16
0
/* Restriction bci = ai - bi + xi = Ti and z += ai*Ei + bi*Li */
void addBasicRestriction(glp_prob * Prob, int plane) {
    int cardinal, constr[4], i = plane, cardRow;
	double cValues[4];
	char buf[AUXSIZE];

	cardinal = glp_add_cols(Prob, 3);
	
	sprintf(buf,"a%i",i);
	glp_set_col_name(Prob, cardinal, buf);
	glp_set_col_bnds(Prob, cardinal, GLP_LO, 0, 0);
	glp_set_obj_coef(Prob, cardinal, planes[i].costE);
	
	sprintf(buf,"b%i",i);
	glp_set_col_name(Prob, cardinal+1, buf);
	glp_set_col_bnds(Prob, cardinal+1, GLP_LO, 0, 0);
	glp_set_obj_coef(Prob, cardinal+1, planes[i].costL);
	
	sprintf(buf,"x%i",i);
	glp_set_col_name(Prob, cardinal+2, buf);
	if( planes[i].earliest == planes[i].latest )
	    glp_set_col_bnds(Prob, cardinal+2, GLP_FX, planes[i].earliest, 0);
	else
		glp_set_col_bnds(Prob, cardinal+2, GLP_DB, planes[i].earliest, planes[i].latest);
	
    cardRow = glp_add_rows(Prob, 1);
    
	sprintf(buf,"bc%i",i);
	glp_set_row_name(Prob, cardRow, buf);
	glp_set_row_bnds(Prob, cardRow, GLP_FX, planes[i].ideal, 0);
	
	constr[3] = 1 + (constr[2] = 1 + (constr[1] = cardinal));
	cValues[3] = cValues[1] = 1;
	cValues[2] = -1;
	
	glp_set_mat_row(Prob, cardRow, 3, constr, cValues);
}
Ejemplo n.º 17
0
void Add_obj()
{

	int i;
	char num[5];
	sig = (float*)malloc(n * sizeof(float));
	for(i = 0; i < n; i++)
		sig[i] = 1.0;

	stdev = sqrt(1.0 / (2*CODERATE*pow(10,SNR/10)));

	for(i = 0; i < n; i++)
		sig[i] += GeneGauss(0,stdev);

	/////////////////////////////////////////////////////////
	//	assign the sig to cplex as the r
	//  here just print them
/*	glpk_data = fopen("//home//why/document//lp//lp.txt","a+");
	fprintf(glpk_data,"minimize f : %f*f1",sig[0]);
	for(int i =1; i < n; i ++)
		fprintf(glpk_data,"+%f*f%d" , sig[i],i+1);
	fprintf(glpk_data,";\n");
	fclose(glpk_data);
*/
	/////////////////////////////////////////////////////////
	glp_add_cols(lp, n);
	glp_set_obj_dir(lp, GLP_MIN);
	for(i = 1; i <= n; i ++)
	{

		sprintf(num, "%d", i);
		glp_set_col_name(lp, i, num);
		glp_set_col_bnds(lp, i, GLP_DB, 0.0, 1.0);
		glp_set_obj_coef(lp, i, sig[i-1]);
	}
     free(sig);

}
Ejemplo n.º 18
0
void glp_set_col_kind(glp_prob *mip, int j, int kind)
{     GLPCOL *col;
      if (!(1 <= j && j <= mip->n))
         xerror("glp_set_col_kind: j = %d; column number out of range\n"
            , j);
      col = mip->col[j];
      switch (kind)
      {  case GLP_CV:
            col->kind = GLP_CV;
            break;
         case GLP_IV:
            col->kind = GLP_IV;
            break;
         case GLP_BV:
            col->kind = GLP_IV;
            if (!(col->type == GLP_DB && col->lb == 0.0 && col->ub ==
               1.0)) glp_set_col_bnds(mip, j, GLP_DB, 0.0, 1.0);
            break;
         default:
            xerror("glp_set_col_kind: j = %d; kind = %d; invalid column"
               " kind\n", j, kind);
      }
      return;
}
/**
 * Create the MLP problem
 *
 * @param mlp the MLP handle
 * @param addresses the hashmap containing all adresses
 * @return GNUNET_OK or GNUNET_SYSERR
 */
static int
mlp_create_problem (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_MultiHashMap * addresses)
{
  int res = GNUNET_OK;
  int col;
  int c;
  char *name;

  GNUNET_assert (mlp->prob == NULL);

  /* create the glpk problem */
  mlp->prob = glp_create_prob ();

  /* Set a problem name */
  glp_set_prob_name (mlp->prob, "gnunet ats bandwidth distribution");

  /* Set optimization direction to maximize */
  glp_set_obj_dir (mlp->prob, GLP_MAX);

  /* Adding invariant columns */

  /* Diversity d column  */
  col = glp_add_cols (mlp->prob, 1);
  mlp->c_d = col;
  /* Column name */
  glp_set_col_name (mlp->prob, col, "d");
  /* Column objective function coefficient */
  glp_set_obj_coef (mlp->prob, col, mlp->co_D);
  /* Column lower bound = 0.0 */
  glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0);

  /* Utilization u column  */
  col = glp_add_cols (mlp->prob, 1);
  mlp->c_u = col;
  /* Column name */
  glp_set_col_name (mlp->prob, col, "u");
  /* Column objective function coefficient */
  glp_set_obj_coef (mlp->prob, col, mlp->co_U);
  /* Column lower bound = 0.0 */
  glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0);

#if ENABLE_C9
  /* Relativity r column  */
  col = glp_add_cols (mlp->prob, 1);
  mlp->c_r = col;
  /* Column name */
  glp_set_col_name (mlp->prob, col, "r");
  /* Column objective function coefficient */
  glp_set_obj_coef (mlp->prob, col, mlp->co_R);
  /* Column lower bound = 0.0 */
  glp_set_col_bnds (mlp->prob, col, GLP_LO, 0.0, 0.0);
#endif

  /* Quality metric columns */
  col = glp_add_cols(mlp->prob, mlp->m_q);
  for (c = 0; c < mlp->m_q; c++)
  {
    mlp->c_q[c] = col + c;
    GNUNET_asprintf (&name, "q_%u", mlp->q[c]);
    glp_set_col_name (mlp->prob, col + c, name);
    /* Column lower bound = 0.0 */
    glp_set_col_bnds (mlp->prob, col + c, GLP_LO, 0.0, 0.0);
    GNUNET_free (name);
    /* Coefficient == Qm */
    glp_set_obj_coef (mlp->prob, col + c, mlp->co_Q[c]);
  }

  /* Add columns for addresses */
  GNUNET_CONTAINER_multihashmap_iterate (addresses, create_columns_it, mlp);

  /* Add constraints */
  mlp_add_constraints_all_addresses (mlp, addresses);

  /* Load the matrix */
  glp_load_matrix(mlp->prob, (mlp->ci-1), mlp->ia, mlp->ja, mlp->ar);

  return res;
}
Ejemplo n.º 20
0
int c_simplex_sparse(int m, int n, DMAT(c), DMAT(b), DVEC(s)) {
    glp_prob *lp;
    lp = glp_create_prob();
    glp_set_obj_dir(lp, GLP_MAX);
    int i,j,k;
    int tot = cr - n;
    glp_add_rows(lp, m);
    glp_add_cols(lp, n);

    //printf("%d %d\n",m,n);

    // the first n values
    for (k=1;k<=n;k++) {
        glp_set_obj_coef(lp, k, AT(c, k-1, 2));
        //printf("%d %f\n",k,AT(c, k-1, 2));
    }

    int * ia = malloc((1+tot)*sizeof(int));
    int * ja = malloc((1+tot)*sizeof(int));
    double * ar = malloc((1+tot)*sizeof(double));

    for (k=1; k<= tot; k++) {
        ia[k] = rint(AT(c,k-1+n,0));
        ja[k] = rint(AT(c,k-1+n,1));
        ar[k] =      AT(c,k-1+n,2);
        //printf("%d %d %f\n",ia[k],ja[k],ar[k]);
    }
    glp_load_matrix(lp, tot, ia, ja, ar);

    int t;
    for (i=1;i<=m;i++) {
    switch((int)rint(AT(b,i-1,0))) {
        case 0: { t = GLP_FR; break; }
        case 1: { t = GLP_LO; break; }
        case 2: { t = GLP_UP; break; }
        case 3: { t = GLP_DB; break; }
       default: { t = GLP_FX; break; }
    }
    glp_set_row_bnds(lp, i, t , AT(b,i-1,1), AT(b,i-1,2));
    }
    for (j=1;j<=n;j++) {
    switch((int)rint(AT(b,m+j-1,0))) {
        case 0: { t = GLP_FR; break; }
        case 1: { t = GLP_LO; break; }
        case 2: { t = GLP_UP; break; }
        case 3: { t = GLP_DB; break; }
       default: { t = GLP_FX; break; }
    }
    glp_set_col_bnds(lp, j, t , AT(b,m+j-1,1), AT(b,m+j-1,2));
    }
    glp_term_out(0);
    glp_simplex(lp, NULL);
    sp[0] = glp_get_status(lp);
    sp[1] = glp_get_obj_val(lp);
    for (k=1; k<=n; k++) {
        sp[k+1] = glp_get_col_prim(lp, k);
    }
    glp_delete_prob(lp);
    free(ia);
    free(ja);
    free(ar);

    return 0;
}
Ejemplo n.º 21
0
int main()
{
   // Variáveis auxiliares
   int i, j, constraintNumber, *constraintIndices;
   double *constraintCoefficients;

   // Aloca os vetores utilizados para criar as restrições do problema
   // ***********************************************************************************************
   // ATENÇÃO ===> É importante dizer que estes vetores serão utilizados da posição 1 em diante
   //              Ou seja, no GLPK você aloca uma posição a mais e descarta a posição 0 dos vetores.
   // ***********************************************************************************************
   constraintIndices = (int*)malloc((n+1)*sizeof(int));
   constraintCoefficients = (double*)malloc((n+1)*sizeof(double));

   // Cria um modelo com nenhuma variável e nenhuma restrição
   glp_prob *model = glp_create_prob();

   // Define o sentido da otimização que, para este problema, é minimização
   glp_set_obj_dir(model, GLP_MIN);

   // Cria as variáveis (colunas) no modelo
   // Para este problema são necessárias n*n variáveis
   // Estas n*n variáveis são definidas pelo GLPK através dos indices que vão de 1 até n*n (x1, x2, ..., x(n*n))
   // Portanto, neste momento é importante determinar qual variável no GLPK (índice) representará qual variável x[i,j]
   // Para tanto, fazemos o mapeamento das variáveis x[i,j] utilizando a fórmula (i-1)*n + j
   // Isto é, a variável x[i,j] será representada pela variável de índice (i-1)*n + j no modelo do GLPK
   // Note que é imprescindível que cada índice (variável do GLPK) seja associado a no máximo uma variável x[i,j]
   // Caso contrário, uma variável do GLPK pode representar duas variáveis x[i,j] diferentes que assumem valores distintos nas soluções ótimas
   // Neste caso, o modelo estará incorreto
   glp_add_cols(model, n*n);
   
   // Ajuste dos tipos, limitantes e coeficientes da função objetivo das variáveis do modelo
   
   for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++)
      {
         // Define o tipo da variável como sendo binária (em outros modelos poderia ser contínua (GLP_CV) ou inteira (GLP_IV))
         glp_set_col_kind(model, (i-1)*n + j, GLP_BV);
         
         // Define o limitante inferior (0) e superior (1) da variável
         // Consultem no manual as outras forma para definir apenas o limitante inferior ou superior
         glp_set_col_bnds(model, (i-1)*n + j, GLP_DB, 0.0, 1.0);

         // Define o coeficiente da variável na função objetivo
         glp_set_obj_coef(model, (i-1)*n + j, c[i-1][j-1]);
      }
      
   // Cria no modelo 2n restrições (linhas) nulas (com os coeficientes e limitantes zerados)
   // Ou seja, neste momento é criada uma matriz de zeros que correspondem aos coeficientes das restrições do modelo
   // O próximo passo será modificar esta matriz de tal forma que ela represente as restrições do problema descrito
   glp_add_rows(model, 2*n);

   // Esta variável define qual das restrições (qual linha da matriz de coeficientes) estamos modificando
   // ************************************************************************************************************
   // ATENÇÃO: perceba que as restrições (linhas), assim como as variáveis (colunas), são indexadas a partir de 1.
   // ************************************************************************************************************
   constraintNumber = 1;
   
   // Preenchimento das restrições limitando a soma das linhas:  
   // sum{j in 1..n} w[i,j]*x[i,j] <= u[i] para i in 1..n

   for (i = 1; i <= n; i++)
   {
      // Define o limite superior (RHS) da restrição      
      glp_set_row_bnds(model, constraintNumber, GLP_UP, 0.0, u[i-1]);

      for (j = 1; j <= n; j++)
      {
         // Ajusta o índice da variável que será informado à rotina do GLPK
         constraintIndices[j] = (i-1)*n + j;
         
         // Ajusta o coeficiente da variável cujo índice foi definido na linha anterior para ser informado ao GLPK
         // ******************************************************************************************************
         // ATENÇÃO: perceba que na matriz w os índices e colunas são indexados a partir de ZERO !
         // ******************************************************************************************************
         constraintCoefficients[j] = w[i-1][j-1];
      }

      // Passa ao GLPK a restrição que acabou de ser definida nos vetores constraintIndices e constraintCoefficients
      glp_set_mat_row(model, constraintNumber, n, constraintIndices, constraintCoefficients);
      
      // atualiza o indice da próxima restrição a ser inserida
      constraintNumber++;
   }
 
   // Preenchimento das restrições limitando a soma das colunas: 
   //    sum{i in 1..n} w[i,j]*x[i,j] >= l[i] para j in 1..n   
   for (j = 1; j <= n; j++)
   {
      // Define o limite inferior (RHS)  da restrição      
      glp_set_row_bnds(model, constraintNumber, GLP_LO, l[j-1], 0.0);

      for (i = 1; i <= n; i++)
      {
         // Ajusta o índice da variável que será informado a rotina do GLPK
         constraintIndices[i] = (i-1)*n + j;
         
         // Ajusta o coeficiente da variável cujo índice foi definido na linha anterior para ser informado ao GLPK
         constraintCoefficients[i] = w[i-1][j-1];
      }

      // Passa ao GLPK a restrição que acabou de ser definida nos vetores constraintIndices e constraintCoefficients
      glp_set_mat_row(model, constraintNumber, n, constraintIndices, constraintCoefficients);
      
      // atualiza o indice da próxima restrição a ser inserida
      constraintNumber++;
   } 
 
   // Define os parâmetros que serão passados ao resolvedor   
   glp_iocp param;
   glp_init_iocp(&param);
   
   // Ativa o presolver
   param.presolve = GLP_ON;

   // Resolve o modelo
   int status = glp_intopt(model, &param);

   // Verifica se houve algum erro durante a otimização
   if (status)
   {
      printf("Ocorreu um erro durante o processo de otimizacao.\n");
   }
   else
   {
      // Verifica se o método encontrou uma solução
      
      status = glp_mip_status(model);
      
      if ((status == GLP_OPT) || (status == GLP_FEAS))
      {
         // Imprime a solução encontrada
         
         if (status == GLP_OPT)
            printf("Solucao otima encontrada!\n");
         else
            printf("A solucao encontrada pode nao ser otima!\n");
      
         printf("Custo da solucao: %f\n", glp_mip_obj_val(model));

         for (i = 1; i <= n; i++)
         {
            for (j = 1; j <= n; j++)
               printf("%f ", glp_mip_col_val(model, (i-1)*n + j));
         
            printf("\n");
         }
      }
      else
      {
         printf("Nenhuma solucao foi encontrada!\n");
      }
   }


   // Desaloca os vetores
   free(constraintIndices);
   free(constraintCoefficients);

   return 0;
}
Ejemplo n.º 22
0
int max_flow_lp(int nn, int ne, const int beg[/*1+ne*/],
      const int end[/*1+ne*/], const int cap[/*1+ne*/], int s, int t,
      int x[/*1+ne*/])
{     glp_prob *lp;
      glp_smcp smcp;
      int i, k, nz, flow, *rn, *cn;
      double temp, *aa;
      /* create LP problem instance */
      lp = glp_create_prob();
      /* create LP rows; i-th row is the conservation condition of the
       * flow at i-th node, i = 1, ..., nn */
      glp_add_rows(lp, nn);
      for (i = 1; i <= nn; i++)
         glp_set_row_bnds(lp, i, GLP_FX, 0.0, 0.0);
      /* create LP columns; k-th column is the elementary flow thru
       * k-th edge, k = 1, ..., ne; the last column with the number
       * ne+1 is the total flow through the network, which goes along
       * a dummy feedback edge from the sink to the source */
      glp_add_cols(lp, ne+1);
      for (k = 1; k <= ne; k++)
      {  xassert(cap[k] > 0);
         glp_set_col_bnds(lp, k, GLP_DB, -cap[k], +cap[k]);
      }
      glp_set_col_bnds(lp, ne+1, GLP_FR, 0.0, 0.0);
      /* build the constraint matrix; structurally this matrix is the
       * incidence matrix of the network, so each its column (including
       * the last column for the dummy edge) has exactly two non-zero
       * entries */
      rn = xalloc(1+2*(ne+1), sizeof(int));
      cn = xalloc(1+2*(ne+1), sizeof(int));
      aa = xalloc(1+2*(ne+1), sizeof(double));
      nz = 0;
      for (k = 1; k <= ne; k++)
      {  /* x[k] > 0 means the elementary flow thru k-th edge goes from
          * node beg[k] to node end[k] */
         nz++, rn[nz] = beg[k], cn[nz] = k, aa[nz] = -1.0;
         nz++, rn[nz] = end[k], cn[nz] = k, aa[nz] = +1.0;
      }
      /* total flow thru the network goes from the sink to the source
       * along the dummy feedback edge */
      nz++, rn[nz] = t, cn[nz] = ne+1, aa[nz] = -1.0;
      nz++, rn[nz] = s, cn[nz] = ne+1, aa[nz] = +1.0;
      /* check the number of non-zero entries */
      xassert(nz == 2*(ne+1));
      /* load the constraint matrix into the LP problem object */
      glp_load_matrix(lp, nz, rn, cn, aa);
      xfree(rn);
      xfree(cn);
      xfree(aa);
      /* objective function is the total flow through the network to
       * be maximized */
      glp_set_obj_dir(lp, GLP_MAX);
      glp_set_obj_coef(lp, ne + 1, 1.0);
      /* solve LP instance with the (primal) simplex method */
      glp_term_out(0);
      glp_adv_basis(lp, 0);
      glp_term_out(1);
      glp_init_smcp(&smcp);
      smcp.msg_lev = GLP_MSG_ON;
      smcp.out_dly = 5000;
      xassert(glp_simplex(lp, &smcp) == 0);
      xassert(glp_get_status(lp) == GLP_OPT);
      /* obtain optimal elementary flows thru edges of the network */
      /* (note that the constraint matrix is unimodular and the data
       * are integral, so all elementary flows in basic solution should
       * also be integral) */
      for (k = 1; k <= ne; k++)
      {  temp = glp_get_col_prim(lp, k);
         x[k] = (int)floor(temp + .5);
         xassert(fabs(x[k] - temp) <= 1e-6);
      }
      /* obtain the maximum flow thru the original network which is the
       * flow thru the dummy feedback edge */
      temp = glp_get_col_prim(lp, ne+1);
      flow = (int)floor(temp + .5);
      xassert(fabs(flow - temp) <= 1e-6);
      /* delete LP problem instance */
      glp_delete_prob(lp);
      /* return to the calling program */
      return flow;
}
Ejemplo n.º 23
0
/* add the next column. 
   col  = index of the column (1<=col<=cols)
   n    = number of entries in row_idx[],row_val[] (1<=i<=n)
   type = GLP_FR: free; GLP_LO: >=0 */
static void add_column(int col,int n,int type)
{   sort_rowidx(n);
    glp_set_col_bnds(P,col,type,0.0,0.0);
    glp_set_mat_col(P,col,n,row_idx,row_val);
}
Ejemplo n.º 24
0
void glp_maxflow_lp(glp_prob *lp, glp_graph *G, int names, int s,
      int t, int a_cap)
{     glp_vertex *v;
      glp_arc *a;
      int i, j, type, ind[1+2];
      double cap, val[1+2];
      if (!(names == GLP_ON || names == GLP_OFF))
         xerror("glp_maxflow_lp: names = %d; invalid parameter\n",
            names);
      if (!(1 <= s && s <= G->nv))
         xerror("glp_maxflow_lp: s = %d; source node number out of rang"
            "e\n", s);
      if (!(1 <= t && t <= G->nv))
         xerror("glp_maxflow_lp: t = %d: sink node number out of range "
            "\n", t);
      if (s == t)
         xerror("glp_maxflow_lp: s = t = %d; source and sink nodes must"
            " be distinct\n", s);
      if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
         xerror("glp_maxflow_lp: a_cap = %d; invalid offset\n", a_cap);
      glp_erase_prob(lp);
      if (names) glp_set_prob_name(lp, G->name);
      glp_set_obj_dir(lp, GLP_MAX);
      glp_add_rows(lp, G->nv);
      for (i = 1; i <= G->nv; i++)
      {  v = G->v[i];
         if (names) glp_set_row_name(lp, i, v->name);
         if (i == s)
            type = GLP_LO;
         else if (i == t)
            type = GLP_UP;
         else
            type = GLP_FX;
         glp_set_row_bnds(lp, i, type, 0.0, 0.0);
      }
      if (G->na > 0) glp_add_cols(lp, G->na);
      for (i = 1, j = 0; i <= G->nv; i++)
      {  v = G->v[i];
         for (a = v->out; a != NULL; a = a->t_next)
         {  j++;
            if (names)
            {  char name[50+1];
               sprintf(name, "x[%d,%d]", a->tail->i, a->head->i);
               xassert(strlen(name) < sizeof(name));
               glp_set_col_name(lp, j, name);
            }
            if (a->tail->i != a->head->i)
            {  ind[1] = a->tail->i, val[1] = +1.0;
               ind[2] = a->head->i, val[2] = -1.0;
               glp_set_mat_col(lp, j, 2, ind, val);
            }
            if (a_cap >= 0)
               memcpy(&cap, (char *)a->data + a_cap, sizeof(double));
            else
               cap = 1.0;
            if (cap == DBL_MAX)
               type = GLP_LO;
            else if (cap != 0.0)
               type = GLP_DB;
            else
               type = GLP_FX;
            glp_set_col_bnds(lp, j, type, 0.0, cap);
            if (a->tail->i == s)
               glp_set_obj_coef(lp, j, +1.0);
            else if (a->head->i == s)
               glp_set_obj_coef(lp, j, -1.0);
         }
      }
      xassert(j == G->na);
      return;
}
Ejemplo n.º 25
0
void ios_feas_pump(glp_tree *T)
{     glp_prob *P = T->mip;
      int n = P->n;
      glp_prob *lp = NULL;
      struct VAR *var = NULL;
      RNG *rand = NULL;
      GLPCOL *col;
      glp_smcp parm;
      int j, k, new_x, nfail, npass, nv, ret, stalling;
      double dist, tol;
      xassert(glp_get_status(P) == GLP_OPT);
      /* this heuristic is applied only once on the root level */
      if (!(T->curr->level == 0 && T->curr->solved == 1)) goto done;
      /* determine number of binary variables */
      nv = 0;
      for (j = 1; j <= n; j++)
      {  col = P->col[j];
         /* if x[j] is continuous, skip it */
         if (col->kind == GLP_CV) continue;
         /* if x[j] is fixed, skip it */
         if (col->type == GLP_FX) continue;
         /* x[j] is non-fixed integer */
         xassert(col->kind == GLP_IV);
         if (col->type == GLP_DB && col->lb == 0.0 && col->ub == 1.0)
         {  /* x[j] is binary */
            nv++;
         }
         else
         {  /* x[j] is general integer */
            if (T->parm->msg_lev >= GLP_MSG_ALL)
               xprintf("FPUMP heuristic cannot be applied due to genera"
                  "l integer variables\n");
            goto done;
         }
      }
      /* there must be at least one binary variable */
      if (nv == 0) goto done;
      if (T->parm->msg_lev >= GLP_MSG_ALL)
         xprintf("Applying FPUMP heuristic...\n");
      /* build the list of binary variables */
      var = xcalloc(1+nv, sizeof(struct VAR));
      k = 0;
      for (j = 1; j <= n; j++)
      {  col = P->col[j];
         if (col->kind == GLP_IV && col->type == GLP_DB)
            var[++k].j = j;
      }
      xassert(k == nv);
      /* create working problem object */
      lp = glp_create_prob();
more: /* copy the original problem object to keep it intact */
      glp_copy_prob(lp, P, GLP_OFF);
      /* we are interested to find an integer feasible solution, which
         is better than the best known one */
      if (P->mip_stat == GLP_FEAS)
      {  int *ind;
         double *val, bnd;
         /* add a row and make it identical to the objective row */
         glp_add_rows(lp, 1);
         ind = xcalloc(1+n, sizeof(int));
         val = xcalloc(1+n, sizeof(double));
         for (j = 1; j <= n; j++)
         {  ind[j] = j;
            val[j] = P->col[j]->coef;
         }
         glp_set_mat_row(lp, lp->m, n, ind, val);
         xfree(ind);
         xfree(val);
         /* introduce upper (minimization) or lower (maximization)
            bound to the original objective function; note that this
            additional constraint is not violated at the optimal point
            to LP relaxation */
#if 0 /* modified by xypron <*****@*****.**> */
         if (P->dir == GLP_MIN)
         {  bnd = P->mip_obj - 0.10 * (1.0 + fabs(P->mip_obj));
            if (bnd < P->obj_val) bnd = P->obj_val;
            glp_set_row_bnds(lp, lp->m, GLP_UP, 0.0, bnd - P->c0);
         }
         else if (P->dir == GLP_MAX)
         {  bnd = P->mip_obj + 0.10 * (1.0 + fabs(P->mip_obj));
            if (bnd > P->obj_val) bnd = P->obj_val;
            glp_set_row_bnds(lp, lp->m, GLP_LO, bnd - P->c0, 0.0);
         }
         else
            xassert(P != P);
#else
         bnd = 0.1 * P->obj_val + 0.9 * P->mip_obj;
         /* xprintf("bnd = %f\n", bnd); */
         if (P->dir == GLP_MIN)
            glp_set_row_bnds(lp, lp->m, GLP_UP, 0.0, bnd - P->c0);
         else if (P->dir == GLP_MAX)
            glp_set_row_bnds(lp, lp->m, GLP_LO, bnd - P->c0, 0.0);
         else
            xassert(P != P);
#endif
      }
      /* reset pass count */
      npass = 0;
      /* invalidate the rounded point */
      for (k = 1; k <= nv; k++)
         var[k].x = -1;
pass: /* next pass starts here */
      npass++;
      if (T->parm->msg_lev >= GLP_MSG_ALL)
         xprintf("Pass %d\n", npass);
      /* initialize minimal distance between the basic point and the
         rounded one obtained during this pass */
      dist = DBL_MAX;
      /* reset failure count (the number of succeeded iterations failed
         to improve the distance) */
      nfail = 0;
      /* if it is not the first pass, perturb the last rounded point
         rather than construct it from the basic solution */
      if (npass > 1)
      {  double rho, temp;
         if (rand == NULL)
            rand = rng_create_rand();
         for (k = 1; k <= nv; k++)
         {  j = var[k].j;
            col = lp->col[j];
            rho = rng_uniform(rand, -0.3, 0.7);
            if (rho < 0.0) rho = 0.0;
            temp = fabs((double)var[k].x - col->prim);
            if (temp + rho > 0.5) var[k].x = 1 - var[k].x;
         }
         goto skip;
      }
loop: /* innermost loop begins here */
      /* round basic solution (which is assumed primal feasible) */
      stalling = 1;
      for (k = 1; k <= nv; k++)
      {  col = lp->col[var[k].j];
         if (col->prim < 0.5)
         {  /* rounded value is 0 */
            new_x = 0;
         }
         else
         {  /* rounded value is 1 */
            new_x = 1;
         }
         if (var[k].x != new_x)
         {  stalling = 0;
            var[k].x = new_x;
         }
      }
      /* if the rounded point has not changed (stalling), choose and
         flip some its entries heuristically */
      if (stalling)
      {  /* compute d[j] = |x[j] - round(x[j])| */
         for (k = 1; k <= nv; k++)
         {  col = lp->col[var[k].j];
            var[k].d = fabs(col->prim - (double)var[k].x);
         }
         /* sort the list of binary variables by descending d[j] */
         qsort(&var[1], nv, sizeof(struct VAR), fcmp);
         /* choose and flip some rounded components */
         for (k = 1; k <= nv; k++)
         {  if (k >= 5 && var[k].d < 0.35 || k >= 10) break;
            var[k].x = 1 - var[k].x;
         }
      }
skip: /* check if the time limit has been exhausted */
      if (T->parm->tm_lim < INT_MAX &&
         (double)(T->parm->tm_lim - 1) <=
         1000.0 * xdifftime(xtime(), T->tm_beg)) goto done;
      /* build the objective, which is the distance between the current
         (basic) point and the rounded one */
      lp->dir = GLP_MIN;
      lp->c0 = 0.0;
      for (j = 1; j <= n; j++)
         lp->col[j]->coef = 0.0;
      for (k = 1; k <= nv; k++)
      {  j = var[k].j;
         if (var[k].x == 0)
            lp->col[j]->coef = +1.0;
         else
         {  lp->col[j]->coef = -1.0;
            lp->c0 += 1.0;
         }
      }
      /* minimize the distance with the simplex method */
      glp_init_smcp(&parm);
      if (T->parm->msg_lev <= GLP_MSG_ERR)
         parm.msg_lev = T->parm->msg_lev;
      else if (T->parm->msg_lev <= GLP_MSG_ALL)
      {  parm.msg_lev = GLP_MSG_ON;
         parm.out_dly = 10000;
      }
      ret = glp_simplex(lp, &parm);
      if (ret != 0)
      {  if (T->parm->msg_lev >= GLP_MSG_ERR)
            xprintf("Warning: glp_simplex returned %d\n", ret);
         goto done;
      }
      ret = glp_get_status(lp);
      if (ret != GLP_OPT)
      {  if (T->parm->msg_lev >= GLP_MSG_ERR)
            xprintf("Warning: glp_get_status returned %d\n", ret);
         goto done;
      }
      if (T->parm->msg_lev >= GLP_MSG_DBG)
         xprintf("delta = %g\n", lp->obj_val);
      /* check if the basic solution is integer feasible; note that it
         may be so even if the minimial distance is positive */
      tol = 0.3 * T->parm->tol_int;
      for (k = 1; k <= nv; k++)
      {  col = lp->col[var[k].j];
         if (tol < col->prim && col->prim < 1.0 - tol) break;
      }
      if (k > nv)
      {  /* okay; the basic solution seems to be integer feasible */
         double *x = xcalloc(1+n, sizeof(double));
         for (j = 1; j <= n; j++)
         {  x[j] = lp->col[j]->prim;
            if (P->col[j]->kind == GLP_IV) x[j] = floor(x[j] + 0.5);
         }
#if 1 /* modified by xypron <*****@*****.**> */
         /* reset direction and right-hand side of objective */
         lp->c0  = P->c0;
         lp->dir = P->dir;
         /* fix integer variables */
         for (k = 1; k <= nv; k++)
#if 0 /* 18/VI-2013; fixed by mao
       * this bug causes numerical instability, because column statuses
       * are not changed appropriately */
         {  lp->col[var[k].j]->lb   = x[var[k].j];
            lp->col[var[k].j]->ub   = x[var[k].j];
            lp->col[var[k].j]->type = GLP_FX;
         }
#else
            glp_set_col_bnds(lp, var[k].j, GLP_FX, x[var[k].j], 0.);
#endif
         /* copy original objective function */
         for (j = 1; j <= n; j++)
            lp->col[j]->coef = P->col[j]->coef;
         /* solve original LP and copy result */
         ret = glp_simplex(lp, &parm);
         if (ret != 0)
         {  if (T->parm->msg_lev >= GLP_MSG_ERR)
               xprintf("Warning: glp_simplex returned %d\n", ret);
            goto done;
         }
         ret = glp_get_status(lp);
         if (ret != GLP_OPT)
         {  if (T->parm->msg_lev >= GLP_MSG_ERR)
               xprintf("Warning: glp_get_status returned %d\n", ret);
            goto done;
         }
         for (j = 1; j <= n; j++)
            if (P->col[j]->kind != GLP_IV) x[j] = lp->col[j]->prim;
#endif
         ret = glp_ios_heur_sol(T, x);
         xfree(x);
         if (ret == 0)
         {  /* the integer solution is accepted */
            if (ios_is_hopeful(T, T->curr->bound))
            {  /* it is reasonable to apply the heuristic once again */
               goto more;
            }
            else
            {  /* the best known integer feasible solution just found
                  is close to optimal solution to LP relaxation */
               goto done;
            }
         }
      }
Ejemplo n.º 26
0
void glp_mincost_lp(glp_prob *lp, glp_graph *G, int names, int v_rhs,
      int a_low, int a_cap, int a_cost)
{     glp_vertex *v;
      glp_arc *a;
      int i, j, type, ind[1+2];
      double rhs, low, cap, cost, val[1+2];
      if (!(names == GLP_ON || names == GLP_OFF))
         xerror("glp_mincost_lp: names = %d; invalid parameter\n",
            names);
      if (v_rhs >= 0 && v_rhs > G->v_size - (int)sizeof(double))
         xerror("glp_mincost_lp: v_rhs = %d; invalid offset\n", v_rhs);
      if (a_low >= 0 && a_low > G->a_size - (int)sizeof(double))
         xerror("glp_mincost_lp: a_low = %d; invalid offset\n", a_low);
      if (a_cap >= 0 && a_cap > G->a_size - (int)sizeof(double))
         xerror("glp_mincost_lp: a_cap = %d; invalid offset\n", a_cap);
      if (a_cost >= 0 && a_cost > G->a_size - (int)sizeof(double))
         xerror("glp_mincost_lp: a_cost = %d; invalid offset\n", a_cost)
            ;
      glp_erase_prob(lp);
      if (names) glp_set_prob_name(lp, G->name);
      if (G->nv > 0) glp_add_rows(lp, G->nv);
      for (i = 1; i <= G->nv; i++)
      {  v = G->v[i];
         if (names) glp_set_row_name(lp, i, v->name);
         if (v_rhs >= 0)
            memcpy(&rhs, (char *)v->data + v_rhs, sizeof(double));
         else
            rhs = 0.0;
         glp_set_row_bnds(lp, i, GLP_FX, rhs, rhs);
      }
      if (G->na > 0) glp_add_cols(lp, G->na);
      for (i = 1, j = 0; i <= G->nv; i++)
      {  v = G->v[i];
         for (a = v->out; a != NULL; a = a->t_next)
         {  j++;
            if (names)
            {  char name[50+1];
               sprintf(name, "x[%d,%d]", a->tail->i, a->head->i);
               xassert(strlen(name) < sizeof(name));
               glp_set_col_name(lp, j, name);
            }
            if (a->tail->i != a->head->i)
            {  ind[1] = a->tail->i, val[1] = +1.0;
               ind[2] = a->head->i, val[2] = -1.0;
               glp_set_mat_col(lp, j, 2, ind, val);
            }
            if (a_low >= 0)
               memcpy(&low, (char *)a->data + a_low, sizeof(double));
            else
               low = 0.0;
            if (a_cap >= 0)
               memcpy(&cap, (char *)a->data + a_cap, sizeof(double));
            else
               cap = 1.0;
            if (cap == DBL_MAX)
               type = GLP_LO;
            else if (low != cap)
               type = GLP_DB;
            else
               type = GLP_FX;
            glp_set_col_bnds(lp, j, type, low, cap);
            if (a_cost >= 0)
               memcpy(&cost, (char *)a->data + a_cost, sizeof(double));
            else
               cost = 0.0;
            glp_set_obj_coef(lp, j, cost);
         }
      }
      xassert(j == G->na);
      return;
}
Ejemplo n.º 27
0
int main(int argc, char *argv[]) {

	/* Structures de données propres à GLPK */
	glp_prob *prob; // Déclaration d'un pointeur sur le problème
	int ia[1 + NBCREUX];
	int ja[1 + NBCREUX];
	double ar[1 + NBCREUX]; // Déclaration des 3 tableaux servant à définir la partie creuse de la matrice des contraintes

    /* Variables récupérant les résultats de la résolution du problème (fonction objectif et valeur des variables) */
	int i, j;
	double z; 
	double x[NBVAR];

	// Autres variables
	int * p = (int*)malloc(n * sizeof(int));
	p[1] = 34;
	p[2] = 6;
	p[3] = 8;
	p[4] = 17;
	p[5] = 16;
	p[6] = 5;
	p[7] = 13;
	p[8] = 21;
	p[9] = 25;
	p[10] = 31;
	p[11] = 14;
	p[12] = 13;
	p[13] = 33;
	p[14] = 9;
	p[15] = 25;
	p[16] = 25;

	/* Transfert de ces données dans les structures utilisées par la bibliothèque GLPK */
	
	prob = glp_create_prob(); /* allocation mémoire pour le problème */ 
	glp_set_prob_name(prob, "wagons"); /* affectation d'un nom */
	glp_set_obj_dir(prob, GLP_MIN); /* Il s'agit d'un problème de minimisation */
	
	/* Déclaration du nombre de contraintes (nombre de lignes de la matrice des contraintes) */
	
	glp_add_rows(prob, NBCONTR); 

	/* On commence par préciser les bornes sur les contraintes, les indices commencent à 1 (!) dans GLPK */

	/* Premier ensemble de contraintes ( c = 1 ) */
	for(i = 1; i <= n; i++) {
		glp_set_row_bnds(prob, i, GLP_FX, 1.0, 1.0);
	}

	/* Second ensembles de contraintes (c <= 0 ) */
	for(i = n + 1; i <= NBCONTR; i++) {
		glp_set_row_bnds(prob, i, GLP_UP, 0.0, 0.0);
	}

	/* Déclaration du nombre de variables */
	
	glp_add_cols(prob, NBVAR); 
	
	/* On précise le type des variables, les indices commencent à 1 également pour les variables! */
	
	for(i = 1; i <= NBVAR - 1; i++) {
		glp_set_col_bnds(prob, i, GLP_DB, 0.0, 1.0);
		glp_set_col_kind(prob, i, GLP_BV);	/* les variables sont binaires */	
	}

	glp_set_col_bnds(prob, NBVAR, GLP_LO, 0.0, 0.0); /* La dernière variables est continue (par défaut) non négative */

	/* Définition des coefficients des variables dans la fonction objectif */

	for(i = 1;i <= n*m;i++) {
		glp_set_obj_coef(prob,i,0.0); // Tous les coûts sont à 0 (sauf le dernier) 
	}

	/* Dernier coût (qui vaut 1) */
	glp_set_obj_coef(prob,n*m + 1,1.0); 
	
	/* Définition des coefficients non-nuls dans la matrice des contraintes, autrement dit les coefficients de la matrice creuse */

	int pos = 1;
	for(i = 1; i <= n; i++) {
		for(j = 1; j <= m; j++) {

			// Première moitié de la matrice
			ja[pos] = (i - 1)*m + j;
			ia[pos] = i;
			ar[pos] = 1;
			pos++;

			// Deuxième moitié de la matrice
			ja[pos] = (i - 1)*m + j;
			ia[pos] = n + j;
			ar[pos] = p[i];
			pos++;
		}
	}

	// ajout des -1 dans la dernière colonne
	for(i = n + 1; i <= n + m; i++) {
		ja[pos] = n*m + 1;
		ia[pos] = i;
		ar[pos] = -1;
		pos++;
	}

	/* Chargement de la matrice dans le problème */
	
	glp_load_matrix(prob,NBCREUX,ia,ja,ar); 
	
	/* Ecriture de la modélisation dans un fichier */

	glp_write_lp(prob,NULL,"wagons.lp");

	/* Résolution, puis lecture des résultats */
	
	glp_simplex(prob,NULL);	glp_intopt(prob,NULL); /* Résolution */
	z = glp_mip_obj_val(prob); /* Récupération de la valeur optimale. Dans le cas d'un problème en variables continues, l'appel est différent : z = glp_get_obj_val(prob); */
	for(i = 0;i < NBVAR; i++) x[i] = glp_mip_col_val(prob,i+1); /* Récupération de la valeur des variables, Appel différent dans le cas d'un problème en variables continues : for(i = 0;i < p.nbvar;i++) x[i] = glp_get_col_prim(prob,i+1); */

	printf("z = %lf\n",z);
	for(i = 0;i < NBVAR;i++) printf("x%c = %d, ",'B'+i,(int)(x[i] + 0.5)); /* un cast est ajouté, x[i] pourrait être égal à 0.99999... */ 
	puts("");

	/* Libération de la mémoire */
	glp_delete_prob(prob); 
	free(p);

	return 0;

}
Ejemplo n.º 28
0
void glp_mpl_build_prob(glp_tran *tran, glp_prob *prob)
{     /* build LP/MIP problem instance from the model */
      int m, n, i, j, t, kind, type, len, *ind;
      double lb, ub, *val;
      if (tran->phase != 3)
         xerror("glp_mpl_build_prob: invalid call sequence\n");
      /* erase the problem object */
      glp_erase_prob(prob);
      /* set problem name */
      glp_set_prob_name(prob, mpl_get_prob_name(tran));
      /* build rows (constraints) */
      m = mpl_get_num_rows(tran);
      if (m > 0)
         glp_add_rows(prob, m);
      for (i = 1; i <= m; i++)
      {  /* set row name */
         glp_set_row_name(prob, i, mpl_get_row_name(tran, i));
         /* set row bounds */
         type = mpl_get_row_bnds(tran, i, &lb, &ub);
         switch (type)
         {  case MPL_FR: type = GLP_FR; break;
            case MPL_LO: type = GLP_LO; break;
            case MPL_UP: type = GLP_UP; break;
            case MPL_DB: type = GLP_DB; break;
            case MPL_FX: type = GLP_FX; break;
            default: xassert(type != type);
         }
         if (type == GLP_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb)))
         {  type = GLP_FX;
            if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub;
         }
         glp_set_row_bnds(prob, i, type, lb, ub);
         /* warn about non-zero constant term */
         if (mpl_get_row_c0(tran, i) != 0.0)
            xprintf("glp_mpl_build_prob: row %s; constant term %.12g ig"
               "nored\n",
               mpl_get_row_name(tran, i), mpl_get_row_c0(tran, i));
      }
      /* build columns (variables) */
      n = mpl_get_num_cols(tran);
      if (n > 0)
         glp_add_cols(prob, n);
      for (j = 1; j <= n; j++)
      {  /* set column name */
         glp_set_col_name(prob, j, mpl_get_col_name(tran, j));
         /* set column kind */
         kind = mpl_get_col_kind(tran, j);
         switch (kind)
         {  case MPL_NUM:
               break;
            case MPL_INT:
            case MPL_BIN:
               glp_set_col_kind(prob, j, GLP_IV);
               break;
            default:
               xassert(kind != kind);
         }
         /* set column bounds */
         type = mpl_get_col_bnds(tran, j, &lb, &ub);
         switch (type)
         {  case MPL_FR: type = GLP_FR; break;
            case MPL_LO: type = GLP_LO; break;
            case MPL_UP: type = GLP_UP; break;
            case MPL_DB: type = GLP_DB; break;
            case MPL_FX: type = GLP_FX; break;
            default: xassert(type != type);
         }
         if (kind == MPL_BIN)
         {  if (type == GLP_FR || type == GLP_UP || lb < 0.0) lb = 0.0;
            if (type == GLP_FR || type == GLP_LO || ub > 1.0) ub = 1.0;
            type = GLP_DB;
         }
         if (type == GLP_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb)))
         {  type = GLP_FX;
            if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub;
         }
         glp_set_col_bnds(prob, j, type, lb, ub);
      }
      /* load the constraint matrix */
      ind = xcalloc(1+n, sizeof(int));
      val = xcalloc(1+n, sizeof(double));
      for (i = 1; i <= m; i++)
      {  len = mpl_get_mat_row(tran, i, ind, val);
         glp_set_mat_row(prob, i, len, ind, val);
      }
      /* build objective function (the first objective is used) */
      for (i = 1; i <= m; i++)
      {  kind = mpl_get_row_kind(tran, i);
         if (kind == MPL_MIN || kind == MPL_MAX)
         {  /* set objective name */
            glp_set_obj_name(prob, mpl_get_row_name(tran, i));
            /* set optimization direction */
            glp_set_obj_dir(prob, kind == MPL_MIN ? GLP_MIN : GLP_MAX);
            /* set constant term */
            glp_set_obj_coef(prob, 0, mpl_get_row_c0(tran, i));
            /* set objective coefficients */
            len = mpl_get_mat_row(tran, i, ind, val);
            for (t = 1; t <= len; t++)
               glp_set_obj_coef(prob, ind[t], val[t]);
            break;
         }
      }
      /* free working arrays */
      xfree(ind);
      xfree(val);
      return;
}
Ejemplo n.º 29
0
void npp_build_prob(NPP *npp, glp_prob *prob)
{     /* build resultant (preprocessed) problem */
      NPPROW *row;
      NPPCOL *col;
      NPPAIJ *aij;
      int i, j, type, len, *ind;
      double dir, *val;
      glp_erase_prob(prob);
      glp_set_prob_name(prob, npp->name);
      glp_set_obj_name(prob, npp->obj);
      glp_set_obj_dir(prob, npp->orig_dir);
      if (npp->orig_dir == GLP_MIN)
         dir = +1.0;
      else if (npp->orig_dir == GLP_MAX)
         dir = -1.0;
      else
         xassert(npp != npp);
      glp_set_obj_coef(prob, 0, dir * npp->c0);
      /* build rows */
      for (row = npp->r_head; row != NULL; row = row->next)
      {  row->temp = i = glp_add_rows(prob, 1);
         glp_set_row_name(prob, i, row->name);
         if (row->lb == -DBL_MAX && row->ub == +DBL_MAX)
            type = GLP_FR;
         else if (row->ub == +DBL_MAX)
            type = GLP_LO;
         else if (row->lb == -DBL_MAX)
            type = GLP_UP;
         else if (row->lb != row->ub)
            type = GLP_DB;
         else
            type = GLP_FX;
         glp_set_row_bnds(prob, i, type, row->lb, row->ub);
      }
      /* build columns and the constraint matrix */
      ind = xcalloc(1+prob->m, sizeof(int));
      val = xcalloc(1+prob->m, sizeof(double));
      for (col = npp->c_head; col != NULL; col = col->next)
      {  j = glp_add_cols(prob, 1);
         glp_set_col_name(prob, j, col->name);
#if 0
         glp_set_col_kind(prob, j, col->kind);
#else
         glp_set_col_kind(prob, j, col->is_int ? GLP_IV : GLP_CV);
#endif
         if (col->lb == -DBL_MAX && col->ub == +DBL_MAX)
            type = GLP_FR;
         else if (col->ub == +DBL_MAX)
            type = GLP_LO;
         else if (col->lb == -DBL_MAX)
            type = GLP_UP;
         else if (col->lb != col->ub)
            type = GLP_DB;
         else
            type = GLP_FX;
         glp_set_col_bnds(prob, j, type, col->lb, col->ub);
         glp_set_obj_coef(prob, j, dir * col->coef);
         len = 0;
         for (aij = col->ptr; aij != NULL; aij = aij->c_next)
         {  len++;
            ind[len] = aij->row->temp;
            val[len] = aij->val;
         }
         glp_set_mat_col(prob, j, len, ind, val);
      }
      xfree(ind);
      xfree(val);
      /* resultant problem has been built */
      npp->m = prob->m;
      npp->n = prob->n;
      npp->nnz = prob->nnz;
      npp->row_ref = xcalloc(1+npp->m, sizeof(int));
      npp->col_ref = xcalloc(1+npp->n, sizeof(int));
      for (row = npp->r_head, i = 0; row != NULL; row = row->next)
         npp->row_ref[++i] = row->i;
      for (col = npp->c_head, j = 0; col != NULL; col = col->next)
         npp->col_ref[++j] = col->j;
      /* transformed problem segment is no longer needed */
      dmp_delete_pool(npp->pool), npp->pool = NULL;
      npp->name = npp->obj = NULL;
      npp->c0 = 0.0;
      npp->r_head = npp->r_tail = NULL;
      npp->c_head = npp->c_tail = NULL;
      return;
}
Ejemplo n.º 30
0
Archivo: tp3.c Proyecto: dennisman/L3
int main(int argc, char *argv[])
{	
	/* structures de données propres à GLPK */
	
	glp_prob *prob; // déclaration d'un pointeur sur le problème
	int ia[1 + NBCREUX];
	int ja[1 + NBCREUX];
	double ar[1 + NBCREUX]; // déclaration des 3 tableaux servant à définir la partie creuse de la matrice des contraintes
	int p[N+1];
	p[1] = 34;
	  p[2] = 6; p[3] = 8; p[4] = 17; p[5] = 16; p[6] = 5; p[7] = 13; p[8] = 21; p[9] = 25; p[10] = 31; p[11] = 14; p[12] = 13; p[13] = 33; p[14] = 9; p[15] = 25; p[16] = 25;

	/* variables récupérant les résultats de la résolution du problème (fonction objectif et valeur des variables) */

	int i,j,pos;
	double z;
	double x[NBVAR];
	
	/* Les déclarations suivantes sont optionnelles, leur but est de donner des noms aux variables et aux contraintes.
	   Cela permet de lire plus facilement le modèle saisi si on en demande un affichage à GLPK, ce qui est souvent utile pour détecter une erreur! */
	
	char nomcontr[NBCONTR][8]; /* ici, les contraintes seront nommées "caisse1", "caisse2",... */
	char numero[NBCONTR][3]; /* pour un nombre à deux chiffres */	
	char nomvar[NBVAR][3]; /* "xA", "xB", ... */
	
	/* Création d'un problème (initialement vide) */
	
	prob = glp_create_prob(); /* allocation mémoire pour le problème */ 
	glp_set_prob_name(prob, "wagons"); /* affectation d'un nom (on pourrait mettre NULL) */
	glp_set_obj_dir(prob, GLP_MIN); /* Il s'agit d'un problème de minimisation, on utiliserait la constante GLP_MAX dans le cas contraire */
	
	/* Déclaration du nombre de contraintes (nombre de lignes de la matrice des contraintes) : NBCONTR */
	
	glp_add_rows(prob, NBCONTR); 

	/* On commence par préciser les bornes sur les constrainte, les indices des contraintes commencent à 1 (!) dans GLPK */

	for(i = 1;i <= N;i++)
	{
		/* partie optionnelle : donner un nom aux contraintes */
		strcpy(nomcontr[i-1], "caisse");
		sprintf(numero[i-1], "%d", i);
		strcat(nomcontr[i-1], numero[i-1]); /* Les contraintes sont nommés "salle1", "salle2"... */		
		glp_set_row_name(prob, i, nomcontr[i-1]); /* Affectation du nom à la contrainte i */
		
		/* partie indispensable : les bornes sur les contraintes */
		glp_set_row_bnds(prob, i, GLP_FX, 1.0, 1.0);
	}	
for(i = N+1;i <= NBCONTR;i++)
	{
	/* partie optionnelle : donner un nom aux contraintes */
		strcpy(nomcontr[i-1], "chaMax");
		sprintf(numero[i-1], "%d", i);
		strcat(nomcontr[i-1], numero[i-1]); /* Les contraintes sont nommés "chargemax", "chargemax2"... */		
		glp_set_row_name(prob, i, nomcontr[i-1]); /* Affectation du nom à la contrainte i */
		// il doit manquer un bout ici
		glp_set_row_bnds(prob, i, GLP_UP, 0.0, 0.0);
		//<=0
		// on met cmax a gauche car c'est une variable
		// il aura le coeff -1 dans la mat creuse
	}

	/* Déclaration du nombre de variables : NBVAR */
	
	glp_add_cols(prob, NBVAR); 
	
	/* On précise le type des variables, les indices commencent à 1 également pour les variables! */
	
	for(i = 1;i <= NBVAR;i++)
	{
		if(i==NBVAR){
			sprintf(nomvar[i-1],"Cm");
			glp_set_col_name(prob, i , nomvar[i-1]);
			glp_set_col_bnds(prob, i, GLP_LO, 0.0, 0.0);
		}else{
			/* partie optionnelle : donner un nom aux variables */
			sprintf(nomvar[i-1],"x%d",i-1);
			glp_set_col_name(prob, i , nomvar[i-1]); /* Les variables sont nommées "xA", "xB"... afin de respecter les noms de variables de l'exercice 2.2 */
		
			/* partie obligatoire : bornes éventuelles sur les variables, et type */
			glp_set_col_bnds(prob, i, GLP_DB, 0.0, 1.0); /* bornes sur les variables, comme sur les contraintes */
			glp_set_col_kind(prob, i, GLP_BV);	/* les variables sont par défaut continues, nous précisons ici qu'elles sont binaires avec la constante GLP_BV, on utiliserait GLP_IV pour des variables entières */	
		}
	} 

	/* définition des coefficients des variables dans la fonction objectif */

	for(i = 1;i <= N*M;i++) glp_set_obj_coef(prob,i,0.0); // Tous les coûts sont ici à 0! Mais on doit specifier quand meme
	glp_set_obj_coef(prob,N*M+1,1.0); // 1 fois cmax
	
	/* Définition des coefficients non-nuls dans la matrice des contraintes, autrement dit les coefficients de la matrice creuse */
	/* Les indices commencent également à 1 ! */



// pour i de 1 a n
//pour i de 1 a m
 /* xij intervient dans la ligne i avec un coeff 1 et dans la ligne 
 n+j avec un coeff pi
 
 ia -> i et n+j
 ar -> 1 et pi
 ja -> xij -> (i-1)*m+j
*/

	pos = 1;
	for(i=1; i<=N; i++){
		for(j=1; j<=M; j++){
			ia[pos] = i;
			ja[pos] = (i-1)*M+j;		ar[pos] = 1;
			pos++; 
			
			ia[pos] = N+j;
			ja[pos] = (i-1)*M+j;	ar[pos] = p[i];
			pos++;
		}
	}
	
//Cmax a -1 !!!
for(i=N+1; i<=N+M;i++){
	ia[pos] = i;
	ja[pos] = N*M+1;				ar[pos] = -1;
	pos++; 	
}
	

	
	/* chargement de la matrice dans le problème */
	
	glp_load_matrix(prob,NBCREUX,ia,ja,ar); 
	
	/* Optionnel : écriture de la modélisation dans un fichier (TRES utile pour debugger!) */

	glp_write_lp(prob,NULL,"wagons.lp");

	/* Résolution, puis lecture des résultats */
	
	glp_simplex(prob,NULL);	glp_intopt(prob,NULL); /* Résolution */
	z = glp_mip_obj_val(prob); /* Récupération de la valeur optimale. Dans le cas d'un problème en variables continues, l'appel est différent : z = glp_get_obj_val(prob); */
	for(i = 0;i < NBVAR; i++) x[i] = glp_mip_col_val(prob,i+1); /* Récupération de la valeur des variables, Appel différent dans le cas d'un problème en variables continues : for(i = 0;i < p.nbvar;i++) x[i] = glp_get_col_prim(prob,i+1); */

	printf("z = %lf\n",z);
	for(i = 0;i < NBVAR;i++) printf("x%d = %d, ",i,(int)(x[i] + 0.5)); /* un cast est ajouté, x[i] pourrait être égal à 0.99999... */ 
	puts("");

	/* libération mémoire */
	glp_delete_prob(prob); 

	/* J'adore qu'un plan se déroule sans accroc! */
	return 0;
}