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).

1  
!752
2  
3  
!include #1000522 // image helper functions
4  
5  
static RGBImage targetImg;
6  
7  
static class Runner {
8  
  S strategy;
9  
  S bestScript;
10  
  double score;
11  
  long tries;
12  
}
13  
14  
static ImageSurface imageSurface;
15  
16  
static L<S> strategies = litlist(
17  
  "random;",
18  
  "random; vary;"
19  
);
20  
21  
static S metaStrategy = "race strategies to 100%";
22  
23  
static double score, targetScore;
24  
static new L<Runner> runners;
25  
static boolean warned;
26  
static long startTime;
27  
28  
p {
29  
  targetImg = new RGBImage(200, 200, Color.blue);
30  
  
31  
  startTime = now();
32  
  new Matches m;
33  
  if (jmatch("race strategies to *%", metaStrategy, m)) {
34  
    targetScore = parseDouble(m.get(0));
35  
    makeRunners();
36  
    while (score < targetScore) {
37  
      // TODO: fair time sharing?
38  
      for (Runner r : runners)
39  
        step(r);
40  
    }
41  
  } else
42  
    fail("Unknown meta strategy: " + metaStrategy);
43  
}
44  
45  
static void step(Runner r) {
46  
  for (L<S> s : statements(r.strategy)) {
47  
    if (jmatch("random;", s)) {
48  
      S scriptSchema = "fill(<color1>);";
49  
      tryScript(r, makeScript(scriptSchema));
50  
    } else if (jmatch("vary;", s)) {
51  
      if (r.bestScript != null) {
52  
        S variation = varyScript(r.bestScript);
53  
        if (variation != null && neq(r.bestScript, variation))
54  
          tryScript(r, variation);
55  
      }
56  
    } else if (!warned) {
57  
      print("Unknown strategy statement: " + join(s));
58  
      warned = true;
59  
    }
60  
  }
61  
}
62  
63  
static S varyScript(S script) {
64  
  L<S> tok = javaTok(script);
65  
  for (int i = 1; i < l(tok); i += 2)
66  
    if (isQuoted(tok.get(i))) {
67  
      S s = unquote(tok.get(i));
68  
      tok.set(i, quote(varyColor(s)));
69  
    }
70  
  ret join(tok);
71  
}
72  
73  
static S varyColor(S color) {
74  
  ret varyColor(new RGB(color)).toString();
75  
}
76  
77  
static void tryScript(Runner r, S script) {
78  
  ++r.tries;
79  
  //print ("Script: " + script);
80  
  RGBImage img = new RGBImage(200, 200, Color.white);
81  
  render(img, script);
82  
  double newScore = diffToPercent(diff(img, targetImg));
83  
  if (newScore > r.score) {
84  
    r.score = newScore;
85  
    r.bestScript = script;
86  
    if (newScore > score) {
87  
      score = newScore; // global score
88  
      imageSurface = showImage(img, imageSurface);
89  
      print("Try " + r.tries + " of runner " + (runners.indexOf(r)+1) + " after " + (now()-startTime+999)/1000 + "s. New best score: " + score + ", script: " + script);
90  
    }
91  
  }
92  
}
93  
94  
// assumes diff is between 0.0 (full score) and 1.0 (least score)
95  
static double diffToPercent(double diff) {
96  
  ret 100*(1-diff);
97  
}
98  
99  
static S makeScript(S schema) {
100  
  L<S> tok = javaTok(schema);
101  
  int i = findCodeTokens(tok, "<", "color1", ">");
102  
  if (i >= 0) {
103  
    clearAllTokens(tok, i, i+5);
104  
    tok.set(i, quote(randomColor().toString()));
105  
    tok = javaTok(tok);
106  
  }
107  
  ret join(tok);
108  
}
109  
110  
static void render(RGBImage img, S script) {
111  
  L<S> tok = javaTok(script);
112  
  new Matches m;
113  
  for (L<S> s : statements(tok)) {
114  
    if (jmatch("fill(*);", s, m)) {
115  
      fill(img, m.unq(0));
116  
    } else
117  
      fail("unknown statement: " + join(s));
118  
  }
119  
}
120  
121  
static void makeRunners() {
122  
  for (S s : strategies) {
123  
    new Runner r;
124  
    r.strategy = s;
125  
    runners.add(r);
126  
  }
127  
}

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