<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>CuriosityArb</title>
    <description>Focus on the interesting. Ignore the uninteresting.</description>
    <link>https://curiosityarb.blog/</link>
    <atom:link href="https://curiosityarb.blog/feed.xml" rel="self" type="application/rss+xml" />
    <generator>Jekyll v4.3.3</generator>
    
    
    <item>
      <title>Logging Workouts for Accountability</title>
      <description>&lt;p&gt;I log all my weightlifting sessions, with pen and paper, and have for years. 
Doing so greatly increases my level of personal accountability. 
Specifically, it helps me do what I planned, and stay honest about my progress over time. 
I think everyone would benefit from logging their workouts and here I document how I do it and why it’s worthwhile.&lt;/p&gt;

&lt;h2 id=&quot;how&quot;&gt;How&lt;/h2&gt;
&lt;p&gt;My primary objective in the gym is hypertrophy, followed by strength, so those dictate the organization of my specific logs. 
However I think the underlying principles are important regardless of your fitness objectives. &lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;h3 id=&quot;logging-principles&quot;&gt;Logging Principles&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;I track what I do&lt;/li&gt;
  &lt;li&gt;What I could have done&lt;/li&gt;
  &lt;li&gt;And how it compares to last time&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;log-header&quot;&gt;Log Header&lt;/h3&gt;

&lt;p&gt;I start every log with the date, time, and the workout I’m doing (usually &lt;em&gt;push&lt;/em&gt;, &lt;em&gt;pull&lt;/em&gt;, or &lt;em&gt;legs&lt;/em&gt;).&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Sat Push 10/11/25 13:57
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;It’s pretty easy to think I workout more often than I really do. 
Last Fall I got busy and knew it’d been a while, maybe a couple weeks. 
Then I finally got in a leg day and saw in my log that it had been much longer, over a month since my last leg workout. 
Logging my workouts helps me stay consistent.&lt;/p&gt;

&lt;h3 id=&quot;logging-exercises&quot;&gt;Logging Exercises&lt;/h3&gt;
&lt;p&gt;For each exercise I track the weight and reps. 
This is principle 1, &lt;em&gt;What I do&lt;/em&gt;, and is pretty standard for a workout log. 
However there are three additional important components.&lt;/p&gt;

&lt;h4 id=&quot;reps-in-reserve-rir&quot;&gt;Reps in Reserve (RiR)&lt;/h4&gt;
&lt;p&gt;After I complete a set, I record how many additional reps I could have done before reaching muscle failure. 
If I stop doing shoulder presses after 10 reps, but think I could’ve done 2 more reps before my arms got all wobbly and I couldn’t move the weight, that’s 2 RiR.&lt;/p&gt;

&lt;p&gt;Logging RiR is important for principle 2, &lt;em&gt;What I could have done&lt;/em&gt;. 
It helps me avoid simply doing an exercise until I “feel tired” or “think that’s enough.” 
Of course, not every workout can be 100% or even 90% effort. 
That would be insane. 
100% effort is what you do when a bear is chasing you, and it’s not possible to go that hard any given Tuesday in the gym. 
Reps in reserve allows me to set expectations for workout intensity (usually 1-2 RiR) and stick to them.&lt;/p&gt;

&lt;h4 id=&quot;rep-cadence&quot;&gt;Rep cadence&lt;/h4&gt;
&lt;p&gt;Repetition cadence, or the speed with which a rep is performed, is part of principle 1, &lt;em&gt;What I do&lt;/em&gt;. 
Controlling the weight is important both during the concentric motion (tightening the muscle to pull, push, lift the weight) and the often overlooked eccentric motion (extending the muscle to let the weight come back to rest). 
For hypertrophy, I aim for an explosive concentric movement that takes 1 time unit, followed by a slower eccentric movement that takes 3 time units.&lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt; 
I log this rep cadence as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 / 3&lt;/code&gt;. 
In a rowing exercise, for example, I pull the weight toward my chest (concentric) for a 1 count, then let the weight back down for a 3 count.&lt;/p&gt;

&lt;p&gt;While I always aim for this same rep cadence of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 / 3&lt;/code&gt;, I still always write it down for every exercise. 
This style of lifting is quite tiring and if I were to do a different rep cadence like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 / 1&lt;/code&gt; that would totally change the exercise. 
Trying to slowly lower the leg press weight for a 3 count while on the last rep of the last set is damn hard. 
If I don’t hold myself accountable by logging cadence, it’s easy to throw in the towel.&lt;/p&gt;

&lt;h4 id=&quot;last-time-i-did-this-exercise-what-did-i-do&quot;&gt;Last time I did this exercise, what did I do?&lt;/h4&gt;
&lt;p&gt;Before I do the first set of an exercise, I look back in my log to the last time I did that exercise, and note the weight, reps, and RiR of the first set. 
This provides a benchmark for today’s first set and relates to principle 3, &lt;em&gt;How it compares to last time&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;If last push workout I did 180 lb for 9 reps with 1 RiR on the machine press, I record that in today’s log as&lt;/p&gt;

&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Machine Press (180x9 1)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I use this to make an informed decision when choosing today’s weight and set expectations for the number of reps. 
It also keeps me mindful of how I’m progressing over time. 
Whether I’m advancing or plateauing, I’m reminded to push myself at least as hard as last time, or maybe focus on rest and recovery this week.&lt;/p&gt;

&lt;h4 id=&quot;putting-it-all-together&quot;&gt;Putting it all together&lt;/h4&gt;
&lt;p&gt;Combining these elements, my final log for an exercise will look like this&lt;/p&gt;
&lt;div class=&quot;language-text highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;1. Machine Press (180x9 1)  &amp;lt;- what I did last time
180x9   1  1/3  &amp;lt;- 1st set: 1 RiR and a 1/3 cadence
160x10  1
160x8   .5  &amp;lt;- last set I like to approach failure
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1.&lt;/code&gt; marks this is my first exercise of the workout. 
The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.5&lt;/code&gt; reps in reserve means I didn’t push to absolute muscle failure, but I certainly couldn’t have completed another full rep with good form.&lt;/p&gt;

&lt;p&gt;I do this for every exercise and end my log with the time I finish.&lt;/p&gt;

