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