Java Reference

Java Reference

IntVar.java
Go to the documentation of this file.
1// Copyright 2010-2018 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 final class IntVar implements Literal, LinearExpr {
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 this.negation_ = null;
31 }
32
33 @Override
34 public String toString() {
35 return varBuilder.toString();
36 }
37
39 public String getName() {
40 return varBuilder.getName();
41 }
42
44 @Override
45 public int getIndex() {
46 return variableIndex;
47 }
48
50 public IntegerVariableProto.Builder getBuilder() {
51 return varBuilder;
52 }
53
54 // LinearExpr interface.
55 @Override
56 public int numElements() {
57 return 1;
58 }
59
60 @Override
61 public IntVar getVariable(int index) {
62 assert (index == 0);
63 return this;
64 }
65
66 @Override
67 public long getCoefficient(int index) {
68 assert (index == 0);
69 return 1;
70 }
71
72 @Override
73 public long getOffset() {
74 return 0;
75 }
76
78 @Override
79 public String getShortString() {
80 if (varBuilder.getName().isEmpty()) {
81 if (varBuilder.getDomainCount() == 2 && varBuilder.getDomain(0) == varBuilder.getDomain(1)) {
82 return String.format("%d", varBuilder.getDomain(0));
83 } else {
84 return String.format("var_%d(%s)", getIndex(), displayBounds());
85 }
86 } else {
87 return String.format("%s(%s)", getName(), displayBounds());
88 }
89 }
90
92 public String displayBounds() {
93 String out = "";
94 for (int i = 0; i < varBuilder.getDomainCount(); i += 2) {
95 if (i != 0) {
96 out += ", ";
97 }
98 if (varBuilder.getDomain(i) == varBuilder.getDomain(i + 1)) {
99 out += String.format("%d", varBuilder.getDomain(i));
100 } else {
101 out += String.format("%d..%d", varBuilder.getDomain(i), varBuilder.getDomain(i + 1));
102 }
103 }
104 return out;
105 }
106
108 @Override
109 public Literal not() {
110 if (negation_ == null) {
111 negation_ = new NotBooleanVariable(this);
112 }
113 return negation_;
114 }
115
117 public Domain getDomain() {
118 return SatHelper.variableDomain(varBuilder.build());
119 }
120
121 private final CpModelProto.Builder modelBuilder;
122 private final int variableIndex;
123 private final IntegerVariableProto.Builder varBuilder;
124 private NotBooleanVariable negation_ = null;
125}
An integer variable.
Definition: IntVar.java:21
long getCoefficient(int index)
Returns the ith coefficient.
Definition: IntVar.java:67
int getIndex()
Internal, returns the index of the variable in the underlying CpModelProto.
Definition: IntVar.java:45
int numElements()
Returns the number of elements in the interface.
Definition: IntVar.java:56
Domain getDomain()
Returns the domain of the variable.
Definition: IntVar.java:117
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:92
Literal not()
Returns the negation of a boolean variable.
Definition: IntVar.java:109
long getOffset()
Returns the constant part of the expression.
Definition: IntVar.java:73
String getShortString()
Returns a short string describing the variable.
Definition: IntVar.java:79
IntegerVariableProto.Builder getBuilder()
Returns the variable protobuf builder.
Definition: IntVar.java:50
IntVar getVariable(int index)
Returns the ith variable.
Definition: IntVar.java:61
The negation of a boolean variable.
A linear expression interface that can be parsed.
Definition: LinearExpr.java:17
Interface to describe a boolean variable or its negation.
Definition: Literal.java:17