Class: LpSolver::Solution

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

Overview

Represents the solution returned by solving a model.

The Solution object contains the optimal (or best-found) values for all decision variables, the optimal objective value, and metadata about the solver’s execution.

Examples:

Accessing solution values

solution = model.solve
solution[:x]        # => 4.0 (value of variable x)
solution[:y]        # => 0.0 (value of variable y)
solution.objective_value  # => 12.0 (optimal objective)

Checking solution status

solution.feasible?      # => true
solution.infeasible?    # => false
solution.unbounded?     # => false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables:, objective_value:, model_status:, iterations:) ⇒ Solution

Creates a new Solution object.

Parameters:

  • variables (Hash{String => Float})

    Maps variable names to values.

  • objective_value (Float)

    The optimal objective value.

  • model_status (String)

    The solver-reported model status.

  • iterations (Integer)

    The number of solver iterations.



49
50
51
52
53
54
# File 'lib/lpsolver/solution.rb', line 49

def initialize(variables:, objective_value:, model_status:, iterations:)
  @variables = variables
  @objective_value = objective_value
  @model_status = model_status
  @iterations = iterations
end

Instance Attribute Details

#iterationsInteger (readonly)

Returns The number of iterations the solver performed. This is a diagnostic metric; may be 0 for some solver types.

Returns:

  • (Integer)

    The number of iterations the solver performed. This is a diagnostic metric; may be 0 for some solver types.



41
42
43
# File 'lib/lpsolver/solution.rb', line 41

def iterations
  @iterations
end

#model_statusString (readonly)

Returns The status of the model as reported by HiGHS. Possible values include:

  • “optimal”: An optimal solution was found.

  • “infeasible”: No feasible solution exists.

  • “unbounded”: The objective can be improved without bound.

  • “unknown”: The solver could not determine the status.

Returns:

  • (String)

    The status of the model as reported by HiGHS. Possible values include:

    • “optimal”: An optimal solution was found.

    • “infeasible”: No feasible solution exists.

    • “unbounded”: The objective can be improved without bound.

    • “unknown”: The solver could not determine the status.



37
38
39
# File 'lib/lpsolver/solution.rb', line 37

def model_status
  @model_status
end

#objective_valueFloat (readonly)

Returns The optimal objective function value. For minimization problems, this is the minimum value. For maximization problems, this is the maximum value.

Returns:

  • (Float)

    The optimal objective function value. For minimization problems, this is the minimum value. For maximization problems, this is the maximum value.



29
30
31
# File 'lib/lpsolver/solution.rb', line 29

def objective_value
  @objective_value
end

#variablesHash{String => Float} (readonly)

Returns Maps variable names to their optimal values. The keys are the variable names as strings (as produced by HiGHS), and the values are the optimal decision variable values.

Returns:

  • (Hash{String => Float})

    Maps variable names to their optimal values. The keys are the variable names as strings (as produced by HiGHS), and the values are the optimal decision variable values.



24
25
26
# File 'lib/lpsolver/solution.rb', line 24

def variables
  @variables
end

Instance Method Details

#[](name) ⇒ Float

Retrieves the value of a variable by name.

Examples:

solution[:x]  # => 4.0

Parameters:

  • name (Symbol, String)

    The variable name.

Returns:

  • (Float)

    The optimal value of the variable.

Raises:

  • (KeyError)

    If the variable name is not found in the solution.



63
64
65
# File 'lib/lpsolver/solution.rb', line 63

def [](name)
  variables[name.to_s]
end

#feasible?Boolean

Checks if the solution is feasible.

A solution is feasible if the solver found a solution that satisfies all constraints and bounds.

Returns:

  • (Boolean)

    True if the model status is “optimal”.



84
85
86
# File 'lib/lpsolver/solution.rb', line 84

def feasible?
  @model_status == 'optimal'
end

#infeasible?Boolean

Checks if the model is infeasible.

A model is infeasible if no solution exists that satisfies all constraints simultaneously.

Returns:

  • (Boolean)

    True if the model status is “infeasible”.



94
95
96
# File 'lib/lpsolver/solution.rb', line 94

def infeasible?
  @model_status == 'infeasible'
end

#to_sString

Returns a string representation of the solution.

Examples:

puts solution
# x = 4.0
# y = 0.0
# Objective: 12.0

Returns:

  • (String)

    A formatted string showing variable values and the objective value.



117
118
119
120
121
# File 'lib/lpsolver/solution.rb', line 117

def to_s
  lines = variables.map { |name, value| "#{name} = #{value}" }
  lines << "Objective: #{objective_value}"
  lines.join("\n")
end

#unbounded?Boolean

Checks if the model is unbounded.

A model is unbounded if the objective can be improved indefinitely without violating any constraints.

Returns:

  • (Boolean)

    True if the model status is “unbounded”.



104
105
106
# File 'lib/lpsolver/solution.rb', line 104

def unbounded?
  @model_status == 'unbounded'
end

#values_at(*names) ⇒ Array<Float>

Retrieves values for multiple variables by name.

Examples:

solution.values_at(:x, :y)  # => [4.0, 0.0]

Parameters:

  • *names (Symbol, String)

    The variable names to retrieve.

  • names (Array<Object>)

Returns:

  • (Array<Float>)

    An array of variable values in the same order.



74
75
76
# File 'lib/lpsolver/solution.rb', line 74

def values_at(*names)
  names.map { |name| variables[name.to_s] }
end