forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primal_dual_hybrid_gradient_test.cc
2198 lines (1981 loc) · 95.1 KB
/
primal_dual_hybrid_gradient_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2010-2024 Google LLC
// 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.
#include "ortools/pdlp/primal_dual_hybrid_gradient.h"
#include <algorithm>
#include <atomic>
#include <cmath>
#include <cstdint>
#include <limits>
#include <optional>
#include <ostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "Eigen/Core"
#include "Eigen/SparseCore"
#include "absl/container/flat_hash_map.h"
#include "absl/log/check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "gtest/gtest.h"
#include "ortools/base/gmock.h"
#include "ortools/base/logging.h"
#include "ortools/glop/parameters.pb.h"
#include "ortools/linear_solver/linear_solver.pb.h"
#include "ortools/lp_data/lp_data.h"
#include "ortools/lp_data/lp_types.h"
#include "ortools/pdlp/iteration_stats.h"
#include "ortools/pdlp/quadratic_program.h"
#include "ortools/pdlp/quadratic_program_io.h"
#include "ortools/pdlp/sharded_quadratic_program.h"
#include "ortools/pdlp/solve_log.pb.h"
#include "ortools/pdlp/solvers.pb.h"
#include "ortools/pdlp/termination.h"
#include "ortools/pdlp/test_util.h"
namespace operations_research::pdlp {
namespace {
using ::Eigen::VectorXd;
using ::operations_research::glop::ConstraintStatus;
using ::operations_research::glop::VariableStatus;
using ::testing::AnyOf;
using ::testing::DoubleNear;
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::Ge;
using ::testing::HasSubstr;
using ::testing::IsEmpty;
using ::testing::Not;
using ::testing::Pair;
using ::testing::SizeIs;
using ::testing::UnorderedElementsAre;
const double kInfinity = std::numeric_limits<double>::infinity();
PrimalDualHybridGradientParams CreateSolverParams(
const int iteration_limit, const double eps_optimal_absolute,
const bool enable_scaling, const int num_threads,
const bool use_iteration_limit, const bool use_malitsky_pock_linesearch,
const bool use_diagonal_qp_trust_region_solver) {
PrimalDualHybridGradientParams params;
if (!enable_scaling) {
params.set_l2_norm_rescaling(false);
params.set_l_inf_ruiz_iterations(0);
}
if (use_malitsky_pock_linesearch) {
params.set_linesearch_rule(
PrimalDualHybridGradientParams::MALITSKY_POCK_LINESEARCH_RULE);
}
params.mutable_termination_criteria()
->mutable_simple_optimality_criteria()
->set_eps_optimal_relative(0.0);
if (use_iteration_limit) {
// This effectively forces convergence on the iteration limit only.
params.mutable_termination_criteria()->set_iteration_limit(iteration_limit);
params.mutable_termination_criteria()
->mutable_simple_optimality_criteria()
->set_eps_optimal_absolute(0.0);
} else {
params.mutable_termination_criteria()
->mutable_simple_optimality_criteria()
->set_eps_optimal_absolute(eps_optimal_absolute);
}
if (use_diagonal_qp_trust_region_solver) {
params.set_use_diagonal_qp_trust_region_solver(true);
params.set_diagonal_qp_trust_region_solver_tolerance(1.0e-8);
}
params.set_num_threads(num_threads);
// Protect against infinite loops if the termination criteria fail.
params.mutable_termination_criteria()->set_kkt_matrix_pass_limit(1'000'000.0);
return params;
}
// Verifies expected termination reason and iteration count for an instance
// where an optimal solution exists.
// `params` must have been generated by `CreateSolverParams()` with the same
// `use_iteration_limit`.
// Intended usage:
// const bool use_iteration_limit = ...;
// PrimalDualHybridGradientParams params =
// CreateSolverParams(..., use_iteration_limit, ...);
// SolverResult output = PrimalDualHybridGradient(..., params);
// VerifyTerminationReasonAndIterationCount(params, output,
// use_iteration_limit);
void VerifyTerminationReasonAndIterationCount(
const PrimalDualHybridGradientParams& params, const SolverResult& output,
const bool use_iteration_limit) {
if (use_iteration_limit) {
// When a PDHG step has zero length PDHG can no longer make progress and
// hence terminates immediately. In theory a zero step length implies
// optimality but in practice PDHG terminates with a reason of OPTIMAL if
// the optimality checks pass and NUMERICAL_ERROR otherwise.
// When `use_iteration_limit==true`, `CreateSolverParams()` sets all the
// epsilons to 0, which makes the optimality checks harder to pass but not
// impossible. Both OPTIMAL and NUMERICAL_ERROR are therefore ok termination
// reasons.
EXPECT_THAT(
output.solve_log.termination_reason(),
AnyOf(TERMINATION_REASON_ITERATION_LIMIT,
TERMINATION_REASON_NUMERICAL_ERROR, TERMINATION_REASON_OPTIMAL));
if (output.solve_log.termination_reason() ==
TERMINATION_REASON_ITERATION_LIMIT) {
EXPECT_EQ(output.solve_log.iteration_count(),
params.termination_criteria().iteration_limit());
} else {
EXPECT_LE(output.solve_log.iteration_count(),
params.termination_criteria().iteration_limit());
}
} else {
EXPECT_EQ(output.solve_log.termination_reason(),
TERMINATION_REASON_OPTIMAL);
EXPECT_LE(output.solve_log.iteration_count(),
params.termination_criteria().iteration_limit());
}
}
// Verifies the primal and dual objective values.
void VerifyObjectiveValues(const SolverResult& result,
const double objective_value,
const double tolerance) {
const auto& convergence_info = GetConvergenceInformation(
result.solve_log.solution_stats(), result.solve_log.solution_type());
ASSERT_TRUE(convergence_info.has_value());
EXPECT_THAT(convergence_info->primal_objective(),
DoubleNear(objective_value, tolerance));
EXPECT_THAT(convergence_info->dual_objective(),
DoubleNear(objective_value, tolerance));
}
class PrimalDualHybridGradientLPTest
: public testing::TestWithParam<
std::tuple</*enable_scaling=*/bool, /*num_threads=*/int,
/*use_iteration_limit=*/bool,
/*use_malitsky_pock_linesearch=*/bool>> {
protected:
PrimalDualHybridGradientParams CreateSolverParamsForFixture(
const int iteration_limit, const double eps_optimal_absolute) {
const auto [enable_scaling, num_threads, use_iteration_limit,
use_malitsky_pock_linesearch] = GetParam();
return CreateSolverParams(iteration_limit, eps_optimal_absolute,
enable_scaling, num_threads, use_iteration_limit,
use_malitsky_pock_linesearch,
/*use_diagonal_qp_trust_region_solver=*/false);
}
void VerifyTerminationReasonAndIterationCountForFixture(
const PrimalDualHybridGradientParams& params,
const SolverResult& output) {
const auto [enable_scaling, num_threads, use_iteration_limit,
use_malitsky_pock_linesearch] = GetParam();
VerifyTerminationReasonAndIterationCount(params, output,
use_iteration_limit);
}
};
class PrimalDualHybridGradientDiagonalQPTest
: public testing::TestWithParam<
std::tuple</*enable_scaling=*/bool, /*num_threads=*/int,
/*use_iteration_limit=*/bool,
/*use_malitsky_pock_linesearch=*/bool,
/*use_diagonal_qp_trust_region_solver=*/bool>> {
protected:
PrimalDualHybridGradientParams CreateSolverParamsForFixture(
const int iteration_limit, const double eps_optimal_absolute) {
const auto [enable_scaling, num_threads, use_iteration_limit,
use_malitsky_pock_linesearch,
use_diagonal_qp_trust_region_solver] = GetParam();
return CreateSolverParams(iteration_limit, eps_optimal_absolute,
enable_scaling, num_threads, use_iteration_limit,
use_malitsky_pock_linesearch,
use_diagonal_qp_trust_region_solver);
}
void VerifyTerminationReasonAndIterationCountForFixture(
const PrimalDualHybridGradientParams& params,
const SolverResult& output) {
const auto [enable_scaling, num_threads, use_iteration_limit,
use_malitsky_pock_linesearch,
use_diagonal_qp_trust_region_solver] = GetParam();
VerifyTerminationReasonAndIterationCount(params, output,
use_iteration_limit);
}
};
class PrimalDualHybridGradientVerbosityTest
: public testing::TestWithParam</*verbosity_level=*/int> {};
class PresolveDualScalingTest
: public testing::TestWithParam<
std::tuple</*Dualize=*/bool,
/*NegateAndScaleObjective=*/bool>> {};
INSTANTIATE_TEST_SUITE_P(
QP, PrimalDualHybridGradientDiagonalQPTest,
testing::Combine(testing::Bool(), testing::Values(1, 4), testing::Bool(),
testing::Bool(), testing::Bool()),
[](const testing::TestParamInfo<
PrimalDualHybridGradientDiagonalQPTest::ParamType>& info) {
return absl::StrCat(
std::get<0>(info.param) ? "Scaling" : "NoScaling", "_",
std::get<1>(info.param), "Threads_",
std::get<2>(info.param) ? "IterationLimit" : "NoIterationLimit", "_",
std::get<3>(info.param) ? "MalitskyPockLinesearch"
: "AdaptiveLinesearch",
"_", std::get<4>(info.param) ? "TRSolverDiag" : "TRSolverNoDiag");
});
INSTANTIATE_TEST_SUITE_P(
LP, PrimalDualHybridGradientLPTest,
testing::Combine(testing::Bool(), testing::Values(1, 4), testing::Bool(),
testing::Bool()),
[](const testing::TestParamInfo<PrimalDualHybridGradientLPTest::ParamType>&
info) {
return absl::StrCat(
std::get<0>(info.param) ? "Scaling" : "NoScaling", "_",
std::get<1>(info.param), "Threads_",
std::get<2>(info.param) ? "IterationLimit" : "NoIterationLimit", "_",
std::get<3>(info.param) ? "MalitskyPockLinesearch"
: "AdaptiveLinesearch");
});
INSTANTIATE_TEST_SUITE_P(Verbosity, PrimalDualHybridGradientVerbosityTest,
testing::Values(0, 1, 2, 3, 4));
INSTANTIATE_TEST_SUITE_P(
PresolveDualScaling, PresolveDualScalingTest,
testing::Combine(testing::Bool(), testing::Bool()),
[](const testing::TestParamInfo<PresolveDualScalingTest::ParamType>& info) {
return absl::StrCat(std::get<1>(info.param) ? "Dualize" : "NoDualize",
std::get<0>(info.param) ? "NegateAndScaleObjective"
: "NoObjectiveScaling");
});
TEST_P(PrimalDualHybridGradientLPTest, UnboundedVariables) {
const int iteration_upperbound = 980;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-7);
params.set_major_iteration_frequency(100);
SolverResult output = PrimalDualHybridGradient(TestLp(), params);
VerifyTerminationReasonAndIterationCountForFixture(params, output);
VerifyObjectiveValues(output, -34.0, 1.0e-6);
EXPECT_THAT(output.primal_solution,
EigenArrayNear<double>({-1, 8, 1, 2.5}, 1.0e-4));
EXPECT_THAT(output.dual_solution,
EigenArrayNear<double>({-2, 0, 2.375, 2.0 / 3}, 1.0e-4));
EXPECT_EQ(output.solve_log.original_problem_stats().num_variables(), 4);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_variables(), 4);
EXPECT_EQ(output.solve_log.original_problem_stats().num_constraints(), 4);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_constraints(), 4);
}
TEST_P(PrimalDualHybridGradientLPTest, Tiny) {
const int iteration_upperbound = 300;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-5);
params.set_major_iteration_frequency(60);
SolverResult output = PrimalDualHybridGradient(TinyLp(), params);
VerifyTerminationReasonAndIterationCountForFixture(params, output);
VerifyObjectiveValues(output, -1.0, 1.0e-4);
EXPECT_THAT(output.primal_solution,
EigenArrayNear<double>({1, 0, 6, 2}, 1.0e-4));
EXPECT_THAT(output.dual_solution,
EigenArrayNear<double>({0.5, 4.0, 0.0}, 1.0e-4));
EXPECT_THAT(output.reduced_costs,
EigenArrayNear<double>({0.0, 1.5, -3.5, 0.0}, 1.0e-4));
EXPECT_EQ(output.solve_log.original_problem_stats().num_variables(), 4);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_variables(), 4);
EXPECT_EQ(output.solve_log.original_problem_stats().num_constraints(), 3);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_constraints(), 3);
}
TEST_P(PrimalDualHybridGradientLPTest, CorrelationClusteringOne) {
const int iteration_upperbound = 9;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-10);
params.set_major_iteration_frequency(2);
SolverResult output =
PrimalDualHybridGradient(CorrelationClusteringLp(), params);
VerifyTerminationReasonAndIterationCountForFixture(params, output);
VerifyObjectiveValues(output, 1.0, 1.0e-14);
EXPECT_THAT(output.primal_solution,
EigenArrayNear<double>({1, 1, 0, 1, 0, 0}, 1.0e-14));
ASSERT_EQ(output.dual_solution.size(), 3);
// There are multiple optimal dual solutions.
EXPECT_GE(output.dual_solution[0], 0);
EXPECT_GE(output.dual_solution[1], 0);
EXPECT_GE(output.dual_solution[2], 0);
EXPECT_GE(output.dual_solution[0] + output.dual_solution[1], 1 - 1.0e-14);
const auto& convergence_information = GetConvergenceInformation(
output.solve_log.solution_stats(), output.solve_log.solution_type());
ASSERT_TRUE(convergence_information.has_value());
EXPECT_THAT(convergence_information->corrected_dual_objective(),
DoubleNear(1, 1.0e-14));
EXPECT_EQ(output.solve_log.original_problem_stats().num_variables(), 6);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_variables(), 6);
EXPECT_EQ(output.solve_log.original_problem_stats().num_constraints(), 3);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_constraints(), 3);
}
TEST_P(PrimalDualHybridGradientLPTest, CorrelationClusteringStar) {
const int iteration_upperbound = 45;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-6);
params.set_major_iteration_frequency(5);
SolverResult output =
PrimalDualHybridGradient(CorrelationClusteringStarLp(), params);
VerifyTerminationReasonAndIterationCountForFixture(params, output);
VerifyObjectiveValues(output, 1.5, 1.0e-6);
EXPECT_THAT(output.primal_solution,
EigenArrayNear<double>({0.5, 0.5, 0.5, 0.0, 0.0, 0.0}, 1.0e-6));
EXPECT_THAT(output.dual_solution,
EigenArrayNear<double>({0.5, 0.5, 0.5}, 1.0e-6));
EXPECT_EQ(output.solve_log.original_problem_stats().num_variables(), 6);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_variables(), 6);
EXPECT_EQ(output.solve_log.original_problem_stats().num_constraints(), 3);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_constraints(), 3);
}
// A double-sided constraint l <= a^T x <= u where neither constraint is tight
// at optimum could cause the trust region solver to malfunction if we picked
// the wrong dual subgradient. This test verifies that we solve an instance with
// such a constraint quickly to high accuracy.
TEST_P(PrimalDualHybridGradientLPTest, InactiveTwoSidedConstraint) {
const int iteration_upperbound = 500;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-8);
params.set_major_iteration_frequency(60);
QuadraticProgram qp = TestLp();
// This makes this constraint double-sided and inactive at the optimal
// solution.
qp.constraint_lower_bounds[1] = -10;
SolverResult output = PrimalDualHybridGradient(qp, params);
VerifyTerminationReasonAndIterationCountForFixture(params, output);
VerifyObjectiveValues(output, -34.0, 1.0e-6);
EXPECT_THAT(output.primal_solution,
EigenArrayNear<double>({-1, 8, 1, 2.5}, 1.0e-7));
EXPECT_THAT(output.dual_solution,
EigenArrayNear<double>({-2.0, 0.0, 2.375, 2.0 / 3}, 1.0e-7));
}
TEST_P(PrimalDualHybridGradientLPTest, InfeasiblePrimal) {
// This value for `iteration_upperbound` is particularly necessary for
// Malistsky and Pock. The adaptive rule detects infeasibility in less than
// 500 iterations.
const int iteration_upperbound = 2000;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-6);
params.set_major_iteration_frequency(5);
params.mutable_termination_criteria()->set_eps_primal_infeasible(1.0e-6);
SolverResult output =
PrimalDualHybridGradient(SmallPrimalInfeasibleLp(), params);
EXPECT_EQ(output.solve_log.termination_reason(),
TERMINATION_REASON_PRIMAL_INFEASIBLE);
const auto& dual = output.dual_solution;
// The following two conditions check if the certificate is correct. For this
// problem the set of infeasibility certificates is equal to all the rays of
// the form -alpha * (1, 1) with alpha positive.
EXPECT_THAT(dual[0] / dual[1], DoubleNear(1, 1.0e-6));
EXPECT_LT(dual[1], 0.0);
// The reduced costs should be approximately zero. However, a small relative
// difference between `dual[0]` and `dual[1]` could translate to a large
// absolute difference, and hence large reduced costs. The following test uses
// the exact formula to make sure we're not adding the objective vector to the
// reduced costs.
EXPECT_THAT(
output.reduced_costs,
EigenArrayNear<double>({dual[1] - dual[0], dual[0] - dual[1]}, 1.0e-6));
EXPECT_LE(output.solve_log.iteration_count(), iteration_upperbound);
EXPECT_EQ(output.solve_log.original_problem_stats().num_variables(), 2);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_variables(), 2);
EXPECT_EQ(output.solve_log.original_problem_stats().num_constraints(), 2);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_constraints(), 2);
}
TEST_P(PrimalDualHybridGradientLPTest, InfeasibleDual) {
const int iteration_upperbound = 500;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-6);
params.set_major_iteration_frequency(5);
SolverResult output =
PrimalDualHybridGradient(SmallDualInfeasibleLp(), params);
EXPECT_EQ(output.solve_log.termination_reason(),
TERMINATION_REASON_DUAL_INFEASIBLE);
// The following two conditions check if the certificate is correct. For this
// problem the set of infeasibility certificates is equal to all the rays of
// the form alpha * (1, 1) with alpha positive.
EXPECT_THAT(output.primal_solution[0] / output.primal_solution[1],
DoubleNear(1, 1.0e-6));
EXPECT_GT(output.primal_solution[1], 0.0);
EXPECT_LE(output.solve_log.iteration_count(), iteration_upperbound);
}
TEST_P(PrimalDualHybridGradientLPTest, InfeasiblePrimalWithReducedCosts) {
// A trivial LP that has reduced costs within the dual ray.
// min x
// Constraint: 2 <= x
// Variable: 0 <= x <= 1
QuadraticProgram lp(1, 1);
lp.objective_vector = Eigen::VectorXd{{1}};
lp.constraint_lower_bounds = Eigen::VectorXd{{2}};
lp.constraint_upper_bounds =
Eigen::VectorXd{{std::numeric_limits<double>::infinity()}};
lp.variable_lower_bounds = Eigen::VectorXd{{0}};
lp.variable_upper_bounds = Eigen::VectorXd{{1}};
lp.constraint_matrix.coeffRef(0, 0) = 1.0;
lp.constraint_matrix.makeCompressed();
const int iteration_upperbound = 100;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-6);
params.set_major_iteration_frequency(5);
params.mutable_termination_criteria()->set_eps_primal_infeasible(1.0e-6);
SolverResult output = PrimalDualHybridGradient(lp, params);
EXPECT_EQ(output.solve_log.termination_reason(),
TERMINATION_REASON_PRIMAL_INFEASIBLE);
const auto& dual = output.dual_solution;
EXPECT_GT(dual[0], 0.0);
EXPECT_EQ(output.reduced_costs[0], -dual[0]);
EXPECT_LT(output.solve_log.iteration_count(), iteration_upperbound);
}
TEST_P(PrimalDualHybridGradientLPTest, InfeasiblePrimalDual) {
const int iteration_upperbound = 600;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-6);
// Adaptive restarts are disabled because they unexpectedly perform worse on
// this instance.
params.set_restart_strategy(PrimalDualHybridGradientParams::NO_RESTARTS);
params.set_major_iteration_frequency(5);
SolverResult output =
PrimalDualHybridGradient(SmallPrimalDualInfeasibleLp(), params);
EXPECT_THAT(output.solve_log.termination_reason(),
AnyOf(Eq(TERMINATION_REASON_DUAL_INFEASIBLE),
Eq(TERMINATION_REASON_PRIMAL_INFEASIBLE)));
EXPECT_LE(output.solve_log.iteration_count(), iteration_upperbound);
}
TEST_P(PrimalDualHybridGradientDiagonalQPTest, DiagonalQp1) {
const int iteration_upperbound = 96;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-6);
params.set_major_iteration_frequency(12);
SolverResult output = PrimalDualHybridGradient(TestDiagonalQp1(), params);
VerifyTerminationReasonAndIterationCountForFixture(params, output);
VerifyObjectiveValues(output, 6.0, 1.0e-6);
EXPECT_THAT(output.primal_solution,
EigenArrayNear<double>({1.0, 0.0}, 1.0e-6));
EXPECT_THAT(output.dual_solution,
EigenArrayNear(Eigen::ArrayXd::Constant(1, -1.0), 1.0e-6));
EXPECT_THAT(output.reduced_costs, EigenArrayNear<double>({4.0, 0.0}, 1.0e-6));
EXPECT_EQ(output.solve_log.original_problem_stats().num_variables(), 2);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_variables(), 2);
EXPECT_EQ(output.solve_log.original_problem_stats().num_constraints(), 1);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_constraints(), 1);
}
TEST_P(PrimalDualHybridGradientDiagonalQPTest, DiagonalQp2) {
const int iteration_upperbound = 240;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-6);
params.set_major_iteration_frequency(12);
SolverResult output = PrimalDualHybridGradient(TestDiagonalQp2(), params);
VerifyTerminationReasonAndIterationCountForFixture(params, output);
VerifyObjectiveValues(output, -5.0, 1.0e-6);
EXPECT_THAT(output.primal_solution,
EigenArrayNear<double>({3.0, 1.0}, 1.0e-6));
EXPECT_THAT(output.dual_solution, ElementsAre(DoubleNear(0.0, 1.0e-6)));
EXPECT_THAT(output.reduced_costs, EigenArrayNear<double>({0.0, 0.0}, 1.0e-6));
}
TEST_P(PrimalDualHybridGradientDiagonalQPTest, DiagonalQp3) {
const int iteration_upperbound = 300;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-6);
params.set_major_iteration_frequency(15);
SolverResult output = PrimalDualHybridGradient(TestDiagonalQp3(), params);
VerifyTerminationReasonAndIterationCountForFixture(params, output);
VerifyObjectiveValues(output, 2.0, 1.0e-6);
EXPECT_THAT(output.primal_solution,
EigenArrayNear<double>({2.0, 0.0, 1.0}, 1.0e-6));
EXPECT_THAT(output.dual_solution,
EigenArrayNear<double>({-1.0, 1.0}, 1.0e-6));
EXPECT_THAT(output.reduced_costs, EigenArrayNear<double>({0, 0, 0}, 1.0e-6));
}
// This is like `DiagonalQp1` except it starts with a near-optimal solution and
// uses a shorter iteration limit.
TEST_P(PrimalDualHybridGradientDiagonalQPTest, QpWarmStart) {
const int iteration_upperbound = 35;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-6);
params.set_major_iteration_frequency(5);
// Disable primal weight updating. In a warm-start situation, the primal
// weight should be carried over. In this test, the initial primal weight of 1
// is reasonable because the distance from the starting point to the primal
// and dual optimal solutions are about the same.
params.set_primal_weight_update_smoothing(0.0);
const PrimalAndDualSolution initial_solution = {
.primal_solution = Eigen::VectorXd{{0.999, 0.001}},
.dual_solution = Eigen::VectorXd{{-0.999}},
};
SolverResult output = PrimalDualHybridGradient(TestDiagonalQp1(), params,
std::move(initial_solution));
VerifyTerminationReasonAndIterationCountForFixture(params, output);
VerifyObjectiveValues(output, 6.0, 1.0e-6);
EXPECT_THAT(output.primal_solution,
EigenArrayNear<double>({1.0, 0.0}, 1.0e-6));
EXPECT_THAT(output.dual_solution,
EigenArrayNear(Eigen::ArrayXd::Constant(1, -1.0), 1.0e-6));
EXPECT_THAT(output.reduced_costs, EigenArrayNear<double>({4.0, 0.0}, 1.0e-6));
}
// Tests an LP with no constraints.
TEST_P(PrimalDualHybridGradientLPTest, LpWithoutConstraints) {
const int iteration_upperbound = 2;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-6);
QuadraticProgram qp(3, 0);
qp.variable_lower_bounds = Eigen::VectorXd{{-1, -kInfinity, -2}};
qp.variable_upper_bounds = Eigen::VectorXd{{kInfinity, 4, 10}};
qp.objective_vector = Eigen::VectorXd{{1, -1, 2}};
SolverResult output = PrimalDualHybridGradient(qp, params);
VerifyTerminationReasonAndIterationCountForFixture(params, output);
VerifyObjectiveValues(output, -9.0, 1.0e-6);
EXPECT_THAT(output.primal_solution,
EigenArrayNear<double>({-1, 4, -2}, 1.0e-6));
EXPECT_THAT(output.dual_solution, SizeIs(0));
EXPECT_EQ(output.solve_log.original_problem_stats().num_variables(), 3);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_variables(), 3);
EXPECT_EQ(output.solve_log.original_problem_stats().num_constraints(), 0);
EXPECT_EQ(output.solve_log.preprocessed_problem_stats().num_constraints(), 0);
}
// Tests an LP with no variables.
TEST_P(PrimalDualHybridGradientLPTest, LpWithoutVariables) {
const int iteration_upperbound = 2;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-6);
QuadraticProgram qp(0, 3);
qp.constraint_lower_bounds = Eigen::VectorXd{{-1, -kInfinity, -2}};
qp.constraint_upper_bounds = Eigen::VectorXd{{kInfinity, 4, 10}};
SolverResult output = PrimalDualHybridGradient(qp, params);
VerifyTerminationReasonAndIterationCountForFixture(params, output);
VerifyObjectiveValues(output, 0.0, 1.0e-6);
EXPECT_THAT(output.primal_solution, SizeIs(0));
EXPECT_THAT(output.dual_solution, EigenArrayNear<double>({0, 0, 0}, 1.0e-6));
EXPECT_EQ(output.solve_log.original_problem_stats().num_variables(), 0);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_variables(), 0);
EXPECT_EQ(output.solve_log.original_problem_stats().num_constraints(), 3);
EXPECT_EQ(output.solve_log.preprocessed_problem_stats().num_constraints(), 3);
}
TEST_P(PrimalDualHybridGradientLPTest, LpWithOnlyFixedVariable) {
const int iteration_upperbound = 2;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/0.0);
QuadraticProgram qp(1, 0);
qp.variable_lower_bounds = Eigen::VectorXd{{1}};
qp.variable_upper_bounds = Eigen::VectorXd{{1}};
qp.objective_vector = Eigen::VectorXd{{1}};
SolverResult output = PrimalDualHybridGradient(qp, params);
EXPECT_EQ(output.solve_log.termination_reason(), TERMINATION_REASON_OPTIMAL);
EXPECT_THAT(output.primal_solution,
EigenArrayNear(Eigen::ArrayXd::Constant(1, 1.0), 1.0e-6));
EXPECT_THAT(output.dual_solution, testing::SizeIs(0));
EXPECT_LE(output.solve_log.iteration_count(), iteration_upperbound);
}
TEST_P(PrimalDualHybridGradientLPTest, InfeasibleLpWithoutVariables) {
const int iteration_upperbound = 2;
PrimalDualHybridGradientParams params =
CreateSolverParamsForFixture(iteration_upperbound,
/*eps_optimal_absolute=*/1.0e-6);
QuadraticProgram qp(0, 1);
qp.constraint_lower_bounds = Eigen::VectorXd{{-1}};
qp.constraint_upper_bounds = Eigen::VectorXd{{-1}};
SolverResult output = PrimalDualHybridGradient(qp, params);
EXPECT_EQ(output.solve_log.termination_reason(),
TERMINATION_REASON_PRIMAL_INFEASIBLE);
EXPECT_THAT(output.primal_solution, SizeIs(0));
EXPECT_LT(output.dual_solution[0], 0.0);
EXPECT_EQ(output.solve_log.original_problem_stats().num_variables(), 0);
EXPECT_LE(output.solve_log.preprocessed_problem_stats().num_variables(), 0);
EXPECT_EQ(output.solve_log.original_problem_stats().num_constraints(), 1);
EXPECT_EQ(output.solve_log.preprocessed_problem_stats().num_constraints(), 1);
}
PrimalDualHybridGradientParams ParamsWithNoLimits() {
PrimalDualHybridGradientParams params;
// This disables the termination limits. A termination criteria must be set
// for the solver to terminate.
params.mutable_termination_criteria()
->mutable_simple_optimality_criteria()
->set_eps_optimal_relative(0.0);
params.mutable_termination_criteria()
->mutable_simple_optimality_criteria()
->set_eps_optimal_absolute(0.0);
params.set_record_iteration_stats(true);
return params;
}
TEST(PrimalDualHybridGradientTest, ClearsRunningAverage) {
// An arbitrarily chosen major iteration frequency.
const int major_iteration_frequency = 17;
const int iteration_limit = 100;
PrimalDualHybridGradientParams params = ParamsWithNoLimits();
params.mutable_termination_criteria()->set_iteration_limit(iteration_limit);
params.set_termination_check_frequency(1);
params.set_major_iteration_frequency(major_iteration_frequency);
params.set_restart_strategy(PrimalDualHybridGradientParams::NO_RESTARTS);
SolverResult output = PrimalDualHybridGradient(TestLp(), params);
ASSERT_EQ(output.solve_log.iteration_count(), iteration_limit);
// The first entry in `iteration_stats` corresponds to the starting point.
ASSERT_EQ(output.solve_log.iteration_stats_size(), iteration_limit + 1);
for (int i = 0; i < output.solve_log.iteration_stats_size(); ++i) {
const auto& stats = output.solve_log.iteration_stats(i);
const int iterations_completed = stats.iteration_number();
EXPECT_EQ(iterations_completed, i);
if (iterations_completed == 0) {
EXPECT_EQ(stats.restart_used(), RESTART_CHOICE_NO_RESTART);
} else if (iterations_completed % major_iteration_frequency == 0) {
EXPECT_EQ(stats.restart_used(), RESTART_CHOICE_WEIGHTED_AVERAGE_RESET)
<< "iteration = " << i;
} else {
EXPECT_EQ(stats.restart_used(), RESTART_CHOICE_NO_RESTART)
<< "iteration = " << i;
}
}
}
TEST(PrimalDualHybridGradientTest, RestartsEveryMajorIteration) {
// An arbitrarily chosen major iteration frequency.
const int major_iteration_frequency = 17;
const int iteration_limit = 100;
PrimalDualHybridGradientParams params = ParamsWithNoLimits();
params.mutable_termination_criteria()->set_iteration_limit(iteration_limit);
params.set_termination_check_frequency(1);
params.set_major_iteration_frequency(major_iteration_frequency);
params.set_restart_strategy(
PrimalDualHybridGradientParams::EVERY_MAJOR_ITERATION);
SolverResult output = PrimalDualHybridGradient(TestLp(), params);
ASSERT_EQ(output.solve_log.iteration_count(), iteration_limit);
// The first entry in `iteration_stats` corresponds to the starting point.
ASSERT_EQ(output.solve_log.iteration_stats_size(), iteration_limit + 1);
for (int i = 0; i < output.solve_log.iteration_stats_size(); ++i) {
const auto& stats = output.solve_log.iteration_stats(i);
const int iterations_completed = stats.iteration_number();
EXPECT_EQ(iterations_completed, i);
if (iterations_completed == 0) {
EXPECT_EQ(stats.restart_used(), RESTART_CHOICE_NO_RESTART);
} else if (iterations_completed % major_iteration_frequency == 0) {
EXPECT_EQ(stats.restart_used(), RESTART_CHOICE_RESTART_TO_AVERAGE)
<< "iteration = " << i;
} else {
EXPECT_EQ(stats.restart_used(), RESTART_CHOICE_NO_RESTART)
<< "iteration = " << i;
}
}
}
TEST(PrimalDualHybridGradientTest, SolveLogIncludesNameForNamedQP) {
PrimalDualHybridGradientParams params;
params.mutable_termination_criteria()->set_iteration_limit(1);
QuadraticProgram test_lp = TestLp();
test_lp.problem_name = "Test LP";
SolverResult output = PrimalDualHybridGradient(test_lp, params);
EXPECT_EQ(output.solve_log.instance_name(), "Test LP");
}
TEST(PrimalDualHybridGradientTest, SolveLogOmitsNameForUnnamedQP) {
PrimalDualHybridGradientParams params;
params.mutable_termination_criteria()->set_iteration_limit(1);
QuadraticProgram unnamed_test_lp = TestLp();
SolverResult output = PrimalDualHybridGradient(unnamed_test_lp, params);
EXPECT_FALSE(output.solve_log.has_instance_name());
}
TEST(PrimalDualHybridGradientTest, SolveLogIncludesParameters) {
PrimalDualHybridGradientParams params;
params.mutable_termination_criteria()->set_iteration_limit(1);
SolverResult output = PrimalDualHybridGradient(TestLp(), params);
EXPECT_EQ(output.solve_log.params().termination_criteria().iteration_limit(),
1);
}
TEST(PrimalDualHybridGradientTest, AdaptiveDistanceBasedRestartsWorkOnTestLp) {
PrimalDualHybridGradientParams params;
params.set_major_iteration_frequency(16);
params.mutable_termination_criteria()->set_iteration_limit(128);
params.set_restart_strategy(
PrimalDualHybridGradientParams::ADAPTIVE_DISTANCE_BASED);
// Low restart threshold.
params.set_necessary_reduction_for_restart(0.99);
SolverResult output = PrimalDualHybridGradient(TestLp(), params);
EXPECT_EQ(output.solve_log.termination_reason(), TERMINATION_REASON_OPTIMAL);
EXPECT_THAT(output.primal_solution,
EigenArrayNear<double>({-1, 8, 1, 2.5}, 1.0e-4));
EXPECT_THAT(output.dual_solution,
EigenArrayNear<double>({-2, 0, 2.375, 2.0 / 3}, 1.0e-4));
const auto convergence_info = GetConvergenceInformation(
output.solve_log.solution_stats(), output.solve_log.solution_type());
ASSERT_TRUE(convergence_info.has_value());
EXPECT_THAT(convergence_info->primal_objective(), DoubleNear(-34.0, 1.0e-4));
EXPECT_THAT(convergence_info->dual_objective(), DoubleNear(-34.0, 1.0e-4));
}
TEST(PrimalDualHybridGradientTest, AdaptiveDistanceBasedRestartsWorkOnTestQp) {
PrimalDualHybridGradientParams params;
params.set_major_iteration_frequency(16);
params.mutable_termination_criteria()->set_iteration_limit(128);
params.set_restart_strategy(
PrimalDualHybridGradientParams::ADAPTIVE_DISTANCE_BASED);
// Low restart threshold.
params.set_necessary_reduction_for_restart(0.99);
SolverResult output = PrimalDualHybridGradient(TestDiagonalQp1(), params);
EXPECT_EQ(output.solve_log.termination_reason(), TERMINATION_REASON_OPTIMAL);
}
TEST(PrimalDualHybridGradientTest, AdaptiveDistanceBasedRestartsToAverage) {
// An arbitrarily chosen major iteration frequency.
const int major_iteration_frequency = 13;
const int iteration_limit = 100;
PrimalDualHybridGradientParams params = ParamsWithNoLimits();
params.mutable_termination_criteria()->set_iteration_limit(iteration_limit);
params.set_termination_check_frequency(1);
params.set_major_iteration_frequency(major_iteration_frequency);
params.set_restart_strategy(
PrimalDualHybridGradientParams::ADAPTIVE_DISTANCE_BASED);
params.set_necessary_reduction_for_restart(0.75);
SolverResult output = PrimalDualHybridGradient(TestLp(), params);
ASSERT_EQ(output.solve_log.iteration_count(), iteration_limit);
// The first entry in `iteration_stats` corresponds to the starting point.
ASSERT_EQ(output.solve_log.iteration_stats_size(), iteration_limit + 1);
for (int i = 0; i < output.solve_log.iteration_stats_size(); ++i) {
const auto& stats = output.solve_log.iteration_stats(i);
const int iterations_completed = stats.iteration_number();
EXPECT_EQ(iterations_completed, i);
if (iterations_completed == 0) {
EXPECT_EQ(stats.restart_used(), RESTART_CHOICE_NO_RESTART);
} else if (iterations_completed == major_iteration_frequency) {
// An explicit restart should be triggered at the end of the first major
// iteration.
EXPECT_THAT(stats.restart_used(),
AnyOf(RESTART_CHOICE_RESTART_TO_AVERAGE,
RESTART_CHOICE_WEIGHTED_AVERAGE_RESET))
<< "iteration = " << i;
} else if (iterations_completed % major_iteration_frequency != 0) {
// No restarts should happen outside major iterations.
EXPECT_EQ(stats.restart_used(), RESTART_CHOICE_NO_RESTART);
}
}
}
TEST(PrimalDualHybridGradientTest, PrimalWeightFrozen) {
// An arbitrarily chosen major iteration frequency.
const int major_iteration_frequency = 17;
const int iteration_limit = 100;
const double initial_primal_weight = 1.5;
PrimalDualHybridGradientParams params = ParamsWithNoLimits();
params.mutable_termination_criteria()->set_iteration_limit(iteration_limit);
params.set_major_iteration_frequency(major_iteration_frequency);
params.set_restart_strategy(
PrimalDualHybridGradientParams::EVERY_MAJOR_ITERATION);
params.set_initial_primal_weight(initial_primal_weight);
params.set_primal_weight_update_smoothing(0.0);
SolverResult output = PrimalDualHybridGradient(TestLp(), params);
for (const auto& stats : output.solve_log.iteration_stats()) {
EXPECT_EQ(stats.primal_weight(), initial_primal_weight)
<< "iteration = " << stats.iteration_number();
}
}
TEST(PrimalDualHybridGradientTest, ConstantStepSize) {
const int iteration_limit = 100;
PrimalDualHybridGradientParams params = ParamsWithNoLimits();
params.mutable_termination_criteria()->set_iteration_limit(iteration_limit);
params.set_termination_check_frequency(1);
params.set_linesearch_rule(
PrimalDualHybridGradientParams::CONSTANT_STEP_SIZE_RULE);
SolverResult output = PrimalDualHybridGradient(TestLp(), params);
ASSERT_FALSE(output.solve_log.iteration_stats().empty());
const double initial_step_size =
output.solve_log.iteration_stats(0).step_size();
for (const auto& stats : output.solve_log.iteration_stats()) {
EXPECT_EQ(stats.step_size(), initial_step_size)
<< "iteration = " << stats.iteration_number();
}
}
TEST(PrimalDualHybridGradientTest, StepSizeScaling) {
const int iteration_limit = 1;
PrimalDualHybridGradientParams params = ParamsWithNoLimits();
params.mutable_termination_criteria()->set_iteration_limit(iteration_limit);
params.set_termination_check_frequency(1);
params.set_linesearch_rule(
PrimalDualHybridGradientParams::CONSTANT_STEP_SIZE_RULE);
SolverResult unscaled_output = PrimalDualHybridGradient(TestLp(), params);
ASSERT_FALSE(unscaled_output.solve_log.iteration_stats().empty());
const double initial_step_size =
unscaled_output.solve_log.iteration_stats(0).step_size();
const double kStepSizeScaling = 0.5;
params.set_initial_step_size_scaling(kStepSizeScaling);
SolverResult scaled_output = PrimalDualHybridGradient(TestLp(), params);
ASSERT_FALSE(scaled_output.solve_log.iteration_stats().empty());
EXPECT_EQ(scaled_output.solve_log.iteration_stats(0).step_size(),
initial_step_size * kStepSizeScaling);
}
// This verifies that `kkt_matrix_pass_limit` is checked every iteration.
TEST(PrimalDualHybridGradientTest, KktMatrixPassTermination) {
const int kkt_matrix_pass_limit = 13;
PrimalDualHybridGradientParams params = ParamsWithNoLimits();
params.mutable_termination_criteria()->set_kkt_matrix_pass_limit(
kkt_matrix_pass_limit);
params.set_linesearch_rule(
PrimalDualHybridGradientParams::CONSTANT_STEP_SIZE_RULE);
SolverResult output = PrimalDualHybridGradient(TestLp(), params);
ASSERT_FALSE(output.solve_log.iteration_stats().empty());
EXPECT_EQ(output.solve_log.termination_reason(),
TERMINATION_REASON_KKT_MATRIX_PASS_LIMIT);
EXPECT_EQ(output.solve_log.solution_stats().cumulative_kkt_matrix_passes(),
kkt_matrix_pass_limit);
}
TEST(PrimalDualHybridGradientTest,
StatsAtEachIterationWithRecordIterationStatsOn) {
// An arbitrarily chosen major iteration frequency.
const int major_iteration_frequency = 17;
const int iteration_limit = 100;
PrimalDualHybridGradientParams params = ParamsWithNoLimits();
params.set_record_iteration_stats(true);
// This is required for `ConvergenceInformation`, `InfeasibilityInformation`,
// and `PointMetadata` to be generated on each iteration.
params.set_termination_check_frequency(1);
params.mutable_termination_criteria()->set_iteration_limit(iteration_limit);
params.set_major_iteration_frequency(major_iteration_frequency);
SolverResult output = PrimalDualHybridGradient(TestLp(), params);
EXPECT_EQ(output.solve_log.iteration_stats().size(), iteration_limit + 1);
for (const auto& stats : output.solve_log.iteration_stats()) {
EXPECT_NE(GetConvergenceInformation(stats, POINT_TYPE_CURRENT_ITERATE),
std::nullopt);
EXPECT_NE(GetInfeasibilityInformation(stats, POINT_TYPE_CURRENT_ITERATE),
std::nullopt);
EXPECT_NE(GetPointMetadata(stats, POINT_TYPE_CURRENT_ITERATE),
std::nullopt);
if (stats.iteration_number() > 0) {
EXPECT_NE(GetConvergenceInformation(stats, POINT_TYPE_AVERAGE_ITERATE),
std::nullopt);
EXPECT_NE(GetInfeasibilityInformation(stats, POINT_TYPE_AVERAGE_ITERATE),
std::nullopt);
EXPECT_NE(GetPointMetadata(stats, POINT_TYPE_AVERAGE_ITERATE),
std::nullopt);
EXPECT_NE(
GetInfeasibilityInformation(stats, POINT_TYPE_ITERATE_DIFFERENCE),
std::nullopt);
EXPECT_NE(GetPointMetadata(stats, POINT_TYPE_ITERATE_DIFFERENCE),
std::nullopt);
}
}
}
TEST(PrimalDualHybridGradientTest,
NoIterationStatsWithRecordIterationStatsOff) {
// An arbitrarily chosen major iteration frequency.
const int major_iteration_frequency = 17;
const int iteration_limit = 100;
PrimalDualHybridGradientParams params = ParamsWithNoLimits();
params.set_record_iteration_stats(false);
params.mutable_termination_criteria()->set_iteration_limit(iteration_limit);
params.set_major_iteration_frequency(major_iteration_frequency);
// Random projection seeds should have no effect when `record_iteration_stats`
// is false.
params.add_random_projection_seeds(1);
SolverResult output = PrimalDualHybridGradient(TestLp(), params);
EXPECT_EQ(output.solve_log.iteration_stats().size(), 0);
}
TEST(PrimalDualHybridGradientTest, NoRandomProjectionsIfNotRequested) {
// An arbitrarily chosen major iteration frequency.
const int major_iteration_frequency = 17;
const int iteration_limit = 100;
PrimalDualHybridGradientParams params = ParamsWithNoLimits();
params.set_record_iteration_stats(true);
params.mutable_termination_criteria()->set_iteration_limit(iteration_limit);
params.set_major_iteration_frequency(major_iteration_frequency);
params.set_termination_check_frequency(1);
SolverResult output = PrimalDualHybridGradient(TestLp(), params);
EXPECT_EQ(output.solve_log.iteration_stats().size(), iteration_limit + 1);
for (const auto& stats : output.solve_log.iteration_stats()) {
EXPECT_THAT(stats.point_metadata(), Not(IsEmpty()));
for (const auto& metadata : stats.point_metadata()) {
EXPECT_THAT(metadata.random_primal_projections(), IsEmpty());
EXPECT_THAT(metadata.random_dual_projections(), IsEmpty());
}
}
}
TEST(PrimalDualHybridGradientTest, HasRandomProjectionsIfRequested) {
// An arbitrarily chosen major iteration frequency.
const int major_iteration_frequency = 17;