Changeset 2586


Ignore:
Timestamp:
Jun 12, 2019 8:44:06 AM (4 months ago)
Author:
unxusr
Message:

functions to work with OsiSolverInterface? objects in CBC C interface (are useful in callbacks)

Location:
trunk/Cbc/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cbc/src/.ycm_extra_conf.py

    r2487 r2586  
    88
    99flags = [
    10 '-Wall',
    11 '-Wextra',
    12 '-Werror',
    13 '-fopenmp',
    14 '-Wno-long-long',
    15 '-Wno-variadic-macros',
    16 '-Wno-unused-variable',
    17 '-I../../CoinUtils/src/',
    18 '-I../../Osi/src/Osi/',
    19 '-I../../Clp/src/',
    20 '-I../../Clp/src/OsiClp/',
    21 '-I../../ThirdParty/',
    22 '-I../../Cgl/src/',
    23 '-I../../Cgl/src/CglPreProcess/',
    24 '-I../../Cgl/src/CglGomory/',
    25 '-I../../Cgl/src/CglProbing/',
    26 '-I../../Cgl/src/CglKnapsackCover/',
    27 '-I../../Cgl/src/CglRedSplit/',
    28 '-I../../Cgl/src/CglRedSplit2/',
    29 '-I../../Cgl/src/CglGMI/',
    30 '-I../../Cgl/src/CglClique/',
    31 '-I../../Cgl/src/CglFlowCover/',
    32 '-I../../Cgl/src/CglMixedIntegerRounding2/',
    33 '-I../../Cgl/src/CglTwomir/',
    34 '-I../../Cgl/src/CglDuplicateRow/',
    35 '-I../../Cgl/src/CglStored/',
    36 '-I../../Cgl/src/CglLandP/',
    37 '-I../../Cgl/src/CglResidualCapacity/',
    38 '-I../../Cgl/src/CglZeroHalf/',
    39 '-std=c++98',
    40 '-x',
    41 'c++'
     10    '-Wall',
     11    '-Wextra',
     12    '-Werror',
     13    '-fopenmp',
     14    '-Wno-long-long',
     15    '-Wno-variadic-macros',
     16    '-Wno-unused-variable',
     17    '-I../../CoinUtils/src/',
     18    '-I../../Osi/src/Osi/',
     19    '-I../../Clp/src/',
     20    '-I../../Clp/src/OsiClp/',
     21    '-I../../ThirdParty/',
     22    '-I../../Cgl/src/',
     23    '-I../../Cgl/src/CglPreProcess/',
     24    '-I../../Cgl/src/CglGomory/',
     25    '-I../../Cgl/src/CglProbing/',
     26    '-I../../Cgl/src/CglKnapsackCover/',
     27    '-I../../Cgl/src/CglRedSplit/',
     28    '-I../../Cgl/src/CglRedSplit2/',
     29    '-I../../Cgl/src/CglGMI/',
     30    '-I../../Cgl/src/CglClique/',
     31    '-I../../Cgl/src/CglFlowCover/',
     32    '-I../../Cgl/src/CglMixedIntegerRounding2/',
     33    '-I../../Cgl/src/CglTwomir/',
     34    '-I../../Cgl/src/CglDuplicateRow/',
     35    '-I../../Cgl/src/CglStored/',
     36    '-I../../Cgl/src/CglLandP/',
     37    '-I../../Cgl/src/CglResidualCapacity/',
     38    '-I../../Cgl/src/CglZeroHalf/',
     39    '-std=c++98',
     40    '-x',
     41    'c++'
    4242]
    4343
  • trunk/Cbc/src/Cbc_C_Interface.cpp

    r2584 r2586  
    16491649}
    16501650
     1651COINLIBAPI const double *COINLINKAGE
     1652Cbc_getRowPrice(Cbc_Model *model)
     1653{
     1654  if (Cbc_getNumIntegers(model) == 0 || model->relax_ == 1)
     1655    return (model->solver_->getReducedCost());
     1656
     1657  return model->model_->getReducedCost();
     1658}
     1659
    16511660COINLIBAPI int COINLINKAGE
    16521661Cbc_numberSavedSolutions(Cbc_Model *model)
     
    19741983
    19751984  Cbc_addRowBuffer(model, nz, cols, coefs, rowLB, rowUB, name);
     1985}
     1986
     1987COINLIBAPI void COINLINKAGE
     1988Osi_addRow(void *osi, const char *name, int nz,
     1989  const int *cols, const double *coefs, char sense, double rhs)
     1990{
     1991  OsiSolverInterface *solver = (OsiSolverInterface *) osi;
     1992  double rowLB = -DBL_MAX, rowUB = DBL_MAX;
     1993  switch (toupper(sense)) {
     1994  case '=':
     1995    rowLB = rowUB = rhs;
     1996    break;
     1997  case 'E':
     1998    rowLB = rowUB = rhs;
     1999    break;
     2000  case '<':
     2001    rowUB = rhs;
     2002    break;
     2003  case 'L':
     2004    rowUB = rhs;
     2005    break;
     2006  case '>':
     2007    rowLB = rhs;
     2008    break;
     2009  case 'G':
     2010    rowLB = rhs;
     2011    break;
     2012  default:
     2013    fprintf(stderr, "unknow row sense %c.", toupper(sense));
     2014    abort();
     2015  }
     2016
     2017  solver->addRow(nz, cols, coefs, rowLB, rowUB);
     2018  solver->setRowName(solver->getNumRows()-1, std::string(name));
     2019}
     2020
     2021COINLIBAPI void COINLINKAGE
     2022Osi_setObjSense(void *osi, double sense)
     2023{
     2024  OsiSolverInterface *solver = (OsiSolverInterface *) osi;
     2025  solver->setObjSense(sense);
    19762026}
    19772027
     
    22242274}
    22252275
     2276/** @brief Creates a new OsiClpSolverInterface and returns a pointer to an OsiSolverInterface object */
     2277COINLIBAPI void * COINLINKAGE
     2278Osi_newSolver()
     2279{
     2280  OsiClpSolverInterface *clp = new OsiClpSolverInterface();
     2281
     2282  return dynamic_cast<OsiSolverInterface *>(clp);
     2283}
     2284
     2285COINLIBAPI void COINLINKAGE
     2286Osi_setObjCoef(void *osi, int index, double obj)
     2287{
     2288  OsiSolverInterface *osis = (OsiSolverInterface *)osi;
     2289  osis->setObjCoeff( index, obj );
     2290}
     2291
     2292/** @brief Solves initial LP relaxation */
     2293COINLIBAPI void COINLINKAGE
     2294Osi_initialSolve(void *osi)
     2295{
     2296  OsiSolverInterface *osis = ( OsiSolverInterface *)osi;
     2297  osis->initialSolve();
     2298}
     2299
     2300/** @brief Reoptimizes linear program  */
     2301COINLIBAPI void COINLINKAGE
     2302Osi_resolve(void *osi)
     2303{
     2304  OsiSolverInterface *osis = ( OsiSolverInterface *)osi;
     2305  osis->resolve();
     2306}
     2307
     2308/** @brief Performs branch and bound */
     2309COINLIBAPI void COINLINKAGE
     2310Osi_branchAndBound(void *osi)
     2311{
     2312  OsiSolverInterface *osis = ( OsiSolverInterface *)osi;
     2313  osis->branchAndBound();
     2314}
     2315
     2316// solution query methods
     2317
     2318/** @brief Checks if optimization was abandoned */
     2319COINLIBAPI char COINLINKAGE
     2320Osi_isAbandoned(void *osi)
     2321{
     2322  OsiSolverInterface *osis = ( OsiSolverInterface *)osi;
     2323  return (char)osis->isAbandoned();
     2324}
     2325
     2326/** @brief Checks if optimal solution was found */
     2327COINLIBAPI char COINLINKAGE
     2328Osi_isProvenOptimal(void *osi)
     2329{
     2330  OsiSolverInterface *osis = ( OsiSolverInterface *)osi;
     2331  return (char)osis->isProvenOptimal();
     2332}
     2333
     2334/** @brief Checks if problem is primal infeasible */
     2335COINLIBAPI char COINLINKAGE
     2336Osi_isProvenPrimalInfeasible(void *osi)
     2337{
     2338  OsiSolverInterface *osis = ( OsiSolverInterface *)osi;
     2339  return (char)osis->isProvenPrimalInfeasible();
     2340}
     2341
     2342/** @brief Checks if problem is dual infeasible */
     2343COINLIBAPI char COINLINKAGE
     2344Osi_isProvenDualInfeasible(void *osi)
     2345{
     2346  OsiSolverInterface *osis = ( OsiSolverInterface *)osi;
     2347  return (char)osis->isProvenDualInfeasible();
     2348}
     2349
     2350/** @brief Checks if primal objective limit was reached */
     2351COINLIBAPI char COINLINKAGE
     2352Osi_isPrimalObjectiveLimitReached(void *osi)
     2353{
     2354  OsiSolverInterface *osis = ( OsiSolverInterface *)osi;
     2355  return (char)osis->isPrimalObjectiveLimitReached();
     2356}
     2357
     2358/** @brief Checks if dual objective limit was reached */
     2359COINLIBAPI char COINLINKAGE
     2360Osi_isDualObjectiveLimitReached(void *osi)
     2361{
     2362  OsiSolverInterface *osis = ( OsiSolverInterface *)osi;
     2363  return (char)osis->isDualObjectiveLimitReached();
     2364}
     2365
     2366/** @brief Checks if iteration limit was reached */
     2367COINLIBAPI char COINLINKAGE
     2368Osi_isIterationLimitReached(void *osi)
     2369{
     2370  OsiSolverInterface *osis = ( OsiSolverInterface *)osi;
     2371  return (char)osis->isIterationLimitReached();
     2372}
     2373
    22262374COINLIBAPI int COINLINKAGE
    22272375Osi_getNumCols( void *osi )
     
    22692417  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
    22702418  return osiSolver->getNumRows();
     2419}
     2420
     2421/** @brief Returns number non-zeros in the constraint matrix */
     2422COINLIBAPI int COINLINKAGE
     2423Osi_getNumNz( void *osi )
     2424{
     2425  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
     2426  return osiSolver->getNumElements();
     2427}
     2428
     2429/** @brief Returns number integer/binary variables */
     2430COINLIBAPI int COINLINKAGE
     2431Osi_getNumIntegers( void *osi )
     2432{
     2433  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
     2434  return osiSolver->getNumIntegers();
    22712435}
    22722436
     
    23672531}
    23682532
    2369 
     2533/** @brief Sets a variable to integer */
     2534COINLIBAPI void COINLINKAGE
     2535Osi_setInteger(void *osi, int index)
     2536{
     2537  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
     2538
     2539  osiSolver->setInteger(index);
     2540}
     2541
     2542/** @brief Sets a variable to continuous */
     2543COINLIBAPI void COINLINKAGE
     2544Osi_setContinuous(void *osi, int index)
     2545{
     2546  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
     2547
     2548  osiSolver->setContinuous(index);
     2549}
     2550
     2551COINLIBAPI int COINLINKAGE
     2552Osi_getColNz(void *osi, int col)
     2553{
     2554  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
     2555  const CoinPackedMatrix *cpmCol = osiSolver->getMatrixByCol();
     2556  return cpmCol->getVectorLengths()[col];
     2557}
     2558
     2559/** @brief Indices of rows that a column appears
     2560     *
     2561     * @param model problem object
     2562     * @param col column index
     2563     * @return indices of rows that this column appears
     2564     **/
     2565COINLIBAPI const int *COINLINKAGE
     2566Osi_getColIndices(void *osi, int col)
     2567{
     2568  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
     2569  const CoinPackedMatrix *cpmCol = osiSolver->getMatrixByCol();
     2570  const CoinBigIndex *starts = cpmCol->getVectorStarts();
     2571  const int *cidx = cpmCol->getIndices() + starts[col];
     2572  return cidx;
     2573}
     2574
     2575/** @brief Coefficients that a column appear in rows
     2576     *
     2577     * @param model problem object
     2578     * @param col column index
     2579     * @return coefficients of this column in rows
     2580     **/
     2581COINLIBAPI const double *COINLINKAGE
     2582Osi_getColCoeffs(void *osi, int col)
     2583{
     2584  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
     2585
     2586  const CoinPackedMatrix *cpmCol = osiSolver->getMatrixByCol();
     2587  const CoinBigIndex *starts = cpmCol->getVectorStarts();
     2588  const double *rcoef = cpmCol->getElements() + starts[col];
     2589  return rcoef;
     2590}
    23702591
    23712592/** @brief Returns solution vector in OsiSolverInterface object */
     
    23762597
    23772598  return osiSolver->getColSolution();
     2599}
     2600
     2601/** Adds a new column */
     2602COINLIBAPI void COINLINKAGE
     2603Osi_addCol(void *osi, const char *name, double lb,
     2604  double ub, double obj, char isInteger,
     2605  int nz, int *rows, double *coefs)
     2606{
     2607  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
     2608
     2609    osiSolver->addCol(nz, rows, coefs, lb, ub, obj, std::string(name));
     2610    if (isInteger)
     2611      osiSolver->setInteger(osiSolver->getNumCols() - 1);
     2612}
     2613
     2614/** @brief Returns vector of reduced costs */
     2615COINLIBAPI const double * COINLINKAGE
     2616Osi_getReducedCost( void *osi )
     2617{
     2618  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
     2619
     2620  return osiSolver->getColSolution();
     2621}
     2622
     2623/** @brief Returns vector dual variables */
     2624COINLIBAPI const double * COINLINKAGE
     2625Osi_getRowPrice( void *osi )
     2626{
     2627  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
     2628
     2629  return osiSolver->getRowPrice();
     2630}
     2631
     2632COINLIBAPI double COINLINKAGE
     2633Osi_getObjValue( void *osi )
     2634{
     2635  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
     2636
     2637  return osiSolver->getObjValue();
     2638}
     2639
     2640/** @brief Sets column upper bound */
     2641COINLIBAPI void COINLINKAGE
     2642Osi_setColUpper (void *osi, int elementIndex, double ub)
     2643{
     2644  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
     2645  osiSolver->setColUpper(elementIndex, ub);
     2646}
     2647
     2648/** @brief Sets column upper bound */
     2649COINLIBAPI void COINLINKAGE
     2650Osi_setColLower(void *osi, int elementIndex, double lb)
     2651{
     2652  OsiSolverInterface *osiSolver = (OsiSolverInterface *) osi;
     2653  osiSolver->setColLower(elementIndex, lb);
    23782654}
    23792655
  • trunk/Cbc/src/Cbc_C_Interface.h

    r2584 r2586  
    755755Cbc_getReducedCost(Cbc_Model *model);
    756756
     757/** @brief Queries vector of row prices (values for dual variables)
     758     *
     759     * @param model problem object
     760     * @return reduced cost vector
     761     **/
     762COINLIBAPI const double *COINLINKAGE
     763Cbc_getRowPrice(Cbc_Model *model);
     764
    757765/** If optimization was abandoned due to numerical difficulties
    758766     *
     
    888896//@{
    889897
     898/** @brief Creates a new OsiClpSolverInterface and returns a pointer to an OsiSolverInterface object */
     899COINLIBAPI void * COINLINKAGE
     900Osi_newSolver();
     901
     902/** @brief Solves initial LP relaxation */
     903COINLIBAPI void COINLINKAGE
     904Osi_initialSolve(void *osi);
     905
     906/** @brief Reoptimizes linear program  */
     907COINLIBAPI void COINLINKAGE
     908Osi_resolve(void *osi);
     909
     910/** @brief Performs branch and bound */
     911COINLIBAPI void COINLINKAGE
     912Osi_branchAndBound(void *osi);
     913
     914// solution query methods
     915
     916/** @brief Checks if optimization was abandoned */
     917COINLIBAPI char COINLINKAGE
     918Osi_isAbandoned(void *osi);
     919
     920/** @brief Checks if optimal solution was found */
     921COINLIBAPI char COINLINKAGE
     922Osi_isProvenOptimal(void *osi);
     923
     924/** @brief Checks if problem is primal infeasible */
     925COINLIBAPI char COINLINKAGE
     926Osi_isProvenPrimalInfeasible(void *osi);
     927
     928/** @brief Checks if problem is dual infeasible */
     929COINLIBAPI char COINLINKAGE
     930Osi_isProvenDualInfeasible(void *osi);
     931
     932/** @brief Checks if primal objective limit was reached */
     933COINLIBAPI char COINLINKAGE
     934Osi_isPrimalObjectiveLimitReached(void *osi);
     935
     936/** @brief Checks if dual objective limit was reached */
     937COINLIBAPI char COINLINKAGE
     938Osi_isDualObjectiveLimitReached(void *osi);
     939
     940/** @brief Checks if iteration limit was reached */
     941COINLIBAPI char COINLINKAGE
     942Osi_isIterationLimitReached(void *osi);
     943
    890944/** @brief Returns number of cols in OsiSolverInterface object */
    891945COINLIBAPI int COINLINKAGE
     
    912966Osi_getNumRows( void *osi );
    913967
     968/** @brief Returns number non-zeros in the constraint matrix */
     969COINLIBAPI int COINLINKAGE
     970Osi_getNumNz( void *osi );
     971
     972/** @brief Returns number integer/binary variables */
     973COINLIBAPI int COINLINKAGE
     974Osi_getNumIntegers( void *osi );
     975
    914976COINLIBAPI int COINLINKAGE
    915977Osi_getRowNz(void *osi, int row);
     
    9451007Osi_getRowSense(void *osi, int row);
    9461008
     1009/** @brief Returns vector with objective function coefficients */
     1010COINLIBAPI const double * COINLINKAGE
     1011Osi_getObjCoefficients();
     1012
     1013/** @brief Returns the objective sense: 1 for MIN and -1 for MAX */
     1014COINLIBAPI double COINLINKAGE
     1015Osi_getObjSense();
     1016
    9471017/** @brief Returns solution vector in OsiSolverInterface object */
    9481018COINLIBAPI const double * COINLINKAGE
    9491019Osi_getColSolution( void *osi );
    9501020
     1021/** @brief Returns vector of reduced costs */
     1022COINLIBAPI const double * COINLINKAGE
     1023Osi_getReducedCost( void *osi );
     1024
     1025/** @brief Returns of dual variables */
     1026COINLIBAPI const double *COINLINKAGE
     1027Osi_getRowPrice( void *osi );
     1028
     1029/** @brief Returns the objective function value */
     1030COINLIBAPI double COINLINKAGE
     1031Osi_getObjValue( void *osi );
     1032
     1033/** @brief Sets column upper bound */
     1034COINLIBAPI void COINLINKAGE
     1035Osi_setColUpper (void *osi, int elementIndex, double ub);
     1036
     1037/** @brief Sets column upper bound */
     1038COINLIBAPI void COINLINKAGE
     1039Osi_setColLower(void *osi, int elementIndex, double lb);
     1040
     1041/** @brief Sets one objective function coefficient */
     1042COINLIBAPI void COINLINKAGE
     1043Osi_setObjCoef(void *osi, int elementIndex, double obj);
     1044
     1045/** @brief Sets optimization direction
     1046    *
     1047    * @param osi OsiSolverInterface object
     1048    * @param sense: direction of optimization (1 - minimize, -1 - maximize, 0 - ignore)
     1049    **/
     1050COINLIBAPI void COINLINKAGE
     1051Osi_setObjSense(void *osi, double sense);
     1052
     1053/** @brief Sets a variable to integer */
     1054COINLIBAPI void COINLINKAGE
     1055Osi_setInteger(void *osi, int index);
     1056
     1057/** @brief Sets a variable to continuous */
     1058COINLIBAPI void COINLINKAGE
     1059Osi_setContinuous(void *osi, int index);
     1060
     1061/** @brief Number of non-zero entries in a column
     1062     *
     1063     * @param model problem object
     1064     * @param col column index
     1065     * @return numbef of rows that this column appears
     1066     **/
     1067COINLIBAPI int COINLINKAGE
     1068Osi_getColNz(void *model, int col);
     1069
     1070/** @brief Indices of rows that a column appears
     1071     *
     1072     * @param model problem object
     1073     * @param col column index
     1074     * @return indices of rows that this column appears
     1075     **/
     1076COINLIBAPI const int *COINLINKAGE
     1077Osi_getColIndices(void *model, int col);
     1078
     1079/** @brief Coefficients that a column appear in rows
     1080     *
     1081     * @param model problem object
     1082     * @param col column index
     1083     * @return coefficients of this column in rows
     1084     **/
     1085COINLIBAPI const double *COINLINKAGE
     1086Osi_getColCoeffs(void *model, int col);
     1087
     1088/** @brief Creates a new column
     1089     *
     1090     * Creates a new column (variable)
     1091     *
     1092     * @param osi OsiSolverInterface object
     1093     * @param name variable name
     1094     * @param lb column lower bound
     1095     * @param ub column upper bound
     1096     * @param obj objective function coefficient
     1097     * @param isInteger 1 if variable is integral, 0 otherwise
     1098     * @param nz number of rows (constraints) where this column appears, can be 0 if constraints will be added later
     1099     * @param rows index of rows where this column appears, NULL if rows will be added later
     1100     * @param coefs coefficients that this column appears in its rows, NULL if rows will be added later
     1101     ***/
     1102COINLIBAPI void COINLINKAGE
     1103Osi_addCol(void *osi, const char *name, double lb,
     1104  double ub, double obj, char isInteger,
     1105  int nz, int *rows, double *coefs);
     1106
     1107/** @brief Adds a new row
     1108     *
     1109     *  Adds a new row (linear constraint) to the problem
     1110     *
     1111     *  @param osi OsiSolverInterface object
     1112     *  @param name constraint name
     1113     *  @param nz number of variables with non-zero coefficients in this row
     1114     *  @param cols index of variables that appear in this row
     1115     *  @param coefs cofficients that that variables appear
     1116     *  @param sense constraint sense: L if <=, G if >=, E if =, R if ranged and N if free
     1117     *  @param rhs right hand size
     1118     * */
     1119COINLIBAPI void COINLINKAGE
     1120Osi_addRow(void *osi, const char *name, int nz,
     1121  const int *cols, const double *coefs, char sense, double rhs);
     1122
     1123/** @brief Deletes an OsiSolverInterface object */
     1124COINLIBAPI void COINLINKAGE
     1125Osi_deleteSolver( void *osi );
    9511126
    9521127/*@}*/
Note: See TracChangeset for help on using the changeset viewer.