&lt;p&gt;Here’s my log from yesterday’s &lt;em&gt;push&lt;/em&gt; workout, with my own shorthand. 
You can see I didn’t improve much from last &lt;em&gt;push&lt;/em&gt; day, and even regressed a bit on the shoulder press.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/workout_log/push_workout.jpg&quot; alt=&quot;A push workout log entry&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;why&quot;&gt;Why&lt;/h2&gt;
&lt;blockquote&gt;
  &lt;p&gt;The first principle is that you must not fool yourself, and you are the easiest person to fool.&lt;/p&gt;

  &lt;p&gt;– Richard Feynman&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Keeping a log helps me pursue my exercise objectives by giving me a framework that remains constant even as my motivation and focus fluctuate. 
Without a log I’m certain I’d fool myself into thinking that I worked out more often than I did, that I pushed each set as hard as I could, that I maintained control as I had planned, or that I made progress from last time.&lt;/p&gt;

&lt;p&gt;It’s easy to get to the gym when I feel motivated and life is going well. 
It’s easy to apply myself to each exercise when I’m excited for the workout. 
But the other 2/3 of the time I need my workout log to hold me accountable.&lt;/p&gt;

&lt;p&gt;I think everyone should keep a workout log. 
Of course it will look different than mine, but progress with fitness, or really anything, requires knowledge of&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;What you did&lt;/li&gt;
  &lt;li&gt;What you could have done&lt;/li&gt;
  &lt;li&gt;How it compares to last time&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;using-paper&quot;&gt;Using paper&lt;/h3&gt;
&lt;p&gt;My log is pen on paper and that’s ideal. 
Of course this could be put in an app, but for what purpose? 
I like the tactile nature of my notebook and the flexibility it gives me to keep notes however I feel like. 
Not to mention the satisfaction of a stack of notebooks with all my workouts going back to 2017. &lt;sup id=&quot;fnref:3&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:3&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;If you keep a workout log yourself, or just have thoughts on the process, 
&lt;a href=&quot;&amp;#x6d;&amp;#x61;&amp;#x69;&amp;#x6c;&amp;#x74;&amp;#x6f;&amp;#x3a;&amp;#x63;&amp;#x75;&amp;#x72;&amp;#x69;&amp;#x6f;&amp;#x73;&amp;#x69;&amp;#x74;&amp;#x79;&amp;#x61;&amp;#x72;&amp;#x62;&amp;#x40;&amp;#x61;&amp;#x62;&amp;#x72;&amp;#x65;&amp;#x66;&amp;#x65;&amp;#x6c;&amp;#x64;&amp;#x2e;&amp;#x61;&amp;#x6e;&amp;#x6f;&amp;#x6e;&amp;#x61;&amp;#x64;&amp;#x64;&amp;#x79;&amp;#x2e;&amp;#x63;&amp;#x6f;&amp;#x6d;&quot;&gt;shoot me a message&lt;/a&gt;. 
My log format has changed a bit over time and I’d enjoy hearing how others approach this.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;It’s so important to have objectives or goals when working out. 
Of course everyone’s at their own stage, but I’d argue if you’re beyond a total beginner to exercise, simply doing random stuff to get your heart rate up isn’t a great use of time. 
If you’re exercising for your health (such an ambiguous term), then you should decide what better health looks like to you, do things that get you closer to that, and track your progress. 
Or if you just go to the gym to have fun, I don’t blame you. 
I also enjoy the gym. 
I just like achieving something while I’m there. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;The intent of this post isn’t to discuss weight lifting science and that’s not something I’m qualified for anyway. 
Things like reps in reserve or rep cadence and how they relate to hypertrophy are easy to find with an internet search. &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:3&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Ok so not &lt;em&gt;every&lt;/em&gt; workout. 
Like one time I worked out with a friend in a different city and didn’t have my log. 
But every other time. &lt;a href=&quot;#fnref:3&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
      <pubDate>Sun, 12 Oct 2025 00:00:00 -0600</pubDate>
      <link>https://curiosityarb.blog/2025/10/12/logging-workouts-for-accountability.html</link>
      <guid isPermaLink="true">https://curiosityarb.blog/2025/10/12/logging-workouts-for-accountability.html</guid>
    </item>
    
    
    
    <item>
      <title>Productivity Entertainment</title>
      <description>&lt;p&gt;&lt;em&gt;Productivity entertainment&lt;/em&gt; is a form of procrastination in which you amuse yourself by passively consuming something that you could plausibly use to be productive.&lt;/p&gt;

&lt;p&gt;For example, today I spent almost an hour browsing &lt;a href=&quot;#fn:1&quot; id=&quot;fnref:1&quot;&gt;Hugo website themes&lt;/a&gt;&lt;span hidden=&quot;&quot;&gt;&lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/span&gt; instead of just opening vim and creating a website.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;
“Look at all those cool themes. 
Sure will be fun to make a website with one.
I can even view live demos!
Let me check how the links look when I hover, and if there’s dark mode. 
Are there special features I won’t need but it’d be good to know exist? 
This sure is fun - and productive! 
I mean, it’s been hard work carefully considering 72 different themes, but it was a critical step toward making my baller static website.
Probably tomorrow, or maybe Saturday morning if I have time, I’ll just develop the actual site.” 
&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Productivity entertainment involves consuming info that you, or at least someone, &lt;em&gt;could&lt;/em&gt; use to be creative or get something done. 
That’s how, in the moment, it’s rationalized as a good use of time. 
But really it just serves as a way to distract and entertain. 
Instead of actually creating, your attention is absorbed by something that makes you &lt;em&gt;feel&lt;/em&gt; as though you’re creating.&lt;sup id=&quot;fnref:2&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;A few forms of productivity entertainment I find myself engaging in:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Watching cooking tutorials on YouTube&lt;/li&gt;
  &lt;li&gt;Browsing Bootstrap CSS features&lt;/li&gt;
  &lt;li&gt;Researching morning routine broscience I don’t have time for&lt;/li&gt;
  &lt;li&gt;Watching videos of people playing piano or guitar (instead of just practicing myself)&lt;/li&gt;
  &lt;li&gt;Reading GrapheneOS docs so I can escape the matrix and become Mr. Robot&lt;/li&gt;
  &lt;li&gt;Reading indie blog posts about being productive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is not to say any of these things are unproductive in their own right. 
