Java Reference

Java Reference

DoubleLinearExpr.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 class DoubleLinearExpr {
18 private final IntVar[] variables;
19 private final double[] coefficients;
20 private double offset;
21
23 static DoubleLinearExpr sum(IntVar[] variables) {
24 return sumWithOffset(variables, 0.0);
25 }
26
28 static DoubleLinearExpr sumWithOffset(IntVar[] variables, double offset) {
29 return new DoubleLinearExpr(variables, offset);
30 }
31
33 static DoubleLinearExpr booleanSum(Literal[] literals) {
34 // We need the scalar product for the negative coefficient of negated Boolean variables.
35 return booleanSumWithOffset(literals, 0.0);
36 }
37
39 static DoubleLinearExpr booleanSumWithOffset(Literal[] literals, double offset) {
40 // We need the scalar product for the negative coefficient of negated Boolean variables.
41 return new DoubleLinearExpr(literals, offset);
42 }
43
45 static DoubleLinearExpr scalProd(IntVar[] variables, double[] coefficients) {
46 return scalProdWithOffset(variables, coefficients, 0.0);
47 }
48
50 static DoubleLinearExpr scalProdWithOffset(
51 IntVar[] variables, double[] coefficients, double offset) {
52 if (variables.length != coefficients.length) {
54 "DoubleLinearExpr.scalProd", "variables", "coefficients");
55 }
56 return new DoubleLinearExpr(variables, coefficients, offset);
57 }
58
60 static DoubleLinearExpr booleanScalProd(Literal[] literals, double[] coefficients) {
61 return booleanScalProdWithOffset(literals, coefficients, 0.0);
62 }
63
65 static DoubleLinearExpr booleanScalProdWithOffset(
66 Literal[] literals, double[] coefficients, double offset) {
67 if (literals.length != coefficients.length) {
69 "DoubleLinearExpr.scalProd", "literals", "coefficients");
70 }
71 return new DoubleLinearExpr(literals, coefficients, offset);
72 }
73
75 static DoubleLinearExpr term(IntVar variable, double coefficient) {
76 return new DoubleLinearExpr(variable, coefficient, 0.0);
77 }
78
80 static DoubleLinearExpr term(Literal lit, double coefficient) {
81 return new DoubleLinearExpr(lit, coefficient, 0.0);
82 }
83
85 static DoubleLinearExpr affine(IntVar variable, double coefficient, double offset) {
86 return new DoubleLinearExpr(variable, coefficient, offset);
87 }
88
90 static DoubleLinearExpr affine(Literal lit, double coefficient, double offset) {
91 return new DoubleLinearExpr(lit, coefficient, offset);
92 }
93
95 static DoubleLinearExpr constant(double value) {
96 return new DoubleLinearExpr(new IntVar[0], value);
97 }
98
100 public int numElements() {
101 return variables.length;
102 }
103
105 public IntVar getVariable(int index) {
106 if (index < 0 || index >= variables.length) {
107 throw new IllegalArgumentException("wrong index in LinearExpr.getVariable(): " + index);
108 }
109 return variables[index];
110 }
111
113 public double getCoefficient(int index) {
114 if (index < 0 || index >= variables.length) {
115 throw new IllegalArgumentException("wrong index in LinearExpr.getCoefficient(): " + index);
116 }
117 return coefficients[index];
118 }
119
121 public double getOffset() {
122 return offset;
123 }
124
125 public DoubleLinearExpr(IntVar[] variables, double[] coefficients, double offset) {
126 this.variables = variables;
127 this.coefficients = coefficients;
128 this.offset = offset;
129 }
130
131 public DoubleLinearExpr(Literal[] literals, double[] coefficients, double offset) {
132 int size = literals.length;
133 this.variables = new IntVar[size];
134 this.coefficients = new double[size];
135 this.offset = offset;
136
137 for (int i = 0; i < size; ++i) {
138 Literal lit = literals[i];
139 double coeff = coefficients[i];
140 if (lit.getIndex() >= 0) {
141 this.variables[i] = (IntVar) lit;
142 this.coefficients[i] = coeff;
143 } else {
144 this.variables[i] = (IntVar) lit.not();
145 this.coefficients[i] = -coeff;
146 this.offset -= coeff;
147 }
148 }
149 }
150
151 public DoubleLinearExpr(IntVar var, double coefficient, double offset) {
152 this.variables = new IntVar[] {var};
153 this.coefficients = new double[] {coefficient};
154 this.offset = offset;
155 }
156
157 public DoubleLinearExpr(Literal lit, double coefficient, double offset) {
158 if (lit.getIndex() >= 0) {
159 this.variables = new IntVar[] {(IntVar) lit};
160 this.coefficients = new double[] {coefficient};
161 this.offset = offset;
162 } else {
163 this.variables = new IntVar[] {(IntVar) lit.not()};
164 this.coefficients = new double[] {-coefficient};
165 this.offset = offset + coefficient;
166 }
167 }
168
169 public DoubleLinearExpr(IntVar[] vars, double offset) {
170 int size = vars.length;
171 this.variables = new IntVar[size];
172 this.coefficients = new double[size];
173 this.offset = offset;
174
175 for (int i = 0; i < size; ++i) {
176 this.variables[i] = vars[i];
177 this.coefficients[i] = 1;
178 }
179 }
180
181 public DoubleLinearExpr(Literal[] literals, double offset) {
182 int size = literals.length;
183 this.variables = new IntVar[size];
184 this.coefficients = new double[size];
185 this.offset = offset;
186
187 for (int i = 0; i < size; ++i) {
188 Literal lit = literals[i];
189 if (lit.getIndex() >= 0) {
190 this.variables[i] = (IntVar) lit;
191 this.coefficients[i] = 1;
192 } else { // NotBooleanVar.
193 this.variables[i] = (IntVar) lit.not();
194 this.coefficients[i] = -1.0;
195 this.offset -= 1.0;
196 }
197 }
198 }
199}
Exception thrown when parallel arrays have mismatched lengths.
Definition: CpModel.java:53
Main modeling class.
Definition: CpModel.java:44
A linear expression interface that can be parsed.
DoubleLinearExpr(IntVar[] variables, double[] coefficients, double offset)
int numElements()
Returns the number of elements in the interface.
double getCoefficient(int index)
Returns the ith coefficient.
DoubleLinearExpr(IntVar var, double coefficient, double offset)
DoubleLinearExpr(Literal[] literals, double[] coefficients, double offset)
DoubleLinearExpr(IntVar[] vars, double offset)
DoubleLinearExpr(Literal lit, double coefficient, double offset)
double getOffset()
Returns the constant part of the expression.
DoubleLinearExpr(Literal[] literals, double offset)
IntVar getVariable(int index)
Returns the ith variable.
Interface to describe a boolean variable or its negation.
Definition: Literal.java:17
Literal not()
Returns the Boolean negation of the current literal.