Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

160
LINES

< > BotCompany Repo | #1033158 // CompressionSearch_AnyType - receives and tests the submissions

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (7966L/47K).

// A = compressor type (e.g. s), Data = data type
persistable sclass CompressionSearch_AnyType<A, Data> extends Probabilistic {
  replace Regime with CompressionRegime_AnyType<A>.
  
  // user-set
  
  settable Regime regime;
  gettable Data inputData;
  settable long inputSize; // in bytes
  settable bool doFullTesting; // go full javaEval [VERY SLOW]
  
  event onNewBest;
  event onDisqualified(Submission s);

  // public output variables
  
  new Best<Submission> best;
  new L<AbstractCompressor_AnyType> strategies;

  *() { init(); }
  *(Regime *regime) { init(); }
  *(Regime *regime, Data *inputData, long *inputSize) { init(); }
  
  void init {
    best.onChange = r newBest;
  }

  // can submit concurrently!
  Submission submit(A compression, O notes default null) {
    ret _submit(compression, notes);
  }
  
  // private (although Submission is returned)
  
  class Submission {
    settable O notes;
    
    toString {
      if (!correct()) ret linesLL("BAD");
      
      ret linesLL(
        (isTrue(isFullChecked()) ? "FULLCHECKED"
                                 : "           ") + " " + ifloor(score()*100),
        "Exact compression ratio: " + scoreRatio(),
        "Code:",
        indentx(shorten(500, str(decompressor()))),
        "Code as bytes:",
        indentx(shorten(500, bytesToHex(compressed())))
      );
    }
    
    void setDecompressor(A decompressor) {
      this.decompressor_cache = decompressor;
    }
    
    Data inputData() { ret inputData; }

    simplyCached byte[] compressed() {
      ret decompressor_cache == null ?: regime.decompressorToBytes(decompressor_cache);
    }
    
    simplyCached A decompressor() {
      ret compressed_cache == null ?: regime.decompressorFromBytes(compressed_cache);
    }
    
    simplyCached S decompressor_str() {
      ret strOrNull(decompressor());
    }
    
    int compressedSize aka nBytes() { ret l(compressed()); }
    
    simplyCached Ratio scoreRatio() {
      ret Ratio(inputSize(), compressedSize());
    }
    
    simplyCached double score() {
      ret scoreRatio()!;
    }
    
    bool decompressed;
    O decompressedValue;
    
    O decompressed() {
      if (!decompressed) {
        decompressedValue = regime.runDecompressor(decompressor());
        set decompressed;
      }
      ret decompressedValue;
    }
    
    bool checkUnder(Regime regime) {
      ret checkResult(regime.runDecompressor(decompressor()));
    }

    simplyCached TripleBool isFullChecked() {
      if (!isTrue(correct())) false;

      var fullRegime = fullRegime();
      if (fullRegime == null) true;
      ret checkUnder(fullRegime);
    }

    bool checkResult(O decompressed) {
      ret eq(toByteList(decompressed), inputData());
    }
    
    simplyCached Bool correct() {
      ret eq(toByteList(decompressed()), inputData());
    }
  } // end of Submission
  
  Submission _submit(A compression, O notes) {
    if (compression == null) null;
    print("Received submission");
    print("Code: " + takeFirst(500, str(compression)));
    new Submission s;
    s.notes(notes);
    print("Notes: " + takeFirst(500, str(notes)));
    s.setDecompressor(compression);
    print("Size: " + nBytes(s.nBytes()));
    if (s.score() > best.score())
      if (!s.correct()) {
        warn("Compressor didn't verify");
        disqualified(s);
      } else {
        print("Score: " + s.score());
        best.put(s, s.score();
      }
    ret s;
  }
  
  Submission bestSubmission() {
    methodForEach flush(strategies);
    ret best!;
  }
  
  A bestCompression aka get() {
    var s = bestSubmission();
    ret s?.decompressor();
  }
  
  bool has() { ret bestSubmission() != null; }
  
  void addStrategy(AbstractCompressor_AnyType<A, Data> compressor) {
    if (compressor == null) ret;
    initAction(compressor);
    compressor.setSearch(this);
    strategies.add(compressor);
    compressor.step0();
  }
  
  swappable CompressionRegime_AnyType fullRegime() {  
    if (doFullTesting)
      ret regime.fullRegime();
    null;
  }
  
  // actually not needed, addStrategy schedules a strategy immediately
  public void step0 {}
}

Author comment

Began life as a copy of #1033083

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033158
Snippet name: CompressionSearch_AnyType - receives and tests the submissions
Eternal ID of this version: #1033158/37
Text MD5: 4f8991c2a5dd3b3ad2f4952ee8cc21ec
Transpilation MD5: d78d1ab93224b8674ab08b263520948d
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-27 00:00:41
Source code size: 4291 bytes / 160 lines
Pitched / IR pitched: No / No
Views / Downloads: 251 / 464
Version history: 36 change(s)
Referenced in: [show references]