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

221
LINES

< > BotCompany Repo | #1009159 // A. I. Game 7.1 v2 / Haar-Like Features, Fixed Size [dev.]

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

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

!7

static int features = 1;
static int hWidth = 10, hHeight = 10;

!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;
    Pair<Rect> p;
    if (tossCoin()) {
      r = randomRect(image(), hWidth, hHeight*2);
      p = splitRectInVerticalHalves(r);
    } else {
      r = randomRect(image(), hHeight*2, hWidth);
      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) {
    /*int deltaX = random(-range, range+1), deltaY = random(-range, range+1);
    Rect a = translateRect(h.black, deltaX, deltaY);
    Rect b = translateRect(h.white, deltaX, deltaY);
    if (rectContains(imageRect(), a) && rectContains(imageRect(), b)) {
      h.black = a;
      h.white = b;
    }*/
    Rect r = rectUnion(h.black, h.white);
    Rect r2 = randomRect(image(), r.w, r.h);
    h.black = translateRect(h.black, r2.x-r.x, r2.y-r.y);
    h.white = translateRect(h.white, r2.x-r.x, r2.y-r.y);
  }
  
  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 #1009150

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

-
Snippet ID: #1009159
Snippet name: A. I. Game 7.1 v2 / Haar-Like Features, Fixed Size [dev.]
Eternal ID of this version: #1009159/8
Text MD5: 9cd59d7ef44ee57ab0f6207c7e984225
Transpilation MD5: 3733c876b7ae2177431a19cba2d5702c
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-07 13:17:51
Source code size: 5671 bytes / 221 lines
Pitched / IR pitched: No / No
Views / Downloads: 488 / 629
Version history: 7 change(s)
Referenced in: