本文整理汇总了C++中OsiSolverInterface::setObjSense方法的典型用法代码示例。如果您正苦于以下问题:C++ OsiSolverInterface::setObjSense方法的具体用法?C++ OsiSolverInterface::setObjSense怎么用?C++ OsiSolverInterface::setObjSense使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OsiSolverInterface
的用法示例。
在下文中一共展示了OsiSolverInterface::setObjSense方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: objSense
//#############################################################################
void
MibSHeuristic::lowerObjHeuristic()
{
/*
optimize wrt to lower-level objective
over current feasible lp feasible region
*/
MibSModel * model = MibSModel_;
OsiSolverInterface * oSolver = model->getSolver();
//OsiSolverInterface * hSolver = new OsiCbcSolverInterface();
OsiSolverInterface* hSolver = new OsiSymSolverInterface();
double objSense(model->getLowerObjSense());
int lCols(model->getLowerDim());
int uCols(model->getUpperDim());
int * lColIndices = model->getLowerColInd();
int * uColIndices = model->getUpperColInd();
double * lObjCoeffs = model->getLowerObjCoeffs();
//int tCols(lCols + uCols);
int tCols(oSolver->getNumCols());
//assert(tCols == oSolver->getNumCols());
hSolver->loadProblem(*oSolver->getMatrixByCol(),
oSolver->getColLower(), oSolver->getColUpper(),
oSolver->getObjCoefficients(),
oSolver->getRowLower(), oSolver->getRowUpper());
int j(0);
for(j = 0; j < tCols; j++){
if(oSolver->isInteger(j))
hSolver->setInteger(j);
}
double * nObjCoeffs = new double[tCols];
int i(0), index(0);
CoinZeroN(nObjCoeffs, tCols);
for(i = 0; i < lCols; i++){
index = lColIndices[i];
nObjCoeffs[index] = lObjCoeffs[i];
}
//MibS objective sense is the opposite of OSI's!
hSolver->setObjSense(objSense);
hSolver->setObjective(nObjCoeffs);
//double cutoff(model->getCutoff());
double cutoff(model->getKnowledgeBroker()->getIncumbentValue());
if(model->getNumSolutions()){
CoinPackedVector objCon;
//double rhs(cutoff * objSense);
//double smlTol(1.0);
double rhs(cutoff);
for(i = 0; i < tCols; i++){
objCon.insert(i, oSolver->getObjCoefficients()[i]
* oSolver->getObjSense());
}
hSolver->addRow(objCon, - hSolver->getInfinity(), rhs);
}
if(0)
hSolver->writeLp("lobjheurstic");
if(0){
dynamic_cast<OsiCbcSolverInterface *>
(hSolver)->getModelPtr()->messageHandler()->setLogLevel(0);
}
else{
dynamic_cast<OsiSymSolverInterface *>
(hSolver)->setSymParam("prep_level", -1);
dynamic_cast<OsiSymSolverInterface *>
(hSolver)->setSymParam("verbosity", -2);
dynamic_cast<OsiSymSolverInterface *>
(hSolver)->setSymParam("max_active_nodes", 1);
}
hSolver->branchAndBound();
if(hSolver->isProvenOptimal()){
double upperObjVal(0.0);
/*****************NEW ******************/
MibSSolution *mibSol = NULL;
OsiSolverInterface * lSolver = model->bS_->setUpModel(hSolver, true);
//.........这里部分代码省略.........
示例2: if
//.........这里部分代码省略.........
}
if (rowSense[irow] == 'E' && (lhs - rhs[irow] > 1e-6 || lhs - rhs[irow] < -1e-6)) {
feasibility = 0;
break;
}
}
//if feasible, find the objective value and set the cutoff
// for the smallBB and add a new constraint to the LP
// (and update the best solution found so far for the
// return arguments)
if (feasibility) {
double objectiveValue = 0;
for (int j = 0; j < numCols; j++)
objectiveValue += solutions.solution[k][j] * originalObjCoeff[j];
cutoff = objectiveValue;
clpSolver->addRow(numCols, addRowIndex, originalObjCoeff, -COIN_DBL_MAX, cutoff);
// Todo: pick up the best solution in the block (not
// the last).
solutionValue = objectiveValue;
for (int m = 0; m < numCols; m++)
betterSolution[m] = solutions.solution[k][m];
numFeasibles++;
}
}
// Go through the block of solution and decide if to call smallBB
for (int k = startIndex; k < solutions.numberSolutions; k++) {
if (solutions.numberUnsatisfied[k] <= fixThreshold) {
// get new copy
OsiSolverInterface * newSolver;
newSolver = new OsiClpSolverInterface(*clpSolver);
newSolver->setObjSense(1);
newSolver->setObjective(originalObjCoeff);
int numberColumns = newSolver->getNumCols();
int numFixed = 0;
// Fix the first fixThreshold number of integer vars
// that are satisfied
for (int iColumn = 0 ; iColumn < numberColumns ; iColumn++) {
if (newSolver->isInteger(iColumn)) {
double value = solutions.solution[k][iColumn];
double intValue = floor(value + 0.5);
if (fabs(value - intValue) < 1.0e-5) {
newSolver->setColLower(iColumn, intValue);
newSolver->setColUpper(iColumn, intValue);
numFixed++;
if (numFixed > numInt - fixThreshold)
break;
}
}
}
COIN_DETAIL_PRINT(printf("numFixed: %d\n", numFixed));
COIN_DETAIL_PRINT(printf("fixThreshold: %f\n", fixThreshold));
COIN_DETAIL_PRINT(printf("numInt: %d\n", numInt));
double *newSolution = new double[numCols];
double newSolutionValue;
// Call smallBB on the modified problem
int returnCode = smallBranchAndBound(newSolver, maxNode, newSolution,
newSolutionValue, cutoff, "mini");
// If smallBB found a solution, update the better
// solution and solutionValue (we gave smallBB our
// cutoff, so it only finds improving solutions)
示例3: generateCuts
//.........这里部分代码省略.........
BRowUppers[BNumRows-1]=beta_;
// Calculate base objective <<x^T,Atilde^T>,u>
// Note: at each iteration coefficient u_0
// changes to <x^T,e_j>
// w=(u,v,beta,v_0,u_0) size 2m+3
// So, BOjective[2m+2]=x[j]
double * BObjective= new double[BNumCols];
double * Atildex = new double[m];
CoinFillN(BObjective,BNumCols,0.0);
Atilde->times(x,Atildex); // Atildex is size m, x is size n
CoinDisjointCopyN(Atildex,m,BObjective);
// Number of cols and size of Elements vector
// in B without the v_0 and u_0 cols
int BFullSizeLessThree = BFullSize-3;
// Load B matrix into a column orders CoinPackedMatrix
CoinPackedMatrix * BMatrix = new CoinPackedMatrix(true, BNumRows,
BNumColsLessTwo,
BFullSizeLessThree,
BElements,BIndices,
BStarts,BLengths);
// Assign problem into a solver interface
// Note: coneSi will cleanup the memory itself
OsiSolverInterface * coneSi = si.clone(false);
coneSi->assignProblem (BMatrix, BColLowers, BColUppers,
BObjective,
BRowLowers, BRowUppers);
// Problem sense should default to "min" by default,
// but just to be virtuous...
coneSi->setObjSense(1.0);
// The plot outline from here on down:
// coneSi has been assigned B without the u_0 and v_0 columns
// Calculate base objective <<x^T,Atilde^T>,u>
// bool haveWarmStart = false;
// For (j=0; j<n, j++)
// if (!isBinary(x_j) || x_j<=0 || x_j>=1) continue;
// // IMPROVEME: if(haveWarmStart) check if j attractive
// add {-e_j,0,-1} matrix column for v_0
// add {e_j,0,0} matrix column for u_0
// objective coefficient for u_0 is x_j
// if (haveWarmStart)
// set warmstart info
// solve min{objw:Bw=0; w>=0,except v_0, u_0 free}
// if (bounded)
// get warmstart info
// haveWarmStart=true;
// ustar = optimal u solution
// ustar_0 = optimal u_0 solution
// alpha^T= <ustar^T,Atilde> -ustar_0e_j^T
// (double check <alpha^T,x> >= beta_ should be violated)
// add <alpha^T,x> >= beta_ to cutset
// endif
// delete column for u_0 // this deletes all column info.
// delete column for v_0
// endFor
// clean up memory
// return 0;
int * nVectorIndices = new int[n];
CoinIotaN(nVectorIndices, n, 0);
示例4: etol
//.........这里部分代码省略.........
index2 = matIndices[j];
//tmp = findIndex(index, lCols, lColIndices);
tmp = binarySearch(0, lCols - 1, index2, lColIndices);
if(tmp > -1)
row.insert(tmp, matElements[j]);
}
newMat->appendRow(row);
}
*/
for(i = 0; i < lRows; i++){
CoinPackedVector row;
index1 = lRowIndices[i];
for(j = 0; j < lCols; j++){
index2 = lColIndices[j];
tmp = matrix->getCoefficient(index1, index2);
row.insert(j, tmp);
}
newMat->appendRow(row);
}
/*
nSolver->assignProblem(newMat, colLb, colUb,
objCoeffs, rowLb, rowUb);
*/
nSolver->loadProblem(*newMat, colLb, colUb,
objCoeffs, rowLb, rowUb);
for(i = 0; i < intCnt; i++){
nSolver->setInteger(integerVars[i]);
}
//nSolver->setInteger(integerVars, intCnt);
nSolver->setObjSense(objSense); //1 min; -1 max
nSolver->setHintParam(OsiDoReducePrint, true, OsiHintDo);
#if 0
if(0){
dynamic_cast<OsiCbcSolverInterface *>
(nSolver)->getModelPtr()->messageHandler()->setLogLevel(0);
}
else{
dynamic_cast<OsiSymSolverInterface *>
(nSolver)->setSymParam("prep_level", -1);
dynamic_cast<OsiSymSolverInterface *>
(nSolver)->setSymParam("verbosity", -2);
dynamic_cast<OsiSymSolverInterface *>
(nSolver)->setSymParam("max_active_nodes", 1);
}
#endif
delete [] integerVars;
}else{
nSolver = solver_;
}
#define SYM_VERSION_IS_WS strcmp(SYMPHONY_VERSION, "WS")
#if SYMPHONY_VERSION_IS_WS
if (feasCheckSolver == "SYMPHONY" && probType == 1 && warmStartLL &&
!newOsi && doDualFixing){ //Interdiction
/** Get upper bound from best known (feasible) lower level solution and try
示例5: main
int main(int argc, char **argv)
{
VRPH_version();
int i, j, k, n, status, num_attempts, *sol_buff, *IP_sol_buff;
char in_file[200];
double lambda, best_heur_sol=VRP_INFINITY;
bool first_sol=false, bootstrap=false;;
VRPSolution *fresh_solution;
OsiSolverInterface *si;
const double *x;
int last_num_cols=0, route_id=0;
time_t start, stop;
int *orderings[MAX_ROUTES];
for(i=0;i<MAX_ROUTES;i++)
orderings[i]=NULL;
// Set timing counters to 0
heur_time=mip_time=0;
// Check arguments
if(argc<5)
{
fprintf(stderr,"Usage: %s -f input_file -n num_runs [-v,-b,-c max_columns -d cols_to_delete]\n",
argv[0]);
fprintf(stderr,"\t Will solve the problem num_solutions times and add the routes\n");
fprintf(stderr,"\t to a set partitioning problem.\n");
fprintf(stderr,"\t Other options:\n");
fprintf(stderr,"\t -v runs in verbose mode\n");
fprintf(stderr,"\t -b will use bootstrapping where we send the set partitioning\n"
"\t solution back to the metaheuristic solver\n");
fprintf(stderr,"\t -c max_columns will allow this many active columns/variables in the IP.\n");
fprintf(stderr,"\t Default value is max_columns=500\n");
fprintf(stderr,"\t -d num_cols_to_delete will delete this many columns once we have too many\n");
fprintf(stderr,"\t in the IP. Default value is num_cols_to_delete=100\n");
exit(-1);
}
// Set defaults
verbose=false;
max_columns=500;
num_cols_to_delete=100;
// Parse command line
for(i=0;i<argc;i++)
{
if(strcmp(argv[i],"-f")==0)
strcpy(in_file,argv[i+1]);
if(strcmp(argv[i],"-n")==0)
num_attempts=atoi(argv[i+1]);
if(strcmp(argv[i],"-v")==0)
verbose=true;
if(strcmp(argv[i],"-b")==0)
bootstrap=true;
if(strcmp(argv[i],"-c")==0)
max_columns=atoi(argv[i+1]);
if(strcmp(argv[i],"-d")==0)
num_cols_to_delete=atoi(argv[i+1]);
}
// This is the # of non-VRPH_DEPOT nodes
n=VRPGetDimension(in_file);
// This will be used to import/export solutions
fresh_solution = new VRPSolution(n);
// Create buffers for importing solutions
sol_buff= new int[n+2];
IP_sol_buff = new int[n+2];
// Declare an OSI interface
si=new OsiGlpkSolverInterface;
si->setIntParam(OsiNameDiscipline,2);
for(i=0;i<n;i++)
{
si->addRow(0,NULL,NULL,1,1);
}
// Declare a VRP of the right size and import the file
VRP V(n);
ClarkeWright CW(n);
VRPRoute route(n);
V.read_TSPLIB_file(in_file);
// Set up a "route warehouse" to store the routes to be added to the IP
V.route_wh=new VRPRouteWarehouse(HASH_TABLE_SIZE);
// Set up a minimization problem
si->setObjSense(1);
// Set to error only output
si->setHintParam(OsiDoReducePrint,true, OsiHintDo);
// Unfortunately GLPK still prints out something regarding the conflict graph
for(i=0;i<num_attempts;i++)
{
if(i==0 || !bootstrap)
{
lambda=.5+1.5*lcgrand(0);
// Start with a clean VRP object
V.reset();
CW.Construct(&V, lambda, false);
//.........这里部分代码省略.........
示例6: if
//.........这里部分代码省略.........
if (nFix) {
newSolver->setWarmStart(&saveBasis);
newSolver->setColSolution(solution);
newSolver->initialSolve();
if (newSolver->isProvenOptimal()) {
double solValue = newSolver->getObjValue() * direction ;
if (solValue < cutoff) {
// try branch and bound
double * newSolution = new double [numberColumns];
COIN_DETAIL_PRINT(printf("%d fixed after fixing costs\n", nFix));
int returnCode = smallBranchAndBound(newSolver,
numberNodes_, newSolution,
solutionValue,
solutionValue, "CbcHeuristicNaive1");
if (returnCode < 0)
returnCode = 0; // returned on size
if ((returnCode&2) != 0) {
// could add cut
returnCode &= ~2;
}
if (returnCode == 1) {
// solution
solutionFound = true;
memcpy(betterSolution, newSolution,
numberColumns*sizeof(double));
COIN_DETAIL_PRINT(printf("Naive fixing zeros gave solution of %g\n", solutionValue));
cutoff = solutionValue - model_->getCutoffIncrement();
}
delete [] newSolution;
}
}
}
#if 1
newSolver->setObjSense(-direction); // maximize
newSolver->setWarmStart(&saveBasis);
newSolver->setColSolution(solution);
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
double value = solution[iColumn];
double lower = colLower[iColumn];
double upper = colUpper[iColumn];
double newLower;
double newUpper;
if (newSolver->isInteger(iColumn)) {
newLower = CoinMax(lower, floor(value) - 2.0);
newUpper = CoinMin(upper, ceil(value) + 2.0);
} else {
newLower = CoinMax(lower, value - 1.0e5);
newUpper = CoinMin(upper, value + 1.0e-5);
}
newSolver->setColLower(iColumn, newLower);
newSolver->setColUpper(iColumn, newUpper);
}
newSolver->initialSolve();
if (newSolver->isProvenOptimal()) {
double solValue = newSolver->getObjValue() * direction ;
if (solValue < cutoff) {
nFix = 0;
newSolver->setObjSense(direction); // correct direction
//const double * thisSolution = newSolver->getColSolution();
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
double value = solution[iColumn];
double lower = colLower[iColumn];
double upper = colUpper[iColumn];
double newLower = lower;
double newUpper = upper;
if (newSolver->isInteger(iColumn)) {