Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

This uses the same manually-decoded 16-color image approach as my previous answerprevious answer, but this time I actually applied a gradient-descent optimization strategy to significantly improve the score. The previous submission scored 4755.886 points, whereas the new submission scores over 250 points better, beating out a lot of built-in compression approaches in the process.

This uses the same manually-decoded 16-color image approach as my previous answer, but this time I actually applied a gradient-descent optimization strategy to significantly improve the score. The previous submission scored 4755.886 points, whereas the new submission scores over 250 points better, beating out a lot of built-in compression approaches in the process.

This uses the same manually-decoded 16-color image approach as my previous answer, but this time I actually applied a gradient-descent optimization strategy to significantly improve the score. The previous submission scored 4755.886 points, whereas the new submission scores over 250 points better, beating out a lot of built-in compression approaches in the process.

Source Link
nneonneo
  • 12.6k
  • 8
  • 31
  • 52

Python 2 (no built-in compression), score 4497.730

This uses the same manually-decoded 16-color image approach as my previous answer, but this time I actually applied a gradient-descent optimization strategy to significantly improve the score. The previous submission scored 4755.886 points, whereas the new submission scores over 250 points better, beating out a lot of built-in compression approaches in the process.

As before, the final program is exactly 1024 bytes long. In fact, the raw output of the optimization algorithm contained four bytes that were escaped (\0), and which I had to "fudge" in order to reduce the byte count to 1024 bytes. Without the fudge, the 1028-byte program would score 4490.685 - 7 points better.

The basic idea is to optimize both the palette and data jointly. In a single iteration, I search over all tweaks of the palette (basically, every modified palette which differs by 1 in some color component) and pick the modified palette which best improves the score. Then, I search over all tweaks of the data (every modified index array in which one pixel is changed to some other palette entry) and choose a modification which reduces the score (here I don't care about best, because I don't want to fruitlessly search the full space of over 25000 tweaks every iteration).

Finally, when generating the final program output, I run another optimization pass which rearranges the palette to minimize the number of backslashes required in the final output (e.g. for the program shown below, the palette was rearranged using the hex table "0e3428916b7df5ca").

This approach yielded both a significant numerical and perceptual improvement over the previous naïve ImageMagick approach. Previous submission output:

Old submission's output

And new submission output:

New submission's output

The new optimization-based approach has significantly more detail and accurate color reproduction.

Here is the hexdump of the final program:

