source: trunk/Clp/src/ClpConstraintLinear.cpp @ 1035

Last change on this file since 1035 was 1035, checked in by forrest, 13 years ago

forgot to add ClpConstraint?

File size: 5.8 KB
Line 
1// Copyright (C) 2007, International Business Machines
2// Corporation and others.  All Rights Reserved.
3
4#include "CoinPragma.hpp"
5#include "CoinHelperFunctions.hpp"
6#include "CoinIndexedVector.hpp"
7#include "ClpSimplex.hpp"
8#include "ClpConstraintLinear.hpp"
9#include "CoinSort.hpp"
10//#############################################################################
11// Constructors / Destructor / Assignment
12//#############################################################################
13
14//-------------------------------------------------------------------
15// Default Constructor
16//-------------------------------------------------------------------
17ClpConstraintLinear::ClpConstraintLinear () 
18: ClpConstraint()
19{
20  type_=0;
21  column_=NULL;
22  coefficient_ = NULL;
23  numberColumns_=0;
24  numberCoefficients_=0;
25}
26
27//-------------------------------------------------------------------
28// Useful Constructor
29//-------------------------------------------------------------------
30ClpConstraintLinear::ClpConstraintLinear (int row, int numberCoefficents , 
31                                          int numberColumns,
32                                          const int * column, const double * coefficient)
33  : ClpConstraint()
34{
35  type_=0;
36  rowNumber_=row;
37  numberColumns_ = numberColumns;
38  numberCoefficients_=numberCoefficents;
39  column_ = CoinCopyOfArray(column,numberCoefficients_);
40  coefficient_ = CoinCopyOfArray(coefficient,numberCoefficients_);
41  CoinSort_2(column_,column_+numberCoefficients_,coefficient_);
42}
43
44//-------------------------------------------------------------------
45// Copy constructor
46//-------------------------------------------------------------------
47ClpConstraintLinear::ClpConstraintLinear (const ClpConstraintLinear & rhs)
48: ClpConstraint(rhs)
49{ 
50  numberColumns_=rhs.numberColumns_;
51  numberCoefficients_=rhs.numberCoefficients_;
52  column_ = CoinCopyOfArray(rhs.column_,numberCoefficients_);
53  coefficient_ = CoinCopyOfArray(rhs.coefficient_,numberCoefficients_);
54}
55 
56
57//-------------------------------------------------------------------
58// Destructor
59//-------------------------------------------------------------------
60ClpConstraintLinear::~ClpConstraintLinear ()
61{
62  delete [] column_;
63  delete [] coefficient_;
64}
65
66//----------------------------------------------------------------
67// Assignment operator
68//-------------------------------------------------------------------
69ClpConstraintLinear &
70ClpConstraintLinear::operator=(const ClpConstraintLinear& rhs)
71{
72  if (this != &rhs) {
73    delete [] column_;
74    delete [] coefficient_;
75    numberColumns_=rhs.numberColumns_;
76    numberCoefficients_=rhs.numberCoefficients_;
77    column_ = CoinCopyOfArray(rhs.column_,numberCoefficients_);
78    coefficient_ = CoinCopyOfArray(rhs.coefficient_,numberCoefficients_);
79  }
80  return *this;
81}
82//-------------------------------------------------------------------
83// Clone
84//-------------------------------------------------------------------
85ClpConstraint * ClpConstraintLinear::clone() const
86{
87  return new ClpConstraintLinear(*this);
88}
89
90// Returns gradient
91int 
92ClpConstraintLinear::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  memcpy(gradient,lastGradient_,numberColumns_*sizeof(double));
129  return 0;
130}
131// Resize constraint
132void 
133ClpConstraintLinear::resize(int newNumberColumns)
134{
135  if (numberColumns_!=newNumberColumns) {
136#ifndef NDEBUG
137    int lastColumn = column_[numberCoefficients_-1];
138#endif
139    assert (newNumberColumns>lastColumn);
140    delete [] lastGradient_;
141    lastGradient_ = NULL;
142    numberColumns_ = newNumberColumns;
143  } 
144}
145// Delete columns in  constraint
146void 
147ClpConstraintLinear::deleteSome(int numberToDelete, const int * which) 
148{
149  if (numberToDelete) {
150    int i ;
151    char * deleted = new char[numberColumns_];
152    memset(deleted,0,numberColumns_*sizeof(char));
153    for (i=0;i<numberToDelete;i++) {
154      int j = which[i];
155      if (j>=0&&j<numberColumns_&&!deleted[j]) {
156        deleted[j]=1;
157      }
158    }
159    int n=0;
160    for (i=0;i<numberCoefficients_;i++) {
161      int iColumn = column_[i];
162      if (!deleted[iColumn]) {
163        column_[n]=iColumn;
164        coefficient_[n++]=coefficient_[i];
165      }
166    }
167    numberCoefficients_ = n;
168  }
169}
170// Scale constraint
171void 
172ClpConstraintLinear::reallyScale(const double * columnScale) 
173{
174  for (int i=0;i<numberCoefficients_;i++) {
175    int iColumn = column_[i];
176    coefficient_[i] *= columnScale[iColumn];
177  }
178}
179/* Given a zeroed array sets nonlinear columns to 1.
180   Returns number of nonlinear columns
181*/
182int 
183ClpConstraintLinear::markNonlinear(char * which) const
184{
185  return 0;
186}
187/* Given a zeroed array sets possible nonzero coefficients to 1.
188   Returns number of nonzeros
189*/
190int 
191ClpConstraintLinear::markNonzero(char * which) const
192{
193  for (int i=0;i<numberCoefficients_;i++) {
194    int iColumn = column_[i];
195    which[iColumn]=1;
196  }
197  return numberCoefficients_;
198}
199// Number of coefficients
200int 
201ClpConstraintLinear::numberCoefficients() const
202{
203  return numberCoefficients_;
204}
Note: See TracBrowser for help on using the repository browser.