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