Changeset 2020 for trunk/Cbc


Ignore:
Timestamp:
Mar 13, 2014 2:09:41 PM (5 years ago)
Author:
mlubin
Message:

Allow passing arbitrary command-line parameters using the C interface

Location:
trunk/Cbc
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cbc/src/Cbc_C_Interface.cpp

    r2019 r2020  
    237237    OsiClpSolverInterface solver1;
    238238    model->solver_    = &solver1;
    239     model->solver_->OsiClpSolverInterface::setHintParam(OsiDoReducePrint, true, OsiHintTry);
    240239    model->model_     = new CbcModel(solver1);
    241240    CbcMain0(*model->model_);
     
    461460}
    462461
    463 /* Objective offset */
    464 COINLIBAPI double COINLINKAGE
    465 Cbc_objectiveOffset(Cbc_Model * /*model*/)
    466 {
    467     const char prefix[] = "Cbc_C_Interface::Cbc_objectiveOffset(): ";
    468 //  const int  VERBOSE = 1;
    469     if (VERBOSE > 0) printf("%s begin\n", prefix);
    470 
    471     double result = 0.0;
    472 // cannot find names in Cbc, Osi, or OsiClp
    473 //tbd  return model->model_->objectiveOffset();
    474     if (VERBOSE > 0) printf("%s WARNING: NOT IMPLEMENTED\n", prefix);
    475 
    476     if (VERBOSE > 0) printf("%s return %g\n", prefix, result);
    477     return result;
    478 }
    479 COINLIBAPI void COINLINKAGE
    480 Cbc_setObjectiveOffset(Cbc_Model * /*model*/, double /*value*/)
    481 {
    482     const char prefix[] = "Cbc_C_Interface::Cbc_setObjectiveOffset(): ";
    483 //  const int  VERBOSE = 1;
    484     if (VERBOSE > 0) printf("%s begin\n", prefix);
    485 
    486 // cannot find names in Cbc, Osi, or OsiClp
    487 //tbd  model->model_->solver()->setObjectiveOffset(value);
    488     if (VERBOSE > 0) printf("%s WARNING: NOT IMPLEMENTED\n", prefix);
    489 
    490     if (VERBOSE > 0) printf("%s return\n", prefix);
    491 }
     462
     463COINLIBAPI void COINLINKAGE
     464Cbc_setParameter(Cbc_Model * model, const char * name, const char * value)
     465{
     466    model->cmdargs_.push_back(std::string("-")+name);
     467    model->cmdargs_.push_back(value);
     468}
     469
    492470/* Fills in array with problem name  */
    493471COINLIBAPI void COINLINKAGE
    494472Cbc_problemName(Cbc_Model * model, int maxNumberCharacters, char * array)
    495473{
    496     const char prefix[] = "Cbc_C_Interface::Cbc_problemName(): ";
    497 //  const int  VERBOSE = 1;
    498     if (VERBOSE > 0) printf("%s begin\n", prefix);
    499 
    500474    std::string name;
    501475    model->model_->solver()->getStrParam(OsiProbName, name);
    502     maxNumberCharacters = CoinMin(maxNumberCharacters, (int)strlen(name.c_str()));
    503     strncpy(array, name.c_str(), maxNumberCharacters - 1);
    504     array[maxNumberCharacters-1] = '\0';
    505 
    506     if (VERBOSE > 0) printf("%s return\n", prefix);
     476    strncpy(array, name.c_str(), maxNumberCharacters);
    507477}
    508478/* Sets problem name.  Must have \0 at end.  */
    509479COINLIBAPI int COINLINKAGE
    510 Cbc_setProblemName(Cbc_Model * model, int /*maxNumberCharacters*/, char * array)
    511 {
    512     const char prefix[] = "Cbc_C_Interface::Cbc_setProblemName(): ";
    513 //  const int  VERBOSE = 1;
    514     if (VERBOSE > 0) printf("%s begin\n", prefix);
    515 
     480Cbc_setProblemName(Cbc_Model * model, const char * array)
     481{
    516482    bool result = false;
    517483    result = model->model_->solver()->setStrParam(OsiProbName, array);
    518484
    519     if (VERBOSE > 0) printf("%s return\n", prefix);
    520485    return (result) ? 1 : 0;
    521486}
     
    718683    const char prefix[] = "Cbc_C_Interface::Cbc_solve(): ";
    719684    int result = 0;
    720     const char *argv[] = {"Cbc_C_Interface","-solve", "-quit"};
     685    std::vector<const char*> argv;
     686    argv.push_back("Cbc_C_Interface");
     687    for (size_t i = 0; i < model->cmdargs_.size(); i++) {
     688        argv.push_back(model->cmdargs_[i].c_str());
     689    }
     690    argv.push_back("-solve");
     691    argv.push_back("-quit");
    721692    try {
    722693       
    723         CbcMain1(3, argv, *model->model_);
     694        CbcMain1((int)argv.size(), &argv[0], *model->model_);
    724695    } catch (CoinError e) {
    725696        printf("%s ERROR: %s::%s, %s\n", prefix,
  • trunk/Cbc/src/Cbc_C_Interface.h

    r2019 r2020  
    112112    /**@name gets and sets - you will find some synonyms at the end of this file */
    113113    /*@{*/
    114     /** Objective offset */
    115     COINLIBAPI double COINLINKAGE
    116     Cbc_objectiveOffset(Cbc_Model * model)
    117     ;
    118     COINLIBAPI void COINLINKAGE
    119     Cbc_setObjectiveOffset(Cbc_Model * model, double value)
     114    /** Set parameter "name" to value "value". Note that this
     115     * translates directly to using "-name value" as a
     116     * command-line argument to Cbc.*/
     117    COINLIBAPI void COINLINKAGE
     118    Cbc_setParameter(Cbc_Model * model, const char * name, const char * value)
    120119    ;
    121120    /** Fills in array with problem name  */
     
    128127    */
    129128    COINLIBAPI int COINLINKAGE
    130     Cbc_setProblemName(Cbc_Model * model, int maxNumberCharacters, char * array)
     129    Cbc_setProblemName(Cbc_Model * model, const char * array)
    131130    ;
    132131    /** Status of problem:
  • trunk/Cbc/test/CInterfaceTest.c

    r2019 r2020  
    99#include <math.h>
    1010#include <stdio.h>
     11#include <string.h>
    1112
    1213
     
    3031    double rowub[] = {10};
    3132    const double *sol;
     33    const char* setname = "test model";
     34    char *getname = malloc(20);
    3235    int i;
    3336
     
    4447    Cbc_setObjSense(model, -1);
    4548    assert(Cbc_getObjSense(model) == -1);
     49
     50    Cbc_setProblemName(model, setname);
    4651
    4752    Cbc_solve(model);
     
    6368    assert(fabs(sol[3] - 1.0) < 1e-6);
    6469    assert(fabs(sol[4] - 1.0) < 1e-6);
     70
     71    Cbc_problemName(model, 20, getname);
     72    i = strcmp(getname,setname);
     73    assert( (i == 0) );
     74   
     75    Cbc_deleteModel(model);
    6576
    6677}
     
    95106    assert(!Cbc_isProvenOptimal(model));
    96107    assert(Cbc_isProvenInfeasible(model));
     108   
     109    Cbc_deleteModel(model);
    97110
    98111}
     
    122135    Cbc_setInteger(model, 0);
    123136
     137    Cbc_setParameter(model, "log", "0");
     138   
     139    printf("About to solve problem silently. You should see no output except \"Done\".\n");
    124140    Cbc_solve(model);
     141    printf("Done\n");
    125142   
    126143    assert(!Cbc_isProvenOptimal(model));
     
    128145    assert(Cbc_isContinuousUnbounded(model));
    129146
     147    Cbc_deleteModel(model);
    130148
    131149
Note: See TracChangeset for help on using the changeset viewer.