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

127
LINES

< > BotCompany Repo | #1001883 // Image rendering from random scripts v3, with metastrategy, developing

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

Libraryless. Click here for Pure Java version (1583L/12K/34K).

!752

!include #1000522 // image helper functions

static RGBImage targetImg;

static class Runner {
  S strategy;
  S bestScript;
  double score;
  long tries;
}

static ImageSurface imageSurface;

static L<S> strategies = litlist(
  "random;",
  "random; vary;"
);

static S metaStrategy = "race strategies to 100%";

static double score, targetScore;
static new L<Runner> runners;
static boolean warned;
static long startTime;

p {
  targetImg = new RGBImage(200, 200, Color.blue);
  
  startTime = now();
  new Matches m;
  if (jmatch("race strategies to *%", metaStrategy, m)) {
    targetScore = parseDouble(m.get(0));
    makeRunners();
    while (score < targetScore) {
      // TODO: fair time sharing?
      for (Runner r : runners)
        step(r);
    }
  } else
    fail("Unknown meta strategy: " + metaStrategy);
}

static void step(Runner r) {
  for (L<S> s : statements(r.strategy)) {
    if (jmatch("random;", s)) {
      S scriptSchema = "fill(<color1>);";
      tryScript(r, makeScript(scriptSchema));
    } else if (jmatch("vary;", s)) {
      if (r.bestScript != null) {
        S variation = varyScript(r.bestScript);
        if (variation != null && neq(r.bestScript, variation))
          tryScript(r, variation);
      }
    } else if (!warned) {
      print("Unknown strategy statement: " + join(s));
      warned = true;
    }
  }
}

static S varyScript(S script) {
  L<S> tok = javaTok(script);
  for (int i = 1; i < l(tok); i += 2)
    if (isQuoted(tok.get(i))) {
      S s = unquote(tok.get(i));
      tok.set(i, quote(varyColor(s)));
    }
  ret join(tok);
}

static S varyColor(S color) {
  ret varyColor(new RGB(color)).toString();
}

static void tryScript(Runner r, S script) {
  ++r.tries;
  //print ("Script: " + script);
  RGBImage img = new RGBImage(200, 200, Color.white);
  render(img, script);
  double newScore = diffToPercent(diff(img, targetImg));
  if (newScore > r.score) {
    r.score = newScore;
    r.bestScript = script;
    if (newScore > score) {
      score = newScore; // global score
      imageSurface = showImage(img, imageSurface);
      print("Try " + r.tries + " of runner " + (runners.indexOf(r)+1) + " after " + (now()-startTime+999)/1000 + "s. New best score: " + score + ", script: " + script);
    }
  }
}

// assumes diff is between 0.0 (full score) and 1.0 (least score)
static double diffToPercent(double diff) {
  ret 100*(1-diff);
}

static S makeScript(S schema) {
  L<S> tok = javaTok(schema);
  int i = findCodeTokens(tok, "<", "color1", ">");
  if (i >= 0) {
    clearAllTokens(tok, i, i+5);
    tok.set(i, quote(randomColor().toString()));
    tok = javaTok(tok);
  }
  ret join(tok);
}

static void render(RGBImage img, S script) {
  L<S> tok = javaTok(script);
  new Matches m;
  for (L<S> s : statements(tok)) {
    if (jmatch("fill(*);", s, m)) {
      fill(img, m.unq(0));
    } else
      fail("unknown statement: " + join(s));
  }
}

static void makeRunners() {
  for (S s : strategies) {
    new Runner r;
    r.strategy = s;
    runners.add(r);
  }
}

Author comment

Began life as a copy of #1001882

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001883
Snippet name: Image rendering from random scripts v3, with metastrategy, developing
Eternal ID of this version: #1001883/1
Text MD5: 2994611ac5632de4999d3406dd5fbabb
Transpilation MD5: bb84ba2202b954f6eebe7da8219d37b4
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-01 22:14:04
Source code size: 3160 bytes / 127 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 650 / 668
Referenced in: [show references]