OR-Tools  9.6
time_limit.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 <limits>
18 #include <memory>
19 #include <string>
20 #include <utility>
21 
22 #include "absl/strings/str_cat.h"
23 
24 ABSL_FLAG(bool, time_limit_use_usertime, false,
25  "If true, rely on the user time in the TimeLimit class. This is "
26  "only recommended for benchmarking on a non-isolated environment.");
27 
28 ABSL_FLAG(bool, time_limit_use_instruction_count, false,
29  "If true, measures the number of instructions executed");
30 
31 namespace operations_research {
32 
33 // static constants.
34 const double TimeLimit::kSafetyBufferSeconds = 1e-4;
35 const int TimeLimit::kHistorySize = 100;
36 
37 std::string TimeLimit::DebugString() const {
38  std::string buffer = absl::StrCat(
39  "Time left: ", (GetTimeLeft()),
40  "\nDeterministic time left: ", (GetDeterministicTimeLeft()),
41  "\nElapsed time: ", (GetElapsedTime()),
42  "\nElapsed deterministic time: ", (GetElapsedDeterministicTime()));
43 #ifndef NDEBUG
44  for (const auto& counter : deterministic_counters_) {
45  const std::string& counter_name = counter.first;
46  const double counter_value = counter.second;
47  absl::StrAppend(&buffer, "\n", counter_name, ": ", (counter_value));
48  }
49 #endif
50  return buffer;
51 }
52 
54  double limit_in_seconds,
55  double deterministic_limit)
56  : base_time_limit_(ABSL_DIE_IF_NULL(base_time_limit)),
57  time_limit_(std::min(base_time_limit_->GetTimeLeft(), limit_in_seconds),
58  std::min(base_time_limit_->GetDeterministicTimeLeft(),
59  deterministic_limit)) {
60  if (base_time_limit_->external_boolean_as_limit_ != nullptr) {
62  base_time_limit_->external_boolean_as_limit_);
63  }
64 }
65 
67  base_time_limit_->AdvanceDeterministicTime(
68  time_limit_.GetElapsedDeterministicTime());
69 }
70 } // namespace operations_research
int64_t min
Definition: alldiff_cst.cc:139
~NestedTimeLimit()
Updates elapsed deterministic time in the base time limit object.
Definition: time_limit.cc:66
NestedTimeLimit(TimeLimit *base_time_limit, double limit_in_seconds, double deterministic_limit)
Creates the nested time limit.
Definition: time_limit.cc:53
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
Definition: time_limit.h:106
static const double kSafetyBufferSeconds
Definition: time_limit.h:108
double GetDeterministicTimeLeft() const
Returns the remaining deterministic time before LimitReached() returns true due to the deterministic ...
Definition: time_limit.h:212
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:590
void RegisterExternalBooleanAsLimit(std::atomic< bool > *external_boolean_as_limit)
Registers the external Boolean to check when LimitReached() is called.
Definition: time_limit.h:273
std::string DebugString() const
Returns information about the time limit object in a human-readable form.
Definition: time_limit.cc:37
static const int kHistorySize
Definition: time_limit.h:109
double GetElapsedDeterministicTime() const
Returns the elapsed deterministic time since the construction of this object.
Definition: time_limit.h:260
double GetElapsedTime() const
Returns the time elapsed in seconds since the construction of this object.
Definition: time_limit.h:251
void AdvanceDeterministicTime(double deterministic_duration)
Advances the deterministic time.
Definition: time_limit.h:226
Collection of objects used to extend the Constraint Solver library.
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.")