-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathsolver_solution.cu
More file actions
458 lines (409 loc) · 18.5 KB
/
solver_solution.cu
File metadata and controls
458 lines (409 loc) · 18.5 KB
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
/* clang-format off */
/*
* SPDX-FileCopyrightText: Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/* clang-format on */
#include <cuopt/linear_programming/pdlp/solver_solution.hpp>
#include <math_optimization/solution_writer.hpp>
#include <mip/mip_constants.hpp>
#include <utilities/logger.hpp>
#include <utilities/macros.cuh>
#include <raft/common/nvtx.hpp>
#include <raft/util/cudart_utils.hpp>
#include <limits>
#include <vector>
namespace cuopt::linear_programming {
template <typename i_t, typename f_t>
optimization_problem_solution_t<i_t, f_t>::optimization_problem_solution_t(
pdlp_termination_status_t termination_status, rmm::cuda_stream_view stream_view)
: primal_solution_{0, stream_view},
dual_solution_{0, stream_view},
reduced_cost_{0, stream_view},
termination_status_{termination_status},
error_status_(cuopt::logic_error("", cuopt::error_type_t::Success))
{
cuopt_assert(termination_stats_.size() == termination_status_.size(),
"Termination statistics and status vectors must have the same size");
}
template <typename i_t, typename f_t>
optimization_problem_solution_t<i_t, f_t>::optimization_problem_solution_t(
cuopt::logic_error error_status_, rmm::cuda_stream_view stream_view)
: primal_solution_{0, stream_view},
dual_solution_{0, stream_view},
reduced_cost_{0, stream_view},
termination_status_{pdlp_termination_status_t::NoTermination},
error_status_(error_status_)
{
cuopt_assert(termination_stats_.size() == termination_status_.size(),
"Termination statistics and status vectors must have the same size");
}
template <typename i_t, typename f_t>
optimization_problem_solution_t<i_t, f_t>::optimization_problem_solution_t(
rmm::device_uvector<f_t>& final_primal_solution,
rmm::device_uvector<f_t>& final_dual_solution,
rmm::device_uvector<f_t>& final_reduced_cost,
pdlp_warm_start_data_t<i_t, f_t>&& warm_start_data,
const std::string objective_name,
const std::vector<std::string>& var_names,
const std::vector<std::string>& row_names,
std::vector<additional_termination_information_t>&& termination_stats,
std::vector<pdlp_termination_status_t>&& termination_status)
: primal_solution_(std::move(final_primal_solution)),
dual_solution_(std::move(final_dual_solution)),
reduced_cost_(std::move(final_reduced_cost)),
pdlp_warm_start_data_(std::move(warm_start_data)),
objective_name_(objective_name),
var_names_(std::move(var_names)),
row_names_(std::move(row_names)),
termination_stats_(std::move(termination_stats)),
termination_status_(std::move(termination_status)),
error_status_(cuopt::logic_error("", cuopt::error_type_t::Success))
{
cuopt_assert(termination_stats_.size() == termination_status_.size(),
"Termination statistics and status vectors must have the same size");
}
template <typename i_t, typename f_t>
optimization_problem_solution_t<i_t, f_t>::optimization_problem_solution_t(
rmm::device_uvector<f_t>& final_primal_solution,
rmm::device_uvector<f_t>& final_dual_solution,
rmm::device_uvector<f_t>& final_reduced_cost,
const std::string objective_name,
const std::vector<std::string>& var_names,
const std::vector<std::string>& row_names,
std::vector<additional_termination_information_t>&& termination_stats,
std::vector<pdlp_termination_status_t>&& termination_status)
: primal_solution_(std::move(final_primal_solution)),
dual_solution_(std::move(final_dual_solution)),
reduced_cost_(std::move(final_reduced_cost)),
objective_name_(objective_name),
var_names_(std::move(var_names)),
row_names_(std::move(row_names)),
termination_stats_(std::move(termination_stats)),
termination_status_(std::move(termination_status)),
error_status_(cuopt::logic_error("", cuopt::error_type_t::Success))
{
cuopt_assert(termination_stats_.size() == termination_status_.size(),
"Termination statistics and status vectors must have the same size");
}
template <typename i_t, typename f_t>
optimization_problem_solution_t<i_t, f_t>::optimization_problem_solution_t(
rmm::device_uvector<f_t>& final_primal_solution,
rmm::device_uvector<f_t>& final_dual_solution,
rmm::device_uvector<f_t>& final_reduced_cost,
const std::string objective_name,
const std::vector<std::string>& var_names,
const std::vector<std::string>& row_names,
additional_termination_information_t& termination_stats,
pdlp_termination_status_t termination_status,
const raft::handle_t* handler_ptr,
[[maybe_unused]] bool deep_copy)
: primal_solution_(final_primal_solution, handler_ptr->get_stream()),
dual_solution_(final_dual_solution, handler_ptr->get_stream()),
reduced_cost_(final_reduced_cost, handler_ptr->get_stream()),
objective_name_(objective_name),
var_names_(var_names),
row_names_(row_names),
termination_stats_{termination_stats},
termination_status_{termination_status},
error_status_(cuopt::logic_error("", cuopt::error_type_t::Success))
{
cuopt_assert(termination_stats_.size() == termination_status_.size(),
"Termination statistics and status vectors must have the same size");
}
template <typename i_t, typename f_t>
void optimization_problem_solution_t<i_t, f_t>::copy_from(
const raft::handle_t* handle_ptr, const optimization_problem_solution_t<i_t, f_t>& other)
{
// Resize to make sure they are of same size
primal_solution_.resize(other.primal_solution_.size(), handle_ptr->get_stream());
dual_solution_.resize(other.dual_solution_.size(), handle_ptr->get_stream());
reduced_cost_.resize(other.reduced_cost_.size(), handle_ptr->get_stream());
// Copy the data
raft::copy(primal_solution_.data(),
other.primal_solution_.data(),
primal_solution_.size(),
handle_ptr->get_stream());
raft::copy(dual_solution_.data(),
other.dual_solution_.data(),
dual_solution_.size(),
handle_ptr->get_stream());
raft::copy(reduced_cost_.data(),
other.reduced_cost_.data(),
reduced_cost_.size(),
handle_ptr->get_stream());
termination_stats_ = other.termination_stats_;
termination_status_ = other.termination_status_;
objective_name_ = other.objective_name_;
var_names_ = other.var_names_;
row_names_ = other.row_names_;
// We do not copy the warm start info. As it is not needed for this purpose.
handle_ptr->sync_stream();
}
template <typename i_t, typename f_t>
void optimization_problem_solution_t<i_t, f_t>::write_additional_termination_statistics_to_file(
std::ofstream& myfile)
{
cuopt_expects(termination_stats_.size() == 1,
error_type_t::ValidationError,
"Write to file only supported in non batch mode");
const auto& termination_stats = termination_stats_[0];
myfile << "\t\"Additional termination information\" : { " << std::endl;
myfile << "\t\"Number of steps taken\" : " << termination_stats.number_of_steps_taken << ","
<< std::endl;
if (termination_stats.solved_by == lp_solver_type_t::PDLP) {
myfile << "\t\"Total number of attempted steps\" : "
<< termination_stats.total_number_of_attempted_steps << "," << std::endl;
}
myfile << "\t\"Total solve time\" : " << termination_stats.solve_time;
if (termination_stats.solved_by == lp_solver_type_t::PDLP) {
myfile << "," << std::endl;
myfile << "\t\t\"Convergence measures\" : { " << std::endl;
myfile << "\t\t\t\"Absolute primal residual\" : " << termination_stats.l2_primal_residual << ","
<< std::endl;
myfile << "\t\t\t\"Relative primal residual\" : "
<< termination_stats.l2_relative_primal_residual << "," << std::endl;
myfile << "\t\t\t\"Absolute dual residual\" : " << termination_stats.l2_dual_residual << ","
<< std::endl;
myfile << "\t\t\t\"Relative dual residual\" : " << termination_stats.l2_relative_dual_residual
<< "," << std::endl;
myfile << "\t\t\t\"Primal objective value\" : " << termination_stats.primal_objective << ","
<< std::endl;
myfile << "\t\t\t\"Dual objective value\" : " << termination_stats.dual_objective << ","
<< std::endl;
myfile << "\t\t\t\"Gap\" : " << termination_stats.gap << "," << std::endl;
myfile << "\t\t\t\"Relative gap\" : " << termination_stats.relative_gap << std::endl;
myfile << "\t\t}, " << std::endl;
myfile << "\t\t\"Infeasibility measures\" : {" << std::endl;
myfile << "\t\t\t\"Maximum error for the linear constraints and sign constraints\" : "
<< termination_stats.max_primal_ray_infeasibility << "," << std::endl;
myfile << "\t\t\t\"Objective value for the extreme primal ray\" : "
<< termination_stats.primal_ray_linear_objective << "," << std::endl;
myfile << "\t\t\t\"Maximum constraint error\" : "
<< termination_stats.max_dual_ray_infeasibility << "," << std::endl;
myfile << "\t\t\t\"Objective value for the extreme dual ray\" : "
<< termination_stats.dual_ray_linear_objective << std::endl;
myfile << "\t\t} " << std::endl;
} else
myfile << std::endl;
myfile << "\t} " << std::endl;
}
template <typename i_t, typename f_t>
void optimization_problem_solution_t<i_t, f_t>::write_to_file(std::string_view filename,
rmm::cuda_stream_view stream_view,
bool generate_variable_values)
{
raft::common::nvtx::range fun_scope("write final solution to file");
cuopt_expects(termination_stats_.size() == 1,
error_type_t::ValidationError,
"Write to file only supported in non batch mode");
std::ofstream myfile(filename.data());
myfile.precision(std::numeric_limits<f_t>::digits10 + 1);
if (termination_status_[0] == pdlp_termination_status_t::NumericalError) {
myfile << "{ " << std::endl;
myfile << "\t\"Termination reason\" : \"" << get_termination_status_string() << "\"}"
<< std::endl;
return;
}
std::vector<f_t> primal_solution;
std::vector<f_t> dual_solution;
std::vector<f_t> reduced_cost;
primal_solution.resize(primal_solution_.size());
dual_solution.resize(dual_solution_.size());
reduced_cost.resize(reduced_cost_.size());
raft::copy(
primal_solution.data(), primal_solution_.data(), primal_solution_.size(), stream_view.value());
raft::copy(
dual_solution.data(), dual_solution_.data(), dual_solution_.size(), stream_view.value());
raft::copy(reduced_cost.data(), reduced_cost_.data(), reduced_cost_.size(), stream_view.value());
RAFT_CUDA_TRY(cudaStreamSynchronize(stream_view.value()));
myfile << "{ " << std::endl;
myfile << "\t\"Termination reason\" : \"" << get_termination_status_string() << "\","
<< std::endl;
myfile << "\t\"Objective value for " << objective_name_ << "\" : " << get_objective_value(0)
<< "," << std::endl;
if (!var_names_.empty() && generate_variable_values) {
myfile << "\t\"Primal variables\" : {" << std::endl;
for (size_t i = 0; i < primal_solution.size() - 1; i++) {
myfile << "\t\t\"" << var_names_[i] << "\" : " << primal_solution[i] << "," << std::endl;
}
myfile << "\t\t\"" << var_names_[primal_solution.size() - 1]
<< "\" : " << primal_solution[primal_solution.size() - 1] << std::endl;
myfile << "}, " << std::endl;
myfile << "\t\"Dual variables\" : {" << std::endl;
for (size_t i = 0; i < dual_solution.size() - 1; i++) {
myfile << "\t\t\"" << row_names_[i] << "\" : " << dual_solution[i] << "," << std::endl;
}
myfile << "\t\t\"" << row_names_[dual_solution.size() - 1]
<< "\" : " << dual_solution[dual_solution.size() - 1] << std::endl;
myfile << "\t}, " << std::endl;
myfile << "\t\"Reduced costs\" : {" << std::endl;
for (size_t i = 0; i < reduced_cost.size() - 1; i++) {
myfile << "\t\t\"" << i << "\" : " << reduced_cost[i] << "," << std::endl;
}
myfile << "\t\t\"" << reduced_cost.size() - 1
<< "\" : " << reduced_cost[reduced_cost.size() - 1] << std::endl;
myfile << "\t}, " << std::endl;
}
write_additional_termination_statistics_to_file(myfile);
myfile << "} " << std::endl;
myfile.close();
}
template <typename i_t, typename f_t>
void optimization_problem_solution_t<i_t, f_t>::set_solve_time(double ms)
{
// TODO later batch mode: shouldn't we have a different solve time per climber?
// Currently the issue is that we would need one solve time per climber and one overall solve time
std::for_each(termination_stats_.begin(), termination_stats_.end(), [ms](auto& termination) {
termination.solve_time = ms;
});
}
template <typename i_t, typename f_t>
void optimization_problem_solution_t<i_t, f_t>::set_termination_status(
pdlp_termination_status_t termination_status)
{
cuopt_assert(termination_stats_.size() == 1,
"Set termination status only supported in non batch mode");
termination_status_[0] = termination_status;
}
template <typename i_t, typename f_t>
double optimization_problem_solution_t<i_t, f_t>::get_solve_time() const
{
// TODO later batch mode: shouldn't we have a different solve time per climber?
// Currently the issue is that we would need one solve time per climber and one overall solve tim
cuopt_assert(termination_stats_.size() > 0, "Should never happen");
return termination_stats_[0].solve_time;
}
template <typename i_t, typename f_t>
std::string optimization_problem_solution_t<i_t, f_t>::get_termination_status_string(
pdlp_termination_status_t termination_status)
{
switch (termination_status) {
case pdlp_termination_status_t::Optimal: return "Optimal";
case pdlp_termination_status_t::PrimalInfeasible: return "Primal Infeasible";
case pdlp_termination_status_t::DualInfeasible: return "Dual Infeasible";
case pdlp_termination_status_t::IterationLimit: return "Iteration Limit";
case pdlp_termination_status_t::TimeLimit: return "Time Limit";
case pdlp_termination_status_t::NumericalError: return "A numerical error was encountered.";
case pdlp_termination_status_t::PrimalFeasible: return "Primal Feasible";
case pdlp_termination_status_t::ConcurrentLimit: return "Concurrent Limit";
default: return "Unknown cuOpt status";
}
}
template <typename i_t, typename f_t>
std::string optimization_problem_solution_t<i_t, f_t>::get_termination_status_string(i_t id) const
{
cuopt_assert(id < termination_status_.size(), "id too big for batch size");
return get_termination_status_string(termination_status_[id]);
}
template <typename i_t, typename f_t>
f_t optimization_problem_solution_t<i_t, f_t>::get_objective_value(i_t id) const
{
cuopt_assert(id < termination_stats_.size(), "id too big for batch size");
return termination_stats_[id].primal_objective;
}
template <typename i_t, typename f_t>
f_t optimization_problem_solution_t<i_t, f_t>::get_dual_objective_value(i_t id) const
{
cuopt_assert(id < termination_stats_.size(), "id too big for batch size");
return termination_stats_[id].dual_objective;
}
template <typename i_t, typename f_t>
rmm::device_uvector<f_t>& optimization_problem_solution_t<i_t, f_t>::get_primal_solution()
{
return primal_solution_;
}
template <typename i_t, typename f_t>
const rmm::device_uvector<f_t>& optimization_problem_solution_t<i_t, f_t>::get_primal_solution()
const
{
return primal_solution_;
}
template <typename i_t, typename f_t>
rmm::device_uvector<f_t>& optimization_problem_solution_t<i_t, f_t>::get_dual_solution()
{
return dual_solution_;
}
template <typename i_t, typename f_t>
const rmm::device_uvector<f_t>& optimization_problem_solution_t<i_t, f_t>::get_dual_solution() const
{
return dual_solution_;
}
template <typename i_t, typename f_t>
rmm::device_uvector<f_t>& optimization_problem_solution_t<i_t, f_t>::get_reduced_cost()
{
return reduced_cost_;
}
template <typename i_t, typename f_t>
pdlp_termination_status_t optimization_problem_solution_t<i_t, f_t>::get_termination_status(
i_t id) const
{
cuopt_assert(id < termination_status_.size(), "id too big for batch size");
return termination_status_[id];
}
template <typename i_t, typename f_t>
std::vector<pdlp_termination_status_t>&
optimization_problem_solution_t<i_t, f_t>::get_terminations_status()
{
return termination_status_;
}
template <typename i_t, typename f_t>
cuopt::logic_error optimization_problem_solution_t<i_t, f_t>::get_error_status() const
{
return error_status_;
}
template <typename i_t, typename f_t>
typename optimization_problem_solution_t<i_t, f_t>::additional_termination_information_t
optimization_problem_solution_t<i_t, f_t>::get_additional_termination_information(i_t id) const
{
cuopt_assert(id < termination_stats_.size(), "id too big for batch size");
return termination_stats_[id];
}
template <typename i_t, typename f_t>
std::vector<
typename optimization_problem_solution_t<i_t, f_t>::additional_termination_information_t>
optimization_problem_solution_t<i_t, f_t>::get_additional_termination_informations() const
{
return termination_stats_;
}
template <typename i_t, typename f_t>
std::vector<
typename optimization_problem_solution_t<i_t, f_t>::additional_termination_information_t>&
optimization_problem_solution_t<i_t, f_t>::get_additional_termination_informations()
{
return termination_stats_;
}
template <typename i_t, typename f_t>
pdlp_warm_start_data_t<i_t, f_t>&
optimization_problem_solution_t<i_t, f_t>::get_pdlp_warm_start_data()
{
return pdlp_warm_start_data_;
}
template <typename i_t, typename f_t>
void optimization_problem_solution_t<i_t, f_t>::write_to_sol_file(
std::string_view filename, rmm::cuda_stream_view stream_view) const
{
cuopt_expects(termination_stats_.size() == 1,
error_type_t::ValidationError,
"Write to file only supported in non batch mode");
auto status = get_termination_status_string();
if (termination_status_[0] != pdlp_termination_status_t::Optimal &&
termination_status_[0] != pdlp_termination_status_t::PrimalFeasible) {
status = "Infeasible";
}
auto objective_value = get_objective_value(0);
std::vector<f_t> solution;
solution.resize(primal_solution_.size());
raft::copy(
solution.data(), primal_solution_.data(), primal_solution_.size(), stream_view.value());
RAFT_CUDA_TRY(cudaStreamSynchronize(stream_view.value()));
solution_writer_t::write_solution_to_sol_file(
std::string(filename), status, objective_value, var_names_, solution);
}
#if MIP_INSTANTIATE_FLOAT
template class optimization_problem_solution_t<int, float>;
#endif
#if MIP_INSTANTIATE_DOUBLE
template class optimization_problem_solution_t<int, double>;
#endif
} // namespace cuopt::linear_programming