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