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

214
LINES

< > BotCompany Repo | #1009150 // A. I. Game 7.1 / Haar-Like Features [dev.]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 3874K of libraries. Click here for Pure Java version (8691L/60K/213K).

!7

static int features = 1;

!include #1009151 // AI Game & API

p {
  newImageText = "New Letter!";
  makeInstruction();
  pGame();
  swing {
    setFrameTitle(is, "A. I. Game 7");
    final JSpinner spinner = jSpinner(features, 1, 100);
    addToWindowPack(is, withMargin(jRightAligned(withLabel("Number of features to use:", spinner))));
    onChange(spinner, r {
      features = intFromSpinner(spinner);
      makeInstruction();
      restartAIs();
    });
  }
}

svoid makeInstruction {
  setInstruction("Reproduce this image with " + n(features, "Haar-like feature") + ":");
}

sclass Haar {
  Rect black, white;
  
  *() {}
  *(Rect *black, Rect *white) {}
  
  bool vertical() { ret black.x == white.x; }
}

sclass Submission {
  new L<Haar> features;
  
  *() {}
  *(Haar... features) { this.features = asList(features); check(); }
  *(L<Haar> *features) { check(); }
  
  void check {
    assertEquals(main.features, l(features));
  }
}

//////////////////
// PUZZLE MAKER //
//////////////////

sS lastLetter;

static RGBImage makeImage() {
  //ret loadRGBImage(/*#1006930*/#1006944);
  S letter;
  do {
    letter = "" + randomCharBetween('A', 'Z');
  } while (eq(letter, lastLetter));
  lastLetter = letter;
  renderText_withLeading = false;
  ret new RGBImage(renderText(#1004568, 100, letter));
}

////////////////
// FORM MAKER //
////////////////

static JComponent makeTheForm(final GameForAI game) {
  null;
  // TODO
}

static Pt ptFromForm(JTextField x, JTextField y) {
  ret new Pt(intFromTextField(x), intFromTextField(y));
}

static Pt ptFromForm(JSpinner x, JSpinner y) {
  ret new Pt(intFromSpinner(x), intFromSpinner(y));
}

///////////////
// RENDERERS //
///////////////

static RGBImage renderImage(Submission s) {
  ret new RGBImage(renderImage1(s));
}

static BufferedImage renderImage1(Submission s) {
  BufferedImage bi = newBufferedImage(w, h, Color.gray);
  for (Haar h : s.features)
    paintHaar(bi, h.black, h.white);
  ret bi;
}

static RGBImage renderWithHints(Submission s) {
  ret new RGBImage(renderImage1(s));
}

//////////////////////////////////////
// Test AIs. Just add your own here //
//////////////////////////////////////

AI > AI_Random {
  new Best<Submission> best;
  
  Haar randomFeature() {
    Rect r = randomRect(image());
    Pair<Rect> p;
    if (tossCoin()) {
      r.h &= ~1; // make even
      p = splitRectInVerticalHalves(r);
    } else {
      r.w &= ~1; // make even
      p = splitRectInHorizontalHalves(r);
    }
    if (oneInTwoChance()) p = reversePair(p);
    ret new Haar(p.a, p.b);
  }
  
  void go {
    //print("Round: " + round());
    if (round() == 1 && best.has()) // TODO: automate this
      submit(best.get());
    else {
      Submission guess = guess();
      updateBest(guess, submit(guess));
    }
  }
  
  void updateBest(Submission guess, double score) {
    best.put(guess, score);
  }
  
  Submission guess() {
    ret new Submission(produceN(func { randomFeature() }, features));
  }
}

AI_Random > AI_RandomWithVariation {
  int n, range = 10;

  Submission guess() {
    if (odd(n++) && best.has())
      ret vary(best.get());
    else
      ret super.guess();
  }
  
  Submission vary(Submission s) {
    s = cloneThroughStructure(s);
    varyFeature(random(s.features));
    ret s;
  }
  
  void varyFeature(Haar h) {
    bool v = h.vertical();
    if (v)
      h.white.w = h.black.w = varyWidth(h.black);
    else
      h.white.h = h.black.h = varyHeight(h.black);
  }
  
  int varyWidth(Rect r) {
    ret max(r.x+1, min(w, random(r.x2()-range, r.x2()+range+1)))-r.x;
  }
  
  int varyHeight(Rect r) {
    ret max(r.y+1, min(h, random(r.y2()-range, r.y2()+range+1)))-r.y;
  }
}

/*
AI > AI_Racer {
  AI_RandomWithVariation leader, overtaker;
  new Best<Submission> leadersBest;
  int discardEvery = 5000;
  int roundsSinceChange;
  
  AI_RandomWithVariation newAI() { ret new AI_RandomWithVariation; }
  
  void go {
    if (round() == 1 && leadersBest.has()) // TODO: automate this
      submit(leadersBest.get());
    else if (tossACoin()) {
      if (leader == null) leader = newAI();
      initSubAI(leader);
      Submission guess = leader.guess();
      double score = submit(guess);
      leader.updateBest(guess, score);
      leadersBest.put(guess, score);
    } else {
      if (overtaker == null) overtaker = newAI();
      initSubAI(overtaker);
      Submission guess = overtaker.guess();
      double score = submit(guess);
      overtaker.updateBest(guess, score);
      if (score > leadersBest.score()) {
        // displace leader!
        print("Overtake at " + formatScore(overtaker.best.score()) + " vs " + formatScore(leadersBest.score()));
        leader = overtaker;
        overtaker = null;
        roundsSinceChange = 0;
      } else if (roundsSinceChange++ >= discardEvery) {
        // make new overtaker
        print("Discarding overtaker at " + formatScore(overtaker.best.score()) + " vs " + formatScore(leadersBest.score()));
        overtaker = null;
        roundsSinceChange = 0;
      }
    }
  }
}
*/

Author comment

Began life as a copy of #1006945

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, iveijnkanddl, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1009150
Snippet name: A. I. Game 7.1 / Haar-Like Features [dev.]
Eternal ID of this version: #1009150/10
Text MD5: 3e6598eb8b269efcc8f0b60baf410b53
Transpilation MD5: 0151c06778adf64a40697f6567439b22
Author: stefan
Category: javax / gui / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-07-06 01:23:18
Source code size: 5251 bytes / 214 lines
Pitched / IR pitched: No / No
Views / Downloads: 346 / 505
Version history: 9 change(s)
Referenced in: [show references]