In fact, the critical element of productivity entertainment is that it involves an activity that &lt;em&gt;could be done creatively or productively&lt;/em&gt;. 
But if I’m not browsing the documentation or watching the video in order to take action, then it’s really just entertainment, and I should acknowledge that.&lt;sup id=&quot;fnref:3&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:3&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;Alright. 
Did you get here thinking you were going to see themes? 
My whole point is that you don’t need to browse website themes unless you’re actually, really, truly going to use one to style your website.&lt;/p&gt;

      &lt;p&gt;But if you must: &lt;a href=&quot;https://themes.gohugo.io&quot;&gt;https://themes.gohugo.io&lt;/a&gt; &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;It could just as easily be called &lt;em&gt;creativity entertainment&lt;/em&gt; depending on the situation. &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:3&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;There’s nothing wrong with entertainment, of course. I just believe it should be done intentionally. &lt;a href=&quot;#fnref:3&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
      <pubDate>Sat, 29 Mar 2025 00:00:00 -0600</pubDate>
      <link>https://curiosityarb.blog/2025/03/29/productivity-entertainment.html</link>
      <guid isPermaLink="true">https://curiosityarb.blog/2025/03/29/productivity-entertainment.html</guid>
    </item>
    
    
    
    <item>
      <title>The Chicago L&apos;s Vista Paradox</title>
      <description>&lt;p&gt;I’ve noticed while traveling out of Chicago on the L (the elevated train line) that the &lt;del&gt;Willis&lt;/del&gt; Sears Tower appears as though it’s shooting upward out of the surrounding skyline. 
It’s pretty striking when you first see it. 
For a moment it honestly looks like the Tower is growing taller. 
Today I learned from the fantastic &lt;a href=&quot;https://www.futilitycloset.com/2025/02/20/the-vista-paradox/&quot;&gt;Futility Closet&lt;/a&gt; that this effect is called the &lt;a href=&quot;https://en.wikipedia.org/wiki/Vista_paradox&quot;&gt;Vista paradox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can see the effect from the rear car of north bound Brown or Purple line trains as you’re approaching the Chicago stop. 
The Tower appears to rise up behind the Merchandise Mart.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/vista_paradox/L_vista_paradox.gif&quot; alt=&quot;A gif of the vista effect as seen from the Purple line&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The effect occurs because the visual angle between the Tower and the the closer buildings rapidly increases as you move away.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/vista_paradox/st_visual_angle.jpeg&quot; alt=&quot;A sketch of the change in visual angle as you move away from the Tower&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Incidentally, when the train stops after I’ve been looking out the back for a while, I also experience a &lt;a href=&quot;https://en.wikipedia.org/wiki/Illusions_of_self-motion&quot;&gt;vection illusion&lt;/a&gt; (feeling of self motion) where I continue to perceive the tracks as moving away from me even though the train is still.&lt;/p&gt;
</description>
      <pubDate>Fri, 21 Feb 2025 00:00:00 -0700</pubDate>
      <link>https://curiosityarb.blog/2025/02/21/chicago-l-vista-paradox.html</link>
      <guid isPermaLink="true">https://curiosityarb.blog/2025/02/21/chicago-l-vista-paradox.html</guid>
    </item>
    
    
    
    <item>
      <title>Searching for Autograms</title>
      <description>&lt;blockquote&gt;
  &lt;p&gt;This sentence contains three a’s, three c’s, two d’s, twenty-seven e’s, four f’s, one g, five h’s, eleven i’s, two l’s, sixteen n’s, seven o’s, five r’s, twenty-nine s’s, sixteen t’s, two u’s, six v’s, six w’s, six x’s, and three y’s.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;whats-an-autogram&quot;&gt;What’s an autogram&lt;/h2&gt;

&lt;p&gt;An &lt;a href=&quot;https://en.wikipedia.org/wiki/Autogram&quot;&gt;autogram&lt;/a&gt; is a self-referential sentence that writes out a count of its own characters - like the sentence above. 
You can count the number of occurrences of each letter in that first quoted sentence and verify it’s the same as claimed, or just trust that the sentence really does have three a’s, three c’s, two d’s, etc.&lt;/p&gt;

&lt;p&gt;As you can imagine, constructing an autogram is challenging. 
The act of simply writing out how many of a particular letter is in a sentence changes the very letters that the sentence must describe. 
Consider how &lt;a href=&quot;https://www.leesallows.com/index.php&quot;&gt;Lee Sallows&lt;/a&gt;, the inventor of autograms, describes searching for one by hand:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Suppose, for instance, a few day’s painstaking labour have at last yielded a near-perfect specimen: only the x’s are wrong. 
Instead of the five claimed, in reality there are 6.
Writing six in place of five will not merely invalidate the totals for e, f, s, and v, the x in six means that their number has now become 7. 
Yet replacing six with seven will only return the total to 6. What now?&lt;br /&gt;
&lt;br /&gt;
&lt;cite&gt;Lee Sallows - &lt;a href=&quot;https://www.leesallows.com/files/In%20Quest%20of%20a%20Pangram1.pdf&quot;&gt;In Quest of a Pangram&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I’m interested in constructing my own autograms from custom messages. 
For example, instead of a bland “This sentence contains…”, what about a birthday card to a friend: “Happy birthday, friend! This card contains exactly…”. 
Below I outline the steps I use to search for an autogram from a custom starting message.&lt;/p&gt;

&lt;p&gt;A lot of what I’ve learned comes from Sallows’ essay on &lt;a href=&quot;https://www.leesallows.com/files/Reflexicons%20NEW(4c).pdf&quot;&gt;&lt;em&gt;Reflexicons&lt;/em&gt;&lt;/a&gt;. 
He goes much more in-depth, and it’s worth a read if you want a deep dive. 
When I reference Sallows below it’s from this essay, unless I note otherwise.&lt;/p&gt;

&lt;p&gt;So, what’s the autogram search process?&lt;/p&gt;

&lt;h2 id=&quot;self-descriptive-cycles&quot;&gt;Self-descriptive cycles&lt;/h2&gt;
&lt;p&gt;Turns out there’s an easier generalization to finding one sentence that describes its own character count.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Find a self-descriptive &lt;em&gt;cycle&lt;/em&gt; of sentences, where each sentence describes the character count of the previous&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A self-descriptive cycle of &lt;em&gt;n&lt;/em&gt; sentences (period &lt;em&gt;n&lt;/em&gt;) would look like:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;sentence 1&lt;/li&gt;
  &lt;li&gt;sentence 2 → (describes) sentence 1&lt;/li&gt;
  &lt;li&gt;…&lt;/li&gt;
  &lt;li&gt;sentence n → sentence n-1&lt;/li&gt;
  &lt;li&gt;sentence 1 → sentence n&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why is it easier to find a self-descriptive cycle?&lt;/p&gt;

