Exemple #1
0
//==========================================================================
// Restore bounds and type of the first stage variables
int
DDSIP_RestoreBoundAndType (void)
{
    int status = 0;

    if ((DDSIP_bb->DDSIP_step == neobj || DDSIP_bb->DDSIP_step == eev) && DDSIP_param->riskvar)
        DDSIP_UndeleteRiskObj ();

    // Restore original bounds
    status = CPXchgbds (DDSIP_env, DDSIP_lp, DDSIP_bb->firstvar, DDSIP_bb->firstindex, DDSIP_bb->lbident, DDSIP_bb->lborg);
    if (status)
    {
        fprintf (stderr, "ERROR: Failed to change lower bounds \n");
        return status;
    }

    status = CPXchgbds (DDSIP_env, DDSIP_lp, DDSIP_bb->firstvar, DDSIP_bb->firstindex, DDSIP_bb->ubident, DDSIP_bb->uborg);
    if (status)
    {
        fprintf (stderr, "ERROR: Failed to change upper bounds \n");
        return status;
    }
    // probtype=0 (LP)
    if (!CPXgetprobtype (DDSIP_env, DDSIP_lp))
    {
        status = CPXchgprobtype (DDSIP_env, DDSIP_lp, CPXPROB_MILP);
        if (status)
        {
            fprintf (stderr, "ERROR: Failed to change problem type (Restore) \n");
            return status;
        }
        status = CPXchgctype (DDSIP_env, DDSIP_lp, DDSIP_bb->secvar, DDSIP_bb->secondindex, DDSIP_bb->sectype);
        if (status)
        {
            fprintf (stderr, "ERROR: Failed to change types of second-stage variables (Restore) \n");
            return status;
        }
    }
    //Restore ctypes if solving was processed with relaxed first stage
    if (DDSIP_bb->DDSIP_step == solve && DDSIP_param->relax == 1)
    {
        status = CPXchgctype (DDSIP_env, DDSIP_lp, DDSIP_bb->firstvar, DDSIP_bb->firstindex, DDSIP_bb->firsttype);
        if (status)
        {
            fprintf (stderr, "ERROR: Failed to change types of first stage variables (Restore) \n");
            return status;
        }
    }
    return status;
}
Exemple #2
0
long GenModelCplex::AddCol(int* newi, double* newcol, int nz, double obj, double lb, double ub, const char* name, char type)
{
    if(!bcreated)
        return ThrowError("AddCol() not available : Problem not created yet");
    CplexData* d = (CplexData*)solverdata;
    int cmatbeg = 0;

    double clb = lb;
    if(clb == numeric_limits<double>::infinity())
        clb = CPX_INFBOUND;
    else if(clb == -numeric_limits<double>::infinity())
        clb = -CPX_INFBOUND;

    double cub = ub;
        if(cub == numeric_limits<double>::infinity())
            cub = CPX_INFBOUND;
        else if(cub == -numeric_limits<double>::infinity())
            cub = -CPX_INFBOUND;

    CPXaddcols(d->env, d->lp, 1, nz, &obj, &cmatbeg, newi, newcol, &clb, &cub, (char**)(&name));
    if(type != 'C')
    {
        int cind = d->nc;
        CPXchgctype(d->env, d->lp, 1, &cind, &type);
    }
    d->nc++;

    return 0;
}
void PartitionedColoringModel::changeToBinaryVariables() {
	// Cambiamos el tipo de variables a binarias.
	int *indices = new int[this->amountOfVariables];
	char *types = new char[this->amountOfVariables];
	char *setLower = new char[this->amountOfVariables];
	char *setUpper = new char[this->amountOfVariables];
	double *lowerBound = new double[this->amountOfVariables];
	double *upperBound = new double[this->amountOfVariables];

	int i;
	for(i = 0; i < this->amountOfVariables; i++) {
		indices[i] = i;
		types[i] = 'B';
		setLower[i] = 'L';
		setUpper[i] = 'U';
		lowerBound[i] = 0;
		upperBound[i] = 1;
	}
	
	CPXchgctype(this->cplexEnvironment,
				this->linearProblem,
				this->amountOfVariables,
				indices,
				types);
					
	CPXchgbds(this->cplexEnvironment,
			  this->linearProblem,
			  this->amountOfVariables,
			  indices, setLower, lowerBound);
				  
	CPXchgbds(this->cplexEnvironment,
			  this->linearProblem,
			  this->amountOfVariables,
			  indices, setUpper, upperBound);
				  
	delete [] indices;
	delete [] types;
	delete [] setLower;
	delete [] setUpper;
	delete [] lowerBound;
	delete [] upperBound;
}
Exemple #4
0
long GenModelCplex::SwitchToLp()
{
    if(!bcreated)
        return ThrowError("SwitchToLp() not available : Problem not created yet");
    vector<int> ind;
    vector<char> type;
    for(int i = 0; i < int(vars.type.size()); i++)
    {
        if(vars.type[i] == 'B' || vars.type[i] == 'I' || vars.type[i] == 'S' || vars.type[i] == 'N')
        {
            ind.push_back(i);
            type.push_back('C');
        }
    }
    CplexData* d = static_cast<CplexData*>(solverdata);
    CPXchgctype(d->env, d->lp, int(ind.size()), &(ind[0]), &(type[0]));
    boolParam["mip"] = false;
    
    return 0;
}
Exemple #5
0
void CplexSolver::add(ColumnBuffer & buffer) {
	buffer.add_last_begin();
	if (buffer.name().empty()) {
		CPXaddcols(_env, _prob, buffer.size(), buffer.nz(), buffer.rhsObj(), buffer.begin(), buffer.index(), buffer.value(), buffer.lower(), buffer.upper(), NULL);
	} else {
		assert((int )buffer.name().size() == buffer.size() && "you should provide a name for each element");

		std::vector<char*> cpxName(buffer.name().size());
		for (int i(0); i < buffer.name().size(); ++i) {
			cpxName[i] = const_cast<char*>(buffer.name()[i].c_str());
		}
		CPXaddcols(_env, _prob, buffer.size(), buffer.nz(), buffer.rhsObj(), buffer.begin(), buffer.index(), buffer.value(), buffer.lower(), buffer.upper(), cpxName.data());
	}
	buffer.rem_last_begin();
	if (!buffer.only_continous()) {
		_is_mip = true;
		std::vector<int> sequence(buffer.size());
		for (int i(0); i < buffer.size(); ++i) {
			sequence[i] = CPXgetnumcols(_env, _prob) - buffer.size() + i;
		}
		CPXchgctype(_env, _prob, buffer.size(), sequence.data(), buffer.type());
	}
}
Exemple #6
0
static int
buildnetwork (CPXENVptr env, CPXLPptr lp)
{
   char     sense[NUMNODES];
   double   rhs[NUMNODES];
   int      ind[2];
   double   val[2];
   char     *name[1];
   char     buffer[100];

   int      i, j, varindex;
   int      zero = 0;
   double   dblzero = 0.0;
   double   dblone  = 1.0;
   char     binary = 'B';
   int      status = 0;

   /* Create constraint placeholders ---
        One constraint for each node (flow constraints)
   */

   for (i = 0; i < NUMNODES; i++) {
      rhs[i]   = demand[i];
      sense[i] = 'G';
   }
   status = CPXnewrows (env, lp, NUMNODES, rhs, sense, NULL, NULL);
   if ( status ) {
      fprintf (stderr, "Could not create new rows.\n");
      goto TERMINATE;
   }

   /* Add flow variables */

   for (j = 0; j < NUMEDGES; j++) {
      ind[0] = orig[j];  /* Flow leaves origin */
      val[0] = -1.0;
      ind[1] = dest[j];  /* Flow arrives at destination */
      val[1] =  1.0;

      name[0] = buffer;
      sprintf(buffer, "x%d%d", orig[j], dest[j]);

      status = CPXaddcols (env, lp, 1, 2, &unitcost[j], &zero, ind, val,
                           NULL, NULL, name);
      if ( status ) {
         fprintf (stderr, "Failed to add flow edge.\n");
         goto TERMINATE;
      }
   }

   /* Add fixed charge variables */
   for (j = 0; j < NUMEDGES; j++) {
      name[0] = buffer;
      sprintf(buffer, "f%d%d", orig[j], dest[j]);

      status = CPXaddcols (env, lp, 1, 0, &fixedcost[j], &zero, ind, val,
                           &dblzero, &dblone, name);
      if ( status ) {
         fprintf (stderr, "Failed to add fixed charge variable.\n");
         goto TERMINATE;
      }

      varindex = NUMEDGES+j;
      status = CPXchgctype (env, lp, 1, &varindex, &binary);
      if ( status ) {
         fprintf (stderr, "Failed to change variable type.\n");
         goto TERMINATE;
      }
      
   }

   /* Add indicator constraints --
        f = 0 -> x <= 0 */

   for (j = 0; j < NUMEDGES; j++) {
      varindex = j;
      sprintf(buffer, "indicator%d", j);

      status = CPXaddindconstr (env, lp, NUMEDGES+j, 1, 1, 
                                0.0, 'L', &varindex, &dblone, buffer);
      if ( status ) {
         fprintf (stderr, "Failed to add indicator constraint.");
         goto TERMINATE;
      }
   }
   
TERMINATE:

   return (status);

}  /* END buildnetwork */
/* This routine initializes the cplex enviorement, sets screen as an output for cplex errors and notifications, 
   and sets parameters for cplex. It calls for a mixed integer program solution and frees the environment.
   To Do:
   Declare the parameters for the problem and fill them accordingly. After creating the program thus, copy it into cplex. 
   Define any integer or binary variables as needed, and change their type before the call to CPXmipopt to solve problem. 
   Use CPXwriteprob to output the problem in lp format, in the name of cluster_editing.lp.
   Read solution (both objective function value and variables assignment). 
   Communicate to pass the problem and the solution between the modules in the best way you see. 
 */
