Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
model.proto
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// An encoding format for mathematical optimization problems.
15
16syntax = "proto3";
17
18package operations_research.service.v1.mathopt;
19
20import "ortools/service/v1/mathopt/sparse_containers.proto";
21
22option java_multiple_files = true;
23option java_package = "com.google.ortools.service.v1.mathopt";
24option csharp_namespace = "Google.OrTools.Service";
25
26// As used below, we define "#variables" = size(VariablesProto.ids).
27message VariablesProto {
28 // Must be nonnegative and strictly increasing. The max(int64) value can't be
29 // used.
30 repeated int64 ids = 1;
31 // Should have length equal to #variables, values in [-inf, inf).
32 repeated double lower_bounds = 2;
33 // Should have length equal to #variables, values in (-inf, inf].
34 repeated double upper_bounds = 3;
35 // Should have length equal to #variables. Value is false for continuous
36 // variables and true for integer variables.
37 repeated bool integers = 4;
38 // If not set, assumed to be all empty strings. Otherwise, should have length
39 // equal to #variables.
40 //
41 // All nonempty names must be distinct. TODO(b/169575522): we may relax this.
42 repeated string names = 5;
43}
44
45message ObjectiveProto {
46 // false is minimize, true is maximize
47 bool maximize = 1;
48
49 // Must be finite and not NaN.
50 double offset = 2;
51
52 // ObjectiveProto terms that are linear in the decision variables.
53 //
54 // Requirements:
55 // * linear_coefficients.ids are elements of VariablesProto.ids.
56 // * VariablesProto not specified correspond to zero.
57 // * linear_coefficients.values must all be finite.
58 // * linear_coefficients.values can be zero, but this just wastes space.
59 SparseDoubleVectorProto linear_coefficients = 3;
60
61 // Objective terms that are quadratic in the decision variables.
62 //
63 // Requirements in addition to those on SparseDoubleMatrixProto messages:
64 // * Each element of quadratic_coefficients.row_ids and each element of
65 // quadratic_coefficients.column_ids must be an element of
66 // VariablesProto.ids.
67 // * The matrix must be upper triangular: for each i,
68 // quadratic_coefficients.row_ids[i] <=
69 // quadratic_coefficients.column_ids[i].
70 //
71 // Notes:
72 // * Terms not explicitly stored have zero coefficient.
73 // * Elements of quadratic_coefficients.coefficients can be zero, but this
74 // just wastes space.
75 SparseDoubleMatrixProto quadratic_coefficients = 4;
76
77 // Parent messages may have uniqueness requirements on this field; e.g., see
78 // ModelProto.objectives and AuxiliaryObjectivesUpdatesProto.new_objectives.
79 string name = 5;
80
81 // For multi-objective problems, the priority of this objective relative to
82 // the others (lower is more important). This value must be nonnegative.
83 // Furthermore, each objective priority in the model must be distinct at solve
84 // time. This condition is not validated at the proto level, so models may
85 // temporarily have objectives with the same priority.
86 int64 priority = 6;
87}
88
89// As used below, we define "#linear constraints" =
90// size(LinearConstraintsProto.ids).
91message LinearConstraintsProto {
92 // Must be nonnegative and strictly increasing. The max(int64) value can't be
93 // used.
94 repeated int64 ids = 1;
95 // Should have length equal to #linear constraints, values in [-inf, inf).
96 repeated double lower_bounds = 2;
97 // Should have length equal to #linear constraints, values in (-inf, inf].
98 repeated double upper_bounds = 3;
99 // If not set, assumed to be all empty strings. Otherwise, should have length
100 // equal to #linear constraints.
101 //
102 // All nonempty names must be distinct. TODO(b/169575522): we may relax this.
103 repeated string names = 4;
104}
105
106// A single quadratic constraint of the form:
107// lb <= sum{linear_terms} + sum{quadratic_terms} <= ub.
108//
109// If a variable involved in this constraint is deleted, it is treated as if it
110// were set to zero.
111message QuadraticConstraintProto {
112 // Terms that are linear in the decision variables.
113 //
114 // In addition to requirements on SparseDoubleVectorProto messages we require
115 // that:
116 // * linear_terms.ids are elements of VariablesProto.ids.
117 // * linear_terms.values must all be finite and not-NaN.
118 //
119 // Notes:
120 // * Variable ids omitted have a corresponding coefficient of zero.
121 // * linear_terms.values can be zero, but this just wastes space.
122 SparseDoubleVectorProto linear_terms = 1;
123
124 // Terms that are quadratic in the decision variables.
125 //
126 // In addition to requirements on SparseDoubleMatrixProto messages we require
127 // that:
128 // * Each element of quadratic_terms.row_ids and each element of
129 // quadratic_terms.column_ids must be an element of VariablesProto.ids.
130 // * The matrix must be upper triangular: for each i,
131 // quadratic_terms.row_ids[i] <= quadratic_terms.column_ids[i].
132 //
133 // Notes:
134 // * Terms not explicitly stored have zero coefficient.
135 // * Elements of quadratic_terms.coefficients can be zero, but this just
136 // wastes space.
137 SparseDoubleMatrixProto quadratic_terms = 2;
138
139 // Must have value in [-inf, inf), and be less than or equal to `upper_bound`.
140 double lower_bound = 3;
141
142 // Must have value in (-inf, inf], and be greater than or equal to
143 // `lower_bound`.
144 double upper_bound = 4;
145
146 // Parent messages may have uniqueness requirements on this field; e.g., see
147 // ModelProto.quadratic_constraints and
148 // QuadraticConstraintUpdatesProto.new_constraints.
149 string name = 5;
150}
151
152// A single second-order cone constraint of the form:
153//
154// ||`arguments_to_norm`||_2 <= `upper_bound`,
155//
156// where `upper_bound` and each element of `arguments_to_norm` are linear
157// expressions.
158//
159// If a variable involved in this constraint is deleted, it is treated as if it
160// were set to zero.
161message SecondOrderConeConstraintProto {
162 LinearExpressionProto upper_bound = 1;
163 repeated LinearExpressionProto arguments_to_norm = 2;
164
165 // Parent messages may have uniqueness requirements on this field; e.g., see
166 // `ModelProto.second_order_cone_constraints` and
167 // `SecondOrderConeConstraintUpdatesProto.new_constraints`.
168 string name = 3;
169}
170
171// Data for representing a single SOS1 or SOS2 constraint.
172//
173// If a variable involved in this constraint is deleted, it is treated as if it
174// were set to zero.
175message SosConstraintProto {
176 // The expressions over which to apply the SOS constraint:
177 // * SOS1: At most one element takes a nonzero value.
178 // * SOS2: At most two elements take nonzero values, and they must be
179 // adjacent in the repeated ordering.
180 repeated LinearExpressionProto expressions = 1;
181
182 // Either empty or of equal length to expressions. If empty, default weights
183 // are 1, 2, ...
184 // If present, the entries must be unique.
185 repeated double weights = 2;
186
187 // Parent messages may have uniqueness requirements on this field; e.g., see
188 // ModelProto.sos1_constraints and SosConstraintUpdatesProto.new_constraints.
189 string name = 3;
190}
191
192// Data for representing a single indicator constraint of the form:
193// Variable(indicator_id) = (activate_on_zero ? 0 : 1) ⇒
194// lower_bound <= expression <= upper_bound.
195//
196// If a variable involved in this constraint (either the indicator, or appearing
197// in `expression`) is deleted, it is treated as if it were set to zero. In
198// particular, deleting the indicator variable means that the indicator
199// constraint is vacuous if `activate_on_zero` is false, and that it is
200// equivalent to a linear constraint if `activate_on_zero` is true.
201message IndicatorConstraintProto {
202 // An ID corresponding to a binary variable, or unset. If unset, the indicator
203 // constraint is ignored. If set, we require that:
204 // * VariablesProto.integers[indicator_id] = true,
205 // * VariablesProto.lower_bounds[indicator_id] >= 0,
206 // * VariablesProto.upper_bounds[indicator_id] <= 1.
207 // These conditions are not validated by MathOpt, but if not satisfied will
208 // lead to the solver returning an error upon solving.
209 optional int64 indicator_id = 1;
210
211 // If true, then if the indicator variable takes value 0, the implied
212 // constraint must hold. Otherwise, if the indicator variable takes value 1,
213 // then the implied constraint must hold.
214 bool activate_on_zero = 6;
215
216 // Must be a valid linear expression with respect to the containing model:
217 // * All stated conditions on `SparseDoubleVectorProto`,
218 // * All elements of `expression.values` must be finite,
219 // * `expression.ids` are a subset of `VariablesProto.ids`.
220 SparseDoubleVectorProto expression = 2;
221
222 // Must have value in [-inf, inf); cannot be NaN.
223 double lower_bound = 3;
224
225 // Must have value in (-inf, inf]; cannot be NaN.
226 double upper_bound = 4;
227
228 // Parent messages may have uniqueness requirements on this field; e.g., see
229 // `ModelProto.indicator_constraints` and
230 // `IndicatorConstraintUpdatesProto.new_constraints`.
231 string name = 5;
232}
233
234// An optimization problem.
235// MathOpt supports:
236// - Continuous and integer decision variables with optional finite bounds.
237// - Linear and quadratic objectives (single or multiple objectives), either
238// minimized or maximized.
239// - A number of constraints types, including:
240// * Linear constraints
241// * Quadratic constraints
242// * Second-order cone constraints
243// * Logical constraints
244// > SOS1 and SOS2 constraints
245// > Indicator constraints
246//
247// By default, constraints are represented in "id-to-data" maps. However, we
248// represent linear constraints in a more efficient "struct-of-arrays" format.
249message ModelProto {
250 string name = 1;
251 VariablesProto variables = 2;
252
253 // The primary objective in the model.
254 ObjectiveProto objective = 3;
255
256 // Auxiliary objectives for use in multi-objective models.
257 //
258 // Map key IDs must be in [0, max(int64)). Each priority, and each nonempty
259 // name, must be unique and also distinct from the primary `objective`.
260 map<int64, ObjectiveProto> auxiliary_objectives = 10;
261
262 LinearConstraintsProto linear_constraints = 4;
263
264 // The variable coefficients for the linear constraints.
265 //
266 // If a variable involved in this constraint is deleted, it is treated as if
267 // it were set to zero.
268 //
269 // Requirements:
270 // * linear_constraint_matrix.row_ids are elements of linear_constraints.ids.
271 // * linear_constraint_matrix.column_ids are elements of variables.ids.
272 // * Matrix entries not specified are zero.
273 // * linear_constraint_matrix.values must all be finite.
274 SparseDoubleMatrixProto linear_constraint_matrix = 5;
275
276 // Mapped constraints (i.e., stored in "constraint ID"-to-"constraint data"
277 // map). For each subsequent submessage, we require that:
278 // * Each key is in [0, max(int64)).
279 // * Each key is unique in its respective map (but not necessarily across
280 // constraint types)
281 // * Each value contains a name field (called `name`), and each nonempty
282 // name must be distinct across all map entries (but not necessarily
283 // across constraint types).
284
285 // Quadratic constraints in the model.
286 map<int64, QuadraticConstraintProto> quadratic_constraints = 6;
287
288 // Second-order cone constraints in the model.
289 map<int64, SecondOrderConeConstraintProto> second_order_cone_constraints = 11;
290
291 // SOS1 constraints in the model, which constrain that at most one
292 // `expression` can be nonzero. The optional `weights` entries are an
293 // implementation detail used by the solver to (hopefully) converge more
294 // quickly. In more detail, solvers may (or may not) use these weights to
295 // select branching decisions that produce "balanced" children nodes.
296 map<int64, SosConstraintProto> sos1_constraints = 7;
297
298 // SOS2 constraints in the model, which constrain that at most two entries of
299 // `expression` can be nonzero, and they must be adjacent in their ordering.
300 // If no `weights` are provided, this ordering is their linear ordering in the
301 // `expressions` list; if `weights` are presented, the ordering is taken with
302 // respect to these values in increasing order.
303 map<int64, SosConstraintProto> sos2_constraints = 8;
304
305 // Indicator constraints in the model, which enforce that, if a binary
306 // "indicator variable" is set to one, then an "implied constraint" must hold.
307 map<int64, IndicatorConstraintProto> indicator_constraints = 9;
308}