OR-Tools  9.2
update_tracker.h
Go to the documentation of this file.
1// Copyright 2010-2021 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#ifndef OR_TOOLS_MATH_OPT_CPP_UPDATE_TRACKER_H_
15#define OR_TOOLS_MATH_OPT_CPP_UPDATE_TRACKER_H_
16
17#include <memory>
18#include <optional>
19
20#include "absl/strings/string_view.h"
22#include "ortools/math_opt/model.pb.h"
23#include "ortools/math_opt/model_update.pb.h" // IWYU pragma: export
24
25namespace operations_research {
26namespace math_opt {
27
28// Tracks the changes of the model.
29//
30// This is an advanced feature that most users won't need. It is used internally
31// to implement incrementalism but users don't have to understand how it works
32// to use incremental solve.
33//
34// For each update tracker we define a checkpoint that is the starting point
35// used to compute the ModelUpdateProto.
36//
37// No member function should be called after the destruction of the Model
38// object. Note though that it is safe to call the destructor of UpdateTracker
39// even if the Model object has been destroyed already.
40//
41// Thread-safety: UpdateTracker methods must not be used while modifying the
42// model (variables, constraints, ...). The user is expected to use proper
43// synchronization primitives to serialize changes to the model and the use of
44// the update trackers. The methods of different instances of UpdateTracker are
45// safe to be called concurrently (i.e. multiple trackers can be called
46// concurrently on ExportModelUpdate() or Checkpoint()). The destructor of
47// UpdateTracker is thread-safe.
48//
49// Example:
50// Model model;
51// ...
52// const std::unique_ptr<UpdateTracker> update_tracker =
53// model.NewUpdateTracker();
54//
55// model.AddVariable(0.0, 1.0, true, "y");
56// model.set_maximize(true);
57//
58// const std::optional<ModelUpdateProto> update_proto =
59// update_tracker.ExportModelUpdate();
60// update_tracker.Checkpoint();
61//
62// if (update_proto) {
63// ... use *update_proto here ...
64// }
66 public:
67 // This constructor should not be used directly. Instead use
68 // Model::NewUpdateTracker().
69 explicit UpdateTracker(const std::shared_ptr<ModelStorage>& storage);
70
72
73 // Returns a proto representation of the changes to the model since the most
74 // recent checkpoint (i.e. last time Checkpoint() was called); nullopt if
75 // the update would have been empty.
76 std::optional<ModelUpdateProto> ExportModelUpdate();
77
78 // Uses the current model state as the starting point to calculate the
79 // ModelUpdateProto next time ExportModelUpdate() is called.
80 void Checkpoint();
81
82 // Returns a proto representation of the whole model.
83 //
84 // This is a shortcut method that is equivalent to calling
85 // Model::ExportModel(). It is there so that users of the UpdateTracker
86 // can avoid having to keep a reference to the Model model.
87 ModelProto ExportModel() const;
88
89 private:
90 const std::weak_ptr<ModelStorage> storage_;
91 const UpdateTrackerId update_tracker_;
92};
93
94namespace internal {
95
96// The CHECK message used when a function of UpdateTracker is called after the
97// destruction of the model..
98constexpr absl::string_view kModelIsDestroyed =
99 "Can't call this function after the associated model has been destroyed.";
100
101} // namespace internal
102
103} // namespace math_opt
104} // namespace operations_research
105
106#endif // OR_TOOLS_MATH_OPT_CPP_UPDATE_TRACKER_H_
std::optional< ModelUpdateProto > ExportModelUpdate()
UpdateTracker(const std::shared_ptr< ModelStorage > &storage)
constexpr absl::string_view kModelIsDestroyed
Collection of objects used to extend the Constraint Solver library.