Java Reference

Java Reference

modelbuilder/ConstantExpression.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 
17 public final class ConstantExpression implements LinearExpr {
18  private final double offset;
19 
20  public ConstantExpression(double offset) {
21  this.offset = offset;
22  }
23 
24  // LinearArgument interface.
25  @Override
26  public LinearExpr build() {
27  return this;
28  }
29 
30  // LinearExpr interface.
31  @Override
32  public int numElements() {
33  return 0;
34  }
35 
36  @Override
37  public int getVariableIndex(int index) {
38  throw new IllegalArgumentException("wrong index in LinearExpr.getVariable(): " + index);
39  }
40 
41  @Override
42  public double getCoefficient(int index) {
43  throw new IllegalArgumentException("wrong index in LinearExpr.getCoefficient(): " + index);
44  }
45 
46  @Override
47  public double getOffset() {
48  return offset;
49  }
50 
51  @Override
52  public String toString() {
53  return String.format("%f", offset);
54  }
55 }
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.
A linear expression (sum (ai * xi) + b).