Google OR-Tools v9.9
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-2024 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
24ABSL_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
28namespace operations_research {
29
30// static constants.
31const double TimeLimit::kSafetyBufferSeconds = 1e-4;
32const int TimeLimit::kHistorySize = 100;
33
34std::string TimeLimit::DebugString() const {
35 std::string buffer = absl::StrCat(
36 "Time left: ", (GetTimeLeft()),
37 "\nDeterministic time left: ", (GetDeterministicTimeLeft()),
38 "\nElapsed time: ", (GetElapsedTime()),
39 "\nElapsed deterministic time: ", (GetElapsedDeterministicTime()));
40#ifndef NDEBUG
41 for (const auto& counter : deterministic_counters_) {
42 const std::string& counter_name = counter.first;
43 const double counter_value = counter.second;
44 absl::StrAppend(&buffer, "\n", counter_name, ": ", (counter_value));
45 }
46#endif
47 return buffer;
48}
49
51 double limit_in_seconds,
52 double deterministic_limit)
53 : base_time_limit_(ABSL_DIE_IF_NULL(base_time_limit)),
54 time_limit_(std::min(base_time_limit_->GetTimeLeft(), limit_in_seconds),
55 std::min(base_time_limit_->GetDeterministicTimeLeft(),
56 deterministic_limit)) {
57 if (base_time_limit_->external_boolean_as_limit_ != nullptr) {
59 base_time_limit_->external_boolean_as_limit_);
60 }
61}
62
67} // namespace operations_research
int64_t min
NestedTimeLimit(TimeLimit *base_time_limit, double limit_in_seconds, double deterministic_limit)
Definition time_limit.cc:50
void AdvanceDeterministicTime(double deterministic_duration)
Definition time_limit.h:178
double GetElapsedDeterministicTime() const
Definition time_limit.h:212
void RegisterExternalBooleanAsLimit(std::atomic< bool > *external_boolean_as_limit)
Definition time_limit.h:225
double GetDeterministicTimeLeft() const
Definition time_limit.h:169
static const int kHistorySize
Definition time_limit.h:92
static const double kSafetyBufferSeconds
static constants.
Definition time_limit.h:91
std::string DebugString() const
Definition time_limit.cc:34
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.")