Files
ortools-clone/ortools/glop/update_row.h

177 lines
6.9 KiB
C
Raw Permalink Normal View History

2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 Google LLC
2014-07-08 17:35:15 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2025-11-05 11:34:49 +01:00
#ifndef ORTOOLS_GLOP_UPDATE_ROW_H_
#define ORTOOLS_GLOP_UPDATE_ROW_H_
2021-03-04 18:26:01 +01:00
#include <cstdint>
2022-05-02 16:51:52 +02:00
#include <string>
2022-07-22 14:35:40 +02:00
#include <vector>
2021-03-04 18:26:01 +01:00
2024-01-18 14:20:21 +01:00
#include "absl/types/span.h"
#include "ortools/glop/basis_representation.h"
#include "ortools/glop/parameters.pb.h"
#include "ortools/glop/variables_info.h"
#include "ortools/lp_data/lp_types.h"
2019-07-24 13:49:08 -07:00
#include "ortools/lp_data/scattered_vector.h"
2024-01-18 14:20:21 +01:00
#include "ortools/lp_data/sparse.h"
#include "ortools/util/stats.h"
namespace operations_research {
namespace glop {
// During a simplex iteration, when the basis 'leaving_row' has been
// selected, one of the main quantities needed in the primal or dual simplex
// algorithm is called the update row.
//
// By definition, update_row[col] is the coefficient at position
// 'leaving_row' in the current basis of the column 'col' of the matrix A.
//
// One efficient way to compute it is to compute the left inverse by B of the
// unit vector with a one at the given leaving_row, and then to take the
// scalar product of this left inverse with all the columns of A:
// update_row[col] = (unit_{leaving_row} . B^{-1}) . A_col
class UpdateRow {
2020-10-22 23:36:58 +02:00
public:
// Takes references to the linear program data we need.
2020-10-29 14:25:39 +01:00
UpdateRow(const CompactSparseMatrix& matrix,
const CompactSparseMatrix& transposed_matrix,
const VariablesInfo& variables_info, const RowToColMapping& basis,
const BasisFactorization& basis_factorization);
2023-08-30 10:04:47 -04:00
// This type is neither copyable nor movable.
UpdateRow(const UpdateRow&) = delete;
UpdateRow& operator=(const UpdateRow&) = delete;
// Invalidates the current update row and unit_row_left_inverse so the next
// call to ComputeUpdateRow() will recompute everything and not just return
// right away.
void Invalidate();
2022-11-07 16:39:23 +01:00
// Computes the left inverse of the given unit row, and stores it in
// unit_row_left_inverse_. The result is computed only once if leaving_row do
// not change, this until the next Invalidate() call.
void ComputeUnitRowLeftInverse(RowIndex leaving_row);
// Computes the relevant coefficients (See GetIsRelevantBitRow() in
2022-11-07 16:39:23 +01:00
// VariablesInfo) of the update row. The result is only computed once
// if leaving_row do not change, this until the next Invalidate() call.
void ComputeUpdateRow(RowIndex leaving_row);
// Returns the left inverse of the unit row as computed by the last call to
2021-09-15 17:37:52 +02:00
// ComputeUpdateRow().
2020-10-29 14:25:39 +01:00
const ScatteredRow& GetUnitRowLeftInverse() const;
2021-09-15 17:37:52 +02:00
// Returns true if ComputeUpdateRow() was called since the last Invalidate().
2023-05-24 15:33:13 +02:00
bool IsComputedFor(RowIndex leaving_row) const {
2022-11-07 16:39:23 +01:00
return update_row_computed_for_ == leaving_row;
}
2021-09-15 17:37:52 +02:00
// Returns the update coefficients and non-zero positions corresponding to the
// last call to ComputeUpdateRow().
2022-11-07 16:39:23 +01:00
//
// TODO(user): Consider returning a packed vector of coefficient parallel to
// GetNonZeroPositions() instead. It should be fast to compute and iteration
// later should be quicker.
2020-10-29 14:25:39 +01:00
const DenseRow& GetCoefficients() const;
2023-10-15 17:53:48 +02:00
absl::Span<const ColIndex> GetNonZeroPositions() const;
2023-05-24 15:33:13 +02:00
Fractional GetCoefficient(ColIndex col) const { return coefficient_[col]; }
2022-11-07 16:39:23 +01:00
// Computes the update row including all position and fill output with it.
// We only use this when ComputeUnitRowLeftInverse() has already been called
// and we CHECK that.
void ComputeFullUpdateRow(RowIndex leaving_row, DenseRow* output) const;
2017-03-28 16:07:49 +02:00
// Sets the algorithm parameters.
2020-10-29 14:25:39 +01:00
void SetParameters(const GlopParameters& parameters);
// Returns statistics about this class as a string.
std::string StatString() const { return stats_.StatString(); }
// Only used for testing.
// Computes as the update row the product 'lhs' times the linear program
// matrix given at construction. Only the relevant columns matter (see
// VariablesInfo) and 'algorithm' can be one of "column", "row" or
// "row_hypersparse".
2020-10-29 14:25:39 +01:00
void ComputeUpdateRowForBenchmark(const DenseRow& lhs,
const std::string& algorithm);
// Deterministic time used by the scalar product computation of this class.
double DeterministicTime() const {
return DeterministicTimeForFpOperations(num_operations_);
}
2019-01-02 20:19:25 +01:00
// This returns the asked unit row left inverse. It temporarily invalidate
// the class state by calling Invalidate().
2020-10-29 14:25:39 +01:00
const ScatteredRow& ComputeAndGetUnitRowLeftInverse(RowIndex leaving_row);
2019-01-02 20:19:25 +01:00
2021-09-15 17:37:52 +02:00
private:
2019-07-18 11:35:32 -07:00
// ComputeUpdateRow() does the common work and calls one of these functions
// depending on the situation.
void ComputeUpdatesRowWise();
void ComputeUpdatesRowWiseHypersparse();
void ComputeUpdatesColumnWise();
2022-11-07 16:39:23 +01:00
void ComputeUpdatesForSingleRow(ColIndex row_as_col);
// Problem data that should be updated from outside.
2020-10-29 14:25:39 +01:00
const CompactSparseMatrix& matrix_;
const CompactSparseMatrix& transposed_matrix_;
const VariablesInfo& variables_info_;
const RowToColMapping& basis_;
const BasisFactorization& basis_factorization_;
// Left inverse by B of a unit row. Its scalar product with a column 'a' of A
// gives the value of the right inverse of 'a' on the 'leaving_row'.
2018-02-12 11:35:51 +01:00
ScatteredRow unit_row_left_inverse_;
2018-07-17 10:32:22 -07:00
// The non-zeros of unit_row_left_inverse_ above the drop tolerance.
std::vector<ColIndex> unit_row_left_inverse_filtered_non_zeros_;
// Holds the current update row data.
2022-11-07 16:39:23 +01:00
// Note that non_zero_position_set_ is not always up to date.
2023-10-15 17:53:48 +02:00
int num_non_zeros_ = 0;
ColIndexVector non_zero_position_list_;
DenseBitRow non_zero_position_set_;
DenseRow coefficient_;
// Boolean used to avoid recomputing many times the same thing.
2022-11-07 16:39:23 +01:00
RowIndex left_inverse_computed_for_ = kInvalidRow;
RowIndex update_row_computed_for_ = kInvalidRow;
// Statistics about this class.
struct Stats : public StatsGroup {
Stats()
: StatsGroup("UpdateRow"),
unit_row_left_inverse_density("unit_row_left_inverse_density", this),
unit_row_left_inverse_accuracy("unit_row_left_inverse_accuracy",
this),
update_row_density("update_row_density", this) {}
RatioDistribution unit_row_left_inverse_density;
DoubleDistribution unit_row_left_inverse_accuracy;
RatioDistribution update_row_density;
};
// Track the number of basic floating point multiplication.
// Used by DeterministicTime().
2021-03-04 18:26:01 +01:00
int64_t num_operations_;
// Glop standard classes.
GlopParameters parameters_;
Stats stats_;
};
2020-10-22 23:36:58 +02:00
} // namespace glop
} // namespace operations_research
2025-11-05 11:34:49 +01:00
#endif // ORTOOLS_GLOP_UPDATE_ROW_H_