Java Reference

Java Reference

LinearExpr.java
Go to the documentation of this file.
1// Copyright 2010-2021 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
14package com.google.ortools.sat;
15
16import com.google.ortools.sat.CpModelProto;
17import com.google.ortools.sat.LinearExpressionProto;
18
20public interface LinearExpr {
23
25 IntVar getVariable(int index);
26
28 long getCoefficient(int index);
29
31 long getOffset();
32
34 static LinearExpr sum(IntVar[] variables) {
35 return new SumOfVariables(variables);
36 }
37
39 static LinearExpr booleanSum(Literal[] literals) {
40 // We need the scalar product for the negative coefficient of negated Boolean variables.
41 return new ScalProd(literals);
42 }
43
45 static LinearExpr scalProd(IntVar[] variables, long[] coefficients) {
46 if (variables.length != coefficients.length) {
47 throw new CpModel.MismatchedArrayLengths("LinearExpr.scalProd", "variables", "coefficients");
48 }
49 return new ScalProd(variables, coefficients);
50 }
51
53 static LinearExpr scalProd(IntVar[] variables, int[] coefficients) {
54 if (variables.length != coefficients.length) {
55 throw new CpModel.MismatchedArrayLengths("LinearExpr.scalProd", "variables", "coefficients");
56 }
57 long[] tmp = new long[coefficients.length];
58 for (int i = 0; i < coefficients.length; ++i) {
59 tmp[i] = coefficients[i];
60 }
61 return new ScalProd(variables, tmp);
62 }
63
65 static LinearExpr booleanScalProd(Literal[] literals, long[] coefficients) {
66 if (literals.length != coefficients.length) {
67 throw new CpModel.MismatchedArrayLengths("LinearExpr.scalProd", "literals", "coefficients");
68 }
69 return new ScalProd(literals, coefficients);
70 }
71
73 static LinearExpr booleanScalProd(Literal[] literals, int[] coefficients) {
74 if (literals.length != coefficients.length) {
75 throw new CpModel.MismatchedArrayLengths("LinearExpr.scalProd", "literals", "coefficients");
76 }
77
78 long[] tmp = new long[coefficients.length];
79 for (int i = 0; i < coefficients.length; ++i) {
80 tmp[i] = coefficients[i];
81 }
82 return new ScalProd(literals, tmp);
83 }
84
86 static LinearExpr term(IntVar variable, long coefficient) {
87 return new ScalProd(variable, coefficient, 0);
88 }
89
91 static LinearExpr term(Literal lit, long coefficient) {
92 return new ScalProd(lit, coefficient, 0);
93 }
94
96 static LinearExpr affine(IntVar variable, long coefficient, long offset) {
97 return new ScalProd(variable, coefficient, offset);
98 }
99
101 static LinearExpr affine(Literal lit, long coefficient, long offset) {
102 return new ScalProd(lit, coefficient, offset);
103 }
104
106 static LinearExpr constant(long value) {
107 return new Constant(value);
108 }
109
112 int numElements = proto.getVarsCount();
113 if (numElements == 0) {
114 return constant(proto.getOffset());
115 } else if (numElements == 1) {
116 return affine(new IntVar(builder, proto.getVars(0)), proto.getCoeffs(0), proto.getOffset());
117 } else {
118 IntVar[] vars = new IntVar[numElements];
119 long[] coeffs = new long[numElements];
120 long offset = proto.getOffset();
121 boolean allOnes = true;
122 for (int i = 0; i < numElements; ++i) {
123 vars[i] = new IntVar(builder, proto.getVars(i));
124 coeffs[i] = proto.getCoeffs(i);
125 if (coeffs[i] != 1) {
126 allOnes = false;
127 }
128 }
129 if (allOnes) {
130 return new SumOfVariables(vars, offset);
131 } else {
132 return new ScalProd(vars, coeffs, offset);
133 }
134 }
135 }
136}
A linear expression interface that can be parsed.
Definition: Constant.java:17
Exception thrown when parallel arrays have mismatched lengths.
Definition: CpModel.java:53
Main modeling class.
Definition: CpModel.java:44
.lang.Override long getOffset()
int64 offset = 3;
int getVars(int index)
repeated int32 vars = 1;
long getCoeffs(int index)
repeated int64 coeffs = 2;
A linear expression interface that can be parsed.
Definition: ScalProd.java:17
A linear expression interface that can be parsed.
A linear expression interface that can be parsed.
Definition: LinearExpr.java:20
static LinearExpr affine(Literal lit, long coefficient, long offset)
Creates an affine expression (lit * coefficient + offset).
static LinearExpr booleanSum(Literal[] literals)
Creates a sum expression.
Definition: LinearExpr.java:39
long getCoefficient(int index)
Returns the ith coefficient.
static LinearExpr rebuildFromLinearExpressionProto(LinearExpressionProto proto, CpModelProto.Builder builder)
int numElements()
Returns the number of elements in the interface.
static LinearExpr term(Literal lit, long coefficient)
Creates a linear term (lit * coefficient).
Definition: LinearExpr.java:91
long getOffset()
Returns the constant part of the expression.
static LinearExpr booleanScalProd(Literal[] literals, int[] coefficients)
Creates a scalar product.
Definition: LinearExpr.java:73
static LinearExpr constant(long value)
Creates an constant expression.
static LinearExpr sum(IntVar[] variables)
Creates a sum expression.
Definition: LinearExpr.java:34
static LinearExpr affine(IntVar variable, long coefficient, long offset)
Creates an affine expression (var * coefficient + offset).
Definition: LinearExpr.java:96
static LinearExpr booleanScalProd(Literal[] literals, long[] coefficients)
Creates a scalar product.
Definition: LinearExpr.java:65
static LinearExpr term(IntVar variable, long coefficient)
Creates a linear term (var * coefficient).
Definition: LinearExpr.java:86
static LinearExpr scalProd(IntVar[] variables, int[] coefficients)
Creates a scalar product.
Definition: LinearExpr.java:53
static LinearExpr scalProd(IntVar[] variables, long[] coefficients)
Creates a scalar product.
Definition: LinearExpr.java:45
IntVar getVariable(int index)
Returns the ith variable.
Interface to describe a boolean variable or its negation.
Definition: Literal.java:17