&lt;p&gt;The length of these sentences is bounded so there must be a finite number of them. 
This means that creating a sequence of sentences, each of which describes the previous one, is guaranteed to eventually produce a sentence that’s already in the sequence.
Once one sentence repeats, the one sentence that describes it will also be a repeat of the sentence that described the first occurrence.
After this the sequence is stuck in a loop of sentences which make up a self-descriptive cycle.&lt;/p&gt;

&lt;p&gt;In short, counting the letter occurrences in this sentence and writing those out in the next sentence, then repeating, will always produce a self-descriptive cycle.&lt;/p&gt;

&lt;p&gt;For example when I initiate with the phrase, “The previous sentence has”, my script proceeds with:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Epoch 0: The previous sentence has
Epoch 1: The previous sentence has one a, one c, five e&apos;s, two h&apos;s, one i, two n&apos;s, one o, one p, one r, three s&apos;s, two t&apos;s, one u, and one v.
Epoch 2: The previous sentence has three a&apos;s, two c&apos;s, one d, seventeen e&apos;s, one f, four h&apos;s, three i&apos;s, twelve n&apos;s, thirteen o&apos;s, two p&apos;s, three r&apos;s, nine s&apos;s, seven t&apos;s, two u&apos;s, three v&apos;s, and three w&apos;s.
Epoch 3: ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After a few thousand iterations it lands in this monstrosity:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0000: The previous sentence has three a&apos;s, two c&apos;s, two d&apos;s, twenty-seven e&apos;s, six f&apos;s, three g&apos;s, seven h&apos;s, twelve i&apos;s, two l&apos;s, fourteen n&apos;s, eight o&apos;s, two p&apos;s, five r&apos;s, twenty-eight s&apos;s, seventeen t&apos;s, three u&apos;s, six v&apos;s, eight w&apos;s, four x&apos;s, and three y&apos;s.
0001: The previous sentence has three a&apos;s, two c&apos;s, two d&apos;s, thirty-two e&apos;s, four f&apos;s, four g&apos;s, ten h&apos;s, eight i&apos;s, two l&apos;s, eleven n&apos;s, eight o&apos;s, two p&apos;s, eight r&apos;s, twenty-nine s&apos;s, twenty-one t&apos;s, four u&apos;s, seven v&apos;s, eight w&apos;s, three x&apos;s, and three y&apos;s.
...
0314: The previous sentence has three a&apos;s, two c&apos;s, two d&apos;s, twenty-six e&apos;s, five f&apos;s, two g&apos;s, six h&apos;s, six i&apos;s, one l, fourteen n&apos;s, eleven o&apos;s, two p&apos;s, seven r&apos;s, twenty-nine s&apos;s, twenty t&apos;s, five u&apos;s, seven v&apos;s, nine w&apos;s, two x&apos;s, and three y&apos;s.
...
1618: The previous sentence has three a&apos;s, two c&apos;s, two d&apos;s, twenty-six e&apos;s, five f&apos;s, one g, five h&apos;s, eight i&apos;s, three l&apos;s, thirteen n&apos;s, eleven o&apos;s, two p&apos;s, seven r&apos;s, twenty-eight s&apos;s, seventeen t&apos;s, five u&apos;s, six v&apos;s, nine w&apos;s, five x&apos;s, and three y&apos;s.
...
ಠ_ಠ
...
1729: The previous sentence has three a&apos;s, two c&apos;s, two d&apos;s, thirty-two e&apos;s, three f&apos;s, one g, nine h&apos;s, seven i&apos;s, two l&apos;s, seventeen n&apos;s, ten o&apos;s, two p&apos;s, nine r&apos;s, twenty-four s&apos;s, twenty-one t&apos;s, three u&apos;s, six v&apos;s, nine w&apos;s, one x, and four y&apos;s.
...
2061: The previous sentence has three a&apos;s, two c&apos;s, two d&apos;s, twenty-seven e&apos;s, six f&apos;s, three g&apos;s, seven h&apos;s, twelve i&apos;s, two l&apos;s, fourteen n&apos;s, eight o&apos;s, two p&apos;s, five r&apos;s, twenty-eight s&apos;s, seventeen t&apos;s, three u&apos;s, six v&apos;s, eight w&apos;s, four x&apos;s, and three y&apos;s.
0001: The previous sentence has three a&apos;s, two c&apos;s, two d&apos;s, thirty-two e&apos;s, four f&apos;s, four g&apos;s, ten h&apos;s, eight i&apos;s, two l&apos;s, eleven n&apos;s, eight o&apos;s, two p&apos;s, eight r&apos;s, twenty-nine s&apos;s, twenty-one t&apos;s, four u&apos;s, seven v&apos;s, eight w&apos;s, three x&apos;s, and three y&apos;s.
0002: ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;See that? 
Sentences 2061 and 0000 are the same!
Sentence 0001 describes both 2061 and 0000.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;2061: The previous sentence has three a&apos;s, two c&apos;s, two d&apos;s, twenty-seven e&apos;s, six f&apos;s, three g&apos;s, seven h&apos;s, twelve i&apos;s, two l&apos;s, fourteen n&apos;s, eight o&apos;s, two p&apos;s, five r&apos;s, twenty-eight s&apos;s, seventeen t&apos;s, three u&apos;s, six v&apos;s, eight w&apos;s, four x&apos;s, and three y&apos;s.
0000: The previous sentence has three a&apos;s, two c&apos;s, two d&apos;s, twenty-seven e&apos;s, six f&apos;s, three g&apos;s, seven h&apos;s, twelve i&apos;s, two l&apos;s, fourteen n&apos;s, eight o&apos;s, two p&apos;s, five r&apos;s, twenty-eight s&apos;s, seventeen t&apos;s, three u&apos;s, six v&apos;s, eight w&apos;s, four x&apos;s, and three y&apos;s.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is a self-descriptive cycle with a period of 2,061 sentences. 
Which I’m proud of because it’s fun to solve puzzles with code - but I won’t claim it’s elegant.&lt;/p&gt;

