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

157
LINES

< > BotCompany Repo | #1033352 // CompressionSearch_AnyType - receives and tests the submissions [backup]

JavaX fragment (include)

// 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
  
  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(
        (isHardChecked() ? " HARD " : "MAYBE ") + 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 bool isHardChecked() {
      if (!correct()) false;
      
      var nextRegime = regime.harderRegime();
      if (nextRegime == null) true;
      ret checkUnder(nextRegime);
    }

    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!;
  }
  
  Submission bestHardSubmission() {
    var s = best!;
    if (s == null || !s.isHardChecked()) null;
    ret s;
  }
  
  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.run();
  }
  
  run {}
}

Author comment

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: 115 / 134
Referenced in: [show references]