Class: Dicey::DistributionCalculators::BaseCalculator Abstract
- Inherits:
-
Object
- Object
- Dicey::DistributionCalculators::BaseCalculator
- Defined in:
- lib/dicey/distribution_calculators/base_calculator.rb
Overview
Base class for implementing distribution calculators.
Calculators have the following methods, each taking an array of dice:
-
#call to actually calculate the distribution;
-
#valid_for? to check if the calculator can handle the dice;
-
#heuristic_complexity to determine the complexity of calculation, mostly useful for AutoSelector.
By default, #call returns weights as they are easier to calculate and can be represented with integers (except for Empirical calculator). If probabilities are requested, they are calculated using Rational numbers to produce exact results.
An empty list of dice is considered a degenerate case, always valid for any calculator.
Options:
Calculators may have calculator-specific options, passed as extra keyword arguments to #call. If present, they will be documented under Options heading on the class itself.
Direct Known Subclasses
Binomial, Empirical, Iterative, MultinomialCoefficients, PolynomialConvolution, Trivial
Constant Summary collapse
- RESULT_TYPES =
Possible values for
result_typeargument in #call. i[weights probabilities].freeze
Instance Method Summary collapse
-
#call(dice, result_type: :weights, **options) ⇒ Hash{Any => Numeric}
Calculate distribution (probability mass function) for the list of dice.
-
#heuristic_complexity(dice) ⇒ Integer
Heuristic complexity of the calculator, used to determine best calculator.
-
#valid_for?(dice) ⇒ Boolean
Whether this calculator can be used for the list of dice.
Instance Method Details
#call(dice, result_type: :weights, **options) ⇒ Hash{Any => Numeric}
Calculate distribution (probability mass function) for the list of dice.
Returns empty hash for an empty list of dice.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dicey/distribution_calculators/base_calculator.rb', line 46 def call(dice, result_type: :weights, **) unless RESULT_TYPES.include?(result_type) raise DiceyError, "#{result_type} is not a valid result type!" end raise DiceyError, "#{self.class} can not handle these dice!" unless valid_for?(dice) # Short-circuit for a degenerate case. return {} if dice.empty? distribution = calculate(dice, **) verify_result(distribution, dice) distribution = sort_result(distribution) transform_result(distribution, result_type) end |
#heuristic_complexity(dice) ⇒ Integer
Heuristic complexity of the calculator, used to determine best calculator.
Will always return a value, even if the calculator is not valid for the dice.
77 78 79 80 81 |
# File 'lib/dicey/distribution_calculators/base_calculator.rb', line 77 def heuristic_complexity(dice) return 0 if dice.empty? calculate_heuristic(dice.length, dice.map(&:sides_num).max).to_i end |
#valid_for?(dice) ⇒ Boolean
Whether this calculator can be used for the list of dice.
65 66 67 |
# File 'lib/dicey/distribution_calculators/base_calculator.rb', line 65 def valid_for?(dice) dice.is_a?(Enumerable) && (dice.empty? || (dice.all?(AbstractDie) && validate(dice))) end |