Java Reference

Java Reference

com/google/ortools/sat/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 
14 package com.google.ortools.sat;
15 
19 
21 public 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 
73  @Override
74  public String getShortString() {
75  if (varBuilder.getName().isEmpty()) {
76  if (varBuilder.getDomainCount() == 2 && varBuilder.getDomain(0) == varBuilder.getDomain(1)) {
77  return String.format("%d", varBuilder.getDomain(0));
78  } else {
79  return String.format("var_%d(%s)", getIndex(), displayBounds());
80  }
81  } else {
82  return String.format("%s(%s)", getName(), displayBounds());
83  }
84  }
85 
87  public String displayBounds() {
88  String out = "";
89  for (int i = 0; i < varBuilder.getDomainCount(); i += 2) {
90  if (i != 0) {
91  out += ", ";
92  }
93  if (varBuilder.getDomain(i) == varBuilder.getDomain(i + 1)) {
94  out += String.format("%d", varBuilder.getDomain(i));
95  } else {
96  out += String.format("%d..%d", varBuilder.getDomain(i), varBuilder.getDomain(i + 1));
97  }
98  }
99  return out;
100  }
101 
103  @Override
104  public Literal not() {
105  if (negation_ == null) {
106  negation_ = new NotBooleanVariable(this);
107  }
108  return negation_;
109  }
110 
111  private final CpModelProto.Builder modelBuilder;
112  private final int variableIndex;
113  private final IntegerVariableProto.Builder varBuilder;
114  private NotBooleanVariable negation_;
115 }
long getCoefficient(int index)
Returns the ith coefficient.
long[] flattenedIntervals()
This method returns the flattened list of interval bounds of the domain.
Definition: Domain.java:111
Literal not()
Returns the negation of a boolean variable.
IntegerVariableProto.Builder getBuilder()
Returns the variable protobuf builder.
String displayBounds()
Returns the domain as a string without the enclosing [].
Interface to describe a boolean variable or its negation.
Definition: Literal.java:17
We call domain any subset of Int64 = [kint64min, kint64max].
Definition: Domain.java:21
int getVariablesCount()
String toString()
int getIndex()
Internal, returns the index of the variable in the underlying CpModelProto.
An integer variable.
String getName()
Returns the name of the variable given upon creation.
IntVar getVariable(int index)
Returns the ith variable.
int numElements()
Returns the number of elements in the interface.
Definition: Domain.java:9
A linear expression interface that can be parsed.
Definition: LinearExpr.java:17
String getShortString()
Returns a short string describing the variable.