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.LinearExpressionProto;
17
19public interface LinearExpr extends LinearArgument {
22
24 int getVariableIndex(int index);
25
27 long getCoefficient(int index);
28
30 long getOffset();
31
34 return new LinearExprBuilder();
35 }
36
38 static LinearExpr constant(long value) {
39 return newBuilder().add(value).build();
40 }
41
43 static LinearExpr term(LinearArgument expr, long coeff) {
44 return newBuilder().addTerm(expr, coeff).build();
45 }
46
48 static LinearExpr affine(LinearArgument expr, long coeff, long offset) {
49 return newBuilder().addTerm(expr, coeff).add(offset).build();
50 }
51
53 static LinearExpr sum(LinearArgument[] exprs) {
54 return newBuilder().addSum(exprs).build();
55 }
56
58 static LinearExpr weightedSum(LinearArgument[] exprs, long[] coeffs) {
59 return newBuilder().addWeightedSum(exprs, coeffs).build();
60 }
61
62 static LinearExpr rebuildFromLinearExpressionProto(LinearExpressionProto proto) {
63 int numElements = proto.getVarsCount();
64 if (numElements == 0) {
65 return new ConstantExpression(proto.getOffset());
66 } else if (numElements == 1) {
67 return new AffineExpression(proto.getVars(0), proto.getCoeffs(0), proto.getOffset());
68 } else {
69 int[] varsIndices = new int[numElements];
70 long[] coeffs = new long[numElements];
71 long offset = proto.getOffset();
72 for (int i = 0; i < numElements; ++i) {
73 varsIndices[i] = proto.getVars(i);
74 coeffs[i] = proto.getCoeffs(i);
75 }
76 return new WeightedSumExpression(varsIndices, coeffs, offset);
77 }
78 }
79}
A specialized linear expression: a * x + b.
A specialized constant linear expression.
Builder class for the LinearExpr container.
LinearExprBuilder addSum(LinearArgument[] exprs)
LinearExprBuilder add(LinearArgument expr)
LinearExpr build()
Builds a linear expression.
LinearExprBuilder addWeightedSum(LinearArgument[] exprs, long[] coeffs)
LinearExprBuilder addTerm(LinearArgument expr, long coeff)
A specialized linear expression: sum(ai * xi) + b.
A object that can build a LinearExpr object.
A linear expression (sum (ai * xi) + b).
Definition: LinearExpr.java:19
static LinearExpr term(LinearArgument expr, long coeff)
Shortcut for newBuilder().addTerm(expr, coeff).build()
Definition: LinearExpr.java:43
long getCoefficient(int index)
Returns the ith coefficient.
int numElements()
Returns the number of terms (excluding the constant one) in this expression.
static LinearExpr affine(LinearArgument expr, long coeff, long offset)
Shortcut for newBuilder().addTerm(expr, coeff).add(offset).build()
Definition: LinearExpr.java:48
static LinearExprBuilder newBuilder()
Returns a builder.
Definition: LinearExpr.java:33
long getOffset()
Returns the constant part of the expression.
static LinearExpr constant(long value)
Shortcut for newBuilder().add(value).build()
Definition: LinearExpr.java:38
static LinearExpr weightedSum(LinearArgument[] exprs, long[] coeffs)
Shortcut for newBuilder().addWeightedSum(exprs, coeffs).build()
Definition: LinearExpr.java:58
static LinearExpr rebuildFromLinearExpressionProto(LinearExpressionProto proto)
Definition: LinearExpr.java:62
int getVariableIndex(int index)
Returns the index of the ith variable.
static LinearExpr sum(LinearArgument[] exprs)
Shortcut for newBuilder().addSum(exprs).build()
Definition: LinearExpr.java:53