-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathcp_model_test_utils.cc
111 lines (100 loc) · 3.52 KB
/
cp_model_test_utils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/sat/cp_model_test_utils.h"
#include <stdint.h>
#include <limits>
#include <vector>
#include "absl/random/random.h"
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/cp_model_utils.h"
namespace operations_research {
namespace sat {
CpModelProto Random3SatProblem(int num_variables,
double proportion_of_constraints) {
CpModelProto result;
absl::BitGen random;
result.set_name("Random 3-SAT");
for (int i = 0; i < num_variables; ++i) {
sat::IntegerVariableProto* var = result.add_variables();
var->add_domain(0);
var->add_domain(1);
}
const int num_constraints = proportion_of_constraints * num_variables;
for (int i = 0; i < num_constraints; ++i) {
auto* ct = result.add_constraints()->mutable_bool_or();
std::vector<int> clause;
while (ct->literals_size() != 3) {
const int literal =
absl::Uniform(random, NegatedRef(num_variables - 1), num_variables);
bool is_already_present = false;
for (const int lit : ct->literals()) {
if (lit != literal) continue;
is_already_present = true;
break;
}
if (!is_already_present) ct->add_literals(literal);
}
}
return result;
}
CpModelProto RandomLinearProblem(int num_variables, int num_constraints) {
CpModelProto result;
absl::BitGen random;
result.set_name("Random 0-1 linear problem");
for (int i = 0; i < num_variables; ++i) {
sat::IntegerVariableProto* var = result.add_variables();
var->add_domain(0);
var->add_domain(1);
}
for (int i = 0; i < num_constraints; ++i) {
// Sum >= num_variables / 10.
auto* ct = result.add_constraints()->mutable_linear();
const int min_value = num_variables / 10;
ct->add_domain(min_value);
ct->add_domain(std::numeric_limits<int64_t>::max());
for (int v = 0; v < num_variables; ++v) {
if (absl::Bernoulli(random, 0.5) ||
// To ensure that the constraint is feasible, we enforce that it has
// at least the 'minimum' number of terms. This clause should only
// rarely be used, when num_variables is high.
num_variables - v <= min_value - ct->vars_size()) {
ct->add_vars(v);
ct->add_coeffs(1);
}
}
}
// Objective: minimize variables at one.
{
const int objective_var_index = result.variables_size();
{
sat::IntegerVariableProto* var = result.add_variables();
var->add_domain(0);
var->add_domain(num_variables);
}
result.mutable_objective()->add_vars(objective_var_index);
result.mutable_objective()->add_coeffs(1);
// Sum of all other variables == 0
auto* ct = result.add_constraints()->mutable_linear();
ct->add_domain(0);
ct->add_domain(0);
for (int v = 0; v < num_variables; ++v) {
ct->add_vars(v);
ct->add_coeffs(1);
}
ct->add_vars(objective_var_index);
ct->add_coeffs(-1);
}
return result;
}
} // namespace sat
} // namespace operations_research