14package com.google.ortools.sat;
17public class DoubleLinearExpr {
18 private final int[] variableIndices;
19 private final double[] coefficients;
20 private double offset;
24 return sumWithOffset(variables, 0.0);
30 return sumWithOffset(literals, 0.0);
45 static DoubleLinearExpr weightedSum(IntVar[] variables,
double[] coefficients) {
46 return weightedSumWithOffset(variables, coefficients, 0.0);
50 static DoubleLinearExpr weightedSum(Literal[] literals,
double[] coefficients) {
51 return weightedSumWithOffset(literals, coefficients, 0.0);
56 IntVar[] variables,
double[] coefficients,
double offset) {
57 if (variables.length != coefficients.length) {
58 throw new CpModel.MismatchedArrayLengths(
59 "DoubleLinearExpr.weightedSum",
"variables",
"coefficients");
66 Literal[] literals,
double[] coefficients,
double offset) {
67 if (literals.length != coefficients.length) {
68 throw new CpModel.MismatchedArrayLengths(
69 "DoubleLinearExpr.weightedSum",
"literals",
"coefficients");
85 static DoubleLinearExpr affine(IntVar variable,
double coefficient,
double offset) {
90 static DoubleLinearExpr affine(Literal lit,
double coefficient,
double offset) {
101 return variableIndices.length;
106 if (index < 0 || index >= variableIndices.length) {
107 throw new IllegalArgumentException(
"wrong index in LinearExpr.getVariable(): " + index);
109 return variableIndices[index];
114 if (index < 0 || index >= variableIndices.length) {
115 throw new IllegalArgumentException(
"wrong index in LinearExpr.getCoefficient(): " + index);
117 return coefficients[index];
126 this.variableIndices =
new int[variables.length];
127 for (
int i = 0; i < variables.length; ++i) {
128 this.variableIndices[i] = variables[i].
getIndex();
130 this.coefficients = coefficients;
131 this.offset = offset;
135 int size = literals.length;
136 this.variableIndices =
new int[size];
137 this.coefficients =
new double[size];
138 this.offset = offset;
140 for (
int i = 0; i < size; ++i) {
142 double coeff = coefficients[i];
144 this.variableIndices[i] = lit.
getIndex();
145 this.coefficients[i] = coeff;
148 this.coefficients[i] = -coeff;
149 this.offset -= coeff;
155 this.variableIndices =
new int[] {var.
getIndex()};
156 this.coefficients =
new double[] {coefficient};
157 this.offset = offset;
162 this.variableIndices =
new int[] {lit.
getIndex()};
163 this.coefficients =
new double[] {coefficient};
164 this.offset = offset;
166 this.variableIndices =
new int[] {lit.
not().
getIndex()};
167 this.coefficients =
new double[] {-coefficient};
168 this.offset = offset + coefficient;
173 int size = vars.length;
174 this.variableIndices =
new int[size];
175 this.coefficients =
new double[size];
176 this.offset = offset;
178 for (
int i = 0; i < size; ++i) {
179 this.variableIndices[i] = vars[i].
getIndex();
180 this.coefficients[i] = 1;
185 int size = literals.length;
186 this.variableIndices =
new int[size];
187 this.coefficients =
new double[size];
188 this.offset = offset;
190 for (
int i = 0; i < size; ++i) {
193 this.variableIndices[i] = lit.
getIndex();
194 this.coefficients[i] = 1;
197 this.coefficients[i] = -1.0;