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