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.
-
#all_variables ⇒ Hash{Symbol => Float}
Returns all variables as a hash with Symbol keys.
-
#each_variable {|name, value| ... } ⇒ self
Iterates over all variables and their optimal values.
-
#feasible? ⇒ Boolean
Checks if the solution is feasible.
-
#has_variable?(name) ⇒ Boolean
Checks if a variable exists in the solution.
-
#infeasible? ⇒ Boolean
Checks if the model is infeasible.
-
#initialize(variables:, objective_value:, model_status:, iterations:) ⇒ Solution
constructor
Creates a new Solution object.
-
#status ⇒ Symbol
Returns the model status as a Symbol.
-
#to_h ⇒ Hash{Symbol => Float}
Returns the solution as a plain hash with Symbol keys.
-
#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.
66 67 68 69 70 71 |
# File 'lib/lpsolver/solution.rb', line 66 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.
45 46 47 |
# File 'lib/lpsolver/solution.rb', line 45 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.
41 42 43 |
# File 'lib/lpsolver/solution.rb', line 41 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. When the solution is infeasible, this returns ‘0.0`. Always check `infeasible?` or `unbounded?` before reading this value.
33 34 35 |
# File 'lib/lpsolver/solution.rb', line 33 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. When the solution is infeasible, this hash is empty. Always check ‘infeasible?` or `unbounded?` before reading variable values.
26 27 28 |
# File 'lib/lpsolver/solution.rb', line 26 def variables @variables end |
Instance Method Details
#[](name) ⇒ Float
When the solution is infeasible, all variables are empty. Check ‘infeasible?` first before accessing variable values.
Retrieves the value of a variable by name.
86 87 88 89 90 91 92 93 |
# File 'lib/lpsolver/solution.rb', line 86 def [](name) key = if name.is_a?(Variable) name.name.to_s else name.to_s end variables[key] end |
#all_variables ⇒ Hash{Symbol => Float}
Returns all variables as a hash with Symbol keys.
This is a convenience method that converts the internal String-keyed variables hash to a Symbol-keyed hash for easier Ruby-style access.
117 118 119 |
# File 'lib/lpsolver/solution.rb', line 117 def all_variables variables.transform_keys(&:to_sym) end |
#each_variable {|name, value| ... } ⇒ self
Iterates over all variables and their optimal values.
Yields each variable name (as a Symbol) and its value.
142 143 144 145 |
# File 'lib/lpsolver/solution.rb', line 142 def each_variable all_variables.each { |name, value| yield name, value } self 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.
169 170 171 |
# File 'lib/lpsolver/solution.rb', line 169 def feasible? @model_status == 'optimal' end |
#has_variable?(name) ⇒ Boolean
Checks if a variable exists in the solution.
154 155 156 157 158 159 160 161 |
# File 'lib/lpsolver/solution.rb', line 154 def has_variable?(name) key = if name.is_a?(Variable) name.name.to_s else name.to_s end variables.key?(key) end |
#infeasible? ⇒ Boolean
Checks if the model is infeasible.
A model is infeasible if no solution exists that satisfies all constraints simultaneously.
179 180 181 |
# File 'lib/lpsolver/solution.rb', line 179 def infeasible? @model_status == 'infeasible' end |
#status ⇒ Symbol
Returns the model status as a Symbol.
56 57 58 |
# File 'lib/lpsolver/solution.rb', line 56 def status @model_status.to_sym end |
#to_h ⇒ Hash{Symbol => Float}
Returns the solution as a plain hash with Symbol keys.
This is equivalent to #all_variables and is provided for Ruby convention compatibility.
129 130 131 |
# File 'lib/lpsolver/solution.rb', line 129 def to_h all_variables end |
#to_s ⇒ String
Returns a string representation of the solution.
202 203 204 205 206 |
# File 'lib/lpsolver/solution.rb', line 202 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.
189 190 191 |
# File 'lib/lpsolver/solution.rb', line 189 def unbounded? @model_status == 'unbounded' end |
#values_at(*names) ⇒ Array<Float>
Retrieves values for multiple variables by name.
103 104 105 |
# File 'lib/lpsolver/solution.rb', line 103 def values_at(*names) names.map { |name| self[name] } end |