Google OR-Tools v9.9
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
LinearExprBuilder.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 java.util.Map;
17import java.util.TreeMap;
18
20public final class LinearExprBuilder implements LinearArgument {
21 private final TreeMap<Integer, Long> coefficients;
22 private long offset;
23
24 LinearExprBuilder() {
25 this.coefficients = new TreeMap<>();
26 this.offset = 0;
27 }
28
30 addTerm(expr, 1);
31 return this;
32 }
33
34 public LinearExprBuilder add(long constant) {
35 offset = offset + constant;
36 return this;
37 }
38
39 public LinearExprBuilder addTerm(LinearArgument expr, long coeff) {
40 final LinearExpr e = expr.build();
41 final int numElements = e.numElements();
42 for (int i = 0; i < numElements; ++i) {
43 coefficients.merge(e.getVariableIndex(i), e.getCoefficient(i) * coeff, Long::sum);
44 }
45 offset = offset + e.getOffset() * coeff;
46 return this;
47 }
48
50 for (final LinearArgument expr : exprs) {
51 addTerm(expr, 1);
52 }
53 return this;
54 }
55
56 public LinearExprBuilder addWeightedSum(LinearArgument[] exprs, long[] coeffs) {
57 for (int i = 0; i < exprs.length; ++i) {
58 addTerm(exprs[i], coeffs[i]);
59 }
60 return this;
61 }
62
63 public LinearExprBuilder addWeightedSum(LinearArgument[] exprs, int[] coeffs) {
64 for (int i = 0; i < exprs.length; ++i) {
65 addTerm(exprs[i], coeffs[i]);
66 }
67 return this;
68 }
69
70 @Override
71 public LinearExpr build() {
72 int numElements = 0;
73 int lastVarIndex = -1;
74 long lastCoeff = 0;
75 for (Map.Entry<Integer, Long> entry : coefficients.entrySet()) {
76 if (entry.getValue() != 0) {
77 numElements++;
78 lastVarIndex = entry.getKey();
79 lastCoeff = entry.getValue();
80 }
81 }
82 if (numElements == 0) {
83 return new ConstantExpression(offset);
84 } else if (numElements == 1) {
85 return new AffineExpression(lastVarIndex, lastCoeff, offset);
86 } else {
87 int[] varIndices = new int[numElements];
88 long[] coeffs = new long[numElements];
89 int index = 0;
90 for (Map.Entry<Integer, Long> entry : coefficients.entrySet()) {
91 if (entry.getValue() != 0) {
92 varIndices[index] = entry.getKey();
93 coeffs[index] = entry.getValue();
94 index++;
95 }
96 }
97 return new WeightedSumExpression(varIndices, coeffs, offset);
98 }
99 }
100}
LinearExprBuilder add(LinearArgument expr)
LinearExprBuilder addWeightedSum(LinearArgument[] exprs, long[] coeffs)
LinearExprBuilder addSum(LinearArgument[] exprs)
LinearExprBuilder addWeightedSum(LinearArgument[] exprs, int[] coeffs)
LinearExprBuilder addTerm(LinearArgument expr, long coeff)
LinearExprBuilder add(long constant)