OR-Tools  9.6
update_tracker.h
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 
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 
30 namespace operations_research {
31 namespace 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 
85  // Uses the current model state as the starting point to calculate the
86  // ModelUpdateProto next time ExportModelUpdate() is called.
87  //
88  // If fails if the Model has been destroyed.
89  absl::Status AdvanceCheckpoint();
90 
91  // Returns a proto representation of the whole model.
92  //
93  // This is a shortcut method that is equivalent to calling
94  // Model::ExportModel(). It is there so that users of the UpdateTracker
95  // can avoid having to keep a reference to the Model model.
96  //
97  // If fails if the Model has been destroyed.
98  absl::StatusOr<ModelProto> ExportModel() const;
99 
100  private:
101  const std::weak_ptr<ModelStorage> storage_;
102  const UpdateTrackerId update_tracker_;
103 };
104 
105 namespace internal {
106 
107 // The failure message used when a function of UpdateTracker is called after the
108 // destruction of the model..
109 constexpr absl::string_view kModelIsDestroyed =
110  "can't call this function after the associated model has been destroyed";
111 
112 } // namespace internal
113 
114 } // namespace math_opt
115 } // namespace operations_research
116 
117 #endif // OR_TOOLS_MATH_OPT_CPP_UPDATE_TRACKER_H_
absl::StatusOr< ModelProto > ExportModel() const
UpdateTracker(const std::shared_ptr< ModelStorage > &storage)
absl::StatusOr< std::optional< ModelUpdateProto > > ExportModelUpdate()
constexpr absl::string_view kModelIsDestroyed
Collection of objects used to extend the Constraint Solver library.