latticerb 0.3.1 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +288 -0
- data/Cargo.toml +7 -0
- data/README.md +35 -5
- data/ext/lattice_native/Cargo.toml +13 -0
- data/ext/lattice_native/extconf.rb +6 -0
- data/ext/lattice_native/rustfmt.toml +1 -0
- data/ext/lattice_native/src/lib.rs +114 -0
- data/lib/lattice/cli.rb +3 -3
- data/lib/lattice/constraints.rb +60 -152
- data/lib/lattice/formatters.rb +1 -1
- data/lib/lattice/ipog.rb +47 -166
- data/lib/lattice/native.rb +10 -0
- data/lib/lattice/parser.rb +3 -0
- data/lib/lattice/schema_text.rb +35 -15
- data/lib/lattice/version.rb +1 -1
- data/lib/lattice.rb +2 -2
- metadata +27 -5
- data/lib/lattice/mersenne_twister.rb +0 -102
data/lib/lattice/constraints.rb
CHANGED
|
@@ -3,18 +3,21 @@
|
|
|
3
3
|
require "set"
|
|
4
4
|
|
|
5
5
|
require_relative "model"
|
|
6
|
+
require_relative "native"
|
|
6
7
|
|
|
7
8
|
module Lattice
|
|
8
9
|
class ConstraintError < StandardError; end
|
|
9
10
|
|
|
10
|
-
# Compiles model constraints
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
11
|
+
# Compiles a model's constraints to the engine's primitives (invalid pairs,
|
|
12
|
+
# requirements, higher-order exclusions, forced interactions), checks the
|
|
13
|
+
# compiled closure for contradictions, and asks the Rust engine whether the
|
|
14
|
+
# model and its forced interactions are satisfiable. Python's
|
|
15
|
+
# lattice.constraints does the same, so both report the same errors.
|
|
16
|
+
# Assignments are `[parameter_index, value_index]` pairs.
|
|
14
17
|
class ConstraintEngine
|
|
15
18
|
EMPTY_SET = Set.new.freeze
|
|
16
19
|
|
|
17
|
-
attr_reader :model, :forced_interactions
|
|
20
|
+
attr_reader :model, :forced_interactions, :native
|
|
18
21
|
|
|
19
22
|
def initialize(model)
|
|
20
23
|
@model = model
|
|
@@ -29,51 +32,56 @@ module Lattice
|
|
|
29
32
|
@requires = {}
|
|
30
33
|
@higher_order = []
|
|
31
34
|
@forced_interactions = []
|
|
32
|
-
@feasibility_cache = {}
|
|
33
35
|
compile_constraints
|
|
34
36
|
compute_implied_pairs
|
|
35
37
|
validate_compiled_constraints
|
|
38
|
+
@native = native_model(model.strength)
|
|
39
|
+
@satisfiable = check_feasibility
|
|
36
40
|
end
|
|
37
41
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
def implied_requirements(assignment)
|
|
43
|
-
requires_for(to_ref(assignment)).map { |ref| from_ref(ref) }.sort
|
|
42
|
+
# Whether any row satisfies every constraint.
|
|
43
|
+
def satisfiable?
|
|
44
|
+
@satisfiable
|
|
44
45
|
end
|
|
45
46
|
|
|
47
|
+
# Whether some valid row contains every assignment in `partial`
|
|
48
|
+
# (a `{parameter_index => value_index}` hash).
|
|
46
49
|
def can_complete?(partial)
|
|
47
|
-
|
|
48
|
-
return false if normalized.nil?
|
|
49
|
-
|
|
50
|
-
signature = normalized.sort
|
|
51
|
-
cached = @feasibility_cache[signature]
|
|
52
|
-
return cached unless cached.nil?
|
|
53
|
-
|
|
54
|
-
result = !backtrack_complete(normalized, rng: nil, failures: Set.new).nil?
|
|
55
|
-
@feasibility_cache[signature] = result
|
|
56
|
-
result
|
|
50
|
+
native.can_complete?(partial.sort)
|
|
57
51
|
end
|
|
58
52
|
|
|
59
|
-
def
|
|
60
|
-
|
|
61
|
-
|
|
53
|
+
def native_model(strength)
|
|
54
|
+
compiled = engine_constraints
|
|
55
|
+
Engine::Model.new(
|
|
56
|
+
model.parameters.map { |parameter| parameter.values.length },
|
|
57
|
+
model.parameters.map(&:normalized_weights),
|
|
58
|
+
strength,
|
|
59
|
+
compiled.fetch(:invalid_pairs),
|
|
60
|
+
compiled.fetch(:requires),
|
|
61
|
+
compiled.fetch(:higher_order),
|
|
62
|
+
compiled.fetch(:forced)
|
|
63
|
+
)
|
|
64
|
+
end
|
|
62
65
|
|
|
63
|
-
|
|
66
|
+
def conflicts?(left, right)
|
|
67
|
+
invalid_pairs_for(to_ref(left)).include?(to_ref(right))
|
|
64
68
|
end
|
|
65
69
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
+
# The compiled constraints as the generation engine takes them: invalid
|
|
71
|
+
# pairs and requirements as sorted [[p, v], [q, w]] lists, higher-order
|
|
72
|
+
# exclusions and forced interactions in compile order. Python's
|
|
73
|
+
# lattice.ipog builds the same lists, so the engine gets identical input.
|
|
74
|
+
def engine_constraints
|
|
75
|
+
{
|
|
76
|
+
invalid_pairs: ref_pairs(@invalid_pairs),
|
|
77
|
+
requires: ref_pairs(@requires),
|
|
78
|
+
higher_order: @higher_order,
|
|
79
|
+
forced: @forced_interactions
|
|
80
|
+
}
|
|
81
|
+
end
|
|
70
82
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return false if normalized[forbidden[0]] == forbidden[1]
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
true
|
|
83
|
+
def implied_requirements(assignment)
|
|
84
|
+
requires_for(to_ref(assignment)).map { |ref| from_ref(ref) }.sort
|
|
77
85
|
end
|
|
78
86
|
|
|
79
87
|
private
|
|
@@ -187,125 +195,21 @@ module Lattice
|
|
|
187
195
|
end
|
|
188
196
|
end
|
|
189
197
|
|
|
190
|
-
@forced_interactions.each do |forced|
|
|
191
|
-
partial = forced.to_h { |parameter_index, value_index| [parameter_index, value_index] }
|
|
192
|
-
next unless complete_assignment(partial, rng: nil).nil?
|
|
193
|
-
|
|
194
|
-
assignments = forced.map do |parameter_index, value_index|
|
|
195
|
-
parameter = model.parameters[parameter_index]
|
|
196
|
-
"#{parameter.name}=#{parameter.values[value_index]}"
|
|
197
|
-
end.join(", ")
|
|
198
|
-
raise ConstraintError, "Forced interaction is not feasible: #{assignments}."
|
|
199
|
-
end
|
|
200
|
-
end
|
|
201
|
-
|
|
202
|
-
def normalize_partial(partial)
|
|
203
|
-
normalized = partial.dup
|
|
204
|
-
changed = true
|
|
205
|
-
while changed
|
|
206
|
-
changed = false
|
|
207
|
-
return nil unless partial_consistent?(normalized)
|
|
208
|
-
|
|
209
|
-
normalized.to_a.each do |parameter_index, value_index|
|
|
210
|
-
requires_for([parameter_index, value_index]).each do |required_parameter, required_value|
|
|
211
|
-
current = normalized[required_parameter]
|
|
212
|
-
if current.nil?
|
|
213
|
-
normalized[required_parameter] = required_value
|
|
214
|
-
changed = true
|
|
215
|
-
elsif current != required_value
|
|
216
|
-
return nil
|
|
217
|
-
end
|
|
218
|
-
end
|
|
219
|
-
end
|
|
220
|
-
end
|
|
221
|
-
return nil unless partial_consistent?(normalized)
|
|
222
|
-
|
|
223
|
-
normalized
|
|
224
198
|
end
|
|
225
199
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
return false if invalid_pairs_for([parameter_index, value_index]).include?([other_parameter, other_value])
|
|
232
|
-
end
|
|
233
|
-
end
|
|
234
|
-
|
|
235
|
-
@higher_order.each do |antecedents, forbidden|
|
|
236
|
-
if antecedents.all? { |parameter_index, value_index| partial[parameter_index] == value_index }
|
|
237
|
-
return false if partial[forbidden[0]] == forbidden[1]
|
|
238
|
-
end
|
|
239
|
-
end
|
|
200
|
+
# Raises for the first impossible forced interaction; otherwise returns
|
|
201
|
+
# whether the model has any valid row.
|
|
202
|
+
def check_feasibility
|
|
203
|
+
native.feasibility
|
|
240
204
|
true
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
return complete_assignment_valid?(row) ? row : nil
|
|
250
|
-
end
|
|
251
|
-
|
|
252
|
-
next_parameter, values = next_parameter_values(partial)
|
|
253
|
-
if next_parameter.nil?
|
|
254
|
-
failures.add(signature)
|
|
255
|
-
return nil
|
|
256
|
-
end
|
|
257
|
-
|
|
258
|
-
ordered_values(next_parameter, values, rng).each do |value|
|
|
259
|
-
candidate = partial.dup
|
|
260
|
-
candidate[next_parameter] = value
|
|
261
|
-
normalized = normalize_partial(candidate)
|
|
262
|
-
next if normalized.nil?
|
|
263
|
-
|
|
264
|
-
result = backtrack_complete(normalized, rng:, failures:)
|
|
265
|
-
return result unless result.nil?
|
|
266
|
-
end
|
|
267
|
-
|
|
268
|
-
failures.add(signature)
|
|
269
|
-
nil
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
def next_parameter_values(partial)
|
|
273
|
-
best_parameter = nil
|
|
274
|
-
best_values = []
|
|
275
|
-
best_size = nil
|
|
276
|
-
|
|
277
|
-
model.parameters.each_with_index do |parameter, parameter_index|
|
|
278
|
-
next if partial.key?(parameter_index)
|
|
279
|
-
|
|
280
|
-
feasible = []
|
|
281
|
-
(0...parameter.values.length).each do |value_index|
|
|
282
|
-
candidate = partial.dup
|
|
283
|
-
candidate[parameter_index] = value_index
|
|
284
|
-
feasible << value_index unless normalize_partial(candidate).nil?
|
|
285
|
-
end
|
|
286
|
-
|
|
287
|
-
return [nil, []] if feasible.empty?
|
|
288
|
-
|
|
289
|
-
if best_size.nil? || feasible.length < best_size
|
|
290
|
-
best_parameter = parameter_index
|
|
291
|
-
best_values = feasible
|
|
292
|
-
best_size = feasible.length
|
|
293
|
-
end
|
|
294
|
-
end
|
|
295
|
-
|
|
296
|
-
[best_parameter, best_values]
|
|
297
|
-
end
|
|
298
|
-
|
|
299
|
-
def ordered_values(parameter_index, values, rng)
|
|
300
|
-
parameter = model.parameters[parameter_index]
|
|
301
|
-
if rng.nil?
|
|
302
|
-
return values.sort_by { |value_index| [-parameter.normalized_weights[value_index], value_index] }
|
|
303
|
-
end
|
|
304
|
-
|
|
305
|
-
values
|
|
306
|
-
.map { |value_index| [rng.random / [parameter.normalized_weights[value_index], 1e-9].max, value_index] }
|
|
307
|
-
.sort
|
|
308
|
-
.map { |_, value_index| value_index }
|
|
205
|
+
rescue Engine::ForcedInfeasible => e
|
|
206
|
+
assignments = @forced_interactions.fetch(Integer(e.message)).map do |parameter_index, value_index|
|
|
207
|
+
parameter = model.parameters[parameter_index]
|
|
208
|
+
"#{parameter.name}=#{parameter.values[value_index]}"
|
|
209
|
+
end.join(", ")
|
|
210
|
+
raise ConstraintError, "Forced interaction is not feasible: #{assignments}."
|
|
211
|
+
rescue Engine::NoValidAssignments
|
|
212
|
+
false
|
|
309
213
|
end
|
|
310
214
|
|
|
311
215
|
def exclude_other_values(source, required)
|
|
@@ -330,6 +234,10 @@ module Lattice
|
|
|
330
234
|
@invalid_pairs[ref] || EMPTY_SET
|
|
331
235
|
end
|
|
332
236
|
|
|
237
|
+
def ref_pairs(table)
|
|
238
|
+
table.flat_map { |source, targets| targets.map { |target| [source, target] } }.sort
|
|
239
|
+
end
|
|
240
|
+
|
|
333
241
|
def requires_for(ref)
|
|
334
242
|
@requires[ref] || EMPTY_SET
|
|
335
243
|
end
|
data/lib/lattice/formatters.rb
CHANGED
data/lib/lattice/ipog.rb
CHANGED
|
@@ -1,27 +1,33 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "set"
|
|
4
|
-
|
|
5
3
|
require_relative "constraints"
|
|
6
|
-
require_relative "mersenne_twister"
|
|
7
4
|
|
|
8
5
|
module Lattice
|
|
9
6
|
CoveragePoint = Data.define(:scenario, :cumulative_pct)
|
|
10
7
|
GenerationResult = Data.define(:rows, :coverage_curve, :total_valid_tuples)
|
|
11
8
|
GenerationProgress = Data.define(:event, :scenario, :covered, :total, :cumulative_pct, :uncovered)
|
|
12
9
|
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
10
|
+
# Scenario generation, backed by the Rust engine (Lattice::Engine, built from
|
|
11
|
+
# ext/lattice_native on the lattice-engine crate). The engine builds a covering
|
|
12
|
+
# array greedily, choosing each row from several candidates, then shrinks it
|
|
13
|
+
# with tabu search, and returns the rows ordered so each adds the most new
|
|
14
|
+
# coverage. ConstraintEngine compiles the model into the engine's input; this
|
|
15
|
+
# module applies max_rows and stop_after_coverage by cutting the ordered rows
|
|
16
|
+
# short. Python's lattice.ipog does the same, so both give identical rows.
|
|
17
17
|
module IPOG
|
|
18
18
|
extend self
|
|
19
19
|
|
|
20
|
+
DEFAULT_CANDIDATES = 16
|
|
21
|
+
SHRINK_STEPS = 1_000_000
|
|
22
|
+
PATIENCE = 10_000
|
|
23
|
+
SEED_MODULUS = 2**64
|
|
24
|
+
|
|
25
|
+
# `pool_size` is the number of candidate rows built per greedy step.
|
|
20
26
|
def generate_covering_array(
|
|
21
27
|
model,
|
|
22
28
|
strength: nil,
|
|
23
29
|
seed: 42,
|
|
24
|
-
pool_size:
|
|
30
|
+
pool_size: DEFAULT_CANDIDATES,
|
|
25
31
|
progress: nil,
|
|
26
32
|
max_rows: nil,
|
|
27
33
|
stop_after_coverage: nil
|
|
@@ -33,125 +39,43 @@ module Lattice
|
|
|
33
39
|
coverage_target = coverage_target_for(stop_after_coverage)
|
|
34
40
|
|
|
35
41
|
engine = ConstraintEngine.new(model)
|
|
36
|
-
raise ArgumentError, "Model has no valid full assignments." unless engine.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
record_row = lambda do |row|
|
|
56
|
-
newly_covered = covered_tuples(row, actual_strength, uncovered)
|
|
57
|
-
rows << row
|
|
58
|
-
uncovered.subtract(newly_covered)
|
|
59
|
-
covered = valid_tuples.length - uncovered.length
|
|
60
|
-
cumulative_pct = coverage_pct(covered, valid_tuples.length)
|
|
61
|
-
coverage_points << CoveragePoint.new(scenario: rows.length, cumulative_pct:)
|
|
62
|
-
progress&.call(
|
|
63
|
-
GenerationProgress.new(
|
|
64
|
-
event: "scenario",
|
|
65
|
-
scenario: rows.length,
|
|
66
|
-
covered:,
|
|
67
|
-
total: valid_tuples.length,
|
|
68
|
-
cumulative_pct:,
|
|
69
|
-
uncovered: uncovered.length
|
|
70
|
-
)
|
|
71
|
-
)
|
|
72
|
-
if should_stop?(rows.length, cumulative_pct, max_rows, coverage_target)
|
|
73
|
-
progress&.call(
|
|
74
|
-
GenerationProgress.new(
|
|
75
|
-
event: "stopped",
|
|
76
|
-
scenario: rows.length,
|
|
77
|
-
covered:,
|
|
78
|
-
total: valid_tuples.length,
|
|
79
|
-
cumulative_pct:,
|
|
80
|
-
uncovered: uncovered.length
|
|
81
|
-
)
|
|
82
|
-
)
|
|
83
|
-
true
|
|
84
|
-
else
|
|
85
|
-
false
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
engine.forced_interactions.each do |forced|
|
|
90
|
-
partial = forced.to_h { |parameter_index, value_index| [parameter_index, value_index] }
|
|
91
|
-
row = best_candidate_row(model, engine, partial, uncovered, actual_strength, pool_size, rng)
|
|
92
|
-
next if row.nil? || rows.include?(row)
|
|
93
|
-
|
|
94
|
-
if record_row.call(row)
|
|
95
|
-
limit_reached = true
|
|
96
|
-
break
|
|
97
|
-
end
|
|
42
|
+
raise ArgumentError, "Model has no valid full assignments." unless engine.satisfiable?
|
|
43
|
+
|
|
44
|
+
native = actual_strength == model.strength ? engine.native : engine.native_model(actual_strength)
|
|
45
|
+
rows, new_coverage, total = native.generate(seed % SEED_MODULUS, pool_size, SHRINK_STEPS, PATIENCE)
|
|
46
|
+
|
|
47
|
+
report(progress, "enumerated", 0, 0, total)
|
|
48
|
+
kept = []
|
|
49
|
+
curve = []
|
|
50
|
+
covered = 0
|
|
51
|
+
rows.zip(new_coverage) do |row, gained|
|
|
52
|
+
kept << row.freeze
|
|
53
|
+
covered += gained
|
|
54
|
+
cumulative_pct = coverage_pct(covered, total)
|
|
55
|
+
curve << CoveragePoint.new(scenario: kept.length, cumulative_pct:)
|
|
56
|
+
report(progress, "scenario", kept.length, covered, total)
|
|
57
|
+
next unless should_stop?(kept.length, cumulative_pct, max_rows, coverage_target)
|
|
58
|
+
|
|
59
|
+
report(progress, "stopped", kept.length, covered, total) if kept.length < rows.length
|
|
60
|
+
break
|
|
98
61
|
end
|
|
99
62
|
|
|
100
|
-
|
|
101
|
-
seed_tuple = uncovered.min
|
|
102
|
-
partial = seed_tuple.to_h { |parameter_index, value_index| [parameter_index, value_index] }
|
|
103
|
-
row = best_candidate_row(model, engine, partial, uncovered, actual_strength, pool_size, rng)
|
|
104
|
-
raise "Unable to complete uncovered tuple: #{seed_tuple.inspect}" if row.nil?
|
|
105
|
-
|
|
106
|
-
if record_row.call(row)
|
|
107
|
-
limit_reached = true
|
|
108
|
-
break
|
|
109
|
-
end
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
GenerationResult.new(
|
|
113
|
-
rows: rows.freeze,
|
|
114
|
-
coverage_curve: coverage_points.freeze,
|
|
115
|
-
total_valid_tuples: valid_tuples.length
|
|
116
|
-
)
|
|
63
|
+
GenerationResult.new(rows: kept.freeze, coverage_curve: curve.freeze, total_valid_tuples: total)
|
|
117
64
|
end
|
|
118
65
|
|
|
119
|
-
|
|
120
|
-
tuples = []
|
|
121
|
-
total_candidates = candidate_tuple_count(model, strength)
|
|
122
|
-
checked = 0
|
|
123
|
-
report = lambda do
|
|
124
|
-
progress&.call(
|
|
125
|
-
GenerationProgress.new(
|
|
126
|
-
event: "enumerating",
|
|
127
|
-
scenario: 0,
|
|
128
|
-
covered: checked,
|
|
129
|
-
total: total_candidates,
|
|
130
|
-
cumulative_pct: coverage_pct(checked, total_candidates),
|
|
131
|
-
uncovered: [0, total_candidates - checked].max
|
|
132
|
-
)
|
|
133
|
-
)
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
(0...model.parameters.length).to_a.combination(strength) do |parameter_combo|
|
|
137
|
-
domains = parameter_combo.map { |parameter_index| (0...model.parameters[parameter_index].values.length).to_a }
|
|
138
|
-
domains[0].product(*domains[1..]) do |value_combo|
|
|
139
|
-
checked += 1
|
|
140
|
-
partial = parameter_combo.each_with_index.to_h { |parameter_index, offset| [parameter_index, value_combo[offset]] }
|
|
141
|
-
tuples << parameter_combo.zip(value_combo) if engine.can_complete?(partial)
|
|
142
|
-
report.call if progress && (checked % 1000).zero?
|
|
143
|
-
end
|
|
144
|
-
end
|
|
145
|
-
report.call if progress
|
|
146
|
-
tuples
|
|
147
|
-
end
|
|
66
|
+
private
|
|
148
67
|
|
|
149
|
-
def
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
68
|
+
def report(progress, event, scenario, covered, total)
|
|
69
|
+
progress&.call(
|
|
70
|
+
GenerationProgress.new(
|
|
71
|
+
event:,
|
|
72
|
+
scenario:,
|
|
73
|
+
covered:,
|
|
74
|
+
total:,
|
|
75
|
+
cumulative_pct: coverage_pct(covered, total),
|
|
76
|
+
uncovered: total - covered
|
|
77
|
+
)
|
|
78
|
+
)
|
|
155
79
|
end
|
|
156
80
|
|
|
157
81
|
def coverage_target_for(stop_after_coverage)
|
|
@@ -160,7 +84,6 @@ module Lattice
|
|
|
160
84
|
unless stop_after_coverage.positive? && stop_after_coverage <= 100
|
|
161
85
|
raise ArgumentError, "`stop_after_coverage` must be in the range (0, 100]."
|
|
162
86
|
end
|
|
163
|
-
return nil if stop_after_coverage >= 100
|
|
164
87
|
|
|
165
88
|
stop_after_coverage
|
|
166
89
|
end
|
|
@@ -172,51 +95,9 @@ module Lattice
|
|
|
172
95
|
false
|
|
173
96
|
end
|
|
174
97
|
|
|
175
|
-
|
|
176
|
-
candidates = {}
|
|
177
|
-
deterministic = engine.complete_assignment(partial, rng: nil)
|
|
178
|
-
candidates[deterministic] = score_row(model, deterministic, uncovered, strength) unless deterministic.nil?
|
|
179
|
-
|
|
180
|
-
(pool_size - 1).times do
|
|
181
|
-
attempt_rng = MersenneTwister.new(rng.getrandbits(64))
|
|
182
|
-
candidate = engine.complete_assignment(partial, rng: attempt_rng)
|
|
183
|
-
next if candidate.nil?
|
|
184
|
-
|
|
185
|
-
score = score_row(model, candidate, uncovered, strength)
|
|
186
|
-
previous = candidates[candidate]
|
|
187
|
-
candidates[candidate] = score if previous.nil? || (score <=> previous).positive?
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
return nil if candidates.empty?
|
|
191
|
-
|
|
192
|
-
candidates.max_by { |row, score| [score[0], score[1], reverse_lex(row)] }[0]
|
|
193
|
-
end
|
|
194
|
-
|
|
195
|
-
def score_row(model, row, uncovered, strength)
|
|
196
|
-
newly_covered = covered_tuples(row, strength, uncovered).length
|
|
197
|
-
weight_score = row.each_with_index.inject(0.0) do |acc, (value_index, parameter_index)|
|
|
198
|
-
acc + model.parameters[parameter_index].normalized_weights[value_index]
|
|
199
|
-
end
|
|
200
|
-
[newly_covered, weight_score]
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
def covered_tuples(row, strength, candidate_tuples)
|
|
204
|
-
covered = Set.new
|
|
205
|
-
(0...row.length).to_a.combination(strength) do |parameter_combo|
|
|
206
|
-
tuple_key = parameter_combo.map { |parameter_index| [parameter_index, row[parameter_index]] }
|
|
207
|
-
covered.add(tuple_key) if candidate_tuples.include?(tuple_key)
|
|
208
|
-
end
|
|
209
|
-
covered
|
|
210
|
-
end
|
|
211
|
-
|
|
98
|
+
# A satisfiable model always has valid tuples, so total_count is positive.
|
|
212
99
|
def coverage_pct(covered_count, total_count)
|
|
213
|
-
return 100.0 if total_count.zero?
|
|
214
|
-
|
|
215
100
|
((covered_count.to_f / total_count) * 100).round(1, half: :even)
|
|
216
101
|
end
|
|
217
|
-
|
|
218
|
-
def reverse_lex(row)
|
|
219
|
-
row.map(&:-@)
|
|
220
|
-
end
|
|
221
102
|
end
|
|
222
103
|
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Loads the Rust engine (Lattice::Engine). Precompiled gems carry one build per
|
|
4
|
+
# Ruby version (lib/lattice/3.4/...); a gem compiled on install has a single one
|
|
5
|
+
# next to this file.
|
|
6
|
+
begin
|
|
7
|
+
require_relative "#{RUBY_VERSION[/\A\d+\.\d+/]}/lattice_native"
|
|
8
|
+
rescue LoadError
|
|
9
|
+
require_relative "lattice_native"
|
|
10
|
+
end
|
data/lib/lattice/parser.rb
CHANGED
|
@@ -88,6 +88,9 @@ module Lattice
|
|
|
88
88
|
errors = []
|
|
89
89
|
loaders.each do |loader|
|
|
90
90
|
return loader == :json ? SchemaText.load_json(text) : SchemaText.load_yaml(text)
|
|
91
|
+
rescue SchemaText::TooLarge => e
|
|
92
|
+
# Not a syntax error, so the other loader's complaint would only mislead.
|
|
93
|
+
raise ModelIOError, "Unable to parse model from `#{source_name}`: #{e.message}"
|
|
91
94
|
rescue StandardError => e
|
|
92
95
|
errors << e.message
|
|
93
96
|
end
|
data/lib/lattice/schema_text.rb
CHANGED
|
@@ -21,6 +21,18 @@ module Lattice
|
|
|
21
21
|
|
|
22
22
|
class SyntaxError < StandardError; end
|
|
23
23
|
|
|
24
|
+
# Raised when YAML aliases would expand the schema past MAX_VALUES values
|
|
25
|
+
# (the "billion laughs" pattern). Every value counts once per place it
|
|
26
|
+
# appears after expansion, so the limit holds however the aliases nest.
|
|
27
|
+
class TooLarge < StandardError; end
|
|
28
|
+
|
|
29
|
+
MAX_VALUES = 100_000
|
|
30
|
+
TOO_LARGE = "schema expands to more than #{MAX_VALUES} values (check YAML aliases)".freeze
|
|
31
|
+
|
|
32
|
+
# Anchors seen so far, as anchor => [value, size] once built (nil while
|
|
33
|
+
# still being built), and the running count of expanded values.
|
|
34
|
+
Expansion = Struct.new(:anchors, :count)
|
|
35
|
+
|
|
24
36
|
TRUE_WORDS = %w[yes Yes YES true True TRUE on On ON].freeze
|
|
25
37
|
FALSE_WORDS = %w[no No NO false False FALSE off Off OFF].freeze
|
|
26
38
|
NULL_WORDS = ["", "~", "null", "Null", "NULL"].freeze
|
|
@@ -57,7 +69,7 @@ module Lattice
|
|
|
57
69
|
raise SyntaxError, "expected a single YAML document, found #{documents.length}" if documents.length > 1
|
|
58
70
|
return nil if documents.empty?
|
|
59
71
|
|
|
60
|
-
yaml_value(documents.first.root, {})
|
|
72
|
+
yaml_value(documents.first.root, Expansion.new({}, 0))
|
|
61
73
|
end
|
|
62
74
|
|
|
63
75
|
private
|
|
@@ -71,31 +83,39 @@ module Lattice
|
|
|
71
83
|
end
|
|
72
84
|
end
|
|
73
85
|
|
|
74
|
-
#
|
|
75
|
-
#
|
|
76
|
-
def yaml_value(node,
|
|
77
|
-
return yaml_alias(node,
|
|
86
|
+
# An anchor still being built (nil in `anchors`) means an alias points
|
|
87
|
+
# back into itself.
|
|
88
|
+
def yaml_value(node, expansion)
|
|
89
|
+
return yaml_alias(node, expansion) if node.is_a?(Psych::Nodes::Alias)
|
|
78
90
|
|
|
79
|
-
|
|
91
|
+
start = expansion.count
|
|
92
|
+
add_values(expansion, 1)
|
|
93
|
+
expansion.anchors[node.anchor] = nil if node.anchor
|
|
80
94
|
value =
|
|
81
95
|
case node
|
|
82
96
|
when Psych::Nodes::Scalar then yaml_scalar(node)
|
|
83
97
|
when Psych::Nodes::Sequence
|
|
84
98
|
check_tag(node.tag, SEQUENCE_TAGS)
|
|
85
|
-
node.children.map { |child| yaml_value(child,
|
|
86
|
-
when Psych::Nodes::Mapping then yaml_mapping(node,
|
|
99
|
+
node.children.map { |child| yaml_value(child, expansion) }
|
|
100
|
+
when Psych::Nodes::Mapping then yaml_mapping(node, expansion)
|
|
87
101
|
end
|
|
88
|
-
anchors[node.anchor] = [value] if node.anchor
|
|
102
|
+
expansion.anchors[node.anchor] = [value, expansion.count - start] if node.anchor
|
|
89
103
|
value
|
|
90
104
|
end
|
|
91
105
|
|
|
92
|
-
def yaml_alias(node,
|
|
93
|
-
built = anchors.fetch(node.anchor) { raise SyntaxError, "unknown alias `*#{node.anchor}`" }
|
|
106
|
+
def yaml_alias(node, expansion)
|
|
107
|
+
built = expansion.anchors.fetch(node.anchor) { raise SyntaxError, "unknown alias `*#{node.anchor}`" }
|
|
94
108
|
raise SyntaxError, "recursive alias" if built.nil?
|
|
95
109
|
|
|
110
|
+
add_values(expansion, built.last)
|
|
96
111
|
built.first
|
|
97
112
|
end
|
|
98
113
|
|
|
114
|
+
def add_values(expansion, values)
|
|
115
|
+
expansion.count += values
|
|
116
|
+
raise TooLarge, TOO_LARGE if expansion.count > MAX_VALUES
|
|
117
|
+
end
|
|
118
|
+
|
|
99
119
|
def yaml_scalar(node)
|
|
100
120
|
check_tag(node.tag, [nil, "!"] + SCALAR_TAGS)
|
|
101
121
|
return node.value unless node.style == Psych::Nodes::Scalar::PLAIN && !STR_TAGS.include?(node.tag)
|
|
@@ -108,20 +128,20 @@ module Lattice
|
|
|
108
128
|
|
|
109
129
|
# Keys from `<<` merges come first, earlier sources winning; the
|
|
110
130
|
# mapping's own keys then override them. A key written twice is an error.
|
|
111
|
-
def yaml_mapping(node,
|
|
131
|
+
def yaml_mapping(node, expansion)
|
|
112
132
|
check_tag(node.tag, MAPPING_TAGS)
|
|
113
133
|
merged = {}
|
|
114
134
|
own = {}
|
|
115
135
|
seen_merge = false
|
|
116
136
|
node.children.each_slice(2) do |key_node, value_node|
|
|
117
|
-
key = yaml_value(key_node,
|
|
137
|
+
key = yaml_value(key_node, expansion)
|
|
118
138
|
raise SyntaxError, "mapping keys must be scalars" if key.is_a?(Enumerable)
|
|
119
139
|
|
|
120
140
|
if merge_key?(key_node)
|
|
121
141
|
raise SyntaxError, "duplicate key `<<`" if seen_merge
|
|
122
142
|
|
|
123
143
|
seen_merge = true
|
|
124
|
-
merge_sources(yaml_value(value_node,
|
|
144
|
+
merge_sources(yaml_value(value_node, expansion)).each do |source|
|
|
125
145
|
source.each { |merged_key, merged_value| merged[merged_key] = merged_value unless merged.key?(merged_key) }
|
|
126
146
|
end
|
|
127
147
|
next
|
|
@@ -129,7 +149,7 @@ module Lattice
|
|
|
129
149
|
|
|
130
150
|
raise SyntaxError, "duplicate key `#{Lattice.normalize_scalar(key)}`" if own.key?(key)
|
|
131
151
|
|
|
132
|
-
own[key] = yaml_value(value_node,
|
|
152
|
+
own[key] = yaml_value(value_node, expansion)
|
|
133
153
|
end
|
|
134
154
|
merged.merge(own)
|
|
135
155
|
end
|
data/lib/lattice/version.rb
CHANGED