latticerb 0.3.0 → 0.4.0
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/agent.rb +52 -1
- 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 +34 -2
- data/lib/lattice/resources/claude/lattice-workflow/SKILL.md +3 -1
- data/lib/lattice/resources/codex/lattice-workflow/SKILL.md +3 -1
- data/lib/lattice/resources/generic/lattice-workflow/SKILL.md +3 -1
- 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
|
@@ -18,6 +18,17 @@ module Lattice
|
|
|
18
18
|
module Parser
|
|
19
19
|
extend self
|
|
20
20
|
|
|
21
|
+
# Each constraint type and the keys written beside `type`.
|
|
22
|
+
CONSTRAINT_FIELDS = {
|
|
23
|
+
"invalid_pair" => "`if` and `then_not`",
|
|
24
|
+
"bidirectional" => "`pair`",
|
|
25
|
+
"forward_dep" => "`source` and `target`",
|
|
26
|
+
"conditional" => "`parameter`, `values`, `parent` and `applies_when`",
|
|
27
|
+
"forced" => "`assignments`",
|
|
28
|
+
"higher_order" => "`if_all` and `then_not`"
|
|
29
|
+
}.freeze
|
|
30
|
+
CONSTRAINT_TYPE_LIST = CONSTRAINT_FIELDS.keys.map { |name| "`#{name}`" }.join(", ").freeze
|
|
31
|
+
|
|
21
32
|
def load_model(path, strength_override: nil)
|
|
22
33
|
text, source_name, extension = read_model_input(path)
|
|
23
34
|
data = deserialize_model(text, source_name, extension)
|
|
@@ -177,7 +188,17 @@ module Lattice
|
|
|
177
188
|
next
|
|
178
189
|
end
|
|
179
190
|
|
|
180
|
-
|
|
191
|
+
constraint_type = raw_constraint["type"]
|
|
192
|
+
if constraint_type.nil?
|
|
193
|
+
errors << missing_type_message(index, raw_constraint)
|
|
194
|
+
next
|
|
195
|
+
end
|
|
196
|
+
unless constraint_type.is_a?(String)
|
|
197
|
+
errors << "`constraints[#{index}].type` must be a string, one of: #{CONSTRAINT_TYPE_LIST}."
|
|
198
|
+
next
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
case constraint_type
|
|
181
202
|
when "invalid_pair"
|
|
182
203
|
if_assignment = parse_assignment(raw_constraint["if"], parameters, "`constraints[#{index}].if`", errors)
|
|
183
204
|
then_not = parse_assignment(raw_constraint["then_not"], parameters, "`constraints[#{index}].then_not`", errors)
|
|
@@ -205,12 +226,23 @@ module Lattice
|
|
|
205
226
|
constraint = parse_higher_order_constraint(raw_constraint, index, parameters, errors)
|
|
206
227
|
parsed << constraint if constraint
|
|
207
228
|
else
|
|
208
|
-
errors << "`constraints[#{index}]` has unknown type `#{
|
|
229
|
+
errors << "`constraints[#{index}]` has unknown type `#{constraint_type}`. Use one of: #{CONSTRAINT_TYPE_LIST}."
|
|
209
230
|
end
|
|
210
231
|
end
|
|
211
232
|
parsed
|
|
212
233
|
end
|
|
213
234
|
|
|
235
|
+
# Explains a constraint without `type`. A type name used as a key, as in
|
|
236
|
+
# `- invalid_pair: {...}`, gets the correct form spelled out.
|
|
237
|
+
def missing_type_message(index, raw_constraint)
|
|
238
|
+
shorthand = raw_constraint.keys.find { |key| CONSTRAINT_FIELDS.key?(key) }
|
|
239
|
+
return "`constraints[#{index}]` is missing `type`. Use one of: #{CONSTRAINT_TYPE_LIST}." if shorthand.nil?
|
|
240
|
+
|
|
241
|
+
"`constraints[#{index}]` is missing `type`. Write `type: #{shorthand}` as its own key, " \
|
|
242
|
+
"with #{CONSTRAINT_FIELDS.fetch(shorthand)} beside it. " \
|
|
243
|
+
"Run `lattice agent instructions` for an example of each type."
|
|
244
|
+
end
|
|
245
|
+
|
|
214
246
|
def parse_conditional_constraint(raw_constraint, index, parameters, errors)
|
|
215
247
|
parent = Lattice.normalize_scalar(raw_constraint["parent"])
|
|
216
248
|
parameter_name = Lattice.normalize_scalar(raw_constraint["parameter"])
|
|
@@ -35,7 +35,9 @@ parameters:
|
|
|
35
35
|
payment: [card, apple_pay, paypal]
|
|
36
36
|
cart: [empty, single, multi]
|
|
37
37
|
constraints:
|
|
38
|
-
-
|
|
38
|
+
- type: invalid_pair
|
|
39
|
+
if: { parameter: payment, value: apple_pay }
|
|
40
|
+
then_not: { parameter: cart, value: empty }
|
|
39
41
|
```
|
|
40
42
|
|
|
41
43
|
One generated row (JSON):
|
|
@@ -35,7 +35,9 @@ parameters:
|
|
|
35
35
|
payment: [card, apple_pay, paypal]
|
|
36
36
|
cart: [empty, single, multi]
|
|
37
37
|
constraints:
|
|
38
|
-
-
|
|
38
|
+
- type: invalid_pair
|
|
39
|
+
if: { parameter: payment, value: apple_pay }
|
|
40
|
+
then_not: { parameter: cart, value: empty }
|
|
39
41
|
```
|
|
40
42
|
|
|
41
43
|
One generated row (JSON):
|
|
@@ -35,7 +35,9 @@ parameters:
|
|
|
35
35
|
payment: [card, apple_pay, paypal]
|
|
36
36
|
cart: [empty, single, multi]
|
|
37
37
|
constraints:
|
|
38
|
-
-
|
|
38
|
+
- type: invalid_pair
|
|
39
|
+
if: { parameter: payment, value: apple_pay }
|
|
40
|
+
then_not: { parameter: cart, value: empty }
|
|
39
41
|
```
|
|
40
42
|
|
|
41
43
|
One generated row (JSON):
|
data/lib/lattice/version.rb
CHANGED
data/lib/lattice.rb
CHANGED
|
@@ -57,7 +57,7 @@ module Lattice
|
|
|
57
57
|
#
|
|
58
58
|
# run = Lattice.generate(file: "spec/fixtures/checkout.yaml")
|
|
59
59
|
# run.scenarios.each { |row| ... }
|
|
60
|
-
def generate(source = nil, file: nil, strength: nil, seed: 42, pool_size:
|
|
60
|
+
def generate(source = nil, file: nil, strength: nil, seed: 42, pool_size: 16,
|
|
61
61
|
max_rows: nil, stop_after_coverage: nil, progress: nil)
|
|
62
62
|
model = parse_model(source, file:, strength:)
|
|
63
63
|
result = IPOG.generate_covering_array(
|
|
@@ -80,7 +80,7 @@ module Lattice
|
|
|
80
80
|
def validate!(source = nil, file: nil, strength: nil)
|
|
81
81
|
model = parse_model(source, file:, strength:)
|
|
82
82
|
engine = ConstraintEngine.new(model)
|
|
83
|
-
raise ValidationError, ["Model has no valid complete assignments."] unless engine.
|
|
83
|
+
raise ValidationError, ["Model has no valid complete assignments."] unless engine.satisfiable?
|
|
84
84
|
|
|
85
85
|
model
|
|
86
86
|
end
|