OR-Tools  9.6
sharded_quadratic_program.h
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 
14 #ifndef PDLP_SHARDED_QUADRATIC_PROGRAM_H_
15 #define PDLP_SHARDED_QUADRATIC_PROGRAM_H_
16 
17 #include <cstdint>
18 #include <memory>
19 
20 #include "Eigen/Core"
21 #include "Eigen/SparseCore"
24 #include "ortools/pdlp/sharder.h"
25 
26 namespace operations_research::pdlp {
27 
28 // This class stores:
29 // - A `QuadraticProgram` (QP)
30 // - A transposed version of the QP's constraint matrix
31 // - A thread pool
32 // - Various `Sharder` objects for doing sharded matrix and vector
33 // computations.
35  public:
36  // Requires `num_shards` >= `num_threads` >= 1.
37  // Note that the `qp` is intentionally passed by value.
38  ShardedQuadraticProgram(QuadraticProgram qp, int num_threads, int num_shards);
39 
40  // Movable but not copyable.
45 
46  const QuadraticProgram& Qp() const { return qp_; }
47 
48  // Returns a reference to the transpose of the QP's constraint matrix.
49  const Eigen::SparseMatrix<double, Eigen::ColMajor, int64_t>&
51  return transposed_constraint_matrix_;
52  }
53 
54  // Returns a `Sharder` intended for the columns of the QP's constraint matrix.
56  return constraint_matrix_sharder_;
57  }
58  // Returns a `Sharder` intended for the rows of the QP's constraint matrix.
60  return transposed_constraint_matrix_sharder_;
61  }
62  // Returns a `Sharder` intended for primal vectors.
63  const Sharder& PrimalSharder() const { return primal_sharder_; }
64  // Returns a `Sharder` intended for dual vectors.
65  const Sharder& DualSharder() const { return dual_sharder_; }
66 
67  int64_t PrimalSize() const { return qp_.variable_lower_bounds.size(); }
68  int64_t DualSize() const { return qp_.constraint_lower_bounds.size(); }
69 
70  // Rescales the QP (including objective, variable bounds, constraint bounds,
71  // constraint matrix, and transposed constraint matrix) based on
72  // `col_scaling_vec` and `row_scaling_vec`. That is, rescale the problem so
73  // that each variable is rescaled as variable[i] <- variable[i] /
74  // `col_scaling_vec[i]`, and the j-th constraint is multiplied by
75  // `row_scaling_vec[j]`. `col_scaling_vec` and `row_scaling_vec` must be
76  // positive.
77  void RescaleQuadraticProgram(const Eigen::VectorXd& col_scaling_vec,
78  const Eigen::VectorXd& row_scaling_vec);
79 
81  Eigen::VectorXd& variable_upper_bounds) {
84  }
85 
86  void SwapConstraintBounds(Eigen::VectorXd& constraint_lower_bounds,
87  Eigen::VectorXd& constraint_upper_bounds) {
88  qp_.constraint_lower_bounds.swap(constraint_lower_bounds);
89  qp_.constraint_upper_bounds.swap(constraint_upper_bounds);
90  }
91 
92  // Swaps `objective` with the `objective_vector` in the quadratic program.
93  // Swapping `objective_matrix` is not yet supported because it hasn't been
94  // needed.
95  void SwapObjectiveVector(Eigen::VectorXd& objective) {
96  qp_.objective_vector.swap(objective);
97  }
98 
99  private:
100  QuadraticProgram qp_;
101  Eigen::SparseMatrix<double, Eigen::ColMajor, int64_t>
102  transposed_constraint_matrix_;
103  std::unique_ptr<ThreadPool> thread_pool_;
104  Sharder constraint_matrix_sharder_;
105  Sharder transposed_constraint_matrix_sharder_;
106  Sharder primal_sharder_;
107  Sharder dual_sharder_;
108 };
109 
110 } // namespace operations_research::pdlp
111 
112 #endif // PDLP_SHARDED_QUADRATIC_PROGRAM_H_
void RescaleQuadraticProgram(const Eigen::VectorXd &col_scaling_vec, const Eigen::VectorXd &row_scaling_vec)
ShardedQuadraticProgram(QuadraticProgram qp, int num_threads, int num_shards)
const Eigen::SparseMatrix< double, Eigen::ColMajor, int64_t > & TransposedConstraintMatrix() const
ShardedQuadraticProgram & operator=(ShardedQuadraticProgram &&)=default
ShardedQuadraticProgram(ShardedQuadraticProgram &&)=default
void SwapConstraintBounds(Eigen::VectorXd &constraint_lower_bounds, Eigen::VectorXd &constraint_upper_bounds)
ShardedQuadraticProgram(const ShardedQuadraticProgram &)=delete
void SwapVariableBounds(Eigen::VectorXd &variable_lower_bounds, Eigen::VectorXd &variable_upper_bounds)
ShardedQuadraticProgram & operator=(const ShardedQuadraticProgram &)=delete
VectorXd variable_lower_bounds
VectorXd variable_upper_bounds