Google OR-Tools v9.9
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
DoubleLinearExpr.java
Go to the documentation of this file.
1// Copyright 2010-2024 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 int[] variableIndices;
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 sum(Literal[] literals) {
29 // We need the scalar product for the negative coefficient of negated Boolean variables.
30 return sumWithOffset(literals, 0.0);
31 }
32
34 static DoubleLinearExpr sumWithOffset(IntVar[] variables, double offset) {
35 return new DoubleLinearExpr(variables, offset);
36 }
37
39 static DoubleLinearExpr sumWithOffset(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 weightedSum(IntVar[] variables, double[] coefficients) {
46 return weightedSumWithOffset(variables, coefficients, 0.0);
47 }
48
50 static DoubleLinearExpr weightedSum(Literal[] literals, double[] coefficients) {
51 return weightedSumWithOffset(literals, coefficients, 0.0);
52 }
53
55 static DoubleLinearExpr weightedSumWithOffset(
56 IntVar[] variables, double[] coefficients, double offset) {
57 if (variables.length != coefficients.length) {
59 "DoubleLinearExpr.weightedSum", "variables", "coefficients");
60 }
61 return new DoubleLinearExpr(variables, coefficients, offset);
62 }
63
65 static DoubleLinearExpr weightedSumWithOffset(
66 Literal[] literals, double[] coefficients, double offset) {
67 if (literals.length != coefficients.length) {
69 "DoubleLinearExpr.weightedSum", "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 variableIndices.length;
102 }
103
105 public int getVariableIndex(int index) {
106 if (index < 0 || index >= variableIndices.length) {
107 throw new IllegalArgumentException("wrong index in LinearExpr.getVariable(): " + index);
108 }
109 return variableIndices[index];
110 }
111
113 public double getCoefficient(int index) {
114 if (index < 0 || index >= variableIndices.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.variableIndices = new int[variables.length];
127 for (int i = 0; i < variables.length; ++i) {
128 this.variableIndices[i] = variables[i].getIndex();
129 }
130 this.coefficients = coefficients;
131 this.offset = offset;
132 }
133
134 public DoubleLinearExpr(Literal[] literals, double[] coefficients, double offset) {
135 int size = literals.length;
136 this.variableIndices = new int[size];
137 this.coefficients = new double[size];
138 this.offset = offset;
139
140 for (int i = 0; i < size; ++i) {
141 Literal lit = literals[i];
142 double coeff = coefficients[i];
143 if (lit.getIndex() >= 0) {
144 this.variableIndices[i] = lit.getIndex();
145 this.coefficients[i] = coeff;
146 } else {
147 this.variableIndices[i] = lit.not().getIndex();
148 this.coefficients[i] = -coeff;
149 this.offset -= coeff;
150 }
151 }
152 }
153
154 public DoubleLinearExpr(IntVar var, double coefficient, double offset) {
155 this.variableIndices = new int[] {var.getIndex()};
156 this.coefficients = new double[] {coefficient};
157 this.offset = offset;
158 }
159
160 public DoubleLinearExpr(Literal lit, double coefficient, double offset) {
161 if (lit.getIndex() >= 0) {
162 this.variableIndices = new int[] {lit.getIndex()};
163 this.coefficients = new double[] {coefficient};
164 this.offset = offset;
165 } else {
166 this.variableIndices = new int[] {lit.not().getIndex()};
167 this.coefficients = new double[] {-coefficient};
168 this.offset = offset + coefficient;
169 }
170 }
171
172 public DoubleLinearExpr(IntVar[] vars, double offset) {
173 int size = vars.length;
174 this.variableIndices = new int[size];
175 this.coefficients = new double[size];
176 this.offset = offset;
177
178 for (int i = 0; i < size; ++i) {
179 this.variableIndices[i] = vars[i].getIndex();
180 this.coefficients[i] = 1;
181 }
182 }
183
184 public DoubleLinearExpr(Literal[] literals, double offset) {
185 int size = literals.length;
186 this.variableIndices = new int[size];
187 this.coefficients = new double[size];
188 this.offset = offset;
189
190 for (int i = 0; i < size; ++i) {
191 Literal lit = literals[i];
192 if (lit.getIndex() >= 0) {
193 this.variableIndices[i] = lit.getIndex();
194 this.coefficients[i] = 1;
195 } else { // NotBoolVar.
196 this.variableIndices[i] = lit.not().getIndex();
197 this.coefficients[i] = -1.0;
198 this.offset -= 1.0;
199 }
200 }
201 }
202}
DoubleLinearExpr(IntVar[] vars, double offset)
DoubleLinearExpr(Literal lit, double coefficient, double offset)
DoubleLinearExpr(IntVar[] variables, double[] coefficients, double offset)
DoubleLinearExpr(Literal[] literals, double[] coefficients, double offset)
DoubleLinearExpr(Literal[] literals, double offset)
DoubleLinearExpr(IntVar var, double coefficient, double offset)