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.



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

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

Instance Attribute Details

#candidateObject (readonly)

Returns the value of attribute candidate.



448
449
450
# File 'lib/feldtruby/optimize/objective.rb', line 448

def candidate
  @candidate
end

#objectiveObject (readonly)

Returns the value of attribute objective.



448
449
450
# File 'lib/feldtruby/optimize/objective.rb', line 448

def objective
  @objective
end

#sub_qualitiesObject (readonly)

Returns the value of attribute sub_qualities.



448
449
450
# File 'lib/feldtruby/optimize/objective.rb', line 448

def sub_qualities
  @sub_qualities
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#display_valueObject

The value to display. For this default class we just use the quality value.



477
478
479
# File 'lib/feldtruby/optimize/objective.rb', line 477

def display_value
  value
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.



470
471
472
473
474
# File 'lib/feldtruby/optimize/objective.rb', line 470

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



485
486
487
488
489
490
# File 'lib/feldtruby/optimize/objective.rb', line 485

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.



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

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

#value_to_sObject



481
482
483
# File 'lib/feldtruby/optimize/objective.rb', line 481

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