OR-Tools  9.3
sharded_quadratic_program_test.cc
Go to the documentation of this file.
1// Copyright 2010-2021 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 <limits>
17
18#include "Eigen/Core"
19#include "gmock/gmock.h"
20#include "gtest/gtest.h"
24
26namespace {
27
28using ::testing::ElementsAre;
29
30const double kInfinity = std::numeric_limits<double>::infinity();
31
32TEST(ShardedQuadraticProgramTest, BasicTest) {
33 const int num_threads = 2;
34 const int num_shards = 10;
35 ShardedQuadraticProgram sharded_qp(TestDiagonalQp1(), num_threads,
36 num_shards);
37 const int primal_size = 2;
38 const int dual_size = 1;
39 EXPECT_EQ(sharded_qp.DualSize(), dual_size);
40 EXPECT_EQ(sharded_qp.PrimalSize(), primal_size);
41 EXPECT_THAT(ToDense(sharded_qp.TransposedConstraintMatrix()),
42 EigenArrayEq<double>({{1}, {1}}));
43 EXPECT_EQ(sharded_qp.ConstraintMatrixSharder().NumElements(), primal_size);
44 EXPECT_EQ(sharded_qp.DualSharder().NumElements(), dual_size);
45 EXPECT_EQ(sharded_qp.PrimalSharder().NumElements(), primal_size);
46 EXPECT_EQ(sharded_qp.TransposedConstraintMatrixSharder().NumElements(),
47 dual_size);
48}
49
50TEST(RescaleProblem, BasicTest) {
51 // The original qp is:
52 // min 4x_1^2 + x_2^2 - x_1 - x_2 +5
53 // s.t. -inf <= x_1 + x_2 <=1,
54 // 1<=x_1<=2, -2<=x_2<=4.
55 // After rescaling with row_scaling_vec [0.5] and col_scaling_vec [1,0.5],
56 // the new qp becomes:
57 // min 4x_1^2 + 0.25x_2^2 - x_1 - 0.5x_2 +5
58 // s.t. -inf <= 0.5x_1 + 0.25x_2 <=1,
59 // 1<=x_1<=2, -4<=x_2<=8.
60 const int num_threads = 2;
61 const int num_shards = 10;
62 ShardedQuadraticProgram sharded_qp(TestDiagonalQp1(), num_threads,
63 num_shards);
64 Eigen::VectorXd col_scaling_vec(2);
65 Eigen::VectorXd row_scaling_vec(1);
66 col_scaling_vec << 1, 0.5;
67 row_scaling_vec << 0.5;
68 sharded_qp.RescaleQuadraticProgram(col_scaling_vec, row_scaling_vec);
69
70 EXPECT_THAT(sharded_qp.Qp().constraint_lower_bounds, ElementsAre(-kInfinity));
71 EXPECT_THAT(sharded_qp.Qp().constraint_upper_bounds, ElementsAre(0.5));
72 EXPECT_THAT(sharded_qp.Qp().variable_lower_bounds, ElementsAre(1, -4));
73 EXPECT_THAT(sharded_qp.Qp().variable_upper_bounds, ElementsAre(2, 8));
74 EXPECT_THAT(sharded_qp.Qp().objective_vector, ElementsAre(-1, -0.5));
75 EXPECT_THAT(ToDense(sharded_qp.Qp().constraint_matrix),
76 EigenArrayEq<double>({{0.5, 0.25}}));
77 EXPECT_THAT(ToDense(sharded_qp.TransposedConstraintMatrix()),
78 EigenArrayEq<double>({{0.5}, {0.25}}));
79 EXPECT_THAT(sharded_qp.Qp().objective_matrix->diagonal(),
80 EigenArrayEq<double>({4, 0.25}));
81}
82
83} // namespace
84} // namespace operations_research::pdlp
::Eigen::ArrayXXd ToDense(const Eigen::SparseMatrix< double, Eigen::ColMajor, int64_t > &sparse_mat)
Definition: test_util.cc:284
QuadraticProgram TestDiagonalQp1()
Definition: test_util.cc:142
TEST(LinearAssignmentTest, NullMatrix)