Exemple #1
0
int
main(int   argc,
     char *argv[])
{
  GRBenv   *masterenv = NULL;
  GRBmodel *model     = NULL;
  GRBenv   *modelenv  = NULL;
  int       error     = 0;
  int       optimstatus;
  double    objval;

  if (argc < 2) {
    fprintf(stderr, "Usage: lp_c filename\n");
    exit(1);
  }

  /* Create environment */

  error = GRBloadenv(&masterenv, "lp.log");
  if (error) goto QUIT;

  /* Read model from file */

  error = GRBreadmodel(masterenv, argv[1], &model);
  if (error) goto QUIT;

  /* Solve model */

  error = GRBoptimize(model);
  if (error) goto QUIT;

  /* Capture solution information */

  error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
  if (error) goto QUIT;

  /* If model is infeasible or unbounded, turn off presolve and resolve */

  if (optimstatus == GRB_INF_OR_UNBD) {
    modelenv = GRBgetenv(model);
    if (!modelenv) {
      fprintf(stderr, "Error: could not get model environment\n");
      goto QUIT;
    }

    /* Change parameter on model environment.  The model now has
       a copy of the master environment, so changing the master will
       no longer affect the model.  */

    error = GRBsetintparam(modelenv, "PRESOLVE", 0);
    if (error) goto QUIT;

    error = GRBoptimize(model);
    if (error) goto QUIT;

    error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
    if (error) goto QUIT;
  }

  if (optimstatus == GRB_OPTIMAL) {
    error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
    if (error) goto QUIT;
    printf("Optimal objective: %.4e\n\n", objval);
  } else if (optimstatus == GRB_INFEASIBLE) {
    printf("Model is infeasible\n\n");

    error = GRBcomputeIIS(model);
    if (error) goto QUIT;

    error = GRBwrite(model, "model.ilp");
    if (error) goto QUIT;
  } else if (optimstatus == GRB_UNBOUNDED) {
    printf("Model is unbounded\n\n");
  } else {
    printf("Optimization was stopped with status = %d\n\n", optimstatus);
  }

QUIT:

  /* Error reporting */

  if (error) {
    printf("ERROR: %s\n", GRBgeterrormsg(masterenv));
    exit(1);
  }

  /* Free model */

  GRBfreemodel(model);

  /* Free environment */

  GRBfreeenv(masterenv);

  return 0;
}
Exemple #2
0
int
main(int   argc,
     char *argv[])
{
  GRBenv   *env   = NULL, *modelenv = NULL;
  GRBmodel *model = NULL;
  int       error = 0;
  int       j, numfractional, iter, nfix;
  int       numintvars;
  int      *intvars = NULL;
  int       status;
  char      vtype, *vname;
  double    sol, obj, fixval;
  var_t    *fractional = NULL;

  if (argc < 2)
  {
    fprintf(stderr, "Usage: fixanddive_c filename\n");
    exit(1);
  }

  error = GRBloadenv(&env, "fixanddive.log");
  if (error || env == NULL)
  {
    fprintf(stderr, "Error: could not create environment\n");
    exit(1);
  }

  /* Read model */
  error = GRBreadmodel(env, argv[1], &model);
  if (error) goto QUIT;

  /* Collect integer variables and relax them */
  error = GRBgetintattr(model, "NumIntVars", &numintvars);
  if (error) goto QUIT;
  intvars = malloc(sizeof(int) * numintvars);
  if (!intvars) goto QUIT;
  fractional = malloc(sizeof(var_t) * numintvars);
  if (!fractional) goto QUIT;
  numfractional = 0;
  for (j = 0; j < numintvars; ++j)
  {
    error = GRBgetcharattrelement(model, "VType", j, &vtype);
    if (error) goto QUIT;
    if (vtype != GRB_CONTINUOUS)
    {
      intvars[numfractional++] = j;
      error = GRBsetcharattrelement(model, "VType", j, GRB_CONTINUOUS);
      if (error) goto QUIT;
    }
  }

  modelenv = GRBgetenv(model);
  if (!modelenv) goto QUIT;
  error = GRBsetintparam(modelenv, "OutputFlag", 0);
  if (error) goto QUIT;
  error = GRBoptimize(model);
  if (error) goto QUIT;

  /* Perform multiple iterations. In each iteration, identify the first
     quartile of integer variables that are closest to an integer value
     in the relaxation, fix them to the nearest integer, and repeat. */

  for (iter = 0; iter < 1000; ++iter)
  {

    /* create a list of fractional variables, sorted in order of
       increasing distance from the relaxation solution to the nearest
       integer value */

    numfractional = 0;
    for (j = 0; j < numintvars; ++j)
    {
      error = GRBgetdblattrelement(model, "X", intvars[j], &sol);
      if (error) goto QUIT;
      if (fabs(sol - floor(sol + 0.5)) > 1e-5)
      {
        fractional[numfractional].index = intvars[j];
        fractional[numfractional++].X = sol;
      }
    }

    error = GRBgetdblattr(model, "ObjVal", &obj);
    if (error) goto QUIT;
    printf("Iteration %i, obj %f, fractional %i\n",
           iter, obj, numfractional);

    if (numfractional == 0)
    {
      printf("Found feasible solution - objective %f\n", obj);
      break;
    }

    /* Fix the first quartile to the nearest integer value */
    qsort(fractional, numfractional, sizeof(var_t), vcomp);
    nfix = numfractional / 4;
    nfix = (nfix > 1) ? nfix : 1;
    for (j = 0; j < nfix; ++j)
    {
      fixval = floor(fractional[j].X + 0.5);
      error = GRBsetdblattrelement(model, "LB", fractional[j].index, fixval);
      if (error) goto QUIT;
      error = GRBsetdblattrelement(model, "UB", fractional[j].index, fixval);
      if (error) goto QUIT;
      error = GRBgetstrattrelement(model, "VarName",
                                   fractional[j].index, &vname);
      printf("  Fix %s to %f ( rel %f )\n", vname, fixval, fractional[j].X);
    }

    error = GRBoptimize(model);
    if (error) goto QUIT;

    /* Check optimization result */

    error = GRBgetintattr(model, "Status", &status);
    if (error) goto QUIT;
    if (status != GRB_OPTIMAL)
    {
      printf("Relaxation is infeasible\n");
      break;
    }
  }


QUIT:

  /* Error reporting */

  if (error)
  {
    printf("ERROR: %s\n", GRBgeterrormsg(env));
    exit(1);
  }

  /* Free data */

  free(intvars);
  free(fractional);

  /* Free model */

  GRBfreemodel(model);

  /* Free environment */

  GRBfreeenv(env);

  return 0;
}
Exemple #3
0
int
main(int   argc,
     char *argv[])
{
  GRBenv   *env   = NULL;
  GRBmodel *model = NULL;
  int       error = 0;
  int       vars, optimstatus;
  double    objval;
  struct callback_data mydata;

  mydata.lastmsg  = -GRB_INFINITY;
  mydata.logfile  = NULL;
  mydata.solution = NULL;

  if (argc < 2) {
    fprintf(stderr, "Usage: callback_c filename\n");
    goto QUIT;
  }

  mydata.logfile = fopen("cb.log", "w");
  if (!mydata.logfile) {
    fprintf(stderr, "Cannot open cb.log for callback message\n");
    goto QUIT;
  }

  /* Create environment */

  error = GRBloadenv(&env, NULL);
  if (error || env == NULL) {
    fprintf(stderr, "Error: could not create environment\n");
    exit(1);
  }

  /* Turn off display */

  error = GRBsetintparam(env, GRB_INT_PAR_OUTPUTFLAG, 0);
  if (error) goto QUIT;

  /* Read model from file */

  error = GRBreadmodel(env, argv[1], &model);
  if (error) goto QUIT;

  /* Allocate space for solution */

  error = GRBgetintattr(model, GRB_INT_ATTR_NUMVARS, &vars);
  if (error) goto QUIT;

  mydata.solution = malloc(vars*sizeof(double));
  if (mydata.solution == NULL) {
    fprintf(stderr, "Failed to allocate memory\n");
    exit(1);
  }

  /* Set callback function */

  error = GRBsetcallbackfunc(model, mycallback, (void *) &mydata);
  if (error) goto QUIT;

  /* Solve model */

  error = GRBoptimize(model);
  if (error) goto QUIT;

  /* Capture solution information */

  error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
  if (error) goto QUIT;

  error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
  if (error) goto QUIT;

  printf("\nOptimization complete\n");
  if (optimstatus == GRB_OPTIMAL)
    printf("Optimal objective: %.4e\n", objval);
  else if (optimstatus == GRB_INF_OR_UNBD)
    printf("Model is infeasible or unbounded\n");
  else
    printf("Optimization was stopped early\n");
  printf("\n");

QUIT:

  /* Error reporting */

  if (error) {
    printf("ERROR: %s\n", GRBgeterrormsg(env));
    exit(1);
  }

  /* Close file */

  if (mydata.logfile)
    fclose(mydata.logfile);

  /* Free solution */

  if (mydata.solution)
    free(mydata.solution);

  /* Free model */

  GRBfreemodel(model);

  /* Free environment */

  GRBfreeenv(env);

  return 0;
}
Exemple #4
0
int
main(int   argc,
     char *argv[])
{
  GRBenv   *env = NULL, *aenv;
  GRBmodel *a = NULL, *b = NULL;
  int       error = 0;
  int       i, numvars, status;
  char      vtype, *vname;
  double    x, bnd, aobj, bobj, objchg;

  if (argc < 2)
  {
    fprintf(stderr, "Usage: sensitivity_c filename\n");
    exit(1);
  }

  error = GRBloadenv(&env, "sensitivity.log");
  if (error) goto QUIT;

  /* Read model */
  error = GRBreadmodel(env, argv[1], &a);
  if (error) goto QUIT;
  error = GRBoptimize(a);
  if (error) goto QUIT;
  error = GRBgetdblattr(a, "ObjVal", &aobj);
  if (error) goto QUIT;
  aenv = GRBgetenv(a);
  if (!aenv) goto QUIT;
  error = GRBsetintparam(aenv, "OutputFlag", 0);
  if (error) goto QUIT;

  /* Iterate over all variables */
  error = GRBgetintattr(a, "NumVars", &numvars);
  if (error) goto QUIT;
  for (i = 0; i < numvars; ++i)
  {
    error = GRBgetcharattrelement(a, "VType", i, &vtype);
    if (error) goto QUIT;

    if (vtype == GRB_BINARY)
    {

      /* Create clone and fix variable */
      b = GRBcopymodel(a);
      if (!b) goto QUIT;
      error = GRBgetstrattrelement(a, "VarName", i, &vname);
      if (error) goto QUIT;
      error = GRBgetdblattrelement(a, "X", i, &x);
      if (error) goto QUIT;
      error = GRBgetdblattrelement(a, "LB", i, &bnd);
      if (error) goto QUIT;
      if (x - bnd < 0.5)
      {
        error = GRBgetdblattrelement(b, "UB", i, &bnd);
        if (error) goto QUIT;
        error = GRBsetdblattrelement(b, "LB", i, bnd);
        if (error) goto QUIT;
      }
      else
      {
        error = GRBgetdblattrelement(b, "LB", i, &bnd);
        if (error) goto QUIT;
        error = GRBsetdblattrelement(b, "UB", i, bnd);
        if (error) goto QUIT;
      }

      error = GRBoptimize(b);
      if (error) goto QUIT;

      error = GRBgetintattr(b, "Status", &status);
      if (error) goto QUIT;
      if (status == GRB_OPTIMAL)
      {
        error = GRBgetdblattr(b, "ObjVal", &bobj);
        if (error) goto QUIT;
        objchg = bobj - aobj;
        if (objchg < 0)
        {
          objchg = 0;
        }
        printf("Objective sensitivity for variable %s is %f\n", vname, objchg);
      }
      else
      {
        printf("Objective sensitivity for variable %s is infinite\n", vname);
      }

      GRBfreemodel(b);
      b = NULL;
    }
  }


QUIT:

  /* Error reporting */

  if (error)
  {
    printf("ERROR: %s\n", GRBgeterrormsg(env));
    exit(1);
  }

  /* Free models */

  GRBfreemodel(a);
  GRBfreemodel(b);

  /* Free environment */

  GRBfreeenv(env);

  return 0;
}
Exemple #5
0
double solve_glp_grb(glp_prob *mip, wrapper_params *par){


	GLPK_out = par->glp_out;
	GRB_out = par->grb_out;
	double obj_val;



	/** GLPK: Generate Variable indexing **/
	glp_create_index(mip);

	/** GLPK: Generate LP **/
	glp_write_mps(mip, GLP_MPS_FILE, NULL, "tmp.mps");


	/************/
	/** GUROBI **/
	/************/

	retGRB = GRBloadenv(&env, NULL);
	if (retGRB || env == NULL)
	{
		fprintf(stderr, "Error: could not create environment\n");
		exit(1);
	}

	retGRB = GRBsetintparam(env, "OutputFlag", GRB_out?1:0);
	if (retGRB) freeMem();

	//retGRB = GRBsetintparam(env, "Sensitivity", 1);
	//if (retGRB) freeMem();

	/** GUROBI: Read model **/
	retGRB = GRBreadmodel(env, "tmp.mps", &model);
	if (retGRB) freeMem();

	/** Remove utility files from disk **/
	//remove("tmp.mps");

	/** GUROBI: Get environment **/
	mipenv = GRBgetenv(model);
	if (!mipenv) freeMem();

	/** GUROBI: Set parameters **/

	/** GUROBI: Ask for more precision **/
	retGRB = GRBsetdblparam(mipenv, "FeasibilityTol", 10E-6);
	if (retGRB) freeMem();
	retGRB = GRBsetdblparam(mipenv, "IntFeasTol", 10E-5);
	if (retGRB) freeMem();
	retGRB = GRBsetdblparam(mipenv, "MIPgap", 10E-6);
	if (retGRB) freeMem();

	/* * Playing with gurobi parameters and attr*/

	//gurobi_set_basis();
	retGRB = GRBsetintparam(mipenv, "Cuts", 3);
	if (retGRB) freeMem();

	retGRB = GRBsetintparam(mipenv, "RootMethod", 1);
	if (retGRB) freeMem();

	retGRB = GRBsetintparam(mipenv, "Symmetry", -1);
	if (retGRB) freeMem();

	

	/** GUROBI: get numvars and numrows **/
	retGRB = GRBgetintattr(model, "NumVars", &numvars);
	if (retGRB) freeMem();


	/** Test variable names */
	for(int j=0;j<numvars;j++){	
		retGRB = GRBgetstrattrelement(model, "VarName", j, &nameGRB);
		printf("GRB Var %d Name %s\n",j,nameGRB); 
	}
	/** GUROBI: get model type **/
	retGRB = GRBgetintattr(model, "IsMIP", &GRB_IsMIP);
	if (retGRB) freeMem();

	/** GUROBI: Optimize model **/
	retGRB = GRBoptimize(model);
	if (retGRB) freeMem();

	
	
	/** GUROBI: Retreive the optimization status **/
	GRBgetintattr(model, "Status", &retGRB);
	switch(retGRB){
	case GRB_OPTIMAL:
		break;
	case GRB_INFEASIBLE :
		fprintf(stderr, "Error GRB optimization failed with code GRB_INFEASIBLE\n");
	case GRB_INF_OR_UNBD :
		fprintf(stderr, "Error GRB optimization failed with code GRB_INF_OR_UNBD \n");
	case GRB_UNBOUNDED :
		fprintf(stderr, "Error GRB optimization failed with code GRB_UNBOUNDED \n");
	case GRB_CUTOFF :
		fprintf(stderr, "Error GRB optimization failed with code GRB_CUTOFF \n");
	case GRB_ITERATION_LIMIT :
		fprintf(stderr, "Error GRB optimization failed with code GRB_ITERATION_LIMIT \n");
	case GRB_NODE_LIMIT :
		fprintf(stderr, "Error GRB optimization failed with code GRB_NODE_LIMIT \n");
	case GRB_TIME_LIMIT :
		fprintf(stderr, "Error GRB optimization failed with code GRB_TIME_LIMIT \n");
	case GRB_SOLUTION_LIMIT :
		fprintf(stderr, "Error GRB optimization failed with code GRB_SOLUTION_LIMIT \n");
	case GRB_INTERRUPTED :
		fprintf(stderr, "Error GRB optimization failed with code GRB_INTERRUPTED \n");
	case GRB_SUBOPTIMAL :
		fprintf(stderr, "Error GRB optimization failed with code GRB_SUBOPTIMAL \n");
	case GRB_NUMERIC :
		fprintf(stderr, "Error GRB optimization failed with code GRB_NUMERIC \n");

		/** GUROBI: Quit in any case non optimal **/
		freeMem();
	}

	/** GUROBI: Get obj function value **/
	retGRB = GRBgetdblattr(model, "IntVio", &tmp);
	if (retGRB) freeMem();


	retGRB = GRBgetdblattr(model, "ObjBound", &bound);
	if (retGRB) freeMem();

	retGRB = GRBgetdblattr(model, "ObjVal", &tmp);
	if (retGRB) freeMem();

	/* ********************** */

	obj_val = tmp;


	/* ************ */
	if (verbose) printf ("Objective %lf\n", tmp);
	if (verbose) printf ("Best bound %lf\n", bound);
	if (verbose) printf ("Absolute gap %lf\n", fabs(tmp - bound));

	/** GUROBI: Get variable values **/
	for (j = 0; j < numvars; ++j){

		retGRB = GRBgetdblattrelement(model, "X", j, &tmp);
		if (retGRB) freeMem();

		retGRB = GRBgetstrattrelement(model, "VarName", j, &nameGRB);
		printf("GRB Var %d Name %s\n",j,nameGRB); 
		if (retGRB) freeMem();

		retGRB = GRBgetcharattrelement(model, "VType", j, &type);
		if (retGRB) freeMem();

		/** GLPK search variable index by name **/
		col_index = glp_find_col(mip, nameGRB);

		if (col_index != 0){
			/** GLPK set variable bounds **/
			if ((type == 'B') || (type == 'I')){
				if (verbose) printf ("Variable %s is of type %c value %lf fixed to %lf\n", nameGRB, type, tmp, round(tmp));
				glp_set_col_bnds(mip, col_index, GLP_FX, round(tmp), round(tmp));
			}
			else{
				if (verbose) printf ("Variable %s is of type %c value %lf fixed to %lf\n", nameGRB, type, tmp, tmp);
				glp_set_col_bnds(mip, col_index, GLP_FX, tmp, tmp);
			}
		}
	}

	if (GRB_IsMIP){

		/** GLPK initialize parameters **/
		iparm = (glp_iocp*) malloc(sizeof(glp_iocp));
		glp_init_iocp(iparm);
		iparm->presolve = GLP_ON;
		iparm->mip_gap = glpk_iparm_mip_gap;
		iparm->tol_int = glpk_iparm_tol_int;
		iparm->tol_obj = glpk_iparm_tol_obj;

		/** GLPK get the optimal integer solution **/
		ret = glp_intopt(mip, iparm);
		if (ret){
			fprintf(stderr, "glp_intopt, Error on optimizing the model : %d \n", ret);
			freeMem();
		}

		ret = glp_mip_status(mip);
		switch (ret){
		case GLP_OPT:
			break;
		case GLP_FEAS:
			fprintf(stderr, "Error GLPK simplex is not optimal, GLP_FEAS, code %d\n", ret);
			freeMem();
		case GLP_NOFEAS:
			fprintf(stderr, "Error GLPK simplex is not optimal, GLP_NOFEAS, code %d\n", ret);
			freeMem();
		case GLP_UNDEF:
			fprintf(stderr, "Error GLPK simplex is not optimal, GLP_UNDEF, code %d\n", ret);
			freeMem();
		}
	}
	else{

		/*GLPK initialize parameters */
		parm = (glp_smcp*) malloc(sizeof(glp_smcp));
		glp_init_smcp(parm);
		parm->meth = GLP_DUALP;
		parm->tol_bnd = 10E-4;
		parm->tol_dj = 10E-4;

		/* GLPK get the optimal basis */
		//ret = glp_simplex(mip, parm);
		if (ret){
			fprintf(stderr, "glp_simplex, Error on optimizing the model : %d \n", ret);
			freeMem();
		}
		ret = glp_get_status(mip);
		switch (ret){
		case GLP_OPT:
			break;
		case GLP_FEAS:
			fprintf(stderr, "Error GLPK simplex is not optimal, GLP_FEAS, code %d\n", ret);
			freeMem();
		case GLP_INFEAS:
			fprintf(stderr, "Error GLPK simplex is not optimal, GLP_INFEAS, code %d\n", ret);
			freeMem();
		case GLP_NOFEAS:
			fprintf(stderr, "Error GLPK simplex is not optimal, GLP_NOFEAS, code %d\n", ret);
			freeMem();
		case GLP_UNBND:
			fprintf(stderr, "Error GLPK simplex is not optimal, GLP_UNBND, code %d\n", ret);
			freeMem();
		case GLP_UNDEF:
			fprintf(stderr, "Error GLPK simplex is not optimal, GLP_UNDEF, code %d\n", ret);
			freeMem();
		}


	}

	//GRBmodel *fmod = fixed_model(model);
	//gurobi_sens_output(fmod, "/tmp/sens.sol");
        GRBwrite(model, "/tmp/model.sol");






	/** GUROBI: free structures **/
	if (model) GRBfreemodel(model);
	if (env) GRBfreeenv(env);

	return obj_val;
}