Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
time_limit.cc
Go to the documentation of this file.
1// Copyright 2010-2025 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 <algorithm>
17#include <cstdint>
18#include <limits>
19#include <memory>
20#include <string>
21#include <utility>
22
23#include "absl/flags/flag.h"
24#include "absl/log/die_if_null.h"
25#include "absl/log/log.h"
26#include "absl/strings/str_cat.h"
27#include "absl/time/time.h"
28
29ABSL_FLAG(bool, time_limit_use_usertime, false,
30 "If true, rely on the user time in the TimeLimit class. This is "
31 "only recommended for benchmarking on a non-isolated environment.");
32
33namespace operations_research {
34
35// static constants.
36const double TimeLimit::kSafetyBufferSeconds = 1e-4;
37const int TimeLimit::kHistorySize = 100;
38
39TimeLimit::TimeLimit(double limit_in_seconds, double deterministic_limit)
40 : safety_buffer_ns_(static_cast<int64_t>(kSafetyBufferSeconds * 1e9)),
41 running_max_(kHistorySize),
42 external_boolean_as_limit_(nullptr) {
43 ResetTimers(limit_in_seconds, deterministic_limit);
44}
45
46std::string TimeLimit::DebugString() const {
47 std::string buffer = absl::StrCat(
48 "Time left: ", (GetTimeLeft()),
49 "\nDeterministic time left: ", (GetDeterministicTimeLeft()),
50 "\nElapsed time: ", (GetElapsedTime()),
51 "\nElapsed deterministic time: ", (GetElapsedDeterministicTime()));
52#ifndef NDEBUG
53 for (const auto& counter : deterministic_counters_) {
54 const std::string& counter_name = counter.first;
55 const double counter_value = counter.second;
56 absl::StrAppend(&buffer, "\n", counter_name, ": ", (counter_value));
57 }
58#endif
59 return buffer;
60}
61
63 double limit_in_seconds,
64 double deterministic_limit)
65 : base_time_limit_(ABSL_DIE_IF_NULL(base_time_limit)),
66 time_limit_(std::min(base_time_limit_->GetTimeLeft(), limit_in_seconds),
67 std::min(base_time_limit_->GetDeterministicTimeLeft(),
68 deterministic_limit)) {
69 if (base_time_limit_->external_boolean_as_limit_ != nullptr) {
70 time_limit_.RegisterExternalBooleanAsLimit(
71 base_time_limit_->external_boolean_as_limit_);
72 }
73}
74
76 base_time_limit_->AdvanceDeterministicTime(
77 time_limit_.GetElapsedDeterministicTime());
78}
79} // namespace operations_research
NestedTimeLimit(TimeLimit *base_time_limit, double limit_in_seconds, double deterministic_limit)
Definition time_limit.cc:62
double GetElapsedDeterministicTime() const
Definition time_limit.h:218
double GetDeterministicTimeLeft() const
Definition time_limit.h:175
static const int kHistorySize
Definition time_limit.h:98
static const double kSafetyBufferSeconds
static constants.
Definition time_limit.h:97
std::string DebugString() const
Definition time_limit.cc:46
In SWIG mode, we don't want anything besides these top-level includes.
STL namespace.
ABSL_FLAG(bool, time_limit_use_usertime, false, "If true, rely on the user time in the TimeLimit class. This is " "only recommended for benchmarking on a non-isolated environment.")