Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
update_tracker.h
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
14// IWYU pragma: private, include "ortools/math_opt/cpp/math_opt.h"
15// IWYU pragma: friend "ortools/math_opt/cpp/.*"
16
17#ifndef OR_TOOLS_MATH_OPT_CPP_UPDATE_TRACKER_H_
18#define OR_TOOLS_MATH_OPT_CPP_UPDATE_TRACKER_H_
19
20#include <memory>
21#include <optional>
22
23#include "absl/status/status.h"
24#include "absl/status/statusor.h"
25#include "absl/strings/string_view.h"
26#include "ortools/math_opt/model.pb.h"
27#include "ortools/math_opt/model_update.pb.h" // IWYU pragma: export
29
30namespace operations_research {
31namespace math_opt {
32
33// Tracks the changes of the model.
34//
35// This is an advanced feature that most users won't need. It is used internally
36// to implement incrementalism but users don't have to understand how it works
37// to use incremental solve.
38//
39// For each update tracker we define a checkpoint that is the starting point
40// used to compute the ModelUpdateProto.
41//
42// No member function should be called after the destruction of the Model
43// object. Note though that it is safe to call the destructor of UpdateTracker
44// even if the Model object has been destroyed already.
45//
46// Thread-safety: UpdateTracker methods must not be used while modifying the
47// model (variables, constraints, ...). The user is expected to use proper
48// synchronization primitives to serialize changes to the model and the use of
49// the update trackers. The methods of different instances of UpdateTracker are
50// safe to be called concurrently (i.e. multiple trackers can be called
51// concurrently on ExportModelUpdate() or AdvanceCheckpoint()). The destructor
52// of UpdateTracker is thread-safe.
53//
54// Example:
55// Model model;
56// ...
57// const std::unique_ptr<UpdateTracker> update_tracker =
58// model.NewUpdateTracker();
59//
60// model.AddVariable(0.0, 1.0, true, "y");
61// model.set_maximize(true);
62//
63// ASSIGN_OR_RETURN(const std::optional<ModelUpdateProto> update_proto,
64// update_tracker.ExportModelUpdate());
65// RETURN_IF_ERROR(update_tracker.AdvanceCheckpoint());
66//
67// if (update_proto) {
68// ... use *update_proto here ...
69// }
71 public:
72 // This constructor should not be used directly. Instead use
73 // Model::NewUpdateTracker().
74 explicit UpdateTracker(const std::shared_ptr<ModelStorage>& storage);
75
77
78 // Returns a proto representation of the changes to the model since the most
79 // recent checkpoint (i.e. last time AdvanceCheckpoint() was called); nullopt
80 // if the update would have been empty.
81 //
82 // If fails if the Model has been destroyed.
83 absl::StatusOr<std::optional<ModelUpdateProto>> ExportModelUpdate(
84 bool remove_names = false);
85
86 // Uses the current model state as the starting point to calculate the
87 // ModelUpdateProto next time ExportModelUpdate() is called.
88 //
89 // If fails if the Model has been destroyed.
90 absl::Status AdvanceCheckpoint();
91
92 // Returns a proto representation of the whole model.
93 //
94 // This is a shortcut method that is equivalent to calling
95 // Model::ExportModel(). It is there so that users of the UpdateTracker
96 // can avoid having to keep a reference to the Model model.
97 //
98 // If fails if the Model has been destroyed.
99 absl::StatusOr<ModelProto> ExportModel(bool remove_names = false) const;
100
101 private:
102 const std::weak_ptr<ModelStorage> storage_;
103 const UpdateTrackerId update_tracker_;
104};
105
106namespace internal {
107
108// The failure message used when a function of UpdateTracker is called after the
109// destruction of the model..
110constexpr absl::string_view kModelIsDestroyed =
111 "can't call this function after the associated model has been destroyed";
112
113} // namespace internal
114
115} // namespace math_opt
116} // namespace operations_research
117
118#endif // OR_TOOLS_MATH_OPT_CPP_UPDATE_TRACKER_H_
absl::StatusOr< ModelProto > ExportModel(bool remove_names=false) const
UpdateTracker(const std::shared_ptr< ModelStorage > &storage)
absl::StatusOr< std::optional< ModelUpdateProto > > ExportModelUpdate(bool remove_names=false)
constexpr absl::string_view kModelIsDestroyed
In SWIG mode, we don't want anything besides these top-level includes.