Ignore:
Timestamp:
Jan 6, 2019 2:43:06 PM (4 months ago)
Author:
unxusr
Message:

formatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Clp/src/ClpLinearObjective.cpp

    r1665 r2385  
    1818// Default Constructor
    1919//-------------------------------------------------------------------
    20 ClpLinearObjective::ClpLinearObjective ()
    21      : ClpObjective()
    22 {
    23      type_ = 1;
    24      objective_ = NULL;
    25      numberColumns_ = 0;
     20ClpLinearObjective::ClpLinearObjective()
     21  : ClpObjective()
     22{
     23  type_ = 1;
     24  objective_ = NULL;
     25  numberColumns_ = 0;
    2626}
    2727
     
    2929// Useful Constructor
    3030//-------------------------------------------------------------------
    31 ClpLinearObjective::ClpLinearObjective (const double * objective ,
    32                                         int numberColumns)
    33      : ClpObjective()
    34 {
    35      type_ = 1;
    36      numberColumns_ = numberColumns;
    37      objective_ = CoinCopyOfArray(objective, numberColumns_, 0.0);
     31ClpLinearObjective::ClpLinearObjective(const double *objective,
     32  int numberColumns)
     33  : ClpObjective()
     34{
     35  type_ = 1;
     36  numberColumns_ = numberColumns;
     37  objective_ = CoinCopyOfArray(objective, numberColumns_, 0.0);
    3838}
    3939
     
    4141// Copy constructor
    4242//-------------------------------------------------------------------
    43 ClpLinearObjective::ClpLinearObjective (const ClpLinearObjective & rhs)
    44      : ClpObjective(rhs)
    45 {
    46      numberColumns_ = rhs.numberColumns_;
    47      objective_ = CoinCopyOfArray(rhs.objective_, numberColumns_);
     43ClpLinearObjective::ClpLinearObjective(const ClpLinearObjective &rhs)
     44  : ClpObjective(rhs)
     45{
     46  numberColumns_ = rhs.numberColumns_;
     47  objective_ = CoinCopyOfArray(rhs.objective_, numberColumns_);
    4848}
    4949/* Subset constructor.  Duplicates are allowed
    5050   and order is as given.
    5151*/
    52 ClpLinearObjective::ClpLinearObjective (const ClpLinearObjective &rhs,
    53                                         int numberColumns,
    54                                         const int * whichColumn)
    55      : ClpObjective(rhs)
    56 {
    57      objective_ = NULL;
    58      numberColumns_ = 0;
    59      if (numberColumns > 0) {
    60           // check valid lists
    61           int numberBad = 0;
    62           int i;
    63           for (i = 0; i < numberColumns; i++)
    64                if (whichColumn[i] < 0 || whichColumn[i] >= rhs.numberColumns_)
    65                     numberBad++;
    66           if (numberBad)
    67                throw CoinError("bad column list", "subset constructor",
    68                                "ClpLinearObjective");
    69           numberColumns_ = numberColumns;
    70           objective_ = new double[numberColumns_];
    71           for (i = 0; i < numberColumns_; i++)
    72                objective_[i] = rhs.objective_[whichColumn[i]];
    73      }
    74 }
    75 
     52ClpLinearObjective::ClpLinearObjective(const ClpLinearObjective &rhs,
     53  int numberColumns,
     54  const int *whichColumn)
     55  : ClpObjective(rhs)
     56{
     57  objective_ = NULL;
     58  numberColumns_ = 0;
     59  if (numberColumns > 0) {
     60    // check valid lists
     61    int numberBad = 0;
     62    int i;
     63    for (i = 0; i < numberColumns; i++)
     64      if (whichColumn[i] < 0 || whichColumn[i] >= rhs.numberColumns_)
     65        numberBad++;
     66    if (numberBad)
     67      throw CoinError("bad column list", "subset constructor",
     68        "ClpLinearObjective");
     69    numberColumns_ = numberColumns;
     70    objective_ = new double[numberColumns_];
     71    for (i = 0; i < numberColumns_; i++)
     72      objective_[i] = rhs.objective_[whichColumn[i]];
     73  }
     74}
    7675
    7776//-------------------------------------------------------------------
    7877// Destructor
    7978//-------------------------------------------------------------------
    80 ClpLinearObjective::~ClpLinearObjective ()
    81 {
    82      delete [] objective_;
     79ClpLinearObjective::~ClpLinearObjective()
     80{
     81  delete[] objective_;
    8382}
    8483
     
    8786//-------------------------------------------------------------------
    8887ClpLinearObjective &
    89 ClpLinearObjective::operator=(const ClpLinearObjective& rhs)
    90 {
    91      if (this != &rhs) {
    92           ClpObjective::operator=(rhs);
    93           numberColumns_ = rhs.numberColumns_;
    94           delete [] objective_;
    95           objective_ = CoinCopyOfArray(rhs.objective_, numberColumns_);
    96      }
    97      return *this;
     88ClpLinearObjective::operator=(const ClpLinearObjective &rhs)
     89{
     90  if (this != &rhs) {
     91    ClpObjective::operator=(rhs);
     92    numberColumns_ = rhs.numberColumns_;
     93    delete[] objective_;
     94    objective_ = CoinCopyOfArray(rhs.objective_, numberColumns_);
     95  }
     96  return *this;
    9897}
    9998
     
    101100double *
    102101ClpLinearObjective::gradient(const ClpSimplex * /*model*/,
    103                              const double * /*solution*/, double & offset,
    104                              bool /*refresh*/,
    105                              int /*includeLinear*/)
    106 {
    107      // not sure what to do about scaling
    108      //assert (!model);
    109      //assert (includeLinear==2); //otherwise need to return all zeros
    110      offset = 0.0;
    111      return objective_;
     102  const double * /*solution*/, double &offset,
     103  bool /*refresh*/,
     104  int /*includeLinear*/)
     105{
     106  // not sure what to do about scaling
     107  //assert (!model);
     108  //assert (includeLinear==2); //otherwise need to return all zeros
     109  offset = 0.0;
     110  return objective_;
    112111}
    113112
     
    115114 */
    116115double
    117 ClpLinearObjective::reducedGradient(ClpSimplex * model, double * region,
    118                                     bool /*useFeasibleCosts*/)
    119 {
    120      int numberRows = model->numberRows();
    121      //work space
    122      CoinIndexedVector  * workSpace = model->rowArray(0);
    123 
    124      CoinIndexedVector arrayVector;
    125      arrayVector.reserve(numberRows + 1);
    126 
    127      int iRow;
     116ClpLinearObjective::reducedGradient(ClpSimplex *model, double *region,
     117  bool /*useFeasibleCosts*/)
     118{
     119  int numberRows = model->numberRows();
     120  //work space
     121  CoinIndexedVector *workSpace = model->rowArray(0);
     122
     123  CoinIndexedVector arrayVector;
     124  arrayVector.reserve(numberRows + 1);
     125
     126  int iRow;
    128127#ifdef CLP_DEBUG
    129      workSpace->checkClear();
     128  workSpace->checkClear();
    130129#endif
    131      double * array = arrayVector.denseVector();
    132      int * index = arrayVector.getIndices();
    133      int number = 0;
    134      const double * cost = model->costRegion();
    135      //assert (!useFeasibleCosts);
    136      const int * pivotVariable = model->pivotVariable();
    137      for (iRow = 0; iRow < numberRows; iRow++) {
    138           int iPivot = pivotVariable[iRow];
    139           double value = cost[iPivot];
    140           if (value) {
    141                array[iRow] = value;
    142                index[number++] = iRow;
    143           }
    144      }
    145      arrayVector.setNumElements(number);
    146 
    147      int numberColumns = model->numberColumns();
    148 
    149      // Btran basic costs
    150      double * work = workSpace->denseVector();
    151      model->factorization()->updateColumnTranspose(workSpace, &arrayVector);
    152      ClpFillN(work, numberRows, 0.0);
    153      // now look at dual solution
    154      double * rowReducedCost = region + numberColumns;
    155      double * dual = rowReducedCost;
    156      double * rowCost = model->costRegion(0);
    157      for (iRow = 0; iRow < numberRows; iRow++) {
    158           dual[iRow] = array[iRow];
    159      }
    160      double * dj = region;
    161      ClpDisjointCopyN(model->costRegion(1), numberColumns, dj);
    162      model->transposeTimes(-1.0, dual, dj);
    163      for (iRow = 0; iRow < numberRows; iRow++) {
    164           // slack
    165           double value = dual[iRow];
    166           value += rowCost[iRow];
    167           rowReducedCost[iRow] = value;
    168      }
    169      return 0.0;
     130  double *array = arrayVector.denseVector();
     131  int *index = arrayVector.getIndices();
     132  int number = 0;
     133  const double *cost = model->costRegion();
     134  //assert (!useFeasibleCosts);
     135  const int *pivotVariable = model->pivotVariable();
     136  for (iRow = 0; iRow < numberRows; iRow++) {
     137    int iPivot = pivotVariable[iRow];
     138    double value = cost[iPivot];
     139    if (value) {
     140      array[iRow] = value;
     141      index[number++] = iRow;
     142    }
     143  }
     144  arrayVector.setNumElements(number);
     145
     146  int numberColumns = model->numberColumns();
     147
     148  // Btran basic costs
     149  double *work = workSpace->denseVector();
     150  model->factorization()->updateColumnTranspose(workSpace, &arrayVector);
     151  ClpFillN(work, numberRows, 0.0);
     152  // now look at dual solution
     153  double *rowReducedCost = region + numberColumns;
     154  double *dual = rowReducedCost;
     155  double *rowCost = model->costRegion(0);
     156  for (iRow = 0; iRow < numberRows; iRow++) {
     157    dual[iRow] = array[iRow];
     158  }
     159  double *dj = region;
     160  ClpDisjointCopyN(model->costRegion(1), numberColumns, dj);
     161  model->transposeTimes(-1.0, dual, dj);
     162  for (iRow = 0; iRow < numberRows; iRow++) {
     163    // slack
     164    double value = dual[iRow];
     165    value += rowCost[iRow];
     166    rowReducedCost[iRow] = value;
     167  }
     168  return 0.0;
    170169}
    171170/* Returns step length which gives minimum of objective for
     
    175174*/
    176175double
    177 ClpLinearObjective::stepLength(ClpSimplex * model,
    178                                const double * solution,
    179                                const double * change,
    180                                double maximumTheta,
    181                                double & currentObj,
    182                                double & predictedObj,
    183                                double & thetaObj)
    184 {
    185      const double * cost = model->costRegion();
    186      double delta = 0.0;
    187      int numberRows = model->numberRows();
    188      int numberColumns = model->numberColumns();
    189      currentObj = 0.0;
    190      thetaObj = 0.0;
    191      for (int iColumn = 0; iColumn < numberColumns + numberRows; iColumn++) {
    192           delta += cost[iColumn] * change[iColumn];
    193           currentObj += cost[iColumn] * solution[iColumn];
    194      }
    195      thetaObj = currentObj + delta * maximumTheta;
    196      predictedObj = currentObj + delta * maximumTheta;
    197      if (delta < 0.0) {
    198           return maximumTheta;
    199      } else {
    200           printf("odd linear direction %g\n", delta);
    201           return 0.0;
    202      }
     176ClpLinearObjective::stepLength(ClpSimplex *model,
     177  const double *solution,
     178  const double *change,
     179  double maximumTheta,
     180  double &currentObj,
     181  double &predictedObj,
     182  double &thetaObj)
     183{
     184  const double *cost = model->costRegion();
     185  double delta = 0.0;
     186  int numberRows = model->numberRows();
     187  int numberColumns = model->numberColumns();
     188  currentObj = 0.0;
     189  thetaObj = 0.0;
     190  for (int iColumn = 0; iColumn < numberColumns + numberRows; iColumn++) {
     191    delta += cost[iColumn] * change[iColumn];
     192    currentObj += cost[iColumn] * solution[iColumn];
     193  }
     194  thetaObj = currentObj + delta * maximumTheta;
     195  predictedObj = currentObj + delta * maximumTheta;
     196  if (delta < 0.0) {
     197    return maximumTheta;
     198  } else {
     199    printf("odd linear direction %g\n", delta);
     200    return 0.0;
     201  }
    203202}
    204203// Return objective value (without any ClpModel offset) (model may be NULL)
    205204double
    206 ClpLinearObjective::objectiveValue(const ClpSimplex * model, const double * solution) const
    207 {
    208      const double * cost = objective_;
    209      if (model && model->costRegion())
    210           cost = model->costRegion();
    211      double currentObj = 0.0;
    212      for (int iColumn = 0; iColumn < numberColumns_; iColumn++) {
    213           currentObj += cost[iColumn] * solution[iColumn];
    214      }
    215      return currentObj;
     205ClpLinearObjective::objectiveValue(const ClpSimplex *model, const double *solution) const
     206{
     207  const double *cost = objective_;
     208  if (model && model->costRegion())
     209    cost = model->costRegion();
     210  double currentObj = 0.0;
     211  for (int iColumn = 0; iColumn < numberColumns_; iColumn++) {
     212    currentObj += cost[iColumn] * solution[iColumn];
     213  }
     214  return currentObj;
    216215}
    217216//-------------------------------------------------------------------
    218217// Clone
    219218//-------------------------------------------------------------------
    220 ClpObjective * ClpLinearObjective::clone() const
    221 {
    222      return new ClpLinearObjective(*this);
     219ClpObjective *ClpLinearObjective::clone() const
     220{
     221  return new ClpLinearObjective(*this);
    223222}
    224223/* Subset clone.  Duplicates are allowed
     
    226225*/
    227226ClpObjective *
    228 ClpLinearObjective::subsetClone (int numberColumns,
    229                                  const int * whichColumns) const
    230 {
    231      return new ClpLinearObjective(*this, numberColumns, whichColumns);
     227ClpLinearObjective::subsetClone(int numberColumns,
     228  const int *whichColumns) const
     229{
     230  return new ClpLinearObjective(*this, numberColumns, whichColumns);
    232231}
    233232// Resize objective
    234 void
    235 ClpLinearObjective::resize(int newNumberColumns)
    236 {
    237      if (numberColumns_ != newNumberColumns) {
    238           int i;
    239           double * newArray = new double[newNumberColumns];
    240           if (objective_)
    241                CoinMemcpyN(objective_, CoinMin(newNumberColumns, numberColumns_), newArray);
    242           delete [] objective_;
    243           objective_ = newArray;
    244           for (i = numberColumns_; i < newNumberColumns; i++)
    245                objective_[i] = 0.0;
    246           numberColumns_ = newNumberColumns;
    247      }
    248 
     233void ClpLinearObjective::resize(int newNumberColumns)
     234{
     235  if (numberColumns_ != newNumberColumns) {
     236    int i;
     237    double *newArray = new double[newNumberColumns];
     238    if (objective_)
     239      CoinMemcpyN(objective_, CoinMin(newNumberColumns, numberColumns_), newArray);
     240    delete[] objective_;
     241    objective_ = newArray;
     242    for (i = numberColumns_; i < newNumberColumns; i++)
     243      objective_[i] = 0.0;
     244    numberColumns_ = newNumberColumns;
     245  }
    249246}
    250247// Delete columns in  objective
    251 void
    252 ClpLinearObjective::deleteSome(int numberToDelete, const int * which)
    253 {
    254      if (objective_) {
    255           int i ;
    256           char * deleted = new char[numberColumns_];
    257           int numberDeleted = 0;
    258           CoinZeroN(deleted, numberColumns_);
    259           for (i = 0; i < numberToDelete; i++) {
    260                int j = which[i];
    261                if (j >= 0 && j < numberColumns_ && !deleted[j]) {
    262                     numberDeleted++;
    263                     deleted[j] = 1;
    264                }
    265           }
    266           int newNumberColumns = numberColumns_ - numberDeleted;
    267           double * newArray = new double[newNumberColumns];
    268           int put = 0;
    269           for (i = 0; i < numberColumns_; i++) {
    270                if (!deleted[i]) {
    271                     newArray[put++] = objective_[i];
    272                }
    273           }
    274           delete [] objective_;
    275           objective_ = newArray;
    276           delete [] deleted;
    277           numberColumns_ = newNumberColumns;
    278      }
     248void ClpLinearObjective::deleteSome(int numberToDelete, const int *which)
     249{
     250  if (objective_) {
     251    int i;
     252    char *deleted = new char[numberColumns_];
     253    int numberDeleted = 0;
     254    CoinZeroN(deleted, numberColumns_);
     255    for (i = 0; i < numberToDelete; i++) {
     256      int j = which[i];
     257      if (j >= 0 && j < numberColumns_ && !deleted[j]) {
     258        numberDeleted++;
     259        deleted[j] = 1;
     260      }
     261    }
     262    int newNumberColumns = numberColumns_ - numberDeleted;
     263    double *newArray = new double[newNumberColumns];
     264    int put = 0;
     265    for (i = 0; i < numberColumns_; i++) {
     266      if (!deleted[i]) {
     267        newArray[put++] = objective_[i];
     268      }
     269    }
     270    delete[] objective_;
     271    objective_ = newArray;
     272    delete[] deleted;
     273    numberColumns_ = newNumberColumns;
     274  }
    279275}
    280276// Scale objective
    281 void
    282 ClpLinearObjective::reallyScale(const double * columnScale)
    283 {
    284      for (int iColumn = 0; iColumn < numberColumns_; iColumn++) {
    285           objective_[iColumn] *= columnScale[iColumn];
    286      }
    287 }
    288 
     277void ClpLinearObjective::reallyScale(const double *columnScale)
     278{
     279  for (int iColumn = 0; iColumn < numberColumns_; iColumn++) {
     280    objective_[iColumn] *= columnScale[iColumn];
     281  }
     282}
     283
     284/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
     285*/
Note: See TracChangeset for help on using the changeset viewer.