19#include "absl/container/flat_hash_set.h"
20#include "absl/log/check.h"
21#include "absl/status/statusor.h"
31absl::StatusOr<LpModel> ReorderVariables(
34 const bool allow_skip_old) {
35 constexpr VariableIndex kBad(-1);
37 model.variables().end_index(), kBad);
38 for (
const VariableIndex v_new : new_to_old.
index_range()) {
39 const VariableIndex v_old = new_to_old[v_new];
40 if (v_old < VariableIndex(0) || v_old >=
model.variables().end_index()) {
42 <<
"values of new_to_old be in [0," <<
model.variables().size()
43 <<
"), found: " << v_old;
45 if (old_to_new[v_old] != kBad) {
47 <<
"found value: " << v_old <<
" twice in new_to_old";
49 old_to_new[v_old] = v_new;
51 if (!allow_skip_old) {
52 for (
const VariableIndex v_old : old_to_new.index_range()) {
53 if (old_to_new[v_old] == kBad) {
55 <<
"no new VariableIndex for old VariableIndex: " << v_old;
60 for (
const VariableIndex new_var : new_to_old.
index_range()) {
62 result.AddVariable(
model.variables()[new_to_old[new_var]]).status())
63 <<
"should be unreachable";
67 for (Constraint c :
model.constraints()) {
68 for (
auto& [unused,
var] :
c.terms) {
69 if (old_to_new[
var] == kBad) {
72 <<
" appears in a constraint but is not new_to_old";
77 <<
"should be unreachable";
86 model.variables().end_index());
88 for (
const auto& [unused,
var] : c.terms) {
89 old_vars_used[
var] =
true;
93 for (VariableIndex v_old : old_vars_used.
index_range()) {
94 if (old_vars_used[v_old]) {
98 auto result = ReorderVariables(
model, new_to_old,
true);
100 return *std::move(result);
106 new_index_to_old_index) {
107 return ReorderVariables(
model, new_index_to_old_index,
116 for (
const std::string&
name : order_by_name) {
117 auto it =
model.variable_names().find(
name);
118 if (it ==
model.variable_names().end()) {
120 <<
"no variable with name: " <<
name <<
" in model";
#define RETURN_IF_ERROR(expr)
void push_back(const value_type &val)
StrongIntRange< IntType > index_range() const
const std::string name
A name for logging purposes.
StatusBuilder InvalidArgumentErrorBuilder()