Class: LpSolver::Solution
- Inherits:
-
Object
- Object
- LpSolver::Solution
- 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.
Instance Attribute Summary collapse
-
#iterations ⇒ Integer
readonly
The number of iterations the solver performed.
-
#model_status ⇒ String
readonly
The status of the model as reported by HiGHS.
-
#objective_value ⇒ Float
readonly
The optimal objective function value.
-
#variables ⇒ Hash{String => Float}
readonly
Maps variable names to their optimal values.
Instance Method Summary collapse
-
#[](name) ⇒ Float
Retrieves the value of a variable by name.
-
#feasible? ⇒ Boolean
Checks if the solution is feasible.
-
#infeasible? ⇒ Boolean
Checks if the model is infeasible.
-
#initialize(variables:, objective_value:, model_status:, iterations:) ⇒ Solution
constructor
Creates a new Solution object.
-
#to_s ⇒ String
Returns a string representation of the solution.
-
#unbounded? ⇒ Boolean
Checks if the model is unbounded.
-
#values_at(*names) ⇒ Array<Float>
Retrieves values for multiple variables by name.
Constructor Details
#initialize(variables:, objective_value:, model_status:, iterations:) ⇒ Solution
Creates a new Solution object.
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
#iterations ⇒ Integer (readonly)
Returns 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_status ⇒ String (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.
37 38 39 |
# File 'lib/lpsolver/solution.rb', line 37 def model_status @model_status end |
#objective_value ⇒ Float (readonly)
Returns 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 |
#variables ⇒ Hash{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.
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.
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.
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.
94 95 96 |
# File 'lib/lpsolver/solution.rb', line 94 def infeasible? @model_status == 'infeasible' end |
#to_s ⇒ String
Returns a string representation of the solution.
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.
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.
74 75 76 |
# File 'lib/lpsolver/solution.rb', line 74 def values_at(*names) names.map { |name| variables[name.to_s] } end |