&lt;p&gt;What is elegant, and the next logical step, is finding a cycle of fewer sentences  - specifically a cycle of period 1. 
That is, one self-referential sentence that describes its own character count - an autogram.&lt;/p&gt;

&lt;h2 id=&quot;the-trick-for-a-cycle-of-period-1&quot;&gt;The trick for a cycle of period 1&lt;/h2&gt;

&lt;p&gt;With a modification, this same iterative process can find autograms and avoid getting trapped in undesirable, large cycles. 
The trick comes from Sallows’ modification of a strategy he attributes to John R. Letaw.&lt;/p&gt;

&lt;p&gt;As before construct a sequence of sentences.
Only this time when constructing the next sentence in the sequence, alternate between:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Updating all letter counts (as above)&lt;/li&gt;
  &lt;li&gt;Updating one, random letter’s count and leaving all other letter counts the same as the previous sentence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Updating all letter counts (like when finding cycles) is helpful if the current iteration is one step removed from a cycle (hopefully of period 1).
Updating just one letter’s count avoids getting stuck in any cycles of more than 1 and allows the process to run until it has found an autogram, if any exist. 
While cycles are guaranteed to exist for a given initiating phrase, there’s no guarantee that any of them are period 1.&lt;/p&gt;

&lt;p&gt;In practice, if this technique doesn’t find an autogram after a few million iterations I adjust the initial wording and try again.&lt;/p&gt;

&lt;h2 id=&quot;autograms&quot;&gt;Autograms!&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;Happy birthday, friend! This card contains exactly seven a’s, two b’s, four c’s, five d’s, twenty-six e’s, nine f’s, two g’s, seven h’s, fifteen i’s, two l’s, nineteen n’s, eight o’s, three p’s, nine r’s, thirty s’s, seventeen t’s, four u’s, seven v’s, five w’s, four x’s, and six y’s.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Found on iteration 696,409 after 45 seconds.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;Spam, Spam, Spam, six a’s, two d’s, twenty e’s, seven f’s, four g’s, five h’s, ten i’s, two l’s, five m’s, seven n’s, six o’s, five p’s, six r’s, thirty-one s’s, twelve t’s, three u’s, eight v’s, five w’s, four x’s, three y’s, eggs, and Spam.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Found on iteration 271,551 after 15 seconds.&lt;br /&gt;
At first this was taking a while, so I changed “egg” to “eggs” and got a solution fast.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;The quick brown fox jumped over the alphabet soup containing four a’s, three b’s, three c’s, two d’s, thirty-five e’s, eight f’s, five g’s, ten h’s, twelve i’s, two j’s, two k’s, four l’s, two m’s, nineteen n’s, seventeen o’s, four p’s, two q’s, ten r’s, twenty-nine s’s, twenty-seven t’s, eight u’s, eight v’s, eleven w’s, two x’s, four y’s, one z.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Found on iteration 368,803 after 28 seconds. &lt;br /&gt;
Notice this one’s a &lt;a href=&quot;https://en.wikipedia.org/wiki/Pangram&quot;&gt;pangram&lt;/a&gt; - i.e. includes all letters of the alphabet.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;I am self referential! exclaimed the sentence as it enumerated its nine a’s, four c’s, four d’s, forty-four e’s, fourteen f’s, three g’s, ten h’s, nineteen i’s, five l’s, six m’s, twenty-three n’s, twelve o’s, four p’s, fifteen r’s, thirty-seven s’s, twenty-nine t’s, eight u’s, six v’s, seven w’s, six x’s, eight y’s, twenty-four comma’s, six hyphen’s, twenty-five apostrophe’s, and two !’s&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Found on iteration 226,597 after 19 seconds. &lt;br /&gt;
This one includes punctuation, which can take the search process a while. 
I had to try several initiating phrases before this one worked.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;twenty e&lt;br /&gt;
four f&lt;br /&gt;
one g&lt;br /&gt;
five h&lt;br /&gt;
three i&lt;br /&gt;
one l&lt;br /&gt;
ten n&lt;br /&gt;
seven o&lt;br /&gt;
seven r&lt;br /&gt;
three s&lt;br /&gt;
nine t&lt;br /&gt;
three u&lt;br /&gt;
four v&lt;br /&gt;
three w&lt;br /&gt;
one x&lt;br /&gt;
two y&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Found on iteration 208,117 after 8 seconds. &lt;br /&gt;
A nice, minimalistic, self-enumerating word list, or &lt;em&gt;reflexicon&lt;/em&gt; as Lee Sallows dubbed them.
It includes only the 16 characters &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EFGHILNORSTUVWXY&lt;/code&gt; which appear in number words below 100. 
Sallows found 24 such cycles depending on how you start the iteration. 
9 of those cycles are period 1 autograms.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/alex-b1729/autogram-this&quot;&gt;Autogram-This&lt;/a&gt; is my GitHub repo with the Python I wrote to generate and validate autograms.&lt;/p&gt;

&lt;p&gt;Here are some cool autogram related references and links I found:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Lee Sallows &lt;a href=&quot;https://www.leesallows.com/files/Reflexicons%20NEW(4c).pdf&quot;&gt;&lt;em&gt;Reflexicons&lt;/em&gt;&lt;/a&gt; essay&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.leesallows.com/files/In%20Quest%20of%20a%20Pangram1.pdf&quot;&gt;&lt;em&gt;In Quest of a Pangram&lt;/em&gt;&lt;/a&gt; includes Sallows’ very interesting description of first searching by hand, then engineering an autogram machine in 1983.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://autograms.net/&quot;&gt;autograms.net&lt;/a&gt; has lots of examples in lots of languages with lots of variety.&lt;/li&gt;
  &lt;li&gt;A cool &lt;a href=&quot;https://www.fatrazie.com/jeux-de-mots/recreamots/287-self-enumerating-pangrams&quot;&gt;history of pangrams&lt;/a&gt; at fatrazie.com. 
