Files
ortools-clone/ortools/math_opt/cpp/update_tracker.h

119 lines
4.3 KiB
C
Raw Permalink Normal View History

2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 Google LLC
2022-01-12 16:01:42 +01:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2022-09-05 18:30:25 +02:00
// IWYU pragma: private, include "ortools/math_opt/cpp/math_opt.h"
// IWYU pragma: friend "ortools/math_opt/cpp/.*"
2025-11-05 11:34:49 +01:00
#ifndef ORTOOLS_MATH_OPT_CPP_UPDATE_TRACKER_H_
#define ORTOOLS_MATH_OPT_CPP_UPDATE_TRACKER_H_
2022-01-12 16:01:42 +01:00
#include <memory>
#include <optional>
2022-03-02 22:10:23 +01:00
#include "absl/status/status.h"
#include "absl/status/statusor.h"
2022-01-12 16:01:42 +01:00
#include "absl/strings/string_view.h"
#include "ortools/math_opt/model.pb.h"
#include "ortools/math_opt/model_update.pb.h" // IWYU pragma: export
2022-05-25 17:24:29 +02:00
#include "ortools/math_opt/storage/model_storage.h"
2022-01-12 16:01:42 +01:00
namespace operations_research {
namespace math_opt {
// Tracks the changes of the model.
//
// This is an advanced feature that most users won't need. It is used internally
// to implement incrementalism but users don't have to understand how it works
// to use incremental solve.
//
// For each update tracker we define a checkpoint that is the starting point
// used to compute the ModelUpdateProto.
//
// No member function should be called after the destruction of the Model
// object. Note though that it is safe to call the destructor of UpdateTracker
// even if the Model object has been destroyed already.
//
// Thread-safety: UpdateTracker methods must not be used while modifying the
// model (variables, constraints, ...). The user is expected to use proper
// synchronization primitives to serialize changes to the model and the use of
// the update trackers. The methods of different instances of UpdateTracker are
// safe to be called concurrently (i.e. multiple trackers can be called
2022-09-05 18:30:25 +02:00
// concurrently on ExportModelUpdate() or AdvanceCheckpoint()). The destructor
// of UpdateTracker is thread-safe.
2022-01-12 16:01:42 +01:00
//
// Example:
// Model model;
// ...
// const std::unique_ptr<UpdateTracker> update_tracker =
// model.NewUpdateTracker();
//
// model.AddVariable(0.0, 1.0, true, "y");
// model.set_maximize(true);
//
2022-03-02 22:10:23 +01:00
// ASSIGN_OR_RETURN(const std::optional<ModelUpdateProto> update_proto,
// update_tracker.ExportModelUpdate());
2022-09-05 18:30:25 +02:00
// RETURN_IF_ERROR(update_tracker.AdvanceCheckpoint());
2022-01-12 16:01:42 +01:00
//
// if (update_proto) {
// ... use *update_proto here ...
// }
class UpdateTracker {
public:
// This constructor should not be used directly. Instead use
// Model::NewUpdateTracker().
explicit UpdateTracker(const std::shared_ptr<ModelStorage>& storage);
~UpdateTracker();
// Returns a proto representation of the changes to the model since the most
2022-09-05 18:30:25 +02:00
// recent checkpoint (i.e. last time AdvanceCheckpoint() was called); nullopt
// if the update would have been empty.
2022-03-02 22:10:23 +01:00
//
// If fails if the Model has been destroyed.
2023-09-01 14:01:19 +02:00
absl::StatusOr<std::optional<ModelUpdateProto>> ExportModelUpdate(
bool remove_names = false);
2022-01-12 16:01:42 +01:00
// Uses the current model state as the starting point to calculate the
// ModelUpdateProto next time ExportModelUpdate() is called.
2022-03-02 22:10:23 +01:00
//
// If fails if the Model has been destroyed.
2022-09-05 18:30:25 +02:00
absl::Status AdvanceCheckpoint();
2022-01-12 16:01:42 +01:00
// Returns a proto representation of the whole model.
//
// This is a shortcut method that is equivalent to calling
// Model::ExportModel(). It is there so that users of the UpdateTracker
// can avoid having to keep a reference to the Model model.
2022-03-02 22:10:23 +01:00
//
// If fails if the Model has been destroyed.
2023-09-01 14:01:19 +02:00
absl::StatusOr<ModelProto> ExportModel(bool remove_names = false) const;
2022-01-12 16:01:42 +01:00
private:
const std::weak_ptr<ModelStorage> storage_;
const UpdateTrackerId update_tracker_;
};
namespace internal {
2022-03-02 22:10:23 +01:00
// The failure message used when a function of UpdateTracker is called after the
2022-01-12 16:01:42 +01:00
// destruction of the model..
constexpr absl::string_view kModelIsDestroyed =
2022-03-02 22:10:23 +01:00
"can't call this function after the associated model has been destroyed";
2022-01-12 16:01:42 +01:00
} // namespace internal
} // namespace math_opt
} // namespace operations_research
2025-11-05 11:34:49 +01:00
#endif // ORTOOLS_MATH_OPT_CPP_UPDATE_TRACKER_H_