Java Reference

Java Reference

IntVar.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
16import com.google.ortools.sat.CpModelProto;
17import com.google.ortools.sat.IntegerVariableProto;
18import com.google.ortools.util.Domain;
19
21public class IntVar implements LinearArgument {
22 IntVar(CpModelProto.Builder builder, Domain domain, String name) {
23 this.modelBuilder = builder;
24 this.variableIndex = modelBuilder.getVariablesCount();
25 this.varBuilder = modelBuilder.addVariablesBuilder();
26 this.varBuilder.setName(name);
27 for (long b : domain.flattenedIntervals()) {
28 this.varBuilder.addDomain(b);
29 }
30 }
31
32 IntVar(CpModelProto.Builder builder, int index) {
33 this.modelBuilder = builder;
34 this.variableIndex = index;
35 this.varBuilder = modelBuilder.getVariablesBuilder(index);
36 }
37
39 public String getName() {
40 return varBuilder.getName();
41 }
42
44 public int getIndex() {
45 return variableIndex;
46 }
47
49 public IntegerVariableProto.Builder getBuilder() {
50 return varBuilder;
51 }
52
53 // LinearArgument interface
54 @Override
55 public LinearExpr build() {
56 return new AffineExpression(variableIndex, 1, 0);
57 }
58
60 public String displayBounds() {
61 String out = "";
62 for (int i = 0; i < varBuilder.getDomainCount(); i += 2) {
63 if (i != 0) {
64 out += ", ";
65 }
66 if (varBuilder.getDomain(i) == varBuilder.getDomain(i + 1)) {
67 out += String.format("%d", varBuilder.getDomain(i));
68 } else {
69 out += String.format("%d..%d", varBuilder.getDomain(i), varBuilder.getDomain(i + 1));
70 }
71 }
72 return out;
73 }
74
76 public Domain getDomain() {
77 return CpSatHelper.variableDomain(varBuilder.build());
78 }
79
80 @Override
81 public String toString() {
82 if (varBuilder.getName().isEmpty()) {
83 if (varBuilder.getDomainCount() == 2 && varBuilder.getDomain(0) == varBuilder.getDomain(1)) {
84 return String.format("%d", varBuilder.getDomain(0));
85 } else {
86 return String.format("var_%d(%s)", getIndex(), displayBounds());
87 }
88 } else {
89 return String.format("%s(%s)", getName(), displayBounds());
90 }
91 }
92
93 protected final CpModelProto.Builder modelBuilder;
94 protected final int variableIndex;
95 protected final IntegerVariableProto.Builder varBuilder;
96}
A specialized linear expression: a * x + b.
An integer variable.
Definition: IntVar.java:21
int getIndex()
Returns the index of the variable in the underlying CpModelProto.
Definition: IntVar.java:44
final IntegerVariableProto.Builder varBuilder
Definition: IntVar.java:95
LinearExpr build()
Builds a linear expression.
Definition: IntVar.java:55
Domain getDomain()
Returns the domain of the variable.
Definition: IntVar.java:76
String getName()
Returns the name of the variable given upon creation.
Definition: IntVar.java:39
String displayBounds()
Returns the domain as a string without the enclosing [].
Definition: IntVar.java:60
final CpModelProto.Builder modelBuilder
Definition: IntVar.java:93
IntegerVariableProto.Builder getBuilder()
Returns the variable protobuf builder.
Definition: IntVar.java:49
A object that can build a LinearExpr object.
A linear expression (sum (ai * xi) + b).
Definition: LinearExpr.java:19