OR-Tools  8.1
time_limit.cc
Go to the documentation of this file.
1 // Copyright 2010-2018 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 "absl/strings/str_cat.h"
17 
18 ABSL_FLAG(bool, time_limit_use_usertime, false,
19  "If true, rely on the user time in the TimeLimit class. This is "
20  "only recommended for benchmarking on a non-isolated environment.");
21 
22 ABSL_FLAG(bool, time_limit_use_instruction_count, false,
23  "If true, measures the number of instructions executed");
24 
25 namespace operations_research {
26 
27 // static constants.
28 const double TimeLimit::kSafetyBufferSeconds = 1e-4;
29 const int TimeLimit::kHistorySize = 100;
30 
31 std::string TimeLimit::DebugString() const {
32  std::string buffer = absl::StrCat(
33  "Time left: ", (GetTimeLeft()),
34  "\nDeterministic time left: ", (GetDeterministicTimeLeft()),
35  "\nElapsed time: ", (GetElapsedTime()),
36  "\nElapsed deterministic time: ", (GetElapsedDeterministicTime()));
37 #ifndef NDEBUG
38  for (const auto& counter : deterministic_counters_) {
39  const std::string& counter_name = counter.first;
40  const double counter_value = counter.second;
41  absl::StrAppend(&buffer, "\n", counter_name, ": ", (counter_value));
42  }
43 #endif
44  return buffer;
45 }
46 
48  double limit_in_seconds,
49  double deterministic_limit)
50  : base_time_limit_(ABSL_DIE_IF_NULL(base_time_limit)),
51  time_limit_(std::min(base_time_limit_->GetTimeLeft(), limit_in_seconds),
52  std::min(base_time_limit_->GetDeterministicTimeLeft(),
53  deterministic_limit)) {
54  if (base_time_limit_->external_boolean_as_limit_ != nullptr) {
56  base_time_limit_->external_boolean_as_limit_);
57  }
58 }
59 
61  base_time_limit_->AdvanceDeterministicTime(
62  time_limit_.GetElapsedDeterministicTime());
63 }
64 
65 } // namespace operations_research
min
int64 min
Definition: alldiff_cst.cc:138
time_limit.h
operations_research::TimeLimit::kSafetyBufferSeconds
static const double kSafetyBufferSeconds
Definition: time_limit.h:107
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
operations_research::TimeLimit::GetDeterministicTimeLeft
double GetDeterministicTimeLeft() const
Returns the remaining deterministic time before LimitReached() returns true due to the deterministic ...
Definition: time_limit.h:212
operations_research::TimeLimit
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
Definition: time_limit.h:105
operations_research::TimeLimit::DebugString
std::string DebugString() const
Returns information about the time limit object in a human-readable form.
Definition: time_limit.cc:31
operations_research::NestedTimeLimit::NestedTimeLimit
NestedTimeLimit(TimeLimit *base_time_limit, double limit_in_seconds, double deterministic_limit)
Creates the nested time limit.
Definition: time_limit.cc:47
operations_research::TimeLimit::GetTimeLeft
double GetTimeLeft() const
Returns the time left on this limit, or 0 if the limit was reached (it never returns a negative value...
Definition: time_limit.h:570
operations_research::TimeLimit::RegisterExternalBooleanAsLimit
void RegisterExternalBooleanAsLimit(std::atomic< bool > *external_boolean_as_limit)
Registers the external Boolean to check when LimitReached() is called.
Definition: time_limit.h:272
operations_research::TimeLimit::AdvanceDeterministicTime
void AdvanceDeterministicTime(double deterministic_duration)
Advances the deterministic time.
Definition: time_limit.h:226
ABSL_FLAG
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.")
ABSL_DIE_IF_NULL
#define ABSL_DIE_IF_NULL
Definition: base/logging.h:39
operations_research::TimeLimit::GetElapsedTime
double GetElapsedTime() const
Returns the time elapsed in seconds since the construction of this object.
Definition: time_limit.h:251
operations_research::TimeLimit::kHistorySize
static const int kHistorySize
Definition: time_limit.h:108
operations_research::TimeLimit::GetElapsedDeterministicTime
double GetElapsedDeterministicTime() const
Returns the elapsed deterministic time since the construction of this object.
Definition: time_limit.h:260
operations_research::NestedTimeLimit::~NestedTimeLimit
~NestedTimeLimit()
Updates elapsed deterministic time in the base time limit object.
Definition: time_limit.cc:60