OR-Tools  9.6
lp_data/lp_utils.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
15 
16 #include <algorithm>
17 
19 
20 namespace operations_research {
21 namespace glop {
22 
23 template <typename SparseColumnLike>
24 Fractional SquaredNormTemplate(const SparseColumnLike& column) {
25  Fractional sum(0.0);
26  for (const SparseColumn::Entry e : column) {
27  sum += Square(e.coefficient());
28  }
29  return sum;
30 }
31 
33  return SquaredNormTemplate<SparseColumn>(v);
34 }
35 
37  return SquaredNormTemplate<ColumnView>(v);
38 }
39 
41  KahanSum sum;
42  for (const SparseColumn::Entry e : v) {
43  sum.Add(Square(e.coefficient()));
44  }
45  return sum.Value();
46 }
47 
49  if (v.ShouldUseDenseIteration()) {
50  return SquaredNorm(v.values);
51  }
52  Fractional sum(0.0);
53  for (const RowIndex row : v.non_zeros) {
54  sum += Square(v[row]);
55  }
56  return sum;
57 }
58 
60  if (v.ShouldUseDenseIteration()) {
61  return PreciseSquaredNorm(v.values);
62  }
63  KahanSum sum;
64  for (const RowIndex row : v.non_zeros) {
65  sum.Add(Square(v[row]));
66  }
67  return sum.Value();
68 }
69 
71  Fractional sum(0.0);
72  RowIndex row(0);
73  const size_t num_blocks = column.size().value() / 4;
74  for (size_t i = 0; i < num_blocks; ++i) {
75  // See the comment in ScalarProduct in the header for some notes about the
76  // effect of adding up several squares at a time.
77  sum += Square(column[row++]) + Square(column[row++]) +
78  Square(column[row++]) + Square(column[row++]);
79  }
80  while (row < column.size()) {
81  sum += Square(column[row++]);
82  }
83  return sum;
84 }
85 
87  KahanSum sum;
88  for (RowIndex row(0); row < column.size(); ++row) {
89  sum.Add(Square(column[row]));
90  }
91  return sum.Value();
92 }
93 
95  Fractional infinity_norm = 0.0;
96  for (RowIndex row(0); row < v.size(); ++row) {
97  infinity_norm = std::max(infinity_norm, fabs(v[row]));
98  }
99  return infinity_norm;
100 }
101 
102 template <typename SparseColumnLike>
103 Fractional InfinityNormTemplate(const SparseColumnLike& column) {
104  Fractional infinity_norm = 0.0;
105  for (const SparseColumn::Entry e : column) {
106  infinity_norm = std::max(infinity_norm, fabs(e.coefficient()));
107  }
108  return infinity_norm;
109 }
110 
112  return InfinityNormTemplate<SparseColumn>(v);
113 }
114 
116  return InfinityNormTemplate<ColumnView>(v);
117 }
118 
119 double Density(const DenseRow& row) {
120  if (row.empty()) return 0.0;
121  int sum = 0.0;
122  for (ColIndex col(0); col < row.size(); ++col) {
123  if (row[col] != Fractional(0.0)) ++sum;
124  }
125  return static_cast<double>(sum) / row.size().value();
126 }
127 
129  if (threshold == Fractional(0.0)) return;
130  for (ColIndex col(0); col < row->size(); ++col) {
131  if (fabs((*row)[col]) < threshold) {
132  (*row)[col] = Fractional(0.0);
133  }
134  }
135 }
136 
138  if (threshold == Fractional(0.0)) return;
139  for (RowIndex row(0); row < column->size(); ++row) {
140  if (fabs((*column)[row]) < threshold) {
141  (*column)[row] = Fractional(0.0);
142  }
143  }
144 }
145 
147  const DenseBooleanColumn& rows_to_consider,
148  RowIndex* row_index) {
149  Fractional infinity_norm = 0.0;
150  for (const SparseColumn::Entry e : column) {
151  if (rows_to_consider[e.row()] && fabs(e.coefficient()) > infinity_norm) {
152  infinity_norm = fabs(e.coefficient());
153  *row_index = e.row();
154  }
155  }
156  return infinity_norm;
157 }
158 
160  for (const SparseColumn::Entry e : column) {
161  if (e.coefficient() != 0.0) {
162  (*b)[e.row()] = false;
163  }
164  }
165 }
166 
167 bool IsDominated(const ColumnView& column, const DenseColumn& radius) {
168  for (const SparseColumn::Entry e : column) {
169  DCHECK_GE(radius[e.row()], 0.0);
170  if (fabs(e.coefficient()) > radius[e.row()]) return false;
171  }
172  return true;
173 }
174 
175 } // namespace glop
176 } // namespace operations_research
int64_t max
Definition: alldiff_cst.cc:140
void Add(const FpNumber &value)
Definition: accurate_sum.h:29
int64_t b
ColIndex col
Definition: markowitz.cc:186
RowIndex row
Definition: markowitz.cc:185
Fractional Square(Fractional f)
Fractional PreciseSquaredNorm(const SparseColumn &v)
Fractional InfinityNorm(const DenseColumn &v)
Fractional SquaredNorm(const SparseColumn &v)
void RemoveNearZeroEntries(Fractional threshold, DenseRow *row)
Fractional InfinityNormTemplate(const SparseColumnLike &column)
double Density(const DenseRow &row)
void SetSupportToFalse(const ColumnView &column, DenseBooleanColumn *b)
Fractional SquaredNormTemplate(const SparseColumnLike &column)
bool IsDominated(const ColumnView &column, const DenseColumn &radius)
Fractional RestrictedInfinityNorm(const ColumnView &column, const DenseBooleanColumn &rows_to_consider, RowIndex *row_index)
Collection of objects used to extend the Constraint Solver library.
int column
Definition: parse_proto.cc:32
bool ShouldUseDenseIteration(double ratio_for_using_dense_representation) const
StrictITIVector< Index, Fractional > values