Class: LpSolver::ConstraintSpec

Inherits:
Object
  • Object
show all
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

Examples:

Creating a constraint specification

x = model.add_variable(:x, lb: 0)
y = model.add_variable(:y, lb: 0)
spec = (x * 2 + y * 3 + 5) <= 100
spec.operator    # => :le
spec.terms       # => {x_idx => 2.0, y_idx => 3.0}
spec.lhs_constant # => 5.0
spec.rhs         # => 100.0
spec.bounds      # => [-Infinity, 95.0]

Using in a model

model.add_constraint(:budget, (x * 2 + y * 3 + 5) <= 100)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator, terms, lhs_constant, rhs) ⇒ ConstraintSpec

Creates a new ConstraintSpec.

Parameters:

  • operator (Symbol)

    The constraint operator: :le, :ge, or :eq.

  • terms (Hash{Integer => Float})

    Maps variable indices to coefficients.

  • lhs_constant (Float)

    The constant offset on the left-hand side.

  • rhs (Float)

    The right-hand side value.



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_constantFloat (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.

Returns:

  • (Float)

    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

#operatorSymbol (readonly)

Returns The constraint operator: :le (<=), :ge (>=), or :eq (==).

Returns:

  • (Symbol)

    The constraint operator: :le (<=), :ge (>=), or :eq (==).



33
34
35
# File 'lib/lpsolver/constraint_spec.rb', line 33

def operator
  @operator
end

#rhsFloat (readonly)

Returns The right-hand side value of the comparison. For example, in ‘(x * 2 + y * 3 + 5) <= 100`, this is 100.0.

Returns:

  • (Float)

    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

#termsHash{Integer => Float} (readonly)

Returns Maps variable indices to their coefficients in the expression (excluding the constant offset).

Returns:

  • (Hash{Integer => Float})

    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

#boundsArray<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.

Examples:

For (x * 2 + y * 3 + 5) <= 100

spec.bounds  # => [-Infinity, 95.0]

For (x * 2 + y * 3 + 5) >= 50

spec.bounds  # => [45.0, Infinity]

For (x * 2 + y * 3 + 5) == 75

spec.bounds  # => [70.0, 70.0]

Returns:

  • (Array<Float, Float>)

    An array [lb, ub] representing the lower and upper bounds for the variable terms.



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

#exprArray<[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.

Examples:

spec.expr  # => [[0, 2.0], [1, 3.0]]

Returns:

  • (Array<[Integer, Float]>)

    Array of [var_index, coefficient] pairs.



92
93
94
# File 'lib/lpsolver/constraint_spec.rb', line 92

def expr
  @terms.map { |idx, coeff| [idx, coeff] }
end