Class: Dither::Aetg::Pairwise

Inherits:
Object
  • Object
show all
Includes:
Dither::Aetg
Defined in:
lib/dither/aetg_pairwise.rb

Defined Under Namespace

Modules: Pairs Classes: Pair

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Dither::Aetg

#run

Constructor Details

#initialize(params, opts = {}) ⇒ Pairwise

Pairs

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dither/aetg_pairwise.rb', line 18

def initialize(params, opts = {})

  raise Dither::Error, 't must be >= 2' if opts[:t] < 2
  raise Dither::Error, 't must be <= params.length' if opts[:t] > params.length
  params.each do |param|
    raise Dither::Error, 'param length must be > 1' if param.length < 2
  end
  @params = params
  @n = 50
  @scratch = Array.new(@n)
  seed = opts[:seed] || Random.new.seed
  @random = Random.new(seed)

  @pair_cache = Array.new(params.length)
  params.each_with_index do |param, i|
    pair_cache[i] = (0...param.length).map { |j| Pair.new(i, j).freeze }
  end
  if opts[:previously_tested]
    opts[:constraints] = [] unless opts[:constraints]
    opts[:previously_tested].each do |a|
      arr = []
      a.each_with_index { |b,i| arr << [i, b] }
      opts[:constraints] << Hash[arr]
    end
  end
  @constraints = nil
  if opts[:constraints]
    @constraints = []
    opts[:constraints].each do |a|
      constraint = a.map { |k, v| pair_cache[k][v] }
      constraint.extend(Pairs)
      @constraints << constraint
    end
  end
  @comb = []
  @t = opts[:t]
  (0...params.length).to_a.combination(t).each do |a|
    car, *cdr = a.map { |b| pair_cache[b] }
    tmp = car.product(*cdr)
    tmp.each { |b| b.extend(Pairs) }
    tmp.reject! { |b| constraints.any? { |c| c.all? { |d| b.include?(d)} } } if constraints
    @comb.push(*tmp)
  end
end

Instance Attribute Details

#combObject (readonly)

Returns the value of attribute comb.



7
8
9
# File 'lib/dither/aetg_pairwise.rb', line 7

def comb
  @comb
end

#constraintsObject (readonly)

Returns the value of attribute constraints.



7
8
9
# File 'lib/dither/aetg_pairwise.rb', line 7

def constraints
  @constraints
end

#nObject (readonly)

Returns the value of attribute n.



7
8
9
# File 'lib/dither/aetg_pairwise.rb', line 7

def n
  @n
end

#pair_cacheObject (readonly)

Returns the value of attribute pair_cache.



7
8
9
# File 'lib/dither/aetg_pairwise.rb', line 7

def pair_cache
  @pair_cache
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/dither/aetg_pairwise.rb', line 7

def params
  @params
end

#randomObject (readonly)

Returns the value of attribute random.



7
8
9
# File 'lib/dither/aetg_pairwise.rb', line 7

def random
  @random
end

#scratchObject (readonly)

Returns the value of attribute scratch.



7
8
9
# File 'lib/dither/aetg_pairwise.rb', line 7

def scratch
  @scratch
end

#tObject (readonly)

Returns the value of attribute t.



7
8
9
# File 'lib/dither/aetg_pairwise.rb', line 7

def t
  @t
end

Instance Method Details

#best_fitObject



79
80
81
82
83
84
85
# File 'lib/dither/aetg_pairwise.rb', line 79

def best_fit
  max, _ = scratch.compact
    .map { |a| [a, comb.count { |b| b.in_test_case?(a) }] }
    .max { |a, b| a[1] <=> b[1] }
  comb.delete_if { |a| a.in_test_case?(max) }
  max
end

#filterObject



69
70
71
72
73
74
75
76
77
# File 'lib/dither/aetg_pairwise.rb', line 69

def filter
  return unless constraints
  comb.first.each do |pair|
    scratch[pair.i] = pair.j
  end
  scratch.each_with_index do |e, i|
    scratch[i] = nil if constraints.any? { |a| a.in_test_case?(e) }
  end
end

#generateObject



63
64
65
66
67
# File 'lib/dither/aetg_pairwise.rb', line 63

def generate
  (0...n).each do |i|
    scratch[i] = params.map { |a| random.rand(a.length) }
  end
end

#stop?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/dither/aetg_pairwise.rb', line 87

def stop?
  comb.empty?
end