Google OR-Tools v9.11
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-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
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"
24#include "ortools/sat/boolean_problem.pb.h"
25#include "ortools/sat/cp_model.pb.h"
30
31namespace operations_research {
32namespace sat {
33
34// Converts a LinearBooleanProblem to a CpModelProto which should eventually
35// replace completely the LinearBooleanProblem proto.
36CpModelProto 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.
50void 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.
54void 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.
59absl::Status ValidateBooleanProblem(const LinearBooleanProblem& problem);
60
61// Loads a BooleanProblem into a given SatSolver instance.
62bool 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).
68bool LoadAndConsumeBooleanProblem(LinearBooleanProblem* problem,
69 SatSolver* solver);
70
71// Uses the objective coefficient to drive the SAT search towards an
72// heuristically better solution.
73void UseObjectiveForSatAssignmentPreference(const LinearBooleanProblem& problem,
74 SatSolver* solver);
75
76// Adds the constraint that the objective is smaller than the given upper bound.
77bool 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.
82bool 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.
88Coefficient ComputeObjectiveValue(const LinearBooleanProblem& problem,
89 const std::vector<bool>& assignment);
90
91// Checks that an assignment is valid for the given BooleanProblem.
92bool 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 BooleanAssignment proto.
102// Note that only the assigned variables are stored, so the assignment may be
103// incomplete.
104void StoreAssignment(const VariablesAssignment& assignment,
105 BooleanAssignment* output);
106
107// Constructs a sub-problem formed by the constraints with given indices.
108void 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.
114void 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.
139void ProbeAndSimplifyProblem(SatPostsolver* postsolver,
140 LinearBooleanProblem* problem);
141
142} // namespace sat
143} // namespace operations_research
144
145#endif // OR_TOOLS_SAT_BOOLEAN_PROBLEM_H_
double upper_bound
double lower_bound
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)
void ExtractSubproblem(const LinearBooleanProblem &problem, const std::vector< int > &constraint_indices, LinearBooleanProblem *subproblem)
Constructs a sub-problem formed by the constraints with given indices.
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)
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.