int cluster()
{
	/* Declare and allocate space for the variables and arrays where we
      will store the optimization results including the status, objective
      value and variable values. */

	CPXENVptr p_env              = NULL;
	CPXLPptr  p_lp               = NULL;
	int       status;

	/* Initialize the CPLEX environment */
	p_env = CPXopenCPLEX (&status);

	/* If an error occurs, the status value indicates the reason for
      failure.  A call to CPXgeterrorstring will produce the text of
      the error message. Note that CPXopenCPLEX produces no output,
      so the only way to see the cause of the error is to use
      CPXgeterrorstring. For other CPLEX routines, the errors will
      be seen if the CPX_PARAM_SCRIND indicator is set to CPX_ON.  */

	if ( p_env == NULL ) {
		char  errmsg[1024];
		fprintf (stderr, "Error: Could not open CPLEX environment.\n");
		CPXgeterrorstring (p_env, status, errmsg);
		fprintf (stderr, "%s", errmsg);
		goto TERMINATE;
	}

	/* Turn on output to the screen */
	status = CPXsetintparam (p_env, CPX_PARAM_SCRIND, CPX_ON);
	if ( status ) {
		fprintf (stderr,
				"Error: Failure to turn on screen indicator, error %d.\n", status);
		goto TERMINATE;
	}

	/* Create the problem. */
	p_lp = CPXcreateprob (p_env, &status, probname);

	/* A returned pointer of NULL may mean that not enough memory
      was available or there was some other problem.  In the case of 
      failure, an error message will have been written to the error 
      channel from inside CPLEX. The setting of
      the parameter CPX_PARAM_SCRIND causes the error message to
      appear on stdout.  */

	if ( p_lp == NULL ) {
		fprintf (stderr, "Error: Failed to create problem.\n");
		goto TERMINATE;
	}

	/* Use CPXcopylp to transfer the ILP part of the problem data into the cplex pointer lp */
	CPXcopylp (p_env, p_lp, numcols, numrows, objsen, obj, rhs, sense, matbeg, matcnt, matind, matval, lb, ub, 0);
	CPXchgctype(p_env, p_lp, cnt, indices, ctype);

	/* Optimize the problem. */
	status = CPXmipopt (p_env, p_lp);
	if ( status ) {
		fprintf (stderr, "Error: Failed to optimize problem.\n");
		goto TERMINATE;
	}

	status = CPXsolution(p_env, p_lp, &solstat, &objval, x, NULL, NULL, NULL);
	if ( status ) {
			fprintf (stderr, "Error: Failed to get solution variables.\n");
			goto TERMINATE;
		}

	/* Write a copy of the problem to a file.
      Please put into probname the following string: Output Directory + "clustering_solution.lp" to create clustering_solution.lp in your output directory */
	status = CPXwriteprob (p_env, p_lp, probname, NULL);
	if ( status ) {
		fprintf (stderr, "Error: Failed to write LP to disk.\n");
		goto TERMINATE;
	}


	TERMINATE:

	/* Free up the problem as allocated by CPXcreateprob, if necessary */
	if ( p_lp != NULL ) {
		status = CPXfreeprob (p_env, &p_lp);
		if ( status ) {
			fprintf (stderr, "Error: CPXfreeprob failed, error code %d.\n", status);
		}
	}

	/* Free up the CPLEX environment, if necessary */
	if ( p_env != NULL ) {
		status = CPXcloseCPLEX (&p_env);

		/* Note that CPXcloseCPLEX produces no output,
         so the only way to see the cause of the error is to use
         CPXgeterrorstring.  For other CPLEX routines, the errors will
         be seen if the CPX_PARAM_SCRIND indicator is set to CPX_ON. */

		if ( status ) {
			char  errmsg[1024];
			fprintf (stderr, "Could not close CPLEX environment.\n");
			CPXgeterrorstring (p_env, status, errmsg);
			fprintf (stderr, "%s", errmsg);
		}
	}
	return (status);
}