Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
IntVar.java
Go to the documentation of this file.
1// Copyright 2010-2024 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() {
78 }
79
80 @Override
81 public String toString() {
82 if (varBuilder.getName().isEmpty()) {
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}
static Domain variableDomain(com.google.ortools.sat.IntegerVariableProto variable_proto)
final CpModelProto.Builder modelBuilder
Definition IntVar.java:93
LinearExpr build()
LinearArgument interface.
Definition IntVar.java:55
final IntegerVariableProto.Builder varBuilder
Definition IntVar.java:95
IntegerVariableProto.Builder getBuilder()
Definition IntVar.java:49