Java Reference

Java Reference

Variable.java
Go to the documentation of this file.
1 // Copyright 2010-2022 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.modelbuilder;
15 
17 public 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() {
45  return helper.getVarLowerBound(index);
46  }
47 
49  public void setLowerBound(double lowerBound) {
50  helper.setVarLowerBound(index, lowerBound);
51  }
52 
54  public double getUpperBound() {
55  return helper.getVarUpperBound(index);
56  }
57 
59  public void setUpperBound(double upperBound) {
60  helper.setVarUpperBound(index, upperBound);
61  }
62 
64  public boolean getIntegrality() {
65  return helper.getVarIntegrality(index);
66  }
67 
69  public void setIntegrality(boolean isIntegral) {
70  helper.setVarIntegrality(index, isIntegral);
71  }
72 
74  public double getObjectiveCoefficient() {
75  return helper.getVarObjectiveCoefficient(index);
76  }
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 }
final ModelBuilderHelper helper
Definition: Variable.java:105
double getObjectiveCoefficient()
Returns the objective coefficient of the variable.
Definition: Variable.java:74
int getIndex()
Returns the index of the variable in the underlying ModelBuilderHelper.
Definition: Variable.java:33
double getLowerBound()
Returns the lower bound of the variable.
Definition: Variable.java:44
LinearExpr build()
Builds a linear expression.
Definition: Variable.java:39
double getUpperBound()
Returns the upper bound of the variable.
Definition: Variable.java:54
void setIntegrality(boolean isIntegral)
Sets the integrality of the variable.
Definition: Variable.java:69
void setUpperBound(double upperBound)
Sets the upper bound of the variable.
Definition: Variable.java:59
String getName()
Returns the name of the variable given upon creation.
Definition: Variable.java:84
void setObjectiveCoefficient(double objectiveCoefficient)
Sets the objective coefficient of the variable in the objective.
Definition: Variable.java:79
boolean getIntegrality()
Returns whether the variable is integral.
Definition: Variable.java:64
void setLowerBound(double lowerBound)
Sets the lower bound of the variable.
Definition: Variable.java:49
A object that can build a LinearExpr object.
A linear expression (sum (ai * xi) + b).