Class: LpSolver::ConstraintSpec
- Inherits:
-
Object
- Object
- LpSolver::ConstraintSpec
- Defined in:
- lib/lpsolver/constraint_spec.rb
Overview
Represents a constraint specification derived from comparing an expression with a value.
ConstraintSpec objects are created when comparison operators (<=, >=, ==) are applied to LinearExpression or Variable objects. They encode the constraint’s operator type, variable coefficients, and bounds.
A constraint specification has the form:
operator: sum(coeff_i * x_i) + constant_offset compared to rhs
The bounds are computed by rearranging the expression to isolate the variables on the left-hand side:
sum(coeff_i * x_i) <= (rhs - constant_offset) for <= constraints
sum(coeff_i * x_i) >= (rhs - constant_offset) for >= constraints
sum(coeff_i * x_i) == (rhs - constant_offset) for == constraints
Instance Attribute Summary collapse
-
#lhs_constant ⇒ Float
readonly
The constant offset on the left-hand side of the comparison.
-
#operator ⇒ Symbol
readonly
The constraint operator: :le (<=), :ge (>=), or :eq (==).
-
#rhs ⇒ Float
readonly
The right-hand side value of the comparison.
-
#terms ⇒ Hash{Integer => Float}
readonly
Maps variable indices to their coefficients in the expression (excluding the constant offset).
Instance Method Summary collapse
-
#bounds ⇒ Array<Float, Float>
Converts this constraint specification to lower and upper bounds.
-
#expr ⇒ Array<[Integer, Float]>
Returns the expression terms as an array of [variable_index, coefficient] pairs.
-
#initialize(operator, terms, lhs_constant, rhs) ⇒ ConstraintSpec
constructor
Creates a new ConstraintSpec.
Constructor Details
#initialize(operator, terms, lhs_constant, rhs) ⇒ ConstraintSpec
Creates a new ConstraintSpec.
53 54 55 56 57 58 |
# File 'lib/lpsolver/constraint_spec.rb', line 53 def initialize(operator, terms, lhs_constant, rhs) @operator = operator @terms = terms @lhs_constant = lhs_constant @rhs = rhs end |
Instance Attribute Details
#lhs_constant ⇒ Float (readonly)
Returns The constant offset on the left-hand side of the comparison. For example, in ‘(x * 2 + y * 3 + 5) <= 100`, this is 5.0.
41 42 43 |
# File 'lib/lpsolver/constraint_spec.rb', line 41 def lhs_constant @lhs_constant end |
#operator ⇒ Symbol (readonly)
Returns The constraint operator: :le (<=), :ge (>=), or :eq (==).
33 34 35 |
# File 'lib/lpsolver/constraint_spec.rb', line 33 def operator @operator end |
#rhs ⇒ Float (readonly)
Returns The right-hand side value of the comparison. For example, in ‘(x * 2 + y * 3 + 5) <= 100`, this is 100.0.
45 46 47 |
# File 'lib/lpsolver/constraint_spec.rb', line 45 def rhs @rhs end |
#terms ⇒ Hash{Integer => Float} (readonly)
Returns Maps variable indices to their coefficients in the expression (excluding the constant offset).
37 38 39 |
# File 'lib/lpsolver/constraint_spec.rb', line 37 def terms @terms end |
Instance Method Details
#bounds ⇒ Array<Float, Float>
Converts this constraint specification to lower and upper bounds.
Rearranges the constraint expression to isolate the variable terms, computing the effective bounds for the expression.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/lpsolver/constraint_spec.rb', line 73 def bounds case @operator when :le [-Float::INFINITY, @rhs - @lhs_constant] when :ge [@rhs - @lhs_constant, Float::INFINITY] when :eq v = @rhs - @lhs_constant [v, v] end end |
#expr ⇒ Array<[Integer, Float]>
Returns the expression terms as an array of [variable_index, coefficient] pairs.
This format is used internally when serializing the model to HiGHS LP format.
92 93 94 |
# File 'lib/lpsolver/constraint_spec.rb', line 92 def expr @terms.map { |idx, coeff| [idx, coeff] } end |