Java Reference

Java Reference

ScalProd.java
Go to the documentation of this file.
1// Copyright 2010-2018 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
17public 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(Literal[] literals) {
49 int size = literals.length;
50 this.variables = new IntVar[size];
51 this.coefficients = new long[size];
52 this.offset = 0;
53
54 for (int i = 0; i < size; ++i) {
55 Literal lit = literals[i];
56 if (lit.getIndex() >= 0) {
57 this.variables[i] = (IntVar) lit;
58 this.coefficients[i] = 1;
59 } else { // NotBooleanVar.
60 this.variables[i] = (IntVar) lit.not();
61 this.coefficients[i] = -1;
62 this.offset -= 1;
63 }
64 }
65 }
66
67 @Override
68 public int numElements() {
69 return variables.length;
70 }
71
72 @Override
73 public IntVar getVariable(int index) {
74 if (index < 0 || index >= variables.length) {
75 throw new IllegalArgumentException("wrong index in LinearExpr.getVariable(): " + index);
76 }
77 return variables[index];
78 }
79
80 @Override
81 public long getCoefficient(int index) {
82 if (index < 0 || index >= variables.length) {
83 throw new IllegalArgumentException("wrong index in LinearExpr.getCoefficient(): " + index);
84 }
85 return coefficients[index];
86 }
87
88 @Override
89 public long getOffset() {
90 return offset;
91 }
92}
An integer variable.
Definition: IntVar.java:21
A linear expression interface that can be parsed.
Definition: ScalProd.java:17
long getCoefficient(int index)
Returns the ith coefficient.
Definition: ScalProd.java:81
ScalProd(Literal[] literals)
Definition: ScalProd.java:48
ScalProd(Literal[] literals, long[] coefficients)
Definition: ScalProd.java:28
int numElements()
Returns the number of elements in the interface.
Definition: ScalProd.java:68
long getOffset()
Returns the constant part of the expression.
Definition: ScalProd.java:89
ScalProd(IntVar[] variables, long[] coefficients)
Definition: ScalProd.java:22
IntVar getVariable(int index)
Returns the ith variable.
Definition: ScalProd.java:73
A linear expression interface that can be parsed.
Definition: LinearExpr.java:17
Interface to describe a boolean variable or its negation.
Definition: Literal.java:17
Literal not()
Returns the Boolean negation of the current literal.