OR-Tools  8.2
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
18ABSL_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
22ABSL_FLAG(bool, time_limit_use_instruction_count, false,
23 "If true, measures the number of instructions executed");
24
25namespace operations_research {
26
27// static constants.
28const double TimeLimit::kSafetyBufferSeconds = 1e-4;
29const int TimeLimit::kHistorySize = 100;
30
31std::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
int64 min
Definition: alldiff_cst.cc:138
#define ABSL_DIE_IF_NULL
Definition: base/logging.h:39
~NestedTimeLimit()
Updates elapsed deterministic time in the base time limit object.
Definition: time_limit.cc:60
NestedTimeLimit(TimeLimit *base_time_limit, double limit_in_seconds, double deterministic_limit)
Creates the nested time limit.
Definition: time_limit.cc:47
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
Definition: time_limit.h:105
static const double kSafetyBufferSeconds
Definition: time_limit.h:107
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:570
void RegisterExternalBooleanAsLimit(std::atomic< bool > *external_boolean_as_limit)
Registers the external Boolean to check when LimitReached() is called.
Definition: time_limit.h:272
std::string DebugString() const
Returns information about the time limit object in a human-readable form.
Definition: time_limit.cc:31
static const int kHistorySize
Definition: time_limit.h:108
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
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
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.")