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 
14 package com.google.ortools.sat;
I_Max_Locnull15 
16 import static com.google.common.truth.Truth.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertEquals;
18 import static org.junit.jupiter.api.Assertions.assertNotNull;
19 
20 import com.google.ortools.Loader;
21 import com.google.ortools.util.Domain;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 
25 public final class LinearExprTest {
26   @BeforeEach
27   public void setUp() {
28     Loader.loadNativeLibraries();
29   }
30 
31   @Test
32   public void testLinearExpr_term() {
33     final CpModel model = new CpModel();
34     assertNotNull(model);
35     final Domain domain = new Domain(0, 10);
36     final IntVar y = model.newIntVarFromDomain(domain, "y");
37 
38     final LinearExpr expr = LinearExpr.term(y, 12);
39     assertNotNull(expr);
40 
41     assertEquals(1, expr.numElements());
42     assertEquals(y, expr.getVariable(0));
43     assertEquals(12, expr.getCoefficient(0));
44     assertEquals(0, expr.getOffset());
45   }
46 
47   @Test
48   public void testLinearExpr_booleanTerm() {
49     final CpModel model = new CpModel();
50     assertNotNull(model);
51     final Literal y = model.newBoolVar("y");
52 
53     final LinearExpr expr = LinearExpr.term(y.not(), 12);
54     assertNotNull(expr);
55 
56     assertThat(expr.numElements()).isEqualTo(1);
57     assertThat(expr.getVariable(0)).isEqualTo(y);
58     assertThat(expr.getCoefficient(0)).isEqualTo(-12);
59     assertThat(expr.getOffset()).isEqualTo(12);
60   }
61 
62   @Test
63   public void testLinearExpr_affine() {
64     final CpModel model = new CpModel();
65     assertNotNull(model);
66     final Domain domain = new Domain(0, 10);
67     final IntVar y = model.newIntVarFromDomain(domain, "y");
68 
69     final LinearExpr expr = LinearExpr.affine(y, 12, 5);
70     assertNotNull(expr);
71 
72     assertThat(expr.numElements()).isEqualTo(1);
73     assertThat(expr.getVariable(0)).isEqualTo(y);
74     assertThat(expr.getCoefficient(0)).isEqualTo(12);
75     assertThat(expr.getOffset()).isEqualTo(5);
76   }
77 
78   @Test
79   public void testLinearExpr_booleanAffine() {
80     final CpModel model = new CpModel();
81     assertNotNull(model);
82     final Literal y = model.newBoolVar("y");
83 
84     final LinearExpr expr = LinearExpr.affine(y.not(), 12, 5);
85     assertNotNull(expr);
86 
87     assertThat(expr.numElements()).isEqualTo(1);
88     assertThat(expr.getVariable(0)).isEqualTo(y);
89     assertThat(expr.getCoefficient(0)).isEqualTo(-12);
90     assertThat(expr.getOffset()).isEqualTo(17);
91   }
92 
93   @Test
94   public void testLinearExpr_sum() {
95     final CpModel model = new CpModel();
96     assertNotNull(model);
97     final Domain domain = new Domain(0, 10);
98     final IntVar x = model.newIntVarFromDomain(domain, "x");
99     final IntVar y = model.newIntVarFromDomain(domain, "y");
100 
101     final LinearExpr expr = LinearExpr.sum(new IntVar[] {x, y});
102     assertNotNull(expr);
103 
104     assertThat(expr.numElements()).isEqualTo(2);
105     assertThat(expr.getVariable(0)).isEqualTo(x);
106     assertThat(expr.getCoefficient(0)).isEqualTo(1);
107     assertThat(expr.getVariable(1)).isEqualTo(y);
108     assertThat(expr.getCoefficient(1)).isEqualTo(1);
109     assertThat(expr.getOffset()).isEqualTo(0);
110   }
111 
112   @Test
113   public void testLinearExpr_scalProd() {
114     final CpModel model = new CpModel();
115     assertNotNull(model);
116     final Domain domain = new Domain(0, 10);
117     final IntVar x = model.newIntVarFromDomain(domain, "x");
118     final IntVar y = model.newIntVarFromDomain(domain, "y");
119 
120     final LinearExpr expr = LinearExpr.scalProd(new IntVar[] {x, y}, new long[] {3, 5});
121     assertNotNull(expr);
122 
123     assertThat(expr.numElements()).isEqualTo(2);
124     assertThat(expr.getVariable(0)).isEqualTo(x);
125     assertThat(expr.getCoefficient(0)).isEqualTo(3);
126     assertThat(expr.getVariable(1)).isEqualTo(y);
127     assertThat(expr.getCoefficient(1)).isEqualTo(5);
128     assertThat(expr.getOffset()).isEqualTo(0);
129   }
130 
131   @Test
132   public void testLinearExpr_booleanSum() {
133     final CpModel model = new CpModel();
134     assertNotNull(model);
135     final Literal x = model.newBoolVar("x");
136     final Literal y = model.newBoolVar("y");
137 
138     final LinearExpr expr = LinearExpr.booleanSum(new Literal[] {x, y.not()});
139     assertNotNull(expr);
140 
141     assertThat(expr.numElements()).isEqualTo(2);
142     assertThat(expr.getVariable(0)).isEqualTo(x);
143     assertThat(expr.getCoefficient(0)).isEqualTo(1);
144     assertThat(expr.getVariable(1)).isEqualTo(y);
145     assertThat(expr.getCoefficient(1)).isEqualTo(-1);
146     assertThat(expr.getOffset()).isEqualTo(-1);
147   }
148 
149   @Test
150   public void testLinearExpr_booleanScalProd() {
151     final CpModel model = new CpModel();
152     assertNotNull(model);
153     final Literal x = model.newBoolVar("x");
154     final Literal y = model.newBoolVar("y");
155 
156     final LinearExpr expr =
157         LinearExpr.booleanScalProd(new Literal[] {x, y.not()}, new long[] {3, 5});
158     assertNotNull(expr);
159 
160     assertThat(expr.numElements()).isEqualTo(2);
161     assertThat(expr.getVariable(0)).isEqualTo(x);
162     assertThat(expr.getCoefficient(0)).isEqualTo(3);
163     assertThat(expr.getVariable(1)).isEqualTo(y);
164     assertThat(expr.getCoefficient(1)).isEqualTo(-5);
165     assertThat(expr.getOffset()).isEqualTo(-5);
166   }
167 }
168