OR-Tools  8.1
bop_solution.cc
Go to the documentation of this file.
1 // Copyright 2010-2018 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 namespace operations_research {
17 namespace bop {
18 
19 using ::operations_research::sat::LinearBooleanConstraint;
20 using ::operations_research::sat::LinearBooleanProblem;
21 using ::operations_research::sat::LinearObjective;
22 
23 //------------------------------------------------------------------------------
24 // BopSolution
25 //------------------------------------------------------------------------------
26 BopSolution::BopSolution(const LinearBooleanProblem& problem,
27  const std::string& name)
28  : problem_(&problem),
29  name_(name),
30  values_(problem.num_variables(), false),
31  recompute_cost_(true),
32  recompute_is_feasible_(true),
33  cost_(0),
34  is_feasible_(false) {
35  // Try the lucky assignment, i.e. the optimal one if feasible.
36  const LinearObjective& objective = problem.objective();
37  for (int i = 0; i < objective.coefficients_size(); ++i) {
38  const VariableIndex var(objective.literals(i) - 1);
39  values_[var] = objective.coefficients(i) < 0;
40  }
41 }
42 
43 int64 BopSolution::ComputeCost() const {
44  recompute_cost_ = false;
45  int64 sum = 0;
46  const LinearObjective& objective = problem_->objective();
47  const size_t num_sparse_vars = objective.literals_size();
48  CHECK_EQ(num_sparse_vars, objective.coefficients_size());
49  for (int i = 0; i < num_sparse_vars; ++i) {
50  CHECK_GT(objective.literals(i), 0);
51  const VariableIndex var(abs(objective.literals(i)) - 1);
52  if (values_[var]) {
53  sum += objective.coefficients(i);
54  }
55  }
56  return sum;
57 }
58 
59 bool BopSolution::ComputeIsFeasible() const {
60  recompute_is_feasible_ = false;
61  for (const LinearBooleanConstraint& constraint : problem_->constraints()) {
62  int64 sum = 0;
63  const size_t num_sparse_vars = constraint.literals_size();
64  CHECK_EQ(num_sparse_vars, constraint.coefficients_size());
65 
66  for (int i = 0; i < num_sparse_vars; ++i) {
67  // The solver doesn't support negative literals yet.
68  CHECK_GT(constraint.literals(i), 0);
69  const VariableIndex var(abs(constraint.literals(i)) - 1);
70  if (values_[var]) {
71  sum += constraint.coefficients(i);
72  }
73  }
74 
75  if ((constraint.has_upper_bound() && sum > constraint.upper_bound()) ||
76  (constraint.has_lower_bound() && sum < constraint.lower_bound())) {
77  return false;
78  }
79  }
80  return true;
81 }
82 } // namespace bop
83 } // namespace operations_research
var
IntVar * var
Definition: expr_array.cc:1858
CHECK_GT
#define CHECK_GT(val1, val2)
Definition: base/logging.h:702
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
bop_solution.h
int64
int64_t int64
Definition: integral_types.h:34
operations_research::bop::BopSolution::BopSolution
BopSolution(const sat::LinearBooleanProblem &problem, const std::string &name)
Definition: bop_solution.cc:26
CHECK_EQ
#define CHECK_EQ(val1, val2)
Definition: base/logging.h:697
name
const std::string name
Definition: default_search.cc:808