2025-01-10 11:33:35 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2021-08-31 11:58:46 +02:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
package com.google.ortools.sat;
|
|
|
|
|
|
|
|
|
|
import static com.google.common.truth.Truth.assertThat;
|
2026-01-28 17:42:53 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
2021-08-31 11:58:46 +02:00
|
|
|
|
|
|
|
|
import com.google.ortools.Loader;
|
|
|
|
|
import com.google.ortools.util.Domain;
|
2026-01-28 17:42:53 +01:00
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
2021-08-31 11:58:46 +02:00
|
|
|
|
|
|
|
|
public final class LinearExprTest {
|
2026-01-28 17:42:53 +01:00
|
|
|
@BeforeEach
|
2021-08-31 11:58:46 +02:00
|
|
|
public void setUp() {
|
|
|
|
|
Loader.loadNativeLibraries();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 19:29:15 +01:00
|
|
|
@Test
|
2023-01-24 15:33:55 +01:00
|
|
|
public void testLinearExprAdd() {
|
2022-01-03 19:29:15 +01:00
|
|
|
final CpModel model = new CpModel();
|
|
|
|
|
assertNotNull(model);
|
|
|
|
|
final Domain domain = new Domain(0, 10);
|
|
|
|
|
final IntVar y = model.newIntVarFromDomain(domain, "y");
|
|
|
|
|
|
|
|
|
|
final LinearExpr expr = LinearExpr.newBuilder().add(y).build();
|
|
|
|
|
assertNotNull(expr);
|
|
|
|
|
|
|
|
|
|
assertEquals(1, expr.numElements());
|
2022-01-04 16:39:27 +01:00
|
|
|
assertEquals(y.getIndex(), expr.getVariableIndex(0));
|
2022-01-03 19:29:15 +01:00
|
|
|
assertEquals(1, expr.getCoefficient(0));
|
|
|
|
|
assertEquals(0, expr.getOffset());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2023-01-24 15:33:55 +01:00
|
|
|
public void testLinearExprAddLiteral() {
|
2022-01-03 19:29:15 +01:00
|
|
|
final CpModel model = new CpModel();
|
|
|
|
|
assertNotNull(model);
|
|
|
|
|
final BoolVar y = model.newBoolVar("y");
|
|
|
|
|
|
|
|
|
|
final LinearExpr expr = LinearExpr.newBuilder().add(y).build();
|
|
|
|
|
assertNotNull(expr);
|
|
|
|
|
|
|
|
|
|
assertEquals(1, expr.numElements());
|
2022-01-04 16:39:27 +01:00
|
|
|
assertEquals(y.getIndex(), expr.getVariableIndex(0));
|
2022-01-03 19:29:15 +01:00
|
|
|
assertEquals(1, expr.getCoefficient(0));
|
|
|
|
|
assertEquals(0, expr.getOffset());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2023-01-24 15:33:55 +01:00
|
|
|
public void testLinearExprAddNegatedLiteral() {
|
2022-01-03 19:29:15 +01:00
|
|
|
final CpModel model = new CpModel();
|
|
|
|
|
assertNotNull(model);
|
|
|
|
|
final BoolVar y = model.newBoolVar("y");
|
|
|
|
|
|
|
|
|
|
final LinearExpr expr = LinearExpr.newBuilder().add(y.not()).build();
|
|
|
|
|
assertNotNull(expr);
|
|
|
|
|
|
|
|
|
|
assertEquals(1, expr.numElements());
|
2022-01-04 16:39:27 +01:00
|
|
|
assertEquals(y.getIndex(), expr.getVariableIndex(0));
|
2022-01-03 19:29:15 +01:00
|
|
|
assertEquals(-1, expr.getCoefficient(0));
|
|
|
|
|
assertEquals(1, expr.getOffset());
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 11:58:46 +02:00
|
|
|
@Test
|
2023-01-24 15:33:55 +01:00
|
|
|
public void testLinearExprTerm() {
|
2021-08-31 11:58:46 +02:00
|
|
|
final CpModel model = new CpModel();
|
|
|
|
|
assertNotNull(model);
|
|
|
|
|
final Domain domain = new Domain(0, 10);
|
|
|
|
|
final IntVar y = model.newIntVarFromDomain(domain, "y");
|
|
|
|
|
|
2022-01-03 14:22:37 +01:00
|
|
|
final LinearExpr expr = LinearExpr.newBuilder().addTerm(y, 12).build();
|
2021-08-31 11:58:46 +02:00
|
|
|
assertNotNull(expr);
|
|
|
|
|
|
|
|
|
|
assertEquals(1, expr.numElements());
|
2022-01-04 16:39:27 +01:00
|
|
|
assertEquals(y.getIndex(), expr.getVariableIndex(0));
|
2021-08-31 11:58:46 +02:00
|
|
|
assertEquals(12, expr.getCoefficient(0));
|
|
|
|
|
assertEquals(0, expr.getOffset());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2023-01-24 15:33:55 +01:00
|
|
|
public void testLinearExprBooleanTerm() {
|
2021-08-31 11:58:46 +02:00
|
|
|
final CpModel model = new CpModel();
|
|
|
|
|
assertNotNull(model);
|
|
|
|
|
final Literal y = model.newBoolVar("y");
|
|
|
|
|
|
2022-01-03 14:22:37 +01:00
|
|
|
final LinearExpr expr = LinearExpr.newBuilder().addTerm(y.not(), 12).build();
|
2021-08-31 11:58:46 +02:00
|
|
|
assertNotNull(expr);
|
|
|
|
|
|
|
|
|
|
assertThat(expr.numElements()).isEqualTo(1);
|
2022-01-04 16:39:27 +01:00
|
|
|
assertThat(expr.getVariableIndex(0)).isEqualTo(y.getIndex());
|
2021-08-31 11:58:46 +02:00
|
|
|
assertThat(expr.getCoefficient(0)).isEqualTo(-12);
|
|
|
|
|
assertThat(expr.getOffset()).isEqualTo(12);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2023-01-24 15:33:55 +01:00
|
|
|
public void testLinearExprAffine() {
|
2021-08-31 11:58:46 +02:00
|
|
|
final CpModel model = new CpModel();
|
|
|
|
|
assertNotNull(model);
|
|
|
|
|
final Domain domain = new Domain(0, 10);
|
|
|
|
|
final IntVar y = model.newIntVarFromDomain(domain, "y");
|
|
|
|
|
|
2022-01-03 14:22:37 +01:00
|
|
|
final LinearExpr expr = LinearExpr.newBuilder().addTerm(y, 12).add(5).build();
|
2021-08-31 11:58:46 +02:00
|
|
|
assertNotNull(expr);
|
|
|
|
|
|
|
|
|
|
assertThat(expr.numElements()).isEqualTo(1);
|
2022-01-04 16:39:27 +01:00
|
|
|
assertThat(expr.getVariableIndex(0)).isEqualTo(y.getIndex());
|
2021-08-31 11:58:46 +02:00
|
|
|
assertThat(expr.getCoefficient(0)).isEqualTo(12);
|
|
|
|
|
assertThat(expr.getOffset()).isEqualTo(5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2023-01-24 15:33:55 +01:00
|
|
|
public void testLinearExprBooleanAffine() {
|
2021-08-31 11:58:46 +02:00
|
|
|
final CpModel model = new CpModel();
|
|
|
|
|
assertNotNull(model);
|
|
|
|
|
final Literal y = model.newBoolVar("y");
|
|
|
|
|
|
2022-01-03 14:22:37 +01:00
|
|
|
final LinearExpr expr = LinearExpr.newBuilder().addTerm(y.not(), 12).add(5).build();
|
2021-08-31 11:58:46 +02:00
|
|
|
assertNotNull(expr);
|
|
|
|
|
|
|
|
|
|
assertThat(expr.numElements()).isEqualTo(1);
|
2022-01-04 16:39:27 +01:00
|
|
|
assertThat(expr.getVariableIndex(0)).isEqualTo(y.getIndex());
|
2021-08-31 11:58:46 +02:00
|
|
|
assertThat(expr.getCoefficient(0)).isEqualTo(-12);
|
|
|
|
|
assertThat(expr.getOffset()).isEqualTo(17);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2023-01-24 15:33:55 +01:00
|
|
|
public void testLinearExprSum() {
|
2021-08-31 11:58:46 +02:00
|
|
|
final CpModel model = new CpModel();
|
|
|
|
|
assertNotNull(model);
|
|
|
|
|
final Domain domain = new Domain(0, 10);
|
|
|
|
|
final IntVar x = model.newIntVarFromDomain(domain, "x");
|
|
|
|
|
final IntVar y = model.newIntVarFromDomain(domain, "y");
|
|
|
|
|
|
2022-01-03 14:22:37 +01:00
|
|
|
final LinearExpr expr = LinearExpr.newBuilder().add(x).add(y).build();
|
2021-08-31 11:58:46 +02:00
|
|
|
assertNotNull(expr);
|
|
|
|
|
|
|
|
|
|
assertThat(expr.numElements()).isEqualTo(2);
|
2022-01-04 16:39:27 +01:00
|
|
|
assertThat(expr.getVariableIndex(0)).isEqualTo(x.getIndex());
|
2021-08-31 11:58:46 +02:00
|
|
|
assertThat(expr.getCoefficient(0)).isEqualTo(1);
|
2022-01-04 16:39:27 +01:00
|
|
|
assertThat(expr.getVariableIndex(1)).isEqualTo(y.getIndex());
|
2021-08-31 11:58:46 +02:00
|
|
|
assertThat(expr.getCoefficient(1)).isEqualTo(1);
|
|
|
|
|
assertThat(expr.getOffset()).isEqualTo(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2023-01-24 15:33:55 +01:00
|
|
|
public void testLinearExprWeightedSum() {
|
2021-08-31 11:58:46 +02:00
|
|
|
final CpModel model = new CpModel();
|
|
|
|
|
assertNotNull(model);
|
|
|
|
|
final Domain domain = new Domain(0, 10);
|
|
|
|
|
final IntVar x = model.newIntVarFromDomain(domain, "x");
|
|
|
|
|
final IntVar y = model.newIntVarFromDomain(domain, "y");
|
|
|
|
|
|
2022-01-03 14:22:37 +01:00
|
|
|
final LinearExpr expr = LinearExpr.newBuilder().addTerm(x, 3).addTerm(y, 5).build();
|
2021-08-31 11:58:46 +02:00
|
|
|
assertNotNull(expr);
|
|
|
|
|
|
|
|
|
|
assertThat(expr.numElements()).isEqualTo(2);
|
2022-01-04 16:39:27 +01:00
|
|
|
assertThat(expr.getVariableIndex(0)).isEqualTo(x.getIndex());
|
2021-08-31 11:58:46 +02:00
|
|
|
assertThat(expr.getCoefficient(0)).isEqualTo(3);
|
2022-01-04 16:39:27 +01:00
|
|
|
assertThat(expr.getVariableIndex(1)).isEqualTo(y.getIndex());
|
2021-08-31 11:58:46 +02:00
|
|
|
assertThat(expr.getCoefficient(1)).isEqualTo(5);
|
|
|
|
|
assertThat(expr.getOffset()).isEqualTo(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2023-01-24 15:33:55 +01:00
|
|
|
public void testLinearExprBooleanSum() {
|
2021-08-31 11:58:46 +02:00
|
|
|
final CpModel model = new CpModel();
|
|
|
|
|
assertNotNull(model);
|
|
|
|
|
final Literal x = model.newBoolVar("x");
|
|
|
|
|
final Literal y = model.newBoolVar("y");
|
|
|
|
|
|
2022-01-03 14:22:37 +01:00
|
|
|
final LinearExpr expr = LinearExpr.newBuilder().add(x).add(y.not()).build();
|
2021-08-31 11:58:46 +02:00
|
|
|
assertNotNull(expr);
|
|
|
|
|
|
|
|
|
|
assertThat(expr.numElements()).isEqualTo(2);
|
2022-01-04 16:39:27 +01:00
|
|
|
assertThat(expr.getVariableIndex(0)).isEqualTo(x.getIndex());
|
2021-08-31 11:58:46 +02:00
|
|
|
assertThat(expr.getCoefficient(0)).isEqualTo(1);
|
2022-01-04 16:39:27 +01:00
|
|
|
assertThat(expr.getVariableIndex(1)).isEqualTo(y.getIndex());
|
2021-08-31 11:58:46 +02:00
|
|
|
assertThat(expr.getCoefficient(1)).isEqualTo(-1);
|
2022-01-03 14:22:37 +01:00
|
|
|
assertThat(expr.getOffset()).isEqualTo(1);
|
2021-08-31 11:58:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2023-01-24 15:33:55 +01:00
|
|
|
public void testLinearExprBooleanWeightedSum() {
|
2021-08-31 11:58:46 +02:00
|
|
|
final CpModel model = new CpModel();
|
|
|
|
|
assertNotNull(model);
|
|
|
|
|
final Literal x = model.newBoolVar("x");
|
|
|
|
|
final Literal y = model.newBoolVar("y");
|
|
|
|
|
|
2022-01-03 14:22:37 +01:00
|
|
|
final LinearExpr expr = LinearExpr.newBuilder().addTerm(x, 3).addTerm(y.not(), 5).build();
|
2021-08-31 11:58:46 +02:00
|
|
|
assertNotNull(expr);
|
|
|
|
|
|
|
|
|
|
assertThat(expr.numElements()).isEqualTo(2);
|
2022-01-04 16:39:27 +01:00
|
|
|
assertThat(expr.getVariableIndex(0)).isEqualTo(x.getIndex());
|
2021-08-31 11:58:46 +02:00
|
|
|
assertThat(expr.getCoefficient(0)).isEqualTo(3);
|
2022-01-04 16:39:27 +01:00
|
|
|
assertThat(expr.getVariableIndex(1)).isEqualTo(y.getIndex());
|
2021-08-31 11:58:46 +02:00
|
|
|
assertThat(expr.getCoefficient(1)).isEqualTo(-5);
|
2022-01-03 14:22:37 +01:00
|
|
|
assertThat(expr.getOffset()).isEqualTo(5);
|
2021-08-31 11:58:46 +02:00
|
|
|
}
|
|
|
|
|
}
|