The site is mostly in French but this article is English. 
If you let your browser automatically translates the page to English there are no real autograms left.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://yawn.io/2019/08/28/pangrammatic-autograms.html&quot;&gt;&lt;em&gt;Pangrammatic Autograms&lt;/em&gt;&lt;/a&gt; on the blog &lt;em&gt;Papi Picante’s Spice Log&lt;/em&gt; describes finding autograms using an SAT-solver and also turns the post itself into an autogram.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <pubDate>Sun, 01 Dec 2024 00:00:00 -0700</pubDate>
      <link>https://curiosityarb.blog/2024/12/01/searching-for-autograms.html</link>
      <guid isPermaLink="true">https://curiosityarb.blog/2024/12/01/searching-for-autograms.html</guid>
    </item>
    
    
    
    <item>
      <title>Hill Climbing Art in Python</title>
      <description>&lt;p&gt;I thought it would be cool to write a hill climbing algorithm that replicates images by drawing random circles.&lt;/p&gt;

&lt;p&gt;A &lt;a href=&quot;https://en.wikipedia.org/wiki/Hill_climbing&quot;&gt;hill climbing algorithm&lt;/a&gt; proceeds toward a goal through a trial and error process. 
At each iteration, the algorithm makes a small, random change to its current solution. 
If the change brings the algorithm closer to its goal, the change is kept. 
Otherwise the algorithm discards the change and tries again.&lt;/p&gt;

&lt;p&gt;In this project, the small random changes are random circles which the algorithm draws on its generated image. 
If a random circle makes the generated image more similar to the target image its kept as part of the solution. 
Otherwise the circle is discarded and the algorithm tries a different random circle. 
After enough random circles the algorithm should generate an image that is reminiscent of the given target image but hopefully with a nice random, artistic quality.&lt;/p&gt;

&lt;hr /&gt;
&lt;h2 id=&quot;code&quot;&gt;Code&lt;/h2&gt;

&lt;p&gt;A Jupyter Notebook is available on &lt;a href=&quot;https://github.com/alex-b1729/hill-climbing-art/blob/main/hill_climbing_notes.ipynb&quot;&gt;my GitHub&lt;/a&gt; or just copy and paste code from this post.&lt;/p&gt;

&lt;p&gt;Pip install required packages:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python -m pip install --upgrade Pillow
python -m pip install --upgrade matplotlib
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Python imports:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;perf_counter&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIL&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ImageDraw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ImageChops&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;images-with-the-pillow-library&quot;&gt;Images with the Pillow library&lt;/h3&gt;

&lt;p&gt;For this post I’ll be using &lt;a href=&quot;https://commons.wikimedia.org/wiki/File:Jimi_Hendrix_1967_uncropped.jpg&quot;&gt;this&lt;/a&gt; image of Jimi Hendrix as the target image which the algorithm will replicate. 
I’m using the &lt;a href=&quot;https://pillow.readthedocs.io/en/stable/#&quot;&gt;Pillow&lt;/a&gt; library which is useful for manipulating and generating images in Python.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# open the target image
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target_image_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;images/Jimi_Hendrix_1967_uncropped.jpg&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target_image_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/hill_climbing/Jimi_Hendrix_1967_uncropped.jpg&quot; alt=&quot;Jimi on guitar&quot; /&gt;&lt;/p&gt;

&lt;p&gt;While the original image looks like it’s already in grayscale, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.convert(&apos;L&apos;)&lt;/code&gt; method ensures that the image object uses Pillow’s luminance or grayscale mode. 
We can work with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PIL.Image&lt;/code&gt; object but can also convert to a numpy array. 
Because I’m working in grayscale mode, the numpy array of this image is simply a 2d array where each pixel is represented by a number (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dtype=np.uint8&lt;/code&gt;) between 0, representing black, and 255, representing white.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# the pixels of the image as an array
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;asarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;array([[153, 133, 133, ..., 187, 196, 187],
       [155, 152, 137, ..., 190, 195, 188],
       [160, 158, 155, ..., 190, 193, 173],
       ...,
       [233, 243, 244, ..., 157, 140, 120],
       [235, 237, 237, ..., 157, 112, 109],
       [233, 235, 235, ..., 143,  94,  88]], dtype=uint8)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;asarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;asarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(954, 1280)
True
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The dimensions of the numpy array match those of the target image. 
Note that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Image.size&lt;/code&gt; has the horizontal dimension as the 0th element followed by the vertical dimension which is why I transform (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.T&lt;/code&gt;) the numpy array when checking for equality of dimensions.
Also note that Pillow image coordinates start with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(0, 0)&lt;/code&gt; in the upper left corner.&lt;/p&gt;

&lt;h3 id=&quot;drawing-circles&quot;&gt;Drawing Circles&lt;/h3&gt;
&lt;p&gt;I use Pillow’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ImageDraw&lt;/code&gt; module for drawing circles.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# image with a black circle
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ImageDraw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Draw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ellipse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# starting coordinate
&lt;/span&gt;                 &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;75&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ending coordinate
&lt;/span&gt;             &lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;im1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/hill_climbing/black_circle.jpg&quot; alt=&quot;Circle drawing example&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This code initiates a new image object, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;im1&lt;/code&gt;, that’s 100 by 100 pixels, in grayscale mode, with a white background. 
The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;draw&lt;/code&gt; object modifies the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;im1&lt;/code&gt; object in place and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xy&lt;/code&gt; parameter defines the bounding box of the ellipse.&lt;/p&gt;

&lt;h3 id=&quot;image-comparison&quot;&gt;Image comparison&lt;/h3&gt;

&lt;p&gt;The hill climbing algorithm needs a way to compare images so it can tell whether a random circle makes its generated image look more like the target image. 
Naturally, different ways of comparing images will affect how the algorithm converges toward the target image. 
This choice will change the look of the resulting image and potentially how many iterations the algorithm takes before it starts to look like the target.&lt;/p&gt;

&lt;p&gt;I’ll use the following method to measure the difference between images:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;For each pixel in the generated image, find the absolute difference in luminescence between that pixel and the corresponding pixel in the target image. 
I.e. if a particular generated pixel is white (255) and the corresponding target pixel is gray (125) the difference would be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;abs(255 - 125) = 130&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Do this for all pixels in the image.&lt;/li&gt;
  &lt;li&gt;Average the difference of all pixels and standardize the result to [0, 1].&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When many of the pixels are similar, the overall average  differences will be small, and the difference measure will be close to 0. 
With this method the difference between an image and its negative will be 1, while on average the difference between any two unrelated images will likely be close to 0.5.&lt;/p&gt;

