Class: LpSolver::Variable
- Inherits:
-
Object
- Object
- LpSolver::Variable
- Defined in:
- lib/lpsolver/variable.rb
Overview
Variable * Variable produces a QuadraticExpression (for QP). Variable * Scalar produces a LinearExpression (for LP).
Represents a decision variable in an LP/MIP/QP model.
Variables are the building blocks of optimization models. Each variable represents a quantity to be determined by the solver (e.g., production levels, investment amounts, resource allocations).
Variables support arithmetic operators for building expressions and comparison operators for creating constraints. The operator overloading enables a natural, mathematical DSL:
x = model.add_variable(:x, lb: 0)
model.add_constraint(:budget, (x * 2 + y) <= 100)
Instance Attribute Summary collapse
-
#index ⇒ Integer
readonly
The internal index of this variable in the model.
-
#name ⇒ Symbol
readonly
The human-readable name of this variable.
Instance Method Summary collapse
-
#*(other) ⇒ LinearExpression, QuadraticExpression
Multiplies this variable by a scalar or another variable.
-
#+(other) ⇒ LinearExpression
Adds another variable, expression, or constant to this variable.
-
#-(other) ⇒ LinearExpression
Subtracts another variable, expression, or constant from this variable.
-
#-@ ⇒ LinearExpression
Returns a LinearExpression with this variable negated.
-
#<=(other) ⇒ ConstraintSpec
Creates a less-than-or-equal-to constraint specification.
-
#==(other) ⇒ ConstraintSpec
Creates an equality constraint specification.
-
#>=(other) ⇒ ConstraintSpec
Creates a greater-than-or-equal-to constraint specification.
-
#equals?(other) ⇒ Boolean
(also: #eql?)
Checks if two variables refer to the same underlying variable.
-
#hash ⇒ Integer
Returns the hash code based on this variable’s index.
-
#initialize(index, name) ⇒ Variable
constructor
Creates a new Variable instance.
-
#to_s ⇒ String
Returns a string representation of this variable.
-
#to_sym ⇒ Symbol
Converts this variable’s name to a Symbol.
Constructor Details
#initialize(index, name) ⇒ Variable
Creates a new Variable instance.
42 43 44 45 |
# File 'lib/lpsolver/variable.rb', line 42 def initialize(index, name) @index = index @name = name end |
Instance Attribute Details
#index ⇒ Integer (readonly)
Returns The internal index of this variable in the model. This is used internally to map the variable to a column in the solver’s constraint matrix.
33 34 35 |
# File 'lib/lpsolver/variable.rb', line 33 def index @index end |
#name ⇒ Symbol (readonly)
Returns The human-readable name of this variable.
36 37 38 |
# File 'lib/lpsolver/variable.rb', line 36 def name @name end |
Instance Method Details
#*(other) ⇒ LinearExpression, QuadraticExpression
Multiplies this variable by a scalar or another variable.
When multiplied by a Numeric, returns a LinearExpression with this variable scaled by the given coefficient. When multiplied by another Variable, returns a QuadraticExpression representing the product term (used for quadratic objectives in QP).
59 60 61 62 63 64 65 |
# File 'lib/lpsolver/variable.rb', line 59 def *(other) if other.is_a?(Variable) QuadraticExpression.new({}, [[@index, other.index, 1.0]]) else LinearExpression.new({ @index => other.to_f }) end end |
#+(other) ⇒ LinearExpression
Adds another variable, expression, or constant to this variable.
Creates a new LinearExpression containing the sum of this variable and the other operand.
78 79 80 81 82 83 84 85 86 |
# File 'lib/lpsolver/variable.rb', line 78 def +(other) if other.is_a?(Variable) LinearExpression.new({ @index => 1.0, other.index => 1.0 }) elsif other.is_a?(LinearExpression) LinearExpression.new(merge_terms({ @index => 1.0 }, other.terms), other.constant) else LinearExpression.new({ @index => 1.0 }, other.to_f) end end |
#-(other) ⇒ LinearExpression
Subtracts another variable, expression, or constant from this variable.
Creates a new LinearExpression containing the difference between this variable and the other operand.
99 100 101 102 103 104 105 106 107 |
# File 'lib/lpsolver/variable.rb', line 99 def -(other) if other.is_a?(Variable) LinearExpression.new({ @index => 1.0, other.index => -1.0 }) elsif other.is_a?(LinearExpression) LinearExpression.new(negate_add_terms({ @index => 1.0 }, other.terms), -other.constant) else LinearExpression.new({ @index => 1.0 }, -other.to_f) end end |
#-@ ⇒ LinearExpression
Returns a LinearExpression with this variable negated.
114 115 116 |
# File 'lib/lpsolver/variable.rb', line 114 def -@ LinearExpression.new({ @index => -1.0 }) end |
#<=(other) ⇒ ConstraintSpec
Creates a less-than-or-equal-to constraint specification.
This is used with Model#add_constraint to define upper bounds on linear expressions. The constraint represents: expression <= value.
128 129 130 |
# File 'lib/lpsolver/variable.rb', line 128 def <=(other) ConstraintSpec.new(:le, { @index => 1.0 }, 0, other.to_f) end |
#==(other) ⇒ ConstraintSpec
Creates an equality constraint specification.
This is used with Model#add_constraint to define exact values for linear expressions. The constraint represents: expression == value.
156 157 158 |
# File 'lib/lpsolver/variable.rb', line 156 def ==(other) ConstraintSpec.new(:eq, { @index => 1.0 }, 0, other.to_f) end |
#>=(other) ⇒ ConstraintSpec
Creates a greater-than-or-equal-to constraint specification.
This is used with Model#add_constraint to define lower bounds on linear expressions. The constraint represents: expression >= value.
142 143 144 |
# File 'lib/lpsolver/variable.rb', line 142 def >=(other) ConstraintSpec.new(:ge, { @index => 1.0 }, 0, other.to_f) end |
#equals?(other) ⇒ Boolean Also known as: eql?
Checks if two variables refer to the same underlying variable.
164 165 166 |
# File 'lib/lpsolver/variable.rb', line 164 def equals?(other) other.is_a?(Variable) && other.index == @index end |
#hash ⇒ Integer
Returns the hash code based on this variable’s index.
189 190 191 |
# File 'lib/lpsolver/variable.rb', line 189 def hash @index.hash end |
#to_s ⇒ String
Returns a string representation of this variable.
173 174 175 |
# File 'lib/lpsolver/variable.rb', line 173 def to_s "@#{@name}(#{@index})" end |
#to_sym ⇒ Symbol
Converts this variable’s name to a Symbol.
182 183 184 |
# File 'lib/lpsolver/variable.rb', line 182 def to_sym @name end |