1 | // A = compressor type (e.g. s), Data = data type |
2 | persistable sclass CompressionSearch_AnyType<A, Data> extends Probabilistic { |
3 | replace Regime with CompressionRegime_AnyType<A>. |
4 | |
5 | // user-set |
6 | |
7 | settable Regime regime; |
8 | gettable Data inputData; |
9 | settable long inputSize; // in bytes |
10 | |
11 | event onNewBest; |
12 | event onDisqualified(Submission s); |
13 | |
14 | // public output variables |
15 | |
16 | new Best<Submission> best; |
17 | new L<AbstractCompressor_AnyType> strategies; |
18 | |
19 | *() { init(); } |
20 | *(Regime *regime) { init(); } |
21 | *(Regime *regime, Data *inputData, long *inputSize) { init(); } |
22 | |
23 | void init { |
24 | best.onChange = r newBest; |
25 | } |
26 | |
27 | // can submit concurrently! |
28 | Submission submit(A compression, O notes default null) { |
29 | ret _submit(compression, notes); |
30 | } |
31 | |
32 | // private (although Submission is returned) |
33 | |
34 | class Submission { |
35 | settable O notes; |
36 | |
37 | toString { |
38 | if (!correct()) ret linesLL("BAD"); |
39 | |
40 | ret linesLL( |
41 | (isHardChecked() ? " HARD " : "MAYBE ") + ifloor(score()*100), |
42 | "Exact compression ratio: " + scoreRatio(), |
43 | "Code:", |
44 | indentx(shorten(500, str(decompressor()))), |
45 | "Code as bytes:", |
46 | indentx(shorten(500, bytesToHex(compressed()))) |
47 | ); |
48 | } |
49 | |
50 | void setDecompressor(A decompressor) { |
51 | this.decompressor_cache = decompressor; |
52 | } |
53 | |
54 | Data inputData() { ret inputData; } |
55 | |
56 | simplyCached byte[] compressed() { |
57 | ret decompressor_cache == null ?: regime.decompressorToBytes(decompressor_cache); |
58 | } |
59 | |
60 | simplyCached A decompressor() { |
61 | ret compressed_cache == null ?: regime.decompressorFromBytes(compressed_cache); |
62 | } |
63 | |
64 | simplyCached S decompressor_str() { |
65 | ret strOrNull(decompressor()); |
66 | } |
67 | |
68 | int compressedSize aka nBytes() { ret l(compressed()); } |
69 | |
70 | simplyCached Ratio scoreRatio() { |
71 | ret Ratio(inputSize(), compressedSize()); |
72 | } |
73 | |
74 | simplyCached double score() { |
75 | ret scoreRatio()!; |
76 | } |
77 | |
78 | bool decompressed; |
79 | O decompressedValue; |
80 | |
81 | O decompressed() { |
82 | if (!decompressed) { |
83 | decompressedValue = regime.runDecompressor(decompressor()); |
84 | set decompressed; |
85 | } |
86 | ret decompressedValue; |
87 | } |
88 | |
89 | bool checkUnder(Regime regime) { |
90 | ret checkResult(regime.runDecompressor(decompressor())); |
91 | } |
92 | |
93 | simplyCached bool isHardChecked() { |
94 | if (!correct()) false; |
95 | |
96 | var nextRegime = regime.harderRegime(); |
97 | if (nextRegime == null) true; |
98 | ret checkUnder(nextRegime); |
99 | } |
100 | |
101 | bool checkResult(O decompressed) { |
102 | ret eq(toByteList(decompressed), inputData()); |
103 | } |
104 | |
105 | simplyCached Bool correct() { |
106 | ret eq(toByteList(decompressed()), inputData()); |
107 | } |
108 | } // end of Submission |
109 | |
110 | Submission _submit(A compression, O notes) { |
111 | if (compression == null) null; |
112 | print("Received submission"); |
113 | print("Code: " + takeFirst(500, str(compression))); |
114 | new Submission s; |
115 | s.notes(notes); |
116 | print("Notes: " + takeFirst(500, str(notes))); |
117 | s.setDecompressor(compression); |
118 | print("Size: " + nBytes(s.nBytes())); |
119 | if (s.score() > best.score()) |
120 | if (!s.correct()) { |
121 | warn("Compressor didn't verify"); |
122 | disqualified(s); |
123 | } else { |
124 | print("Score: " + s.score()); |
125 | best.put(s, s.score(); |
126 | } |
127 | ret s; |
128 | } |
129 | |
130 | Submission bestSubmission() { |
131 | methodForEach flush(strategies); |
132 | ret best!; |
133 | } |
134 | |
135 | Submission bestHardSubmission() { |
136 | var s = best!; |
137 | if (s == null || !s.isHardChecked()) null; |
138 | ret s; |
139 | } |
140 | |
141 | A bestCompression aka get() { |
142 | var s = bestSubmission(); |
143 | ret s?.decompressor(); |
144 | } |
145 | |
146 | bool has() { ret bestSubmission() != null; } |
147 | |
148 | void addStrategy(AbstractCompressor_AnyType<A, Data> compressor) { |
149 | if (compressor == null) ret; |
150 | initAction(compressor); |
151 | compressor.setSearch(this); |
152 | strategies.add(compressor); |
153 | compressor.run(); |
154 | } |
155 | |
156 | run {} |
157 | } |
Began life as a copy of #1033158
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1033352 |
Snippet name: | CompressionSearch_AnyType - receives and tests the submissions [backup] |
Eternal ID of this version: | #1033352/1 |
Text MD5: | ac0255fdcda5758a4d5d2324c5764ac9 |
Author: | stefan |
Category: | javax / recognition by compression |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-10-25 01:00:02 |
Source code size: | 4076 bytes / 157 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 117 / 136 |
Referenced in: | [show references] |