Google OR-Tools v9.9
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
LinearExpr.java
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
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
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}
LinearExprBuilder add(LinearArgument expr)
LinearExprBuilder addWeightedSum(LinearArgument[] exprs, long[] coeffs)
LinearExprBuilder addSum(LinearArgument[] exprs)
LinearExprBuilder addTerm(LinearArgument expr, long coeff)
static LinearExpr term(LinearArgument expr, long coeff)
static LinearExpr affine(LinearArgument expr, long coeff, long offset)
static LinearExprBuilder newBuilder()
static LinearExpr weightedSum(LinearArgument[] exprs, long[] coeffs)
static LinearExpr sum(LinearArgument[] exprs)
static LinearExpr rebuildFromLinearExpressionProto(LinearExpressionProto proto)
static LinearExpr constant(long value)