&lt;p&gt;Pillow’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ImageChops.difference&lt;/code&gt; function performs steps 1 and 2 and returns the result.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# image to compare with im1
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ImageDraw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Draw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ellipse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# starting coordinate
&lt;/span&gt;                 &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# ending coordinate
&lt;/span&gt;             &lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;180&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;im2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/hill_climbing/gray_circle.jpg&quot; alt=&quot;Gray circle&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What’s the difference between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;im1&lt;/code&gt; from above and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;im2&lt;/code&gt;?&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;im_diff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ImageChops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;difference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;im_diff&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/hill_climbing/circle_diff.jpg&quot; alt=&quot;Circle diff&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The images have the same white background which shows as black in the difference image (difference = 0). 
The black circle in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;im1&lt;/code&gt; is completely different from the white background of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;im2&lt;/code&gt; and shows up as a white crescent (difference = 255). 
The gray circle in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;im2&lt;/code&gt; is an in between gray relative to the black circle and the white background of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;im1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I apply step 3 and find the average difference of all pixels in the difference image and standardize to between 0 and 1.&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;array_diff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;asarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;percent_diff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;array_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# standardize to percent terms
&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;im1 and im2 are &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;percent_diff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;% different&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;im1 and im2 are 21.44% different
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Putting all this in a function:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;find_img_diff_pct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;asarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ImageChops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;difference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;drawing-random-circles&quot;&gt;Drawing random circles&lt;/h3&gt;

&lt;p&gt;Similar to the choice of method for image comparison, the choice of how the algorithm makes random changes will affect convergence and the look of the generated image. 
I’d like to add some constraints to the random changes which will help the algorithm converge without sacrificing too much of the unpredictability which will make the generated image interesting.&lt;/p&gt;

&lt;p&gt;Take, for example, circle size. 
Intuitively it makes sense that the circle size should decrease as the generated image becomes more complete. 
As the hill climbing algorithm progresses, the differences between the generated and target images will be found mostly in the fine details. 
Filling in these details will require progressively smaller circles. 
Imagine how inefficient it would be if, many iterations in and with a moderately detailed generated image, the algorithm randomly drew a very large circle in the middle of the generated image.&lt;/p&gt;

&lt;p&gt;Rather than simply using a linear decrease in circle size, I’d like the algorithm to spend more time with smaller circles. 
Smaller circles are more difficult to randomly place in the correct spot. 
To flush out some of the fine detail in the target image the algorithm will need more tries to place small circles in the right place. 
Furthermore, a greater number of small circles is required to fill in this detail whereas only a few well placed large circles are needed to appropriately match the target image background.&lt;/p&gt;

&lt;p&gt;After experimenting a bit I decided to use a modified logarithmic function to interpolate between a starting and ending circle radius. 
The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;choose_circle_radius&lt;/code&gt; function takes a float in [0, 1] representing the percentage of iterations completed and returns a circle radius, with some random variability. 
The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alpha_func&lt;/code&gt; function takes the percentage of iterations complete, maps it to a logarithmic function, and returns a value in [0, 1] which I use to lerp between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CIRCLE_START_RADIUS&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CIRCLE_END_RADIUS&lt;/code&gt;. 
I add variability to the circle radius with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CIRCLE_PERCENT_VARIANCE&lt;/code&gt; parameter.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;CIRCLE_START_RADIUS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;CIRCLE_END_RADIUS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;CIRCLE_PERCENT_VARIANCE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;choose_circle_radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;percent_complete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alpha_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;percent_complete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;circle_radius_target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CIRCLE_START_RADIUS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; 
                               &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CIRCLE_START_RADIUS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CIRCLE_END_RADIUS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
                               &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;
                              &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# add a bit of variance to the circle radius
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;circle_radius&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;uniform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CIRCLE_PERCENT_VARIANCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                       &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CIRCLE_PERCENT_VARIANCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
                        &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circle_radius_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circle_radius&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alpha_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;x in range [0, 1] returns range [0, 1]&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# return x  # linear
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loglike&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loglike&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stretch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stretch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stretch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here’s an example of how the circle radius will decrease as iterations increase.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;choose_circle_radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_x&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;xlabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;percent_complete&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ylabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;random circle radius&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/hill_climbing/circle_radius.jpg&quot; alt=&quot;Circle diff&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can see how the radius starts at 100 pixels but quickly decreases and spends around half of the iterations below 20 pixels.&lt;/p&gt;

&lt;h2 id=&quot;complete-script&quot;&gt;Complete script&lt;/h2&gt;

&lt;p&gt;As a reminder, the hill climbing algorithm follows these steps for each iteration:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Measure the difference between the generated image and target image.&lt;/li&gt;
  &lt;li&gt;Draw a random circle on the generated image.&lt;/li&gt;
  &lt;li&gt;Measure the difference between this new generated image and the target.&lt;/li&gt;
  &lt;li&gt;Is the new image with the random circle more similar to the target?
    &lt;ul&gt;
      &lt;li&gt;If so, keep the circle!&lt;/li&gt;
      &lt;li&gt;Otherwise, discard it.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Rinse and repeat…&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s the full code.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;perf_counter&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PIL&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ImageDraw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ImageChops&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;BLACK&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;WHITE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;CIRCLE_START_RADIUS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;CIRCLE_END_RADIUS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;CIRCLE_PERCENT_VARIANCE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;MAX_ITERATIONS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100_000&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# for reproducibility
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SEED&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1111&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SEED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SEED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;find_img_diff_pct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;asarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ImageChops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;difference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;choose_circle_radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;percent_complete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alpha_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;percent_complete&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;circle_radius_target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CIRCLE_START_RADIUS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; 
                               &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CIRCLE_START_RADIUS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CIRCLE_END_RADIUS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
                               &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;
                              &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# add a bit of variance to the circle radius
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;circle_radius&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;uniform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CIRCLE_PERCENT_VARIANCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                       &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CIRCLE_PERCENT_VARIANCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
                        &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circle_radius_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circle_radius&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alpha_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;x in range [0, 1] returns range [0, 1]&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# return x  # linear: for x in [0, 1] just returns x
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loglike&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loglike&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stretch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stretch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stretch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;# open the target image
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target_image_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;images/Jimi_Hendrix_1967_uncropped.jpg&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target_image_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;np_target&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;X_MAX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Y_MAX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# initial blank starting image
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_generated&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;WHITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 1 initial difference
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diff_generated_init&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;find_img_diff_pct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_generated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;diff_generated&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;diff_generated_init&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;diff_compare&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;diff_generated&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;num_pixels&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# keep track of progress
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diff_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;guess_diff_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;good_guess_pct_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;good_guesses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# time progress
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;perf_counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_ITERATIONS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# random radius
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;circle_radius&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;choose_circle_radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_ITERATIONS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# random center point
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;circle_center_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;randrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;X_MAX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;circle_center_y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;randrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Y_MAX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# start/end points as (x, y)
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;circle_start_point&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;circle_center_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circle_radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                          &lt;span class=&quot;n&quot;&gt;circle_center_y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circle_radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;circle_stop_point&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;circle_center_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circle_radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                         &lt;span class=&quot;n&quot;&gt;circle_center_y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circle_radius&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# random color
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;circle_color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;randrange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BLACK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WHITE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# copy current working image
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;im_compare&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ImageChops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;duplicate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_generated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# draw a random shape on it
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ImageDraw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Draw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_compare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;draw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ellipse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;circle_start_point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;circle_stop_point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; 
                 &lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;circle_color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# find difference of new im_compare with target
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;diff_compare&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;find_img_diff_pct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im_compare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# if new image is less different than previous generated im
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;diff_compare&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;diff_generated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;# switch working image to the new im_compare
&lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;im_generated&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ImageChops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;duplicate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_compare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;diff_generated&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;diff_compare&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;good_guesses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# else just keep the working image without the random shape
&lt;/span&gt;    
    &lt;span class=&quot;c1&quot;&gt;# print progress
