Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
model_storage_v2.cc
Go to the documentation of this file.
1// Copyright 2010-2025 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
15
16#include <cstdint>
17#include <memory>
18#include <optional>
19#include <utility>
20#include <vector>
21
22#include "absl/base/nullability.h"
23#include "absl/log/check.h"
24#include "absl/log/log.h"
25#include "absl/memory/memory.h"
26#include "absl/status/status.h"
27#include "absl/status/statusor.h"
28#include "absl/strings/string_view.h"
29#include "absl/types/span.h"
36
38namespace {
39
40template <typename T>
41std::vector<T> ConvertIdVector(absl::Span<const int64_t> ids) {
42 std::vector<T> result;
43 result.reserve(ids.size());
44 for (const int64_t id : ids) {
45 result.push_back(T{id});
46 }
47 return result;
48}
49
50template <typename T, int i, typename AttrKeyT>
51std::vector<T> ConvertKeyVector(const std::vector<AttrKeyT>& keys) {
52 std::vector<T> result;
53 result.reserve(keys.size());
54 for (const AttrKeyT key : keys) {
55 result.push_back(T{key[i]});
56 }
57 return result;
58}
59
60template <typename T>
61std::vector<T> Sorted(std::vector<T> vec) {
62 absl::c_sort(vec);
63 return vec;
64}
65
66} // namespace
67
69 CHECK(elemental_.DeleteElement(id))
70 << "cannot delete variable with id: " << id << ", it is not in the model";
71}
72
74 CHECK(elemental_.DeleteElement(id))
75 << "cannot delete linear constraint with id: " << id
76 << ", it is not in the model";
77}
78
79absl::StatusOr<absl::Nonnull<std::unique_ptr<ModelStorageV2>>>
80ModelStorageV2::FromModelProto(const ModelProto& model_proto) {
82 return absl::WrapUnique(new ModelStorageV2(std::move(e)));
83}
84
85absl::Nonnull<std::unique_ptr<ModelStorageV2>> ModelStorageV2::Clone(
86 const std::optional<absl::string_view> new_name) const {
87 return absl::WrapUnique(new ModelStorageV2(elemental_.Clone(new_name)));
88}
89
90std::vector<VariableId> ModelStorageV2::Variables() const {
91 return ConvertIdVector<VariableId>(
92 elemental_.AllElementsUntyped(ElementType::kVariable));
93}
94std::vector<VariableId> ModelStorageV2::SortedVariables() const {
95 return Sorted(Variables());
96}
97
99 const ObjectiveId id) const {
100 if (id.has_value()) {
101 const auto slice =
102 elemental_.Slice<0>(DoubleAttr2::kAuxObjLinCoef, id->value());
103 return ConvertKeyVector<VariableId, 1>(slice);
104 } else {
105 return ConvertKeyVector<VariableId, 0>(
106 elemental_.AttrNonDefaults(DoubleAttr1::kObjLinCoef));
107 }
108}
109
110std::vector<LinearConstraintId> ModelStorageV2::LinearConstraints() const {
111 return ConvertIdVector<LinearConstraintId>(
112 elemental_.AllElementsUntyped(ElementType::kLinearConstraint));
113}
114
115std::vector<LinearConstraintId> ModelStorageV2::SortedLinearConstraints()
116 const {
117 return Sorted(LinearConstraints());
118}
119
120absl::StatusOr<ModelProto> ModelStorageV2::ExportModelV2(
121 const bool remove_names) const {
122 return elemental_.ExportModel(remove_names);
123}
124
126 return UpdateTrackerId(elemental_.AddDiff().id());
127}
128
129void ModelStorageV2::DeleteUpdateTracker(const UpdateTrackerId update_tracker) {
130 const std::optional<Elemental::DiffHandle> diff =
131 elemental_.GetDiffHandle(update_tracker.value());
132 CHECK(diff.has_value()) << "UpdateTrackerId " << update_tracker
133 << " not found";
134 elemental_.DeleteDiff(*diff);
135}
136
137absl::StatusOr<std::optional<ModelUpdateProto>>
138ModelStorageV2::ExportModelUpdateV2(const UpdateTrackerId update_tracker,
139 const bool remove_names) const {
140 const std::optional<Elemental::DiffHandle> diff =
141 elemental_.GetDiffHandle(update_tracker.value());
142 CHECK(diff.has_value()) << "UpdateTrackerId " << update_tracker
143 << " not found";
144 return elemental_.ExportModelUpdate(*diff, remove_names);
145}
146
147void ModelStorageV2::AdvanceCheckpoint(const UpdateTrackerId update_tracker) {
148 const std::optional<Elemental::DiffHandle> diff =
149 elemental_.GetDiffHandle(update_tracker.value());
150 CHECK(diff.has_value()) << "UpdateTrackerId " << update_tracker
151 << " not found";
152 elemental_.Advance(*diff);
153}
154
156 const ModelUpdateProto& update_proto) {
157 LOG(FATAL) << "not implemented";
158}
159
160} // namespace operations_research::math_opt
#define ASSIGN_OR_RETURN(lhs, rexpr)
static absl::StatusOr< Elemental > FromModelProto(const ModelProto &proto)
Creates an equivalent Elemental to proto.
absl::Status ApplyUpdateProto(const ModelUpdateProto &update_proto)
void DeleteUpdateTracker(UpdateTrackerId update_tracker)
static absl::StatusOr< absl::Nonnull< std::unique_ptr< ModelStorageV2 > > > FromModelProto(const ModelProto &model_proto)
std::vector< LinearConstraintId > LinearConstraints() const
The LinearConstraintsIds in use (not deleted), order not defined.
std::vector< VariableId > Variables() const
The VariableIds in use (not deleted), order not defined.
std::vector< VariableId > LinearObjectiveNonzeros(ObjectiveId id) const
ModelStorageV2(absl::string_view model_name="", absl::string_view primary_objective_name="")
Creates an empty minimization problem.
void AdvanceCheckpoint(UpdateTrackerId update_tracker)
absl::StatusOr< ModelProto > ExportModelV2(bool remove_names=false) const
std::vector< LinearConstraintId > SortedLinearConstraints() const
void DeleteLinearConstraint(LinearConstraintId id)
std::vector< VariableId > SortedVariables() const
absl::Nonnull< std::unique_ptr< ModelStorageV2 > > Clone(std::optional< absl::string_view > new_name=std::nullopt) const
absl::StatusOr< std::optional< ModelUpdateProto > > ExportModelUpdateV2(UpdateTrackerId update_tracker, bool remove_names=false) const
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
ElementId< ElementType::kVariable > VariableId
Definition elements.h:264
ElementId< ElementType::kLinearConstraint > LinearConstraintId
Definition elements.h:265
std::optional< AuxiliaryObjectiveId > ObjectiveId
nullopt denotes the primary objective.