Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
Variable.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.modelbuilder;
15
17public class Variable implements LinearArgument {
18 Variable(ModelBuilderHelper helper, double lb, double ub, boolean isIntegral, String name) {
19 this.helper = helper;
20 this.index = helper.addVar();
21 this.helper.setVarName(index, name);
22 this.helper.setVarIntegrality(index, isIntegral);
23 this.helper.setVarLowerBound(index, lb);
24 this.helper.setVarUpperBound(index, ub);
25 }
26
27 Variable(ModelBuilderHelper helper, int index) {
28 this.helper = helper;
29 this.index = index;
30 }
31
33 public int getIndex() {
34 return index;
35 }
36
37 // LinearArgument interface
38 @Override
39 public LinearExpr build() {
40 return new AffineExpression(index, 1.0, 0.0);
41 }
42
44 public double getLowerBound() {
46 }
47
49 public void setLowerBound(double lowerBound) {
50 helper.setVarLowerBound(index, lowerBound);
51 }
52
54 public double getUpperBound() {
56 }
57
59 public void setUpperBound(double upperBound) {
60 helper.setVarUpperBound(index, upperBound);
61 }
62
64 public boolean getIntegrality() {
66 }
67
69 public void setIntegrality(boolean isIntegral) {
70 helper.setVarIntegrality(index, isIntegral);
71 }
72
77
79 public void setObjectiveCoefficient(double objectiveCoefficient) {
80 helper.setVarObjectiveCoefficient(index, objectiveCoefficient);
81 }
82
84 public String getName() {
85 return helper.getVarName(index);
86 }
87
88 public void setName(String name) {
89 helper.setVarName(index, name);
90 }
91
92 @Override
93 public String toString() {
94 if (getName().isEmpty()) {
95 if (getLowerBound() == getUpperBound()) {
96 return String.format("%f", getLowerBound());
97 } else {
98 return String.format("var_%d(%f, %f)", getIndex(), getLowerBound(), getUpperBound());
99 }
100 } else {
101 return String.format("%s(%f, %f)", getName(), getLowerBound(), getUpperBound());
102 }
103 }
104
105 protected final ModelBuilderHelper helper;
106 protected final int index;
107}
void setVarObjectiveCoefficient(int var_index, double coeff)
void setVarIntegrality(int var_index, boolean is_integer)
LinearExpr build()
LinearArgument interface.
Definition Variable.java:39
void setObjectiveCoefficient(double objectiveCoefficient)
Definition Variable.java:79
void setIntegrality(boolean isIntegral)
Definition Variable.java:69
void setLowerBound(double lowerBound)
Definition Variable.java:49
void setUpperBound(double upperBound)
Definition Variable.java:59