Class: LpSolver::LinearExpression
- Inherits:
-
Object
- Object
- LpSolver::LinearExpression
- Defined in:
- lib/lpsolver/linear_expression.rb
Overview
Represents a linear expression: sum of (coefficient * variable) + constant.
Linear expressions are the fundamental building blocks of linear programming models. They represent quantities like costs, resource usage, or returns that scale linearly with decision variables.
A linear expression has the mathematical form:
c₀ + c₁·x₁ + c₂·x₂ + ... + cₙ·xₙ
where c₀ is the constant offset and cᵢ are the coefficients for each variable.
Instance Attribute Summary collapse
-
#constant ⇒ Float
readonly
The constant offset in the expression.
-
#terms ⇒ Hash{Integer => Float}
readonly
Maps variable indices to their coefficients.
Instance Method Summary collapse
-
#*(other) ⇒ LinearExpression
Multiplies this expression by a scalar.
-
#+(other) ⇒ LinearExpression, QuadraticExpression
Adds another expression, variable, constant, or quadratic expression.
-
#-(other) ⇒ LinearExpression, QuadraticExpression
Subtracts another expression, variable, constant, or quadratic expression.
-
#-@ ⇒ LinearExpression
Returns a LinearExpression with all coefficients and the constant 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.
-
#initialize(terms = {}, constant = 0.0) ⇒ LinearExpression
constructor
Creates a new LinearExpression.
Constructor Details
#initialize(terms = {}, constant = 0.0) ⇒ LinearExpression
Creates a new LinearExpression.
44 45 46 47 |
# File 'lib/lpsolver/linear_expression.rb', line 44 def initialize(terms = {}, constant = 0.0) @terms = terms @constant = constant end |
Instance Attribute Details
#constant ⇒ Float (readonly)
Returns The constant offset in the expression.
38 39 40 |
# File 'lib/lpsolver/linear_expression.rb', line 38 def constant @constant end |
#terms ⇒ Hash{Integer => Float} (readonly)
Returns Maps variable indices to their coefficients. The keys are the internal indices assigned by the model, and the values are the floating-point coefficients.
33 34 35 |
# File 'lib/lpsolver/linear_expression.rb', line 33 def terms @terms end |
Instance Method Details
#*(other) ⇒ LinearExpression
Multiplies this expression by a scalar.
Scales both the variable coefficients and the constant by the given factor.
119 120 121 122 123 124 125 |
# File 'lib/lpsolver/linear_expression.rb', line 119 def *(other) s = other.to_f LinearExpression.new( @terms.transform_values { |c| c * s }, @constant * s ) end |
#+(other) ⇒ LinearExpression, QuadraticExpression
Adds another expression, variable, constant, or quadratic expression.
When adding a LinearExpression or Variable, returns a new LinearExpression. When adding a QuadraticExpression, returns a new QuadraticExpression that combines the linear and quadratic parts.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/lpsolver/linear_expression.rb', line 62 def +(other) if other.is_a?(Variable) LinearExpression.new( merge_terms(@terms, { other.index => 1.0 }), @constant ) elsif other.is_a?(LinearExpression) LinearExpression.new( merge_terms(@terms, other.terms), @constant + other.constant ) elsif other.is_a?(QuadraticExpression) new_linear = other.linear_terms.dup @terms.each { |idx, coeff| new_linear[idx] = (new_linear[idx] || 0) + coeff } QuadraticExpression.new(new_linear.reject { |_, v| v.zero? }, other.quadratic_terms.dup) else LinearExpression.new(@terms.dup, @constant + other.to_f) end end |
#-(other) ⇒ LinearExpression, QuadraticExpression
Subtracts another expression, variable, constant, or quadratic expression.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/lpsolver/linear_expression.rb', line 89 def -(other) if other.is_a?(Variable) LinearExpression.new( merge_terms(@terms, { other.index => -1.0 }), @constant ) elsif other.is_a?(LinearExpression) LinearExpression.new( merge_terms(@terms, negate_terms(other.terms)), @constant - other.constant ) elsif other.is_a?(QuadraticExpression) new_linear = other.linear_terms.dup @terms.each { |idx, coeff| new_linear[idx] = (new_linear[idx] || 0) + coeff } neg_quad = other.quadratic_terms.map { |i1, i2, c| [i1, i2, -c] } QuadraticExpression.new(new_linear.reject { |_, v| v.zero? }, neg_quad) else LinearExpression.new(@terms.dup, @constant - other.to_f) end end |
#-@ ⇒ LinearExpression
Returns a LinearExpression with all coefficients and the constant negated.
132 133 134 135 136 137 |
# File 'lib/lpsolver/linear_expression.rb', line 132 def -@ LinearExpression.new( @terms.transform_values { |c| -c }, -@constant ) end |
#<=(other) ⇒ ConstraintSpec
Creates a less-than-or-equal-to constraint specification.
146 147 148 |
# File 'lib/lpsolver/linear_expression.rb', line 146 def <=(other) ConstraintSpec.new(:le, @terms.dup, @constant, other.to_f) end |
#==(other) ⇒ ConstraintSpec
Creates an equality constraint specification.
168 169 170 |
# File 'lib/lpsolver/linear_expression.rb', line 168 def ==(other) ConstraintSpec.new(:eq, @terms.dup, @constant, other.to_f) end |
#>=(other) ⇒ ConstraintSpec
Creates a greater-than-or-equal-to constraint specification.
157 158 159 |
# File 'lib/lpsolver/linear_expression.rb', line 157 def >=(other) ConstraintSpec.new(:ge, @terms.dup, @constant, other.to_f) end |