&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10_000&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# if epoch % 2500 == 0:
&lt;/span&gt;        &lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;epoch: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;, difference: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diff_generated&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;diff_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diff_generated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;guess_diff_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diff_compare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;good_guess_pct_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;good_guesses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
&lt;span class=&quot;n&quot;&gt;t1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;perf_counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;epoch: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;, difference: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;diff_generated&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;number of good guesses: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;good_guesses&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;good guess percent: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;good_guesses&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epoch&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Total time: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; min&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# combine generated, difference, and target images for easier viewing
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_diff&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ImageChops&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;difference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;im_generated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;new_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;im_display&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;im_display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;paste&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_generated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;resize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;im_display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;paste&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_diff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;resize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;im_display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;paste&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;im_target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;resize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;im_display&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;epoch: 0, difference: 0.3492539346713528
epoch: 10000, difference: 0.11385856635631192
epoch: 20000, difference: 0.10548068054527891
epoch: 30000, difference: 0.0974889815071731
epoch: 40000, difference: 0.09126970547128704
epoch: 50000, difference: 0.08784024052493114
epoch: 60000, difference: 0.0839930876700785
epoch: 70000, difference: 0.080742144145394
epoch: 80000, difference: 0.07778092821247996
epoch: 90000, difference: 0.07505323945615983
epoch: 100000, difference: 0.07270399787273399
number of good guesses: 3940
good guess percent: 0.0394
Total time: 6.09 min
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/hill_climbing/hill_climbing_01.jpg&quot; alt=&quot;Hill Climbing 01&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Left to right - the final generated, difference, and target images.&lt;/p&gt;

&lt;h2 id=&quot;and-the-final-result&quot;&gt;And the final result!&lt;/h2&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;im_generated&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/hill_climbing/jimi_on_guitar.jpg&quot; alt=&quot;Hill Climbing 01&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Overall I’m really pleased with the result. 
At a glance it’s pretty clearly Jimi playing his guitar but when you try to focus on any details they just become a mess of circles. 
I especially like how the headstock on the guitar is just a handful of black and gray circles against the light background and how the curly top righ part of Jimi’s hair was generated.&lt;/p&gt;

&lt;p&gt;The final difference with the target image is 7.3%. 
Although the script tries 100,000 random circles only 3,940 of them improve the generated image and are included in the final result. 
3.94% seems pretty inefficient although I haven’t experimented to see how much better it could get. 
Altering the random circle generation method could improve this.&lt;/p&gt;

&lt;p&gt;Here’s a plot of the difference between generated and target images at each iteration. 
I also plot the percent of circle guesses up to that point which have improved the generated image - i.e. correct guesses.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# plot progress stats
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAX_ITERATIONS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;subplots&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;good_guess_pct_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;correct guess %&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guess_diff_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;guess difference&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;set_yscale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;set_xlabel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Epoch&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;fig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;legend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/hill_climbing/hill_climbing_progress.jpg&quot; alt=&quot;Hill Climbing 01&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I find it interesting that the frequency of correct guesses actually starts increasing around half way through. 
I’ve noticed this pattern across a bunch of different iterations of this project but don’t have a good sense why it happens. 
Maybe once the circles reach a certain, small size they start improving the similarity to the target image simply because they match some of the natural graininess of the photo. 
If you have thoughts shoot me an email.&lt;/p&gt;

&lt;hr /&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;This is just one, simple application of hill climbing to generate art. 
You could apply this technique in many other ways or combine with other methods as part of a larger generative art piece.&lt;/p&gt;

&lt;p&gt;Some variations could include:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Different shapes or a combination of shapes.&lt;/li&gt;
  &lt;li&gt;Adding color: You could choose color completely randomly or use the background image to determine the color of shapes. 
You could even use a color format like hsl, set the hue and saturation, and just randomly choose the luminescence.&lt;/li&gt;
  &lt;li&gt;I tried altering the random placement of the circles by making them more likely to appear in areas of the image where the difference was greatest. 
This took a lot longer (although my implementation was probably inefficient) and didn’t produce results that were that much better. 
But maybe there’s more that could be done along these lines.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re aware of any cool uses of hill climbing algorithms in generative art I’d be interested to see them. 
Or if you have thoughts, questions, or critiques, shoot me an email at hello at abrefeld.anonaddy.com.&lt;/p&gt;
</description>
      <pubDate>Wed, 03 Apr 2024 00:00:00 -0600</pubDate>
      <link>https://curiosityarb.blog/2024/04/03/hill-climbing-art.html</link>
      <guid isPermaLink="true">https://curiosityarb.blog/2024/04/03/hill-climbing-art.html</guid>
    </item>
    
    
  </channel>
</rss>
