Class: FeldtRuby::Optimize::QualityValue

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/feldtruby/optimize/objective.rb

Overview

Class for representing multi-objective sub_qualitites and their summary value. A quality has a version number which was the version of the objective when this quality was calculated. When a quality value is compared to another quality value they are first updated so that they reflect the quality of the candidate for the current version of the objective.

Direct Known Subclasses

PercentageQualityValue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subQvs, candidate, objective) ⇒ QualityValue

Returns a new instance of QualityValue.



443
444
445
446
# File 'lib/feldtruby/optimize/objective.rb', line 443

def initialize(subQvs, candidate, objective)
  @sub_qualities, @objective = subQvs, objective
  @candidate = candidate
end

Instance Attribute Details

#candidateObject (readonly)

Returns the value of attribute candidate.



441
442
443
# File 'lib/feldtruby/optimize/objective.rb', line 441

def candidate
  @candidate
end

#objectiveObject (readonly)

Returns the value of attribute objective.



441
442
443
# File 'lib/feldtruby/optimize/objective.rb', line 441

def objective
  @objective
end

#sub_qualitiesObject (readonly)

Returns the value of attribute sub_qualities.



441
442
443
# File 'lib/feldtruby/optimize/objective.rb', line 441

def sub_qualities
  @sub_qualities
end

Instance Method Details

#<=>(other) ⇒ Object



456
457
458
459
# File 'lib/feldtruby/optimize/objective.rb', line 456

def <=>(other)
  return nil unless @objective == other.objective
  @objective.hat_compare(@candidate, other.candidate)
end

#sub_quality(index, ensureMinimization = false) ⇒ Object

Return the sub quality value with a given index. Can make sure maximization goals are mapped as minimization goals if ensureMinimization is true.



463
464
465
466
467
# File 'lib/feldtruby/optimize/objective.rb', line 463

def sub_quality(index, ensureMinimization = false)
  return @sub_qualities[index] if !ensureMinimization || @objective.is_min_goal?(index)
  # Now we now this is a max goal that should be returned as a min goal => invert it.
  -(@sub_qualities[index])
end

#to_sObject



473
474
475
476
477
478
# File 'lib/feldtruby/optimize/objective.rb', line 473

def to_s
  subqs = sub_qualities.map {|f| f ? f.to_significant_digits(3) : nil}
  # Note! We ask for the value first which guarantees that we then have a version number.
  qstr = value_to_s
  "#{qstr} (SubQs = #{subqs.inspect}, ver. #{@version})"
end

#valueObject

Return the aggregated quality value. Will always return an updated value since it will be recalculated if we have the wrong version.



450
451
452
453
454
# File 'lib/feldtruby/optimize/objective.rb', line 450

def value
  return @value if @version && @version == @objective.current_version
  @version = @objective.current_version
  @value = @objective.aggregated_quality(@sub_qualities)
end

#value_to_sObject



469
470
471
# File 'lib/feldtruby/optimize/objective.rb', line 469

def value_to_s
  "#{value.to_significant_digits(4)}"
end