OR-Tools  9.6
bop_solution.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 <string>
18 
19 #include "absl/strings/string_view.h"
20 
21 namespace operations_research {
22 namespace bop {
23 
24 using ::operations_research::sat::LinearBooleanConstraint;
25 using ::operations_research::sat::LinearBooleanProblem;
26 using ::operations_research::sat::LinearObjective;
27 
28 //------------------------------------------------------------------------------
29 // BopSolution
30 //------------------------------------------------------------------------------
31 BopSolution::BopSolution(const LinearBooleanProblem& problem,
32  absl::string_view name)
33  : problem_(&problem),
34  name_(name),
35  values_(problem.num_variables(), false),
36  recompute_cost_(true),
37  recompute_is_feasible_(true),
38  cost_(0),
39  is_feasible_(false) {
40  // Try the lucky assignment, i.e. the optimal one if feasible.
41  const LinearObjective& objective = problem.objective();
42  for (int i = 0; i < objective.coefficients_size(); ++i) {
43  const VariableIndex var(objective.literals(i) - 1);
44  values_[var] = objective.coefficients(i) < 0;
45  }
46 }
47 
48 int64_t BopSolution::ComputeCost() const {
49  recompute_cost_ = false;
50  int64_t sum = 0;
51  const LinearObjective& objective = problem_->objective();
52  const size_t num_sparse_vars = objective.literals_size();
53  CHECK_EQ(num_sparse_vars, objective.coefficients_size());
54  for (int i = 0; i < num_sparse_vars; ++i) {
55  CHECK_GT(objective.literals(i), 0);
56  const VariableIndex var(abs(objective.literals(i)) - 1);
57  if (values_[var]) {
58  sum += objective.coefficients(i);
59  }
60  }
61  return sum;
62 }
63 
64 bool BopSolution::ComputeIsFeasible() const {
65  recompute_is_feasible_ = false;
66  for (const LinearBooleanConstraint& constraint : problem_->constraints()) {
67  int64_t sum = 0;
68  const size_t num_sparse_vars = constraint.literals_size();
69  CHECK_EQ(num_sparse_vars, constraint.coefficients_size());
70 
71  for (int i = 0; i < num_sparse_vars; ++i) {
72  // The solver doesn't support negative literals yet.
73  CHECK_GT(constraint.literals(i), 0);
74  const VariableIndex var(abs(constraint.literals(i)) - 1);
75  if (values_[var]) {
76  sum += constraint.coefficients(i);
77  }
78  }
79 
80  if ((constraint.has_upper_bound() && sum > constraint.upper_bound()) ||
81  (constraint.has_lower_bound() && sum < constraint.lower_bound())) {
82  return false;
83  }
84  }
85  return true;
86 }
87 } // namespace bop
88 } // namespace operations_research
BopSolution(const sat::LinearBooleanProblem &problem, absl::string_view name)
Definition: bop_solution.cc:31
const std::string name
IntVar * var
Definition: expr_array.cc:1874
Collection of objects used to extend the Constraint Solver library.