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