Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
cp_model_test_utils.cc
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
15
16#include <stdint.h>
17
18#include <limits>
19#include <vector>
20
21#include "absl/random/random.h"
22#include "ortools/sat/cp_model.pb.h"
24
25namespace operations_research {
26namespace sat {
27
28CpModelProto Random3SatProblem(int num_variables,
29 double proportion_of_constraints) {
30 CpModelProto result;
31 absl::BitGen random;
32 result.set_name("Random 3-SAT");
33 for (int i = 0; i < num_variables; ++i) {
34 sat::IntegerVariableProto* var = result.add_variables();
35 var->add_domain(0);
36 var->add_domain(1);
37 }
38 const int num_constraints = proportion_of_constraints * num_variables;
39 for (int i = 0; i < num_constraints; ++i) {
40 auto* ct = result.add_constraints()->mutable_bool_or();
41 std::vector<int> clause;
42 while (ct->literals_size() != 3) {
43 const int literal =
44 absl::Uniform(random, NegatedRef(num_variables - 1), num_variables);
45 bool is_already_present = false;
46 for (const int lit : ct->literals()) {
47 if (lit != literal) continue;
48 is_already_present = true;
49 break;
50 }
51 if (!is_already_present) ct->add_literals(literal);
52 }
53 }
54 return result;
55}
56
57CpModelProto RandomLinearProblem(int num_variables, int num_constraints) {
58 CpModelProto result;
59 absl::BitGen random;
60 result.set_name("Random 0-1 linear problem");
61 for (int i = 0; i < num_variables; ++i) {
62 sat::IntegerVariableProto* var = result.add_variables();
63 var->add_domain(0);
64 var->add_domain(1);
65 }
66 for (int i = 0; i < num_constraints; ++i) {
67 // Sum >= num_variables / 10.
68 auto* ct = result.add_constraints()->mutable_linear();
69 const int min_value = num_variables / 10;
70 ct->add_domain(min_value);
71 ct->add_domain(std::numeric_limits<int64_t>::max());
72 for (int v = 0; v < num_variables; ++v) {
73 if (absl::Bernoulli(random, 0.5) ||
74 // To ensure that the constraint is feasible, we enforce that it has
75 // at least the 'minimum' number of terms. This clause should only
76 // rarely be used, when num_variables is high.
77 num_variables - v <= min_value - ct->vars_size()) {
78 ct->add_vars(v);
79 ct->add_coeffs(1);
80 }
81 }
82 }
83
84 // Objective: minimize variables at one.
85 {
86 const int objective_var_index = result.variables_size();
87 {
88 sat::IntegerVariableProto* var = result.add_variables();
89 var->add_domain(0);
90 var->add_domain(num_variables);
91 }
92 result.mutable_objective()->add_vars(objective_var_index);
93 result.mutable_objective()->add_coeffs(1);
94
95 // Sum of all other variables == 0
96 auto* ct = result.add_constraints()->mutable_linear();
97 ct->add_domain(0);
98 ct->add_domain(0);
99 for (int v = 0; v < num_variables; ++v) {
100 ct->add_vars(v);
101 ct->add_coeffs(1);
102 }
103 ct->add_vars(objective_var_index);
104 ct->add_coeffs(-1);
105 }
106
107 return result;
108}
109
110} // namespace sat
111} // namespace operations_research
CpModelProto Random3SatProblem(int num_variables, double proportion_of_constraints)
CpModelProto RandomLinearProblem(int num_variables, int num_constraints)
int NegatedRef(int ref)
Small utility functions to deal with negative variable/literal references.
In SWIG mode, we don't want anything besides these top-level includes.