0000000: efbb bf66 726f 6d20 5049 4c2e 496d 6167  ...from PIL.Imag
0000010: 6520 696d 706f 7274 2a0a 6672 6f6d 6275  e import*.frombu
0000020: 6666 6572 2827 5247 4227 2c28 3430 2c34  ffer('RGB',(40,4
0000030: 3129 2c27 272e 6a6f 696e 2827 b39b 2620  1),''.join('..& 
0000040: b4b9 7e20 2634 8120 5567 7520 3547 7320  ..~ &4. Ugu 5Gs 
0000050: 242e 5620 1c1f 1b20 7890 a420 4348 3d20  $.V ... x.. CH= 
0000060: 9fae a420 3a52 8e20 262b 3220 7d90 7f20  ... :R. &+2 }.. 
0000070: 3a49 5720 4a67 9720 5d79 9c27 2e73 706c  :IW Jg. ]y.'.spl
0000080: 6974 2829 5b69 6e74 2863 2c31 3629 5d66  it()[int(c,16)]f
0000090: 6f72 2063 2069 6e27 8388 88b6 86b6 6b66  or c in'......kf
00000a0: 6bb8 b8b8 8888 dd8b bbbb bb8d b688 bb66  k..............f
00000b0: 6666 6666 b68d bdb6 bb88 33bd 5b55 bb68  ffff......3.[U.h
00000c0: b888 b66b 6666 6666 66bb 6688 88d8 bbb5  ...kfffff.f.....
00000d0: 553d 868b bb8b 6666 666b 6666 66b6 3d68  U=....fffkfff.=h
00000e0: bb66 5dd4 8363 b8bd dbd8 6666 66b6 66b6  .f]..c....fff.f.
00000f0: bbbb bbb6 d666 6bd6 f3bd d3d4 b5dd 666b  .....fk.......fk
0000100: 6666 6666 b668 63d5 66b8 5bd8 66bb 5b5b  ffff.hc.f.[.f.[[
0000110: 884b b66b 666b 666b 686d 8d85 dbb6 dd4b  .K.kfkfkhm.....K
0000120: 5b33 6db5 b5bd 6b6b 6b66 66bb d5b3 dddb  [3m...kkkff.....
0000130: bad4 58b5 d435 3b33 b555 6b66 6666 66bb  ..X..5;3.Ukffff.
0000140: 5346 db84 d45d bbbd 54d7 4d3b 5bbb b6b6  SF...]..T.M;[...
0000150: 6666 66b5 6bd3 d4f3 eddd 4333 c5d3 3c83  fff.k.....C3..<.
0000160: b2a5 5666 6666 66b6 b534 84d5 4444 b8b8  ..Vffff..4..DD..
0000170: b383 8333 3ffe 7666 66b6 6b42 2555 2eaa  ...3?.vff.kB%U..
0000180: 5b4a 4343 ad33 3388 43fe f666 b6d6 64e3  [JCC.33.C..f..d.
0000190: 564f d534 4455 aaea aaef ffee e737 3666  VO.4DU.......76f
00001a0: 6bb6 c73e ef3a f352 445b b25a eaee ffee  k..>.:.RD[.Z....
00001b0: ff79 3666 6b63 7c73 f3ec ee34 b55b 53b6  .y6fkc|s...4.[S.
00001c0: 5baf eee3 3717 3666 6dda 3fc1 ccc3 cc3a  [...7.6fm.?....:
00001d0: f333 7e34 bbb2 feea 7c7e d666 6ddf 99f3  .3~4....|~.fm...
00001e0: c7c1 c717 c777 7f77 f35b b6bb 374a 4666  .....w.w.[..7JFf
00001f0: be49 9999 aeee fac9 cc99 997c c79b 5bbb  .I.........|..[.
0000200: a3ae 4666 bad9 9197 e7ae fae4 acf3 ef99  ..Ff............
0000210: 9cf7 d6b5 4afe 3666 bf79 9099 a5ef af4a  ....J.6f.y.....J
0000220: ee77 ce49 999f 7b66 aeec 1b66 be39 1999  .w.I..{f...f.9..
0000230: a474 a2ef a7ef fcaf 1979 997c 7ee1 1b66  .t.......y.|~..f
0000240: baf7 999f efff 7cee e77e 7ffa 7999 9999  ......|..~..y...
0000250: 1eac c666 d4fe f7ff 7fff feff eeff e7ef  ...f............
0000260: a919 1999 f3ae a6b6 d4a5 aef7 ecf4 44a3  ..............D.
0000270: 7aa4 a7fe fe79 9199 e777 76b6 e4b4 ae77  z....y...wv....w
0000280: 7f4e ffef 3977 9fee f7ee 7719 9777 f36b  .N..9w....w..w.k
0000290: e45e 4e7f fafe ff7a eec3 3eef feff eaa1  .^N....z..>.....
00002a0: f7f7 3f66 9f5f ceef fe7a e777 aeaa ee3e  ..?f._...z.w...>
00002b0: efee fefe caae c766 77fc 1aff f7ff 7f77  .......fw......w
00002c0: edea eeea faff faff ef9f e7d6 7fff c547  ...............G
00002d0: 37ef fef7 4eaa affe e7ff 77ff fcf7 c7dd  7...N.....w.....
00002e0: 7e9f ff64 e3ef a7fa faef fffa ee19 197f  ~..d............
00002f0: fcf5 2abf f7fc f774 aaa3 e74e 7a3c 77ee  ..*....t...Nz<w.
0000300: ff11 119c ca54 a2ba 2aef f3cc af3f 7ee7  .....T..*....?~.
0000310: f2c1 17ae f110 5c30 17ea aa2a b3c3 2eef  ......\0...*....
0000320: fc33 3fee f745 7ccf ef91 1001 09aa 2aaa  .3?..E|.......*.
0000330: bc0c 2a4e e717 97f7 fa5a affe ef91 1011  ..*N.....Z......
0000340: 1c2a aa2a 6c03 2a44 fea3 3444 24e4 4aa2  .*.*l.*D..4D$.J.
0000350: ee91 0111 195a aa4a 6434 222a 52d4 4422  .....Z.Jd4"*R.D"
0000360: 4f3e 5542 e7c1 1011 9c5a 2a2a 6522 4aa2  O>UB.....Z**e"J.
0000370: a222 a254 7ccc 454e ef9c 1101 174a 77a2  .".T|.EN.....Jw.
0000380: b224 2aa4 2c32 aa42 700c aa45 ea39 c111  .$*.,2.Bp..E.9..
0000390: 9c57 c072 545a 5ecc e0ca 4522 ecce a4a4  .W.rTZ^...E"....
00003a0: a34f 991c cf5f 0175 5315 5acc f4a4 ae44  .O..._.uS.Z....D
00003b0: aa34 ea34 aafa 2ffc ee54 7f4b 5345 2a3c  .4.4../..T.KSE*<
00003c0: a4a3 33aa 4554 445e a4e3 f4ea 4427 2e65  ..3.ETD^....D'.e
00003d0: 6e63 6f64 6528 2768 6578 2729 2929 2e72  ncode('hex'))).r
00003e0: 6573 697a 6528 2833 3836 2c33 3230 292c  esize((386,320),
00003f0: 3129 2e73 6176 6528 276f 2e70 6e67 2729  1).save('o.png')

There's still room to improve. For example, a simple histogram shows that some colors are barely used:

 6: 203
15: 167
14: 154
11: 152
10: 145
 7: 120
 4: 110
 3: 107
 5: 85
 9: 77
12: 77
13: 66
 1: 56
 8: 54
 2: 49
 0: 18

This suggests that a rebalanced palette might improve efficiency, perhaps enough to catch the 5th place BPG solution. However, I am quite doubtful that this optimization approach (or really, anything that doesn't involve the extraordinary machinery of H.265) can catch the first place BPG implementation.