Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
bop_solution.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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 <cstdlib>
18#include <string>
19
20#include "absl/log/check.h"
21#include "absl/strings/string_view.h"
24#include "ortools/sat/boolean_problem.pb.h"
25
26namespace operations_research {
27namespace bop {
28
29using ::operations_research::sat::LinearBooleanConstraint;
30using ::operations_research::sat::LinearBooleanProblem;
31using ::operations_research::sat::LinearObjective;
32
33//------------------------------------------------------------------------------
34// BopSolution
35//------------------------------------------------------------------------------
36BopSolution::BopSolution(const LinearBooleanProblem& problem,
37 absl::string_view name)
38 : problem_(&problem),
39 name_(name),
40 values_(problem.num_variables(), false),
41 recompute_cost_(true),
42 recompute_is_feasible_(true),
43 cost_(0),
44 is_feasible_(false) {
45 // Try the lucky assignment, i.e. the optimal one if feasible.
46 const LinearObjective& objective = problem.objective();
47 for (int i = 0; i < objective.coefficients_size(); ++i) {
48 const VariableIndex var(objective.literals(i) - 1);
49 values_[var] = objective.coefficients(i) < 0;
50 }
51}
52
53int64_t BopSolution::ComputeCost() const {
54 recompute_cost_ = false;
55 int64_t sum = 0;
56 const LinearObjective& objective = problem_->objective();
57 const size_t num_sparse_vars = objective.literals_size();
58 CHECK_EQ(num_sparse_vars, objective.coefficients_size());
59 for (int i = 0; i < num_sparse_vars; ++i) {
60 CHECK_GT(objective.literals(i), 0);
61 const VariableIndex var(abs(objective.literals(i)) - 1);
62 if (values_[var]) {
63 sum += objective.coefficients(i);
64 }
65 }
66 return sum;
67}
68
69bool BopSolution::ComputeIsFeasible() const {
70 recompute_is_feasible_ = false;
71 for (const LinearBooleanConstraint& constraint : problem_->constraints()) {
72 int64_t sum = 0;
73 const size_t num_sparse_vars = constraint.literals_size();
74 CHECK_EQ(num_sparse_vars, constraint.coefficients_size());
75
76 for (int i = 0; i < num_sparse_vars; ++i) {
77 // The solver doesn't support negative literals yet.
78 CHECK_GT(constraint.literals(i), 0);
79 const VariableIndex var(abs(constraint.literals(i)) - 1);
80 if (values_[var]) {
81 sum += constraint.coefficients(i);
82 }
83 }
84
85 if ((constraint.has_upper_bound() && sum > constraint.upper_bound()) ||
86 (constraint.has_lower_bound() && sum < constraint.lower_bound())) {
87 return false;
88 }
89 }
90 return true;
91}
92} // namespace bop
93} // namespace operations_research
BopSolution(const sat::LinearBooleanProblem &problem, absl::string_view name)
const std::string name
A name for logging purposes.
IntVar * var
In SWIG mode, we don't want anything besides these top-level includes.