Java Reference

Java Reference

ScalProd.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 
14 package com.google.ortools.sat;
15 
17 public final class ScalProd implements LinearExpr {
18  private final IntVar[] variables;
19  private final long[] coefficients;
20  private long offset;
21 
22  public ScalProd(IntVar[] variables, long[] coefficients) {
23  this.variables = variables;
24  this.coefficients = coefficients;
25  this.offset = 0;
26  }
27 
28  public ScalProd(Literal[] literals, long[] coefficients) {
29  int size = literals.length;
30  this.variables = new IntVar[size];
31  this.coefficients = new long[size];
32  this.offset = 0;
33 
34  for (int i = 0; i < size; ++i) {
35  Literal lit = literals[i];
36  long coeff = coefficients[i];
37  if (lit.getIndex() >= 0) {
38  this.variables[i] = (IntVar) lit;
39  this.coefficients[i] = coeff;
40  } else {
41  this.variables[i] = (IntVar) lit.not();
42  this.coefficients[i] = -coeff;
43  this.offset -= coeff;
44  }
45  }
46  }
47 
48  public ScalProd(IntVar var, long coefficient, long offset) {
49  this.variables = new IntVar[] {var};
50  this.coefficients = new long[] {coefficient};
51  this.offset = offset;
52  }
53 
54  public ScalProd(Literal lit, long coefficient, long offset) {
55  if (lit.getIndex() >= 0) {
56  this.variables = new IntVar[] {(IntVar) lit};
57  this.coefficients = new long[] {coefficient};
58  this.offset = offset;
59  } else {
60  this.variables = new IntVar[] {(IntVar) lit.not()};
61  this.coefficients = new long[] {-coefficient};
62  this.offset = offset + coefficient;
63  }
64  }
65 
66  public ScalProd(Literal[] literals) {
67  int size = literals.length;
68  this.variables = new IntVar[size];
69  this.coefficients = new long[size];
70  this.offset = 0;
71 
72  for (int i = 0; i < size; ++i) {
73  Literal lit = literals[i];
74  if (lit.getIndex() >= 0) {
75  this.variables[i] = (IntVar) lit;
76  this.coefficients[i] = 1;
77  } else { // NotBooleanVar.
78  this.variables[i] = (IntVar) lit.not();
79  this.coefficients[i] = -1;
80  this.offset -= 1;
81  }
82  }
83  }
84 
85  @Override
86  public int numElements() {
87  return variables.length;
88  }
89 
90  @Override
91  public IntVar getVariable(int index) {
92  if (index < 0 || index >= variables.length) {
93  throw new IllegalArgumentException("wrong index in LinearExpr.getVariable(): " + index);
94  }
95  return variables[index];
96  }
97 
98  @Override
99  public long getCoefficient(int index) {
100  if (index < 0 || index >= variables.length) {
101  throw new IllegalArgumentException("wrong index in LinearExpr.getCoefficient(): " + index);
102  }
103  return coefficients[index];
104  }
105 
106  @Override
107  public long getOffset() {
108  return offset;
109  }
110 }
A linear expression interface that can be parsed.
Definition: LinearExpr.java:17
A linear expression interface that can be parsed.
Definition: ScalProd.java:17
int numElements()
Returns the number of elements in the interface.
Definition: ScalProd.java:86
long getCoefficient(int index)
Returns the ith coefficient.
Definition: ScalProd.java:99
IntVar getVariable(int index)
Returns the ith variable.
Definition: ScalProd.java:91
ScalProd(Literal[] literals, long[] coefficients)
Definition: ScalProd.java:28
ScalProd(IntVar var, long coefficient, long offset)
Definition: ScalProd.java:48
long getOffset()
Returns the constant part of the expression.
Definition: ScalProd.java:107
Literal not()
Returns the Boolean negation of the current literal.
ScalProd(Literal[] literals)
Definition: ScalProd.java:66
Interface to describe a boolean variable or its negation.
Definition: Literal.java:17
ScalProd(Literal lit, long coefficient, long offset)
Definition: ScalProd.java:54
ScalProd(IntVar[] variables, long[] coefficients)
Definition: ScalProd.java:22