OR-Tools  8.1
boolean_problem.h
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 
14 #ifndef OR_TOOLS_SAT_BOOLEAN_PROBLEM_H_
15 #define OR_TOOLS_SAT_BOOLEAN_PROBLEM_H_
16 
17 #include <memory>
18 #include <string>
19 #include <vector>
20 
21 #include "absl/status/status.h"
27 #include "ortools/sat/sat_base.h"
28 #include "ortools/sat/sat_solver.h"
30 
31 namespace operations_research {
32 namespace sat {
33 
34 // Converts a LinearBooleanProblem to a CpModelProto which should eventually
35 // replace completely the LinearBooleanProblem proto.
36 CpModelProto BooleanProblemToCpModelproto(const LinearBooleanProblem& problem);
37 
38 // Adds the offset and returns the scaled version of the given objective value.
40  const LinearBooleanProblem& problem, Coefficient v) {
41  return (static_cast<double>(v.value()) + problem.objective().offset()) *
42  problem.objective().scaling_factor();
43 }
44 
45 // Keeps the same objective but change the optimization direction from a
46 // minimization problem to a maximization problem.
47 //
48 // Ex: if the problem was to minimize 2 + x, the new problem will be to maximize
49 // 2 + x subject to exactly the same constraints.
50 void ChangeOptimizationDirection(LinearBooleanProblem* problem);
51 
52 // Copies the assignment from the solver into the given Boolean vector. Note
53 // that variables with a greater index that the given num_variables are ignored.
54 void ExtractAssignment(const LinearBooleanProblem& problem,
55  const SatSolver& solver, std::vector<bool>* assignment);
56 
57 // Tests the preconditions of the given problem (as described in the proto) and
58 // returns an error if they are not all satisfied.
59 absl::Status ValidateBooleanProblem(const LinearBooleanProblem& problem);
60 
61 // Loads a BooleanProblem into a given SatSolver instance.
62 bool LoadBooleanProblem(const LinearBooleanProblem& problem, SatSolver* solver);
63 
64 // Same as LoadBooleanProblem() but also free the memory used by the problem
65 // during the loading. This allows to use less peak memory. Note that this
66 // function clear all the constraints of the given problem (not the objective
67 // though).
68 bool LoadAndConsumeBooleanProblem(LinearBooleanProblem* problem,
69  SatSolver* solver);
70 
71 // Uses the objective coefficient to drive the SAT search towards an
72 // heuristically better solution.
73 void UseObjectiveForSatAssignmentPreference(const LinearBooleanProblem& problem,
74  SatSolver* solver);
75 
76 // Adds the constraint that the objective is smaller than the given upper bound.
77 bool AddObjectiveUpperBound(const LinearBooleanProblem& problem,
78  Coefficient upper_bound, SatSolver* solver);
79 
80 // Adds the constraint that the objective is smaller or equals to the given
81 // upper bound.
82 bool AddObjectiveConstraint(const LinearBooleanProblem& problem,
83  bool use_lower_bound, Coefficient lower_bound,
84  bool use_upper_bound, Coefficient upper_bound,
85  SatSolver* solver);
86 
87 // Returns the objective value under the current assignment.
88 Coefficient ComputeObjectiveValue(const LinearBooleanProblem& problem,
89  const std::vector<bool>& assignment);
90 
91 // Checks that an assignment is valid for the given BooleanProblem.
92 bool IsAssignmentValid(const LinearBooleanProblem& problem,
93  const std::vector<bool>& assignment);
94 
95 // Converts a LinearBooleanProblem to the cnf file format.
96 // Note that this only works for pure SAT problems (only clauses), max-sat or
97 // weighted max-sat problems. Returns an empty string on error.
99  const LinearBooleanProblem& problem);
100 
101 // Store a variable assignment into the given BooleanAssignement proto.
102 // Note that only the assigned variables are stored, so the assignment may be
103 // incomplete.
104 void StoreAssignment(const VariablesAssignment& assignment,
105  BooleanAssignment* output);
106 
107 // Constructs a sub-problem formed by the constraints with given indices.
108 void ExtractSubproblem(const LinearBooleanProblem& problem,
109  const std::vector<int>& constraint_indices,
110  LinearBooleanProblem* subproblem);
111 
112 // Modifies the given LinearBooleanProblem so that all the literals appearing
113 // inside are positive.
114 void MakeAllLiteralsPositive(LinearBooleanProblem* problem);
115 
116 // Returns a list of generators of the symmetry group of the given problem. Each
117 // generator is a permutation of the integer range [0, 2n) where n is the number
118 // of variables of the problem. They are permutations of the (index
119 // representation of the) problem literals.
121  const LinearBooleanProblem& problem,
122  std::vector<std::unique_ptr<SparsePermutation>>* generators);
123 
124 // Maps all the literals of the problem. Note that this converts the cost of a
125 // variable correctly, that is if a variable with cost is mapped to another, the
126 // cost of the later is updated.
127 //
128 // Preconditions: the mapping must map l and not(l) to the same variable and be
129 // of the correct size. It can also map a literal index to kTrueLiteralIndex
130 // or kFalseLiteralIndex in order to fix the variable.
133  LinearBooleanProblem* problem);
134 
135 // A simple preprocessing step that does basic probing and removes the fixed and
136 // equivalent variables. Note that the variable indices will also be remapped in
137 // order to be dense. The given postsolver will be updated with the information
138 // needed during postsolve.
139 void ProbeAndSimplifyProblem(SatPostsolver* postsolver,
140  LinearBooleanProblem* problem);
141 
142 } // namespace sat
143 } // namespace operations_research
144 
145 #endif // OR_TOOLS_SAT_BOOLEAN_PROBLEM_H_
simplification.h
operations_research::sat::ApplyLiteralMappingToBooleanProblem
void ApplyLiteralMappingToBooleanProblem(const absl::StrongVector< LiteralIndex, LiteralIndex > &mapping, LinearBooleanProblem *problem)
Definition: boolean_problem.cc:743
operations_research::sat::ValidateBooleanProblem
absl::Status ValidateBooleanProblem(const LinearBooleanProblem &problem)
Definition: boolean_problem.cc:131
cp_model.pb.h
sparse_permutation.h
operations_research::sat::ExtractSubproblem
void ExtractSubproblem(const LinearBooleanProblem &problem, const std::vector< int > &constraint_indices, LinearBooleanProblem *subproblem)
Definition: boolean_problem.cc:486
operations_research::sat::AddObjectiveUpperBound
bool AddObjectiveUpperBound(const LinearBooleanProblem &problem, Coefficient upper_bound, SatSolver *solver)
Definition: boolean_problem.cc:328
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
operations_research::sat::StoreAssignment
void StoreAssignment(const VariablesAssignment &assignment, BooleanAssignment *output)
Definition: boolean_problem.cc:475
operations_research::sat::FindLinearBooleanProblemSymmetries
void FindLinearBooleanProblemSymmetries(const LinearBooleanProblem &problem, std::vector< std::unique_ptr< SparsePermutation >> *generators)
Definition: boolean_problem.cc:670
operations_research::sat::LinearBooleanProblemToCnfString
std::string LinearBooleanProblemToCnfString(const LinearBooleanProblem &problem)
Definition: boolean_problem.cc:390
sat_solver.h
sat_base.h
operations_research::sat::ChangeOptimizationDirection
void ChangeOptimizationDirection(LinearBooleanProblem *problem)
Definition: boolean_problem.cc:208
pb_constraint.h
operations_research::sat::UseObjectiveForSatAssignmentPreference
void UseObjectiveForSatAssignmentPreference(const LinearBooleanProblem &problem, SatSolver *solver)
Definition: boolean_problem.cc:307
operations_research::sat::IsAssignmentValid
bool IsAssignmentValid(const LinearBooleanProblem &problem, const std::vector< bool > &assignment)
Definition: boolean_problem.cc:360
boolean_problem.pb.h
operations_research::sat::AddOffsetAndScaleObjectiveValue
double AddOffsetAndScaleObjectiveValue(const LinearBooleanProblem &problem, Coefficient v)
Definition: boolean_problem.h:39
operations_research::sat::ComputeObjectiveValue
Coefficient ComputeObjectiveValue(const LinearBooleanProblem &problem, const std::vector< bool > &assignment)
Definition: boolean_problem.cc:346
operations_research::sat::LoadBooleanProblem
bool LoadBooleanProblem(const LinearBooleanProblem &problem, SatSolver *solver)
Definition: boolean_problem.cc:219
absl::StrongVector< LiteralIndex, LiteralIndex >
operations_research::sat::ProbeAndSimplifyProblem
void ProbeAndSimplifyProblem(SatPostsolver *postsolver, LinearBooleanProblem *problem)
Definition: boolean_problem.cc:825
strong_vector.h
operations_research::sat::BooleanProblemToCpModelproto
CpModelProto BooleanProblemToCpModelproto(const LinearBooleanProblem &problem)
Definition: boolean_problem.cc:151
operations_research::sat::LoadAndConsumeBooleanProblem
bool LoadAndConsumeBooleanProblem(LinearBooleanProblem *problem, SatSolver *solver)
Definition: boolean_problem.cc:259
operations_research::sat::ExtractAssignment
void ExtractAssignment(const LinearBooleanProblem &problem, const SatSolver &solver, std::vector< bool > *assignment)
Definition: boolean_problem.cc:50
operations_research::sat::AddObjectiveConstraint
bool AddObjectiveConstraint(const LinearBooleanProblem &problem, bool use_lower_bound, Coefficient lower_bound, bool use_upper_bound, Coefficient upper_bound, SatSolver *solver)
Definition: boolean_problem.cc:336
operations_research::sat::MakeAllLiteralsPositive
void MakeAllLiteralsPositive(LinearBooleanProblem *problem)
Definition: boolean_problem.cc:635