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

Last change on this file since 2470 was 2385, checked in by unxusr, 8 months ago

formatting

  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1/* $Id: ClpConstraintLinear.cpp 2385 2019-01-06 19:43:06Z stefan $ */
2// Copyright (C) 2007, International Business Machines
3// Corporation and others.  All Rights Reserved.
4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
6#include "CoinPragma.hpp"
7#include "CoinHelperFunctions.hpp"
8#include "CoinIndexedVector.hpp"
9#include "ClpSimplex.hpp"
10#include "ClpConstraintLinear.hpp"
11#include "CoinSort.hpp"
12//#############################################################################
13// Constructors / Destructor / Assignment
14//#############################################################################
15
16//-------------------------------------------------------------------
17// Default Constructor
18//-------------------------------------------------------------------
19ClpConstraintLinear::ClpConstraintLinear()
20  : ClpConstraint()
21{
22  type_ = 0;
23  column_ = NULL;
24  coefficient_ = NULL;
25  numberColumns_ = 0;
26  numberCoefficients_ = 0;
27}
28
29//-------------------------------------------------------------------
30// Useful Constructor
31//-------------------------------------------------------------------
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_);
44}
45
46//-------------------------------------------------------------------
47// Copy constructor
48//-------------------------------------------------------------------
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}
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 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;
130}
131// Resize constraint
132void ClpConstraintLinear::resize(int newNumberColumns)
133{
134  if (numberColumns_ != newNumberColumns) {
135#ifndef NDEBUG
136    int lastColumn = column_[numberCoefficients_ - 1];
137#endif
138    assert(newNumberColumns > lastColumn);
139    delete[] lastGradient_;
140    lastGradient_ = NULL;
141    numberColumns_ = newNumberColumns;
142  }
143}
144// Delete columns in  constraint
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  }
167}
168// Scale constraint
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  }
175}
176/* Given a zeroed array sets nonlinear columns to 1.
177   Returns number of nonlinear columns
178*/
179int ClpConstraintLinear::markNonlinear(char *) const
180{
181  return 0;
182}
183/* Given a zeroed array sets possible nonzero coefficients to 1.
184   Returns number of nonzeros
185*/
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_;
193}
194// Number of coefficients
195int ClpConstraintLinear::numberCoefficients() const
196{
197  return numberCoefficients_;
198}
199
200/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
201*/
Note: See TracBrowser for help on using the repository browser.