source: branches/devel/Clp/src/ClpConstraintLinear.cpp @ 997

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

for nonlinear

File size: 5.3 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 numberCoefficents , 
31                                          int numberColumns,
32                                          const int * column, const double * coefficient)
33  : ClpConstraint()
34{
35  type_=0;
36  numberColumns_ = numberColumns;
37  numberCoefficients_=numberCoefficents;
38  column_ = CoinCopyOfArray(column,numberCoefficients_);
39  coefficient_ = CoinCopyOfArray(coefficient,numberCoefficients_);
40  CoinSort_2(column_,column_+numberCoefficients_,coefficient_);
41}
42
43//-------------------------------------------------------------------
44// Copy constructor
45//-------------------------------------------------------------------
46ClpConstraintLinear::ClpConstraintLinear (const ClpConstraintLinear & rhs)
47: ClpConstraint(rhs)
48{ 
49  numberColumns_=rhs.numberColumns_;
50  numberCoefficients_=rhs.numberCoefficients_;
51  column_ = CoinCopyOfArray(rhs.column_,numberCoefficients_);
52  coefficient_ = CoinCopyOfArray(rhs.coefficient_,numberCoefficients_);
53}
54 
55
56//-------------------------------------------------------------------
57// Destructor
58//-------------------------------------------------------------------
59ClpConstraintLinear::~ClpConstraintLinear ()
60{
61  delete [] column_;
62  delete [] coefficient_;
63}
64
65//----------------------------------------------------------------
66// Assignment operator
67//-------------------------------------------------------------------
68ClpConstraintLinear &
69ClpConstraintLinear::operator=(const ClpConstraintLinear& rhs)
70{
71  if (this != &rhs) {
72    delete [] column_;
73    delete [] coefficient_;
74    numberColumns_=rhs.numberColumns_;
75    numberCoefficients_=rhs.numberCoefficients_;
76    column_ = CoinCopyOfArray(rhs.column_,numberCoefficients_);
77    coefficient_ = CoinCopyOfArray(rhs.coefficient_,numberCoefficients_);
78  }
79  return *this;
80}
81//-------------------------------------------------------------------
82// Clone
83//-------------------------------------------------------------------
84ClpConstraint * ClpConstraintLinear::clone() const
85{
86  return new ClpConstraintLinear(*this);
87}
88
89// Returns gradient
90int 
91ClpConstraintLinear::gradient(const ClpSimplex * model,
92                              const double * solution, 
93                              double * gradient,
94                              double & functionValue, bool refresh) const
95{
96  if (refresh||!lastGradient_) {
97    functionValue_=0.0;
98    if (!lastGradient_)
99      lastGradient_ = new double[numberColumns_];
100    CoinZeroN(lastGradient_,numberColumns_);
101    bool scaling=(model&&model->rowScale());
102    if (!scaling) {
103      for (int i=0;i<numberCoefficients_;i++) {
104        int iColumn = column_[i];
105        double value = solution[iColumn];
106        double coefficient = coefficient_[i];
107        functionValue_ += value*coefficient;
108        lastGradient_[iColumn]=coefficient;
109      }
110    } else {
111      // do scaling
112      const double * columnScale = model->columnScale();
113      for (int i=0;i<numberCoefficients_;i++) {
114        int iColumn = column_[i];
115        double value = solution[iColumn]; // already scaled
116        double coefficient = coefficient_[i]*columnScale[iColumn];
117        functionValue_ += value*coefficient;
118        lastGradient_[iColumn]=coefficient;
119      }
120    }
121  }
122  functionValue = functionValue_;
123  memcpy(gradient,lastGradient_,numberColumns_*sizeof(double));
124  return 0;
125}
126// Resize constraint
127void 
128ClpConstraintLinear::resize(int newNumberColumns)
129{
130  if (numberColumns_!=newNumberColumns) {
131    int lastColumn = column_[numberCoefficients_-1];
132    assert (newNumberColumns>lastColumn);
133    delete [] lastGradient_;
134    lastGradient_ = NULL;
135    numberColumns_ = newNumberColumns;
136  } 
137}
138// Delete columns in  constraint
139void 
140ClpConstraintLinear::deleteSome(int numberToDelete, const int * which) 
141{
142  if (numberToDelete) {
143    int i ;
144    char * deleted = new char[numberColumns_];
145    memset(deleted,0,numberColumns_*sizeof(char));
146    for (i=0;i<numberToDelete;i++) {
147      int j = which[i];
148      if (j>=0&&j<numberColumns_&&!deleted[j]) {
149        deleted[j]=1;
150      }
151    }
152    int n=0;
153    for (i=0;i<numberCoefficients_;i++) {
154      int iColumn = column_[i];
155      if (!deleted[iColumn]) {
156        column_[n]=iColumn;
157        coefficient_[n++]=coefficient_[i];
158      }
159    }
160    numberCoefficients_ = n;
161  }
162}
163// Scale constraint
164void 
165ClpConstraintLinear::reallyScale(const double * columnScale) 
166{
167  for (int i=0;i<numberCoefficients_;i++) {
168    int iColumn = column_[i];
169    coefficient_[i] *= columnScale[iColumn];
170  }
171}
172/* Given a zeroed array sets nonlinear columns to 1.
173   Returns number of nonlinear columns
174*/
175int 
176ClpConstraintLinear::markNonlinear(char * which) const
177{
178  return 0;
179}
Note: See TracBrowser for help on using the repository browser.