Java Reference

Java Reference

modelbuilder/LinearExprBuilder.java
Go to the documentation of this file.
1 // Copyright 2010-2022 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 package com.google.ortools.modelbuilder;
15 
16 import java.util.Map;
17 import java.util.TreeMap;
18 
20 public final class LinearExprBuilder implements LinearArgument {
21  private final TreeMap<Integer, Double> coefficients;
22  private double offset;
23 
25  this.coefficients = new TreeMap<>();
26  this.offset = 0;
27  }
28 
30  addTerm(expr, 1);
31  return this;
32  }
33 
34  public LinearExprBuilder add(double constant) {
35  offset = offset + constant;
36  return this;
37  }
38 
39  public LinearExprBuilder addTerm(LinearArgument expr, double 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, Double::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, double[] 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  public LinearExprBuilder addWeightedSum(LinearArgument[] exprs, long[] coeffs) {
71  for (int i = 0; i < exprs.length; ++i) {
72  addTerm(exprs[i], (double) coeffs[i]);
73  }
74  return this;
75  }
76 
77  @Override
78  public LinearExpr build() {
79  int numElements = 0;
80  int lastVarIndex = -1;
81  double lastCoeff = 0;
82  for (Map.Entry<Integer, Double> entry : coefficients.entrySet()) {
83  if (entry.getValue() != 0) {
84  numElements++;
85  lastVarIndex = entry.getKey();
86  lastCoeff = entry.getValue();
87  }
88  }
89  if (numElements == 0) {
90  return new ConstantExpression(offset);
91  } else if (numElements == 1) {
92  return new AffineExpression(lastVarIndex, lastCoeff, offset);
93  } else {
94  int[] varIndices = new int[numElements];
95  double[] coeffs = new double[numElements];
96  int index = 0;
97  for (Map.Entry<Integer, Double> entry : coefficients.entrySet()) {
98  if (entry.getValue() != 0) {
99  varIndices[index] = entry.getKey();
100  coeffs[index] = entry.getValue();
101  index++;
102  }
103  }
104  return new WeightedSumExpression(varIndices, coeffs, offset);
105  }
106  }
107 }
LinearExprBuilder addWeightedSum(LinearArgument[] exprs, int[] coeffs)
LinearExprBuilder addTerm(LinearArgument expr, double coeff)
LinearExprBuilder addWeightedSum(LinearArgument[] exprs, long[] coeffs)
LinearExprBuilder addWeightedSum(LinearArgument[] exprs, double[] coeffs)
A object that can build a LinearExpr object.
LinearExpr build()
Builds a linear expression.
A linear expression (sum (ai * xi) + b).
int numElements()
Returns the number of terms (excluding the constant one) in this expression.
double getCoefficient(int index)
Returns the ith coefficient.
double getOffset()
Returns the constant part of the expression.
int getVariableIndex(int index)
Returns the index of the ith variable.