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/ClpConstraintLinear.cpp

    r1665 r2385  
    1717// Default Constructor
    1818//-------------------------------------------------------------------
    19 ClpConstraintLinear::ClpConstraintLinear ()
    20      : ClpConstraint()
    21 {
    22      type_ = 0;
    23      column_ = NULL;
    24      coefficient_ = NULL;
    25      numberColumns_ = 0;
    26      numberCoefficients_ = 0;
     19ClpConstraintLinear::ClpConstraintLinear()
     20  : ClpConstraint()
     21{
     22  type_ = 0;
     23  column_ = NULL;
     24  coefficient_ = NULL;
     25  numberColumns_ = 0;
     26  numberCoefficients_ = 0;
    2727}
    2828
     
    3030// Useful Constructor
    3131//-------------------------------------------------------------------
    32 ClpConstraintLinear::ClpConstraintLinear (int row, int numberCoefficents ,
    33           int numberColumns,
    34           const int * column, const double * coefficient)
    35      : ClpConstraint()
    36 {
    37      type_ = 0;
    38      rowNumber_ = row;
    39      numberColumns_ = numberColumns;
    40      numberCoefficients_ = numberCoefficents;
    41      column_ = CoinCopyOfArray(column, numberCoefficients_);
    42      coefficient_ = CoinCopyOfArray(coefficient, numberCoefficients_);
    43      CoinSort_2(column_, column_ + numberCoefficients_, coefficient_);
     32ClpConstraintLinear::ClpConstraintLinear(int row, int numberCoefficents,
     33  int numberColumns,
     34  const int *column, const double *coefficient)
     35  : ClpConstraint()
     36{
     37  type_ = 0;
     38  rowNumber_ = row;
     39  numberColumns_ = numberColumns;
     40  numberCoefficients_ = numberCoefficents;
     41  column_ = CoinCopyOfArray(column, numberCoefficients_);
     42  coefficient_ = CoinCopyOfArray(coefficient, numberCoefficients_);
     43  CoinSort_2(column_, column_ + numberCoefficients_, coefficient_);
    4444}
    4545
     
    4747// Copy constructor
    4848//-------------------------------------------------------------------
    49 ClpConstraintLinear::ClpConstraintLinear (const ClpConstraintLinear & rhs)
    50      : ClpConstraint(rhs)
    51 {
    52      numberColumns_ = rhs.numberColumns_;
    53      numberCoefficients_ = rhs.numberCoefficients_;
    54      column_ = CoinCopyOfArray(rhs.column_, numberCoefficients_);
    55      coefficient_ = CoinCopyOfArray(rhs.coefficient_, numberCoefficients_);
    56 }
    57 
     49ClpConstraintLinear::ClpConstraintLinear(const ClpConstraintLinear &rhs)
     50  : ClpConstraint(rhs)
     51{
     52  numberColumns_ = rhs.numberColumns_;
     53  numberCoefficients_ = rhs.numberCoefficients_;
     54  column_ = CoinCopyOfArray(rhs.column_, numberCoefficients_);
     55  coefficient_ = CoinCopyOfArray(rhs.coefficient_, numberCoefficients_);
     56}
    5857
    5958//-------------------------------------------------------------------
    6059// Destructor
    6160//-------------------------------------------------------------------
    62 ClpConstraintLinear::~ClpConstraintLinear ()
    63 {
    64      delete [] column_;
    65      delete [] coefficient_;
     61ClpConstraintLinear::~ClpConstraintLinear()
     62{
     63  delete[] column_;
     64  delete[] coefficient_;
    6665}
    6766
     
    7069//-------------------------------------------------------------------
    7170ClpConstraintLinear &
    72 ClpConstraintLinear::operator=(const ClpConstraintLinear& rhs)
    73 {
    74      if (this != &rhs) {
    75           delete [] column_;
    76           delete [] coefficient_;
    77           numberColumns_ = rhs.numberColumns_;
    78           numberCoefficients_ = rhs.numberCoefficients_;
    79           column_ = CoinCopyOfArray(rhs.column_, numberCoefficients_);
    80           coefficient_ = CoinCopyOfArray(rhs.coefficient_, numberCoefficients_);
    81      }
    82      return *this;
     71ClpConstraintLinear::operator=(const ClpConstraintLinear &rhs)
     72{
     73  if (this != &rhs) {
     74    delete[] column_;
     75    delete[] coefficient_;
     76    numberColumns_ = rhs.numberColumns_;
     77    numberCoefficients_ = rhs.numberCoefficients_;
     78    column_ = CoinCopyOfArray(rhs.column_, numberCoefficients_);
     79    coefficient_ = CoinCopyOfArray(rhs.coefficient_, numberCoefficients_);
     80  }
     81  return *this;
    8382}
    8483//-------------------------------------------------------------------
    8584// Clone
    8685//-------------------------------------------------------------------
    87 ClpConstraint * ClpConstraintLinear::clone() const
    88 {
    89      return new ClpConstraintLinear(*this);
     86ClpConstraint *ClpConstraintLinear::clone() const
     87{
     88  return new ClpConstraintLinear(*this);
    9089}
    9190
    9291// Returns gradient
    93 int
    94 ClpConstraintLinear::gradient(const ClpSimplex * model,
    95                               const double * solution,
    96                               double * gradient,
    97                               double & functionValue,
    98                               double & offset,
    99                               bool useScaling,
    100                               bool refresh) const
    101 {
    102      if (refresh || !lastGradient_) {
    103           functionValue_ = 0.0;
    104           if (!lastGradient_)
    105                lastGradient_ = new double[numberColumns_];
    106           CoinZeroN(lastGradient_, numberColumns_);
    107           bool scaling = (model && model->rowScale() && useScaling);
    108           if (!scaling) {
    109                for (int i = 0; i < numberCoefficients_; i++) {
    110                     int iColumn = column_[i];
    111                     double value = solution[iColumn];
    112                     double coefficient = coefficient_[i];
    113                     functionValue_ += value * coefficient;
    114                     lastGradient_[iColumn] = coefficient;
    115                }
    116           } else {
    117                // do scaling
    118                const double * columnScale = model->columnScale();
    119                for (int i = 0; i < numberCoefficients_; i++) {
    120                     int iColumn = column_[i];
    121                     double value = solution[iColumn]; // already scaled
    122                     double coefficient = coefficient_[i] * columnScale[iColumn];
    123                     functionValue_ += value * coefficient;
    124                     lastGradient_[iColumn] = coefficient;
    125                }
    126           }
    127      }
    128      functionValue = functionValue_;
    129      offset = 0.0;
    130      CoinMemcpyN(lastGradient_, numberColumns_, gradient);
    131      return 0;
     92int ClpConstraintLinear::gradient(const ClpSimplex *model,
     93  const double *solution,
     94  double *gradient,
     95  double &functionValue,
     96  double &offset,
     97  bool useScaling,
     98  bool refresh) const
     99{
     100  if (refresh || !lastGradient_) {
     101    functionValue_ = 0.0;
     102    if (!lastGradient_)
     103      lastGradient_ = new double[numberColumns_];
     104    CoinZeroN(lastGradient_, numberColumns_);
     105    bool scaling = (model && model->rowScale() && useScaling);
     106    if (!scaling) {
     107      for (int i = 0; i < numberCoefficients_; i++) {
     108        int iColumn = column_[i];
     109        double value = solution[iColumn];
     110        double coefficient = coefficient_[i];
     111        functionValue_ += value * coefficient;
     112        lastGradient_[iColumn] = coefficient;
     113      }
     114    } else {
     115      // do scaling
     116      const double *columnScale = model->columnScale();
     117      for (int i = 0; i < numberCoefficients_; i++) {
     118        int iColumn = column_[i];
     119        double value = solution[iColumn]; // already scaled
     120        double coefficient = coefficient_[i] * columnScale[iColumn];
     121        functionValue_ += value * coefficient;
     122        lastGradient_[iColumn] = coefficient;
     123      }
     124    }
     125  }
     126  functionValue = functionValue_;
     127  offset = 0.0;
     128  CoinMemcpyN(lastGradient_, numberColumns_, gradient);
     129  return 0;
    132130}
    133131// Resize constraint
    134 void
    135 ClpConstraintLinear::resize(int newNumberColumns)
    136 {
    137      if (numberColumns_ != newNumberColumns) {
     132void ClpConstraintLinear::resize(int newNumberColumns)
     133{
     134  if (numberColumns_ != newNumberColumns) {
    138135#ifndef NDEBUG
    139           int lastColumn = column_[numberCoefficients_-1];
     136    int lastColumn = column_[numberCoefficients_ - 1];
    140137#endif
    141           assert (newNumberColumns > lastColumn);
    142           delete [] lastGradient_;
    143           lastGradient_ = NULL;
    144           numberColumns_ = newNumberColumns;
    145      }
     138    assert(newNumberColumns > lastColumn);
     139    delete[] lastGradient_;
     140    lastGradient_ = NULL;
     141    numberColumns_ = newNumberColumns;
     142  }
    146143}
    147144// Delete columns in  constraint
    148 void
    149 ClpConstraintLinear::deleteSome(int numberToDelete, const int * which)
    150 {
    151      if (numberToDelete) {
    152           int i ;
    153           char * deleted = new char[numberColumns_];
    154           memset(deleted, 0, numberColumns_ * sizeof(char));
    155           for (i = 0; i < numberToDelete; i++) {
    156                int j = which[i];
    157                if (j >= 0 && j < numberColumns_ && !deleted[j]) {
    158                     deleted[j] = 1;
    159                }
    160           }
    161           int n = 0;
    162           for (i = 0; i < numberCoefficients_; i++) {
    163                int iColumn = column_[i];
    164                if (!deleted[iColumn]) {
    165                     column_[n] = iColumn;
    166                     coefficient_[n++] = coefficient_[i];
    167                }
    168           }
    169           numberCoefficients_ = n;
    170      }
     145void ClpConstraintLinear::deleteSome(int numberToDelete, const int *which)
     146{
     147  if (numberToDelete) {
     148    int i;
     149    char *deleted = new char[numberColumns_];
     150    memset(deleted, 0, numberColumns_ * sizeof(char));
     151    for (i = 0; i < numberToDelete; i++) {
     152      int j = which[i];
     153      if (j >= 0 && j < numberColumns_ && !deleted[j]) {
     154        deleted[j] = 1;
     155      }
     156    }
     157    int n = 0;
     158    for (i = 0; i < numberCoefficients_; i++) {
     159      int iColumn = column_[i];
     160      if (!deleted[iColumn]) {
     161        column_[n] = iColumn;
     162        coefficient_[n++] = coefficient_[i];
     163      }
     164    }
     165    numberCoefficients_ = n;
     166  }
    171167}
    172168// Scale constraint
    173 void
    174 ClpConstraintLinear::reallyScale(const double * columnScale)
    175 {
    176      for (int i = 0; i < numberCoefficients_; i++) {
    177           int iColumn = column_[i];
    178           coefficient_[i] *= columnScale[iColumn];
    179      }
     169void ClpConstraintLinear::reallyScale(const double *columnScale)
     170{
     171  for (int i = 0; i < numberCoefficients_; i++) {
     172    int iColumn = column_[i];
     173    coefficient_[i] *= columnScale[iColumn];
     174  }
    180175}
    181176/* Given a zeroed array sets nonlinear columns to 1.
    182177   Returns number of nonlinear columns
    183178*/
    184 int
    185 ClpConstraintLinear::markNonlinear(char *) const
    186 {
    187      return 0;
     179int ClpConstraintLinear::markNonlinear(char *) const
     180{
     181  return 0;
    188182}
    189183/* Given a zeroed array sets possible nonzero coefficients to 1.
    190184   Returns number of nonzeros
    191185*/
    192 int
    193 ClpConstraintLinear::markNonzero(char * which) const
    194 {
    195      for (int i = 0; i < numberCoefficients_; i++) {
    196           int iColumn = column_[i];
    197           which[iColumn] = 1;
    198      }
    199      return numberCoefficients_;
     186int ClpConstraintLinear::markNonzero(char *which) const
     187{
     188  for (int i = 0; i < numberCoefficients_; i++) {
     189    int iColumn = column_[i];
     190    which[iColumn] = 1;
     191  }
     192  return numberCoefficients_;
    200193}
    201194// Number of coefficients
    202 int
    203 ClpConstraintLinear::numberCoefficients() const
    204 {
    205      return numberCoefficients_;
    206 }
     195int ClpConstraintLinear::numberCoefficients() const
     196{
     197  return numberCoefficients_;
     198}
     199
     200/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
     201*/
Note: See TracChangeset for help on using the changeset viewer.