Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
test_models.cc
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
15
16#include <memory>
17#include <vector>
18
19#include "absl/log/check.h"
20#include "absl/strings/str_cat.h"
22
24
25std::unique_ptr<Model> SmallModel(const bool integer) {
26 auto model = std::make_unique<Model>("small_model");
27 const Variable x_1 = model->AddVariable(0.0, 1.0, integer, "x_1");
28 const Variable y_1 = model->AddVariable(0.0, 1.0, integer, "y_1");
29 model->AddLinearConstraint(x_1 + y_1 <= 1.5);
30
31 const Variable x_2 = model->AddVariable(0.0, 1.0, integer, "x_2");
32 const Variable y_2 = model->AddVariable(0.0, 1.0, integer, "y_2");
33 model->AddLinearConstraint(x_2 + y_2 <= 1.5);
34
35 const Variable x_3 = model->AddVariable(0.0, 1.0, integer, "x_3");
36 const Variable y_3 = model->AddVariable(0.0, 1.0, integer, "y_3");
37 model->AddLinearConstraint(x_3 + y_3 <= 1.5);
38
39 model->Maximize(3.0 * x_1 + 2.0 * y_1 + 3.0 * x_2 + 2.0 * y_2 + 3.0 * x_3 +
40 2.0 * y_3);
41 return model;
42}
43
44std::unique_ptr<Model> DenseIndependentSet(const bool integer, const int n) {
45 CHECK_GT(n, 0);
46
47 auto model = std::make_unique<Model>("dense_independent_set");
48
49 // Problem data.
50 constexpr int m = 3;
51 const std::vector<double> c = {5.0, 4.0, 3.0};
52
53 // Add the variables
54 std::vector<std::vector<Variable>> x(m);
55 for (int i = 0; i < m; ++i) {
56 for (int j = 0; j < n; ++j) {
57 x[i].push_back(
58 model->AddVariable(0, 1, integer, absl::StrCat("x_", i, "_", j)));
59 }
60 }
61
62 // Set the objective.
63 {
64 LinearExpression objective;
65 for (int i = 0; i < m; ++i) {
66 for (int j = 0; j < n; ++j) {
67 objective += c[i] * x[i][j];
68 }
69 }
70 model->Maximize(objective);
71 }
72
73 // Constraints of type (A).
74 for (int i = 0; i < m; ++i) {
75 for (int j = 0; j < n; ++j) {
76 for (int k = j + 1; k < n; ++k) {
77 model->AddLinearConstraint(x[i][j] + x[i][k] <= 1);
78 }
79 }
80 }
81
82 // Constraints of type (B).
83 static_assert(m >= 3);
84 for (int i = 1; i < m; ++i) {
85 for (int j = 0; j < n; ++j) {
86 for (int k = 0; k < n; ++k) {
87 model->AddLinearConstraint(x[0][j] + x[i][k] <= 1);
88 }
89 }
90 }
91
92 return model;
93}
94
96 const Model& model) {
97 CHECK_EQ(model.num_variables() % 3, 0) << model.num_variables();
99 const auto vars = model.SortedVariables();
100 for (const Variable v : vars) {
101 result.variable_values[v] = 0.0;
102 }
103 if (!vars.empty()) {
104 result.variable_values[vars.front()] = 1.0;
105 }
106 return result;
107}
108
109std::unique_ptr<Model> IndependentSetCompleteGraph(const bool integer,
110 const int n) {
111 CHECK_GT(n, 0);
112
113 auto model = std::make_unique<Model>("Simple incomplete solve LP");
114
115 std::vector<Variable> x;
116 for (int i = 0; i < n; ++i) {
117 x.push_back(model->AddVariable(0.0, 1.0, integer));
118 }
119
120 for (int i = 0; i < n; ++i) {
121 for (int j = i + 1; j < n; ++j) {
122 model->AddLinearConstraint(x[i] + x[j] <= 1);
123 }
124 }
125
126 model->Maximize(Sum(x));
127
128 return model;
129}
130
131} // namespace operations_research::math_opt
GRBmodel * model
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
LinearExpression Sum(const Iterable &items)
std::unique_ptr< Model > SmallModel(const bool integer)
std::unique_ptr< Model > IndependentSetCompleteGraph(const bool integer, const int n)
std::unique_ptr< Model > DenseIndependentSet(const bool integer, const int n)
ModelSolveParameters::SolutionHint DenseIndependentSetHint5(const Model &model)