Class: LpSolver::LpGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/lpsolver/lp_generator.rb

Overview

Generates HiGHS LP format strings from a model’s data.

This class handles all serialization logic for converting a model’s variables, constraints, objectives, and bounds into the HiGHS LP format. It is used by both the CLI and native drivers.

Examples:

Basic usage

generator = LpSolver::LpGenerator.new(model)
puts generator.generate

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ LpGenerator

Creates a new LP generator for the given model.

Parameters:

  • model (Model)

    The model to serialize.



17
18
19
# File 'lib/lpsolver/lp_generator.rb', line 17

def initialize(model)
  @model = model
end

Instance Method Details

#generateString

Generates the HiGHS LP format string.

Examples:

puts generator.generate
# Minimize
#  obj: 3 x + 5 y
# Subject To
#  budget: 2 x + 1 y <= 100
#  demand: 1 x + 2 y >= 50
# Bounds
#  0 <= x <= +Inf
#  0 <= y <= +Inf
# End

Returns:

  • (String)

    The LP format content.



35
36
37
38
39
40
41
42
43
44
# File 'lib/lpsolver/lp_generator.rb', line 35

def generate
  lines = []
  lines << (@model.heading == :minimize ? 'Minimize' : 'Maximize')
  lines << generate_objective
  lines.concat(generate_constraints) if @model.constraints.any?
  lines.concat(generate_bounds) if @model.var_bounds.any?
  lines.concat(generate_integers) if has_integer_variables?
  lines << 'End'
  lines.join("\n")
end