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

Last change on this file since 1402 was 1402, checked in by forrest, 10 years ago

get rid of compiler warnings

  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1/* $Id: ClpConstraintLinear.cpp 1402 2009-07-25 08:39:55Z forrest $ */
2// Copyright (C) 2007, International Business Machines
3// Corporation and others.  All Rights Reserved.
4
5#include "CoinPragma.hpp"
6#include "CoinHelperFunctions.hpp"
7#include "CoinIndexedVector.hpp"
8#include "ClpSimplex.hpp"
9#include "ClpConstraintLinear.hpp"
10#include "CoinSort.hpp"
11//#############################################################################
12// Constructors / Destructor / Assignment
13//#############################################################################
14
15//-------------------------------------------------------------------
16// Default Constructor
17//-------------------------------------------------------------------
18ClpConstraintLinear::ClpConstraintLinear () 
19: ClpConstraint()
20{
21  type_=0;
22  column_=NULL;
23  coefficient_ = NULL;
24  numberColumns_=0;
25  numberCoefficients_=0;
26}
27
28//-------------------------------------------------------------------
29// Useful Constructor
30//-------------------------------------------------------------------
31ClpConstraintLinear::ClpConstraintLinear (int row, int numberCoefficents , 
32                                          int numberColumns,
33                                          const int * column, const double * coefficient)
34  : ClpConstraint()
35{
36  type_=0;
37  rowNumber_=row;
38  numberColumns_ = numberColumns;
39  numberCoefficients_=numberCoefficents;
40  column_ = CoinCopyOfArray(column,numberCoefficients_);
41  coefficient_ = CoinCopyOfArray(coefficient,numberCoefficients_);
42  CoinSort_2(column_,column_+numberCoefficients_,coefficient_);
43}
44
45//-------------------------------------------------------------------
46// Copy constructor
47//-------------------------------------------------------------------
48ClpConstraintLinear::ClpConstraintLinear (const ClpConstraintLinear & rhs)
49: ClpConstraint(rhs)
50{ 
51  numberColumns_=rhs.numberColumns_;
52  numberCoefficients_=rhs.numberCoefficients_;
53  column_ = CoinCopyOfArray(rhs.column_,numberCoefficients_);
54  coefficient_ = CoinCopyOfArray(rhs.coefficient_,numberCoefficients_);
55}
56 
57
58//-------------------------------------------------------------------
59// Destructor
60//-------------------------------------------------------------------
61ClpConstraintLinear::~ClpConstraintLinear ()
62{
63  delete [] column_;
64  delete [] coefficient_;
65}
66
67//----------------------------------------------------------------
68// Assignment operator
69//-------------------------------------------------------------------
70ClpConstraintLinear &
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;
82}
83//-------------------------------------------------------------------
84// Clone
85//-------------------------------------------------------------------
86ClpConstraint * ClpConstraintLinear::clone() const
87{
88  return new ClpConstraintLinear(*this);
89}
90
91// Returns gradient
92int 
93ClpConstraintLinear::gradient(const ClpSimplex * model,
94                              const double * solution, 
95                              double * gradient,
96                              double & functionValue, 
97                              double & offset,
98                              bool useScaling,
99                              bool refresh) const
100{
101  if (refresh||!lastGradient_) {
102    functionValue_=0.0;
103    if (!lastGradient_)
104      lastGradient_ = new double[numberColumns_];
105    CoinZeroN(lastGradient_,numberColumns_);
106    bool scaling=(model&&model->rowScale()&&useScaling);
107    if (!scaling) {
108      for (int i=0;i<numberCoefficients_;i++) {
109        int iColumn = column_[i];
110        double value = solution[iColumn];
111        double coefficient = coefficient_[i];
112        functionValue_ += value*coefficient;
113        lastGradient_[iColumn]=coefficient;
114      }
115    } else {
116      // do scaling
117      const double * columnScale = model->columnScale();
118      for (int i=0;i<numberCoefficients_;i++) {
119        int iColumn = column_[i];
120        double value = solution[iColumn]; // already scaled
121        double coefficient = coefficient_[i]*columnScale[iColumn];
122        functionValue_ += value*coefficient;
123        lastGradient_[iColumn]=coefficient;
124      }
125    }
126  }
127  functionValue = functionValue_;
128  offset=0.0;
129  CoinMemcpyN(lastGradient_,numberColumns_,gradient);
130  return 0;
131}
132// Resize constraint
133void 
134ClpConstraintLinear::resize(int newNumberColumns)
135{
136  if (numberColumns_!=newNumberColumns) {
137#ifndef NDEBUG
138    int lastColumn = column_[numberCoefficients_-1];
139#endif
140    assert (newNumberColumns>lastColumn);
141    delete [] lastGradient_;
142    lastGradient_ = NULL;
143    numberColumns_ = newNumberColumns;
144  } 
145}
146// Delete columns in  constraint
147void 
148ClpConstraintLinear::deleteSome(int numberToDelete, const int * which) 
149{
150  if (numberToDelete) {
151    int i ;
152    char * deleted = new char[numberColumns_];
153    memset(deleted,0,numberColumns_*sizeof(char));
154    for (i=0;i<numberToDelete;i++) {
155      int j = which[i];
156      if (j>=0&&j<numberColumns_&&!deleted[j]) {
157        deleted[j]=1;
158      }
159    }
160    int n=0;
161    for (i=0;i<numberCoefficients_;i++) {
162      int iColumn = column_[i];
163      if (!deleted[iColumn]) {
164        column_[n]=iColumn;
165        coefficient_[n++]=coefficient_[i];
166      }
167    }
168    numberCoefficients_ = n;
169  }
170}
171// Scale constraint
172void 
173ClpConstraintLinear::reallyScale(const double * columnScale) 
174{
175  for (int i=0;i<numberCoefficients_;i++) {
176    int iColumn = column_[i];
177    coefficient_[i] *= columnScale[iColumn];
178  }
179}
180/* Given a zeroed array sets nonlinear columns to 1.
181   Returns number of nonlinear columns
182*/
183int 
184ClpConstraintLinear::markNonlinear(char *) const
185{
186  return 0;
187}
188/* Given a zeroed array sets possible nonzero coefficients to 1.
189   Returns number of nonzeros
190*/
191int 
192ClpConstraintLinear::markNonzero(char * which) const
193{
194  for (int i=0;i<numberCoefficients_;i++) {
195    int iColumn = column_[i];
196    which[iColumn]=1;
197  }
198  return numberCoefficients_;
199}
200// Number of coefficients
201int 
202ClpConstraintLinear::numberCoefficients() const
203{
204  return numberCoefficients_;
205}
Note: See TracBrowser for help on using the repository browser.