You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

490 lines
52 KiB

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <feed xmlns="http://www.w3.org/2005/Atom">
  3. <title>Colin McMillen's Blog</title>
  4. <link href="https://www.mcmillen.dev"/>
  5. <link rel="self" href="https://www.mcmillen.dev/feed.atom"/>
  6. <updated>2021-07-21T20:53:40-04:00</updated>
  7. <author>
  8. <name>Colin McMillen</name>
  9. </author>
  10. <id>https://www.mcmillen.dev/</id>
  11. <entry>
  12. <title>Creating robot behaviors with Python generators</title>
  13. <id>https://www.mcmillen.dev/blog/20070502-robot-behaviors-python.html</id>
  14. <link rel="alternate" href="https://www.mcmillen.dev/blog/20070502-robot-behaviors-python.html"/>
  15. <content type="html">
  16. <![CDATA[
  17. <h1 id="creating-robot-behaviors-with-python-generators">Creating robot behaviors with Python generators</h1>
  18. <p><em>Posted 2007-05-02.</em></p>
  19. <p><a href="https://en.wikipedia.org/wiki/Generator_(computer_programming)">Generators</a> are a <a href="https://docs.python.org/2.7/reference/simple_stmts.html#the-yield-statement">powerful feature</a> of the Python programming language. In a nutshell, generators let you write a function that behaves like an iterator. The standard approach to programming robot behaviors is based on state machines. However, robotics code is <em>full</em> of special cases, so a complex behavior will typically end up with a lot of bookkeeping cruft. Generators let us simplify the bookkeeping and express the desired behavior in a straightforward manner.</p>
  20. <p>(Idea originally due to <a href="http://www.cs.cmu.edu/~jbruce/">Jim Bruce</a>.)</p>
  21. <p>I&rsquo;ve worked for several years on <a href="https://robocup.org">RoboCup</a>, the international robot soccer competition. <a href="http://www.cs.cmu.edu/~robosoccer/legged/">Our software</a> is written in a mixture of C++ (for low-level localization and vision algorithms) and Python (for high-level behaviors). Let&rsquo;s say we want to write a simple goalkeeper for a robot soccer team. Our keeper will be pretty simple; here&rsquo;s a list of the requirements:</p>
  22. <ol>
  23. <li>If the ball is far away, stand in place.</li>
  24. <li>If the ball is near by, dive to block it. Dive to the left if the ball is to the left; dive to the right if the ball is to the right.</li>
  25. <li>If we choose a &ldquo;dive&rdquo; action, then &ldquo;stand&rdquo; on the next frame, nothing will happen. (Well, maybe the robot will twitch briefly....) So when we choose to dive, we need to commit to sending the same dive command for some time (let&rsquo;s say one second).</li>
  26. </ol>
  27. <p>The usual approach to robot behavior design relies on hierarchical state machines. Specifically, we might be in a &ldquo;standing&rdquo; state while the ball is far away; when the ball becomes close, we enter a &ldquo;diving&rdquo; state that persists for one second. Because of requirement 3, this solution will have a few warts: we need to keep track of how much time we&rsquo;ve spent in the dive state. Every time we add a special case like this, we need to keep some extra state information around. Since robotics code is full of special cases, we tend to end up with a lot of bookkeeping cruft. In contrast, generators will let us clearly express the desired behavior.</p>
  28. <p>On to the state-machine approach. First, we&rsquo;ll have a class called Features that abstracts the robot&rsquo;s raw sensor data. For this example, we only care whether the ball is near/far and left/right, so Features will just contain two boolean variables:</p>
  29. <div class="codehilite"><pre><span></span><span class="k">class</span> <span class="nc">Features</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  30. <span class="n">ballFar</span> <span class="o">=</span> <span class="bp">True</span>
  31. <span class="n">ballOnLeft</span> <span class="o">=</span> <span class="bp">True</span>
  32. </pre></div>
  33. <p>Next, we make the goalkeeper. The keeper&rsquo;s behavior is specified by the <code>next()</code> function, which is called thirty times per second by the robot&rsquo;s main event loop (every time the on-board camera produces a new image). The <code>next()</code> function returns one of three actions: <code>"stand"</code>, <code>"diveLeft"</code>, or <code>"diveRight"</code>, based on the current values of the Features object. For now, let&rsquo;s pretend that requirement 3 doesn&rsquo;t exist.</p>
  34. <div class="codehilite"><pre><span></span><span class="k">class</span> <span class="nc">Goalkeeper</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  35. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">features</span><span class="p">):</span>
  36. <span class="bp">self</span><span class="o">.</span><span class="n">features</span> <span class="o">=</span> <span class="n">features</span>
  37. <span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  38. <span class="n">features</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">features</span>
  39. <span class="k">if</span> <span class="n">features</span><span class="o">.</span><span class="n">ballFar</span><span class="p">:</span>
  40. <span class="k">return</span> <span class="s1">&#39;stand&#39;</span>
  41. <span class="k">else</span><span class="p">:</span>
  42. <span class="k">if</span> <span class="n">features</span><span class="o">.</span><span class="n">ballOnLeft</span><span class="p">:</span>
  43. <span class="k">return</span> <span class="s1">&#39;diveLeft&#39;</span>
  44. <span class="k">else</span><span class="p">:</span>
  45. <span class="k">return</span> <span class="s1">&#39;diveRight&#39;</span>
  46. </pre></div>
  47. <p>That was simple enough. The constructor takes in the <code>Features</code> object; the <code>next()</code> method checks the current <code>Features</code> values and returns the correct action. Now, how about satisfying requirement 3? When we choose to dive, we need to keep track of two things: how long we need to stay in the <code>"dive"</code> state and which direction we dove. We&rsquo;ll do this by adding a couple of instance variables (<code>self.diveFramesRemaining</code> and <code>self.lastDiveCommand</code>) to the Goalkeeper class. These variables are set when we initiate the dive. At the top of the <code>next()</code> function, we check if <code>self.diveFramesRemaining</code> is positive; if so, we can immediately return <code>self.lastDiveCommand</code> without consulting the <code>Features</code>. Here&rsquo;s the code:</p>
  48. <div class="codehilite"><pre><span></span><span class="k">class</span> <span class="nc">Goalkeeper</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  49. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">features</span><span class="p">):</span>
  50. <span class="bp">self</span><span class="o">.</span><span class="n">features</span> <span class="o">=</span> <span class="n">features</span>
  51. <span class="bp">self</span><span class="o">.</span><span class="n">diveFramesRemaining</span> <span class="o">=</span> <span class="mi">0</span>
  52. <span class="bp">self</span><span class="o">.</span><span class="n">lastDiveCommand</span> <span class="o">=</span> <span class="bp">None</span>
  53. <span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  54. <span class="n">features</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">features</span>
  55. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">diveFramesRemaining</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
  56. <span class="bp">self</span><span class="o">.</span><span class="n">diveFramesRemaining</span> <span class="o">-=</span> <span class="mi">1</span>
  57. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastDiveCommand</span>
  58. <span class="k">else</span><span class="p">:</span>
  59. <span class="k">if</span> <span class="n">features</span><span class="o">.</span><span class="n">ballFar</span><span class="p">:</span>
  60. <span class="k">return</span> <span class="s1">&#39;stand&#39;</span>
  61. <span class="k">else</span><span class="p">:</span>
  62. <span class="k">if</span> <span class="n">features</span><span class="o">.</span><span class="n">ballOnLeft</span><span class="p">:</span>
  63. <span class="n">command</span> <span class="o">=</span> <span class="s1">&#39;diveLeft&#39;</span>
  64. <span class="k">else</span><span class="p">:</span>
  65. <span class="n">command</span> <span class="o">=</span> <span class="s1">&#39;diveRight&#39;</span>
  66. <span class="bp">self</span><span class="o">.</span><span class="n">lastDiveCommand</span> <span class="o">=</span> <span class="n">command</span>
  67. <span class="bp">self</span><span class="o">.</span><span class="n">diveFramesRemaining</span> <span class="o">=</span> <span class="mi">29</span>
  68. <span class="k">return</span> <span class="n">command</span>
  69. </pre></div>
  70. <p>This satisfies all the requirements, but it&rsquo;s ugly. We&rsquo;ve added a couple of bookkeeping variables to the Goalkeeper class. Code to properly maintain these variables is sprinkled all over the <code>next()</code> function. Even worse, the structure of the code no longer accurately represents the programmer&rsquo;s intent: the top-level if-statement depends on the state of the robot rather than the state of the world. The intent of the original <code>next()</code> function is much easier to discern. (In real code, we could use a state-machine class to tidy things up a bit, but the end result would still be ugly when compared to our original <code>next()</code> function.)</p>
  71. <p>With generators, we can preserve the form of the original <code>next()</code> function and keep the bookkeeping only where it&rsquo;s needed. If you&rsquo;re not familiar with generators, you can think of them as a special kind of function. The <code>yield</code> keyword is essentially equivalent to <code>return</code>, but the next time the generator is called, <em>execution continues from the point of the last <code>yield</code></em>, preserving the state of all local variables. With <code>yield</code>, we can use a <code>for</code> loop to &ldquo;return&rdquo; the same dive command the next 30 times the function is called! Lines 11-16 of the below code show the magic:</p>
  72. <div class="codehilite"><pre><span></span><span class="k">class</span> <span class="nc">GoalkeeperWithGenerator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  73. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">features</span><span class="p">):</span>
  74. <span class="bp">self</span><span class="o">.</span><span class="n">features</span> <span class="o">=</span> <span class="n">features</span>
  75. <span class="k">def</span> <span class="nf">behavior</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  76. <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
  77. <span class="n">features</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">features</span>
  78. <span class="k">if</span> <span class="n">features</span><span class="o">.</span><span class="n">ballFar</span><span class="p">:</span>
  79. <span class="k">yield</span> <span class="s1">&#39;stand&#39;</span>
  80. <span class="k">else</span><span class="p">:</span>
  81. <span class="k">if</span> <span class="n">features</span><span class="o">.</span><span class="n">ballOnLeft</span><span class="p">:</span>
  82. <span class="n">command</span> <span class="o">=</span> <span class="s1">&#39;diveLeft&#39;</span>
  83. <span class="k">else</span><span class="p">:</span>
  84. <span class="n">command</span> <span class="o">=</span> <span class="s1">&#39;diveRight&#39;</span>
  85. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">30</span><span class="p">):</span>
  86. <span class="k">yield</span> <span class="n">command</span>
  87. </pre></div>
  88. <p>Here&rsquo;s a simple driver script that shows how to use our goalkeepers:</p>
  89. <div class="codehilite"><pre><span></span><span class="kn">import</span> <span class="nn">random</span>
  90. <span class="n">f</span> <span class="o">=</span> <span class="n">Features</span><span class="p">()</span>
  91. <span class="n">g1</span> <span class="o">=</span> <span class="n">Goalkeeper</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
  92. <span class="n">g2</span> <span class="o">=</span> <span class="n">GoalkeeperWithGenerator</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="o">.</span><span class="n">behavior</span><span class="p">()</span>
  93. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">10000</span><span class="p">):</span>
  94. <span class="n">f</span><span class="o">.</span><span class="n">ballFar</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mf">0.1</span>
  95. <span class="n">f</span><span class="o">.</span><span class="n">ballOnLeft</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span> <span class="o">&lt;</span> <span class="mf">0.5</span>
  96. <span class="n">g1action</span> <span class="o">=</span> <span class="n">g1</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  97. <span class="n">g2action</span> <span class="o">=</span> <span class="n">g2</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  98. <span class="k">print</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="se">\t</span><span class="si">%s</span><span class="se">\t</span><span class="si">%s</span><span class="se">\t</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
  99. <span class="n">f</span><span class="o">.</span><span class="n">ballFar</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">ballOnLeft</span><span class="p">,</span> <span class="n">g1action</span><span class="p">,</span> <span class="n">g2action</span><span class="p">)</span>
  100. <span class="k">assert</span><span class="p">(</span><span class="n">g1action</span> <span class="o">==</span> <span class="n">g2action</span><span class="p">)</span>
  101. </pre></div>
  102. <p>&hellip; and we&rsquo;re done! I hope you&rsquo;ll agree that the generator-based keeper is much easier to understand and maintain than the state-machine-based keeper. You can grab the full source code below and take a look for yourself.</p>
  103. <div class="codehilite"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
  104. <span class="k">class</span> <span class="nc">Features</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  105. <span class="n">ballFar</span> <span class="o">=</span> <span class="bp">True</span>
  106. <span class="n">ballOnLeft</span> <span class="o">=</span> <span class="bp">True</span>
  107. <span class="k">class</span> <span class="nc">Goalkeeper</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  108. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">features</span><span class="p">):</span>
  109. <span class="bp">self</span><span class="o">.</span><span class="n">features</span> <span class="o">=</span> <span class="n">features</span>
  110. <span class="bp">self</span><span class="o">.</span><span class="n">diveFramesRemaining</span> <span class="o">=</span> <span class="mi">0</span>
  111. <span class="bp">self</span><span class="o">.</span><span class="n">lastDiveCommand</span> <span class="o">=</span> <span class="bp">None</span>
  112. <span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  113. <span class="n">features</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">features</span>
  114. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">diveFramesRemaining</span><span class="p">:</span>
  115. <span class="bp">self</span><span class="o">.</span><span class="n">diveFramesRemaining</span> <span class="o">-=</span> <span class="mi">1</span>
  116. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastDiveCommand</span>
  117. <span class="k">else</span><span class="p">:</span>
  118. <span class="k">if</span> <span class="n">features</span><span class="o">.</span><span class="n">ballFar</span><span class="p">:</span>
  119. <span class="k">return</span> <span class="s1">&#39;stand&#39;</span>
  120. <span class="k">else</span><span class="p">:</span>
  121. <span class="k">if</span> <span class="n">features</span><span class="o">.</span><span class="n">ballOnLeft</span><span class="p">:</span>
  122. <span class="n">command</span> <span class="o">=</span> <span class="s1">&#39;diveLeft&#39;</span>
  123. <span class="k">else</span><span class="p">:</span>
  124. <span class="n">command</span> <span class="o">=</span> <span class="s1">&#39;diveRight&#39;</span>
  125. <span class="bp">self</span><span class="o">.</span><span class="n">lastDiveCommand</span> <span class="o">=</span> <span class="n">command</span>
  126. <span class="bp">self</span><span class="o">.</span><span class="n">diveFramesRemaining</span> <span class="o">=</span> <span class="mi">29</span>
  127. <span class="k">return</span> <span class="n">command</span>
  128. <span class="k">class</span> <span class="nc">GoalkeeperWithGenerator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  129. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">features</span><span class="p">):</span>
  130. <span class="bp">self</span><span class="o">.</span><span class="n">features</span> <span class="o">=</span> <span class="n">features</span>
  131. <span class="k">def</span> <span class="nf">behavior</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  132. <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
  133. <span class="n">features</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">features</span>
  134. <span class="k">if</span> <span class="n">features</span><span class="o">.</span><span class="n">ballFar</span><span class="p">:</span>
  135. <span class="k">yield</span> <span class="s1">&#39;stand&#39;</span>
  136. <span class="k">else</span><span class="p">:</span>
  137. <span class="k">if</span> <span class="n">features</span><span class="o">.</span><span class="n">ballOnLeft</span><span class="p">:</span>
  138. <span class="n">command</span> <span class="o">=</span> <span class="s1">&#39;diveLeft&#39;</span>
  139. <span class="k">else</span><span class="p">:</span>
  140. <span class="n">command</span> <span class="o">=</span> <span class="s1">&#39;diveRight&#39;</span>
  141. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">30</span><span class="p">):</span>
  142. <span class="k">yield</span> <span class="n">command</span>
  143. <span class="kn">import</span> <span class="nn">random</span>
  144. <span class="n">f</span> <span class="o">=</span> <span class="n">Features</span><span class="p">()</span>
  145. <span class="n">g1</span> <span class="o">=</span> <span class="n">Goalkeeper</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
  146. <span class="n">g2</span> <span class="o">=</span> <span class="n">GoalkeeperWithGenerator</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="o">.</span><span class="n">behavior</span><span class="p">()</span>
  147. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">10000</span><span class="p">):</span>
  148. <span class="n">f</span><span class="o">.</span><span class="n">ballFar</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mf">0.1</span>
  149. <span class="n">f</span><span class="o">.</span><span class="n">ballOnLeft</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span> <span class="o">&lt;</span> <span class="mf">0.5</span>
  150. <span class="n">g1action</span> <span class="o">=</span> <span class="n">g1</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  151. <span class="n">g2action</span> <span class="o">=</span> <span class="n">g2</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
  152. <span class="k">print</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="se">\t</span><span class="si">%s</span><span class="se">\t</span><span class="si">%s</span><span class="se">\t</span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span>
  153. <span class="n">f</span><span class="o">.</span><span class="n">ballFar</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">ballOnLeft</span><span class="p">,</span> <span class="n">g1action</span><span class="p">,</span> <span class="n">g2action</span><span class="p">)</span>
  154. <span class="k">assert</span><span class="p">(</span><span class="n">g1action</span> <span class="o">==</span> <span class="n">g2action</span><span class="p">)</span>
  155. </pre></div>
  156. ]]>
  157. </content>
  158. <updated>2007-05-02T12:00:00-04:00</updated>
  159. </entry>
  160. <entry>
  161. <title>Emacs Tips</title>
  162. <id>https://www.mcmillen.dev/blog/20070522-emacs-tips.html</id>
  163. <link rel="alternate" href="https://www.mcmillen.dev/blog/20070522-emacs-tips.html"/>
  164. <content type="html">
  165. <![CDATA[
  166. <h1 id="emacs-tips">Emacs Tips</h1>
  167. <p><em>Posted 2007-05-22, updated 2021-07-01.</em></p>
  168. <p>These are some emacs keybindings (and other functions) that I once found useful. I&rsquo;ve mostly used Sublime Text for the last few years, however.</p>
  169. <h2 id="editing">Editing</h2>
  170. <p><code>C-[SPC]</code>: set mark<br>
  171. <code>C-x C-x</code>: exchange point and mark<br>
  172. <code>C-w</code>: kill (AKA &ldquo;cut&rdquo;)<br>
  173. <code>M-w</code>: kill-ring-save (AKA &ldquo;copy&rdquo;)<br>
  174. <code>C-y</code>: yank (AKA &ldquo;paste&rdquo;)<br>
  175. <code>M-h</code>: Put region around current paragraph (mark-paragraph).<br>
  176. <code>C-x h</code>: Put region around the entire buffer (mark-whole-buffer).<br>
  177. <code>C-u C-[SPC]</code>: Move in mark ring<br>
  178. <code>M-d</code>: Kill word<br>
  179. <code>M-[DEL]</code>: Kill word backwards<br>
  180. <code>C-M-k</code>: Kill the following balanced expression (kill-sexp)</p>
  181. <h2 id="registers">Registers</h2>
  182. <p><code>C-x r r</code>: Save position of point in register r (point-to-register).<br>
  183. <code>C-x r j r</code>: Jump to the position saved in register r (jump-to-register).<br>
  184. <code>C-x r s r</code>: Copy region into register r (copy-to-register).<br>
  185. <code>C-x r i r</code>: Insert text from register r (insert-register).</p>
  186. <h2 id="bookmarks">Bookmarks</h2>
  187. <p><code>C-x r m [RET]</code>: Set the bookmark for the visited file, at point.<br>
  188. <code>C-x r m bookmark [RET]</code>: Set the bookmark named bookmark at point (bookmark-set).<br>
  189. <code>C-x r b bookmark [RET]</code>: Jump to the bookmark named bookmark (bookmark-jump).<br>
  190. <code>C-x r l</code>: List all bookmarks (list-bookmarks).<br>
  191. <code>M-x bookmark-save</code>: Save all the current bookmark values in the default bookmark file.</p>
  192. <h2 id="miscellaneous">Miscellaneous</h2>
  193. <p><code>M-`</code> shows the menu.<br>
  194. <code>M-x highlight-changes-mode</code> toggles showing the changes you&rsquo;ve made to the file since the last save.</p>
  195. ]]>
  196. </content>
  197. <updated>2007-05-22T12:00:00-04:00</updated>
  198. </entry>
  199. <entry>
  200. <title>Gnokii Tips</title>
  201. <id>https://www.mcmillen.dev/blog/20070522-gnokii-tips.html</id>
  202. <link rel="alternate" href="https://www.mcmillen.dev/blog/20070522-gnokii-tips.html"/>
  203. <content type="html">
  204. <![CDATA[
  205. <h1 id="gnokii-tips">Gnokii Tips</h1>
  206. <p><em>Posted 2007-05-22, updated 2021-07-01.</em></p>
  207. <p>I own a Nokia 6102i phone (provided by Cingular). <a href="http://gnokii.org">gnokii</a> is a Linux program that lets me interface with the phone. Here are some recipes:</p>
  208. <h2 id="file-io">File I/O</h2>
  209. <p><code>gnokii --getfilelist "A:\\predefgallery\\predeftones\\predefringtones\\*"</code></p>
  210. <p><code>gnokii --putfile WiiSports.mp3 "A:\\predefgallery\\predeftones\\predefringtones\\WiiSports.mp3"</code></p>
  211. <h2 id="ring-tones">Ring Tones</h2>
  212. <p>Voice mail picks up in 20 seconds, so a ring tone should be about 20 seconds long.</p>
  213. <p>The easiest way to chop an MP3 in Linux is with <code>dd</code>; the drawback is that you need to specify length in KB, not time. To chop an MP3 to be 200 KB long, do:</p>
  214. <p><code>dd if=Mii\ Channel.mp3 of=MiiChan2.mp3 bs=1k count=200</code></p>
  215. <h2 id="phonebook">Phonebook</h2>
  216. <p>To make a Phonebook.ldif file from the phone (suitable for import into Thunderbird):</p>
  217. <p><code>gnokii --getphonebook ME 1 end --ldif &gt; Phonebook.ldif</code></p>
  218. <p>To add the entries in Phonebook.ldif to the phone:</p>
  219. <p><code>cat Phonebook.ldif | gnokii --writephonebook -m ME --find-free --ldif</code></p>
  220. <p>You can specify <code>--overwrite</code> instead of <code>--find-free</code> if you want to overwrite all the entries, but this will lose some data (e.g. speed dial, preferred numbers).</p>
  221. <h2 id="multimedia">Multimedia</h2>
  222. <p>You can get photos like this:<br>
  223. <code>gnokii --getfile "A:\\predefgallery\\predefphotos\\Image000.jpg"</code><br>
  224. They are 640x480 JPG files. (You can also configure the camera so that it takes pictures at 80x96.)</p>
  225. <p>You can also store files:<br>
  226. <code>gnokii --putfile silly.jpg "A:\\predefgallery\\predefphotos\\silly.jpg"</code><br>
  227. These show up on the phone in <code>My Stuff/Images</code>. The files don&rsquo;t need to be any specific size; they are autoscaled. GIFs probably also work.</p>
  228. <p>Videos live here:<br>
  229. <code>gnokii --getfile "A:\\predefgallery\\predefvideos\\Video000.3gp"</code><br>
  230. VLC seems to be able to play <code>.3gp</code> files, but the audio doesn&rsquo;t work.</p>
  231. <p>Audio recordings live here:<br>
  232. <code>gnokii --getfile "A:\\predefgallery\\predefrecordings\\Audio000.amr"</code></p>
  233. <p>Unfortunately, nothing I knew of in 2007 (when this page was first written) would play <code>.amr</code> files, but these days (2021) perhaps <code>ffmpeg input.amr output.mp3</code> would work. You might have to use the <code>-ar</code> flag to specify the audio rate. I haven&rsquo;t actually tried this though!</p>
  234. ]]>
  235. </content>
  236. <updated>2007-05-22T12:00:00-04:00</updated>
  237. </entry>
  238. <entry>
  239. <title>LaTeX Tips</title>
  240. <id>https://www.mcmillen.dev/blog/20070522-latex-tips.html</id>
  241. <link rel="alternate" href="https://www.mcmillen.dev/blog/20070522-latex-tips.html"/>
  242. <content type="html">
  243. <![CDATA[
  244. <h1 id="latex-tips">LaTeX Tips</h1>
  245. <p><em>Posted 2007-05-22; updated 2021-07-01.</em></p>
  246. <p>Note that these instructions are over a decade old. Some documentation may be out of date. :)</p>
  247. <h2 id="embedding-fonts-in-pdfs">Embedding fonts in PDFs</h2>
  248. <p>To check whether fonts are embedded, use <code>pdffonts</code>, which is included with <code>xpdf</code>. <code>pdffonts</code> gives output that looks like this:</p>
  249. <div class="codehilite"><pre><span></span>$ pdffonts paper.pdf
  250. name type emb sub uni object ID
  251. ------------------------------------ ------------ --- --- --- ---------
  252. FHQIOS+NimbusRomNo9L-Medi Type 1 yes yes no 6 0
  253. NEESMN+NimbusRomNo9L-Regu Type 1 yes yes no 9 0
  254. PJQNOS+CMSY10 Type 1 yes yes no 12 0
  255. </pre></div>
  256. <p>You want <code>emb</code> to be <code>yes</code> for all fonts (and possibly <code>sub</code> as well; also, all fonts should be Type 1, not Type 3). By default in Ubuntu, pdflatex should embed all fonts. Just in case, you can check <code>/etc/texmf/updmap.d/00updmap.cfg</code>, which should have a line like this:</p>
  257. <p><code>pdftexDownloadBase14 true</code></p>
  258. <p>If it&rsquo;s set to <code>false</code>, change it to <code>true</code>, then run <code>update-updmap</code> as root. Remake the PDF; if it still has non-embedded fonts, your figures are probably to blame. Check your PDF figures and make sure their fonts are embedded (using the <code>pdffonts</code> command). For anything that doesn&rsquo;t have embedded fonts, you can try the following magical invocation:</p>
  259. <div class="codehilite"><pre><span></span>gs -dSAFER -dNOPLATFONTS -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \
  260. -sPAPERSIZE=letter -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer \
  261. -dCompatibilityLevel=1.4 -dMaxSubsetPct=100 -dSubsetFonts=true \
  262. -dEmbedAllFonts=true -sOutputFile=figures/Mprime-new.pdf -f figures/Mprime.pdf
  263. </pre></div>
  264. <p>This creates a file <code>figures/Mprime-new.pdf</code> that is hopefully identical to the input file <code>figures/Mprime.pdf</code>, except that the fonts are embedded. Run <code>pdffonts</code> on it to check.</p>
  265. <p>Once all your figures are in PDF format, remake the paper again. Hopefully, all your fonts are now embedded &mdash; check again with <code>pdffonts</code>.</p>
  266. ]]>
  267. </content>
  268. <updated>2007-05-22T12:00:00-04:00</updated>
  269. </entry>
  270. <entry>
  271. <title>Vim Tips</title>
  272. <id>https://www.mcmillen.dev/blog/20070807-vim-tips.html</id>
  273. <link rel="alternate" href="https://www.mcmillen.dev/blog/20070807-vim-tips.html"/>
  274. <content type="html">
  275. <![CDATA[
  276. <h1 id="vim-tips">Vim Tips</h1>
  277. <p><em>Posted 2007-08-07.</em></p>
  278. <p>Here&rsquo;s some links about learning/mastering vim.</p>
  279. <h2 id="why-use-vim">Why use vim?</h2>
  280. <ul>
  281. <li><a href="http://blog.ngedit.com/2005/06/03/the-vi-input-model/">The vi input model - laptop keyboards suck</a></li>
  282. <li><a href="http://www.viemu.com/a-why-vi-vim.html">Why, oh WHY, do those nutheads use vi?</a></li>
  283. </ul>
  284. <h2 id="tutorials">Tutorials</h2>
  285. <ul>
  286. <li><a href="http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html">Graphical vim cheat sheet and tutorial</a> &mdash; <a href="http://www.glump.net/dokuwiki/howto/vim_graphical_cheat_sheet">PDF version</a></li>
  287. </ul>
  288. ]]>
  289. </content>
  290. <updated>2007-08-07T12:00:00-04:00</updated>
  291. </entry>
  292. <entry>
  293. <title>93% of Paint Splatters are Valid Perl Programs</title>
  294. <id>https://www.mcmillen.dev/sigbovik/</id>
  295. <link rel="alternate" href="https://www.mcmillen.dev/sigbovik/"/>
  296. <content type="html">
  297. <![CDATA[
  298. <h1 id="93-of-paint-splatters-are-valid-perl-programs">93% of Paint Splatters are Valid Perl Programs</h1>
  299. <p><em>Posted 2019-04-01.</em></p>
  300. <p>TLDR: <a href="2019.pdf">read the paper</a> and <a href="splatters.html">view the gallery of pretty Perl programs</a>.</p>
  301. <p>In this paper, we aim to answer a long-standing open problem in the programming languages community: <em>is it possible to smear paint on the wall without creating valid Perl?</em></p>
  302. <p>We answer this question in the affirmative: it <strong>is possible</strong> to smear paint on the wall without creating a valid Perl program. We employ an empirical approach, using optical character recognition (OCR) software, which finds that merely 93% of paint splatters parse as valid Perl. We analyze the properties of paint-splatter Perl programs, and present seven examples of paint splatters which are not valid Perl programs.</p>
  303. <p><a href="https://twitter.com/jaffathecake/status/1095706032448393217"><img alt="Screenshot of a Twitter conversation. Adrienne Porter Felt says: &quot;I don't want to teach my kid to code. I want him to splash in muddy puddles and smear paint on the walls and read novels under the covers way too late at night. I grew up too soon and wish I'd had more time to be a kid. Why do schools teach vocational skills so young these days?&quot; Jake Archibald replies: &quot;but is it possible to smear paint on the wall without creating valid Perl?&quot;" src="/media/20190401-sigbovik-tweet.png"></a></p>
  304. <p>Accepted for publication at SIGBOVIK 2019, held April 1st 2019 in Pittsburgh. Winner of a Unwitting Participation Ribbon, &ldquo;an unwelcome brand we’ve affixed to each paper determined after careful scrutiny to have included a genuine artifact, thereby furthering the admirable causes of open science and fruitful procrastination.&rdquo;</p>
  305. <p>Read it on <a href="https://docs.google.com/document/d/1ZGGNMfmfpWB-DzWS3Jr-YLcRNRjhp3FKS6v0KELxXK8/preview">Google Docs</a> or download a <a href="2019.pdf">PDF</a>. Or grab the <a href="http://sigbovik.org/2019/proceedings.pdf">entire SIGBOVIK 2019 proceedings</a>; I&rsquo;m on page 174.</p>
  306. <h2 id="supplementary-materials">Supplementary Materials</h2>
  307. <p>Here&rsquo;s <a href="splatters.html">all the paint splatters</a> on a single page, along with the valid Perl source code corresponding to each. &ldquo;Not valid&rdquo; is written in red for those images which did not parse as valid Perl programs. If different OCR settings recognized multiple valid Perl programs, I chose the one that seemed the most &ldquo;interesting&rdquo;, according to my own aesthetic sense.</p>
  308. <p>Here&rsquo;s a <a href="splatters.tar.gz">tarball of 100 paint-splatter images</a> that were used as the main dataset for this paper.</p>
  309. <p><strong>(source code not available yet because i am bad at GitHub)</strong></p>
  310. <h2 id="errata">Errata</h2>
  311. <p>There are a few paint splatter Perl programs that I didn&rsquo;t recognize as &ldquo;interesting&rdquo; until after the SIGBOVIK submission deadline. For example, this splatter is recognized by OCR as the string <code>lerzfijglpFiji-j</code>, which evaluates to the number <code>0</code> in Perl:</p>
  312. <p><img alt="paint splatter" src="splatters/6b78f8696b05f9322b2dda21b6932776.jpg"></p>
  313. <p>The image below is recognized as the string <code>-*?</code>, which also evaluates to the number <code>0</code> in Perl:</p>
  314. <p><img alt="paint splatter" src="splatters/e47b8463b359906947c66ec4c852a2a3.jpg"></p>
  315. <p>Another surprising program is shown below; OCR recognizes this image as the string <code>;i;c;;#\\?z{;?;;fn':.;</code>, which evaluates to the string <code>c</code> in Perl:</p>
  316. <p><img alt="paint splatter" src="splatters/803dd5a54c42ed93462c78ad7da357b0.jpg"></p>
  317. <p>Finally, this image is recognized as the string <code>;E,'__'</code>, which evaluates to the string <code>E__</code> in Perl:</p>
  318. <p><img alt="paint splatter" src="splatters/dc86c1c3553705b7b2f973d5be9e0389.jpg"></p>
  319. ]]>
  320. </content>
  321. <updated>2019-04-01T12:00:00-04:00</updated>
  322. </entry>
  323. <entry>
  324. <title>My first paper in 10 years?!</title>
  325. <id>https://www.mcmillen.dev/blog/20190403-update.html</id>
  326. <link rel="alternate" href="https://www.mcmillen.dev/blog/20190403-update.html"/>
  327. <content type="html">
  328. <![CDATA[
  329. <h1 id="my-first-paper-in-10-years">My first paper in 10 years?!</h1>
  330. <p><em>Posted 2019-04-03.</em></p>
  331. <p>It&rsquo;s been nearly two months since my last day at Google, so I guess I should finally make use of this newsletter :)</p>
  332. <p>I wrote <a href="https://docs.google.com/document/d/1ZGGNMfmfpWB-DzWS3Jr-YLcRNRjhp3FKS6v0KELxXK8/preview">a paper</a> which was published on April 1st as part of SIGBOVIK 2019: &ldquo;93% of Paint Splatters are Valid Perl Programs&rdquo;. In this paper, I answer a long-standing open problem in the programming languages community: <em>is it possible to smear paint on the wall without creating valid Perl?</em></p>
  333. <p>(Long-standing since February 13, 2019, when a <a href="https://twitter.com/jaffathecake/status/1095706032448393217">Twitter conversation</a> between Adrienne Porter Felt &amp; Jake Archibald posed the question.)</p>
  334. <p>To answer this question, I downloaded 100 images of paint splatters from Pinterest, ran the open-source Tesseract OCR engine to turn each into a text string, and then sent that text to the Perl interpreter to see whether that text successfully parsed as Perl. It turns out that 93 of the 100 paint splatters do parse as valid Perl, but since 7% do not, I conclude that it <strong>is possible</strong> to smear paint on a wall without creating valid Perl.</p>
  335. <p>You might suspect there is some chicanery going on with this result. You&rsquo;d be correct, but&hellip; honestly there&rsquo;s not <em>that</em> much chicanery going on. You&rsquo;ll have to read the paper for details&hellip; and for my attempts at academic humor. :)</p>
  336. <p>There&rsquo;s also some <a href="/sigbovik">supporting material</a> on this website, including a <a href="/sigbovik/splatters.html">gallery of all 100 images</a> and their associated valid Perl code. Here&rsquo;s a screenshot of some of them. (Did you know that the string <code>lerzfijglpFiji-j</code> evaluates to the number <code>0</code> in Perl?)</p>
  337. <p><img alt="screenshot of 17 paint splatters, and the Perl programs they represent" src="/media/20190403-update-splatters.png"></p>
  338. <p>As it turns out, the publication date of my paper was exactly 10-years-minus-a-day since my Ph.D. thesis defense. I&rsquo;d planned on travelling back to Carnegie Mellon to give this talk live at SIGBOVIK 2019, but unfortunately came down with a nasty cold-and-cough so I had to cancel my trip. :( Perhaps I can give a belated talk at next year&rsquo;s conference.</p>
  339. <p>For more light-hearted and vaguely CS-shaped research papers, check out the rest of the <a href="http://sigbovik.org/2019/proceedings.pdf">SIGBOVIK 2019 proceedings</a>. I particularly enjoyed &ldquo;Elo World, a framework for benchmarking weak chess engines&rdquo; by tom7 (&ldquo;The computer players include some traditional chess engines, but also many algorithms chosen for their simplicity, as well as some designed to be competitively bad&rdquo;.)</p>
  340. <p>Some other random things that I&rsquo;ve been up to in the last month-and-a-half:</p>
  341. <ul>
  342. <li>
  343. <p><a href="https://twitter.com/mcmillen/status/1095795492196364297">ohnosay</a>, which is like &ldquo;cowsay&rdquo; but for comics in the style of webcomicname. [GitHub] This was a good excuse to get a Linux development environment set up on a persistent Google Cloud instance &amp; to learn how to GitHub. Since then, I also realized that the World Outside Google uses Python 3, so I&rsquo;ve started learning that :)</p>
  344. <p><img alt='a three panel comic displayed on a linux terminal: "i will write a silly program" "hm, what did i do with my ssh credentials?" "oh no"' src="/media/20190403-update-ohno.png"></p>
  345. </li>
  346. <li>
  347. <p>Gardening! Last August I randomly planted some peppermint in a railing container on my balcony, and it went gangbusters. This spring I&rsquo;ve actually planned out a whole porch-garden (like <a href="https://twitter.com/sevandyk/status/1109121188079427585">Stardew Valley but real life</a>). Last year&rsquo;s mint has started growing again, and I&rsquo;ve added spearmint and mojito mint. I&rsquo;ve also got two types of peas, two mixes of salad greens, and spinach planted. Later I&rsquo;ll be planting carrots, basil, and rosemary. The peas just started sprouting a couple days ago, which is exciting!</p>
  348. <p><img alt='a container showing an assortment of "asian salad" greens' src="/media/20190403-update-garden.jpg"></p>
  349. </li>
  350. <li>
  351. <p>Gloomhaven! This is a cooperative legacy-style board game &mdash; a fun dungeon-crawler that doesn&rsquo;t need a DM, so everyone gets to play. Our group is still only a few scenarios in, but we&rsquo;re enjoying it so far. SO MANY HEX TILES. I&rsquo;m also getting ready to paint our party&rsquo;s miniatures, which is another (potential) new hobby of mine; more to come in a future newsletter, I suspect :)</p>
  352. </li>
  353. <li>
  354. <p>Video games: just started Sekiro: Shadows Die Twice on PS4. Recently completed (and really enjoyed) New Super Mario Bros. U Deluxe for Nintendo Switch (though Nintendo seems to be trying to give Google a run for their money on ridiculous product names). I&rsquo;ve also been playing Total War: Warhammer 2 regularly, and Splatoon 2 from time to time. I tried getting into XCOM 2 &amp; enjoyed it, but I&rsquo;m not sure I&rsquo;m interested enough to finish the campaign. I keep going back to Total War when I want something in the tactical / strategy genre.</p>
  355. </li>
  356. <li>
  357. <p>Guitar: starting to learn fingerstyle, with the goal of eventually becoming good enough to play <a href="https://www.songsterr.com/a/wsa/chrono-cross-dream-of-the-shore-boardering-another-world-tab-s5033t2">Dream of the Shore Bordering Another World</a> from Chrono Cross.</p>
  358. </li>
  359. <li>
  360. <p>Computer stuff: upgraded my PC&rsquo;s video card (it was many years old) and upgraded to an all-SSD setup. It turns out that 2TB SSDs aren&rsquo;t that expensive any more.</p>
  361. </li>
  362. <li>
  363. <p>Getting healthcare without an employer is a disaster &mdash; even in Massachusetts, which reportedly has one of the best systems in the US. Still working on straightening out my paperwork. Apparently they refuse to believe in my proof of health-insurance termination, even though it&rsquo;s lettermarked by Google and everything.</p>
  364. </li>
  365. </ul>
  366. <p>Thanks for reading! Hopefully the next update will come sooner than 2 months and thus be a bit shorter than this one ended up being :)</p>
  367. <p>~ Colin</p>
  368. ]]>
  369. </content>
  370. <updated>2019-04-03T12:00:00-04:00</updated>
  371. </entry>
  372. <entry>
  373. <title>A new year &amp; a sneaky new project</title>
  374. <id>https://www.mcmillen.dev/blog/20200209-sneak.html</id>
  375. <link rel="alternate" href="https://www.mcmillen.dev/blog/20200209-sneak.html"/>
  376. <content type="html">
  377. <![CDATA[
  378. <h1 id="a-new-year-a-sneaky-new-project">A new year &amp; a sneaky new project</h1>
  379. <p><em>Posted 2020-02-09.</em></p>
  380. <p>I can&rsquo;t believe it&rsquo;s here so quickly, but: today marks a year since my last day at Google. That seemed like a good occasion to dust off this newsletter &amp; let you know what I&rsquo;ve been up to: making a videogame!</p>
  381. <p>I&rsquo;m working on a stealth-based 2D platformer where you don&rsquo;t have to kill anyone unless you want to. It&rsquo;ll be possible to get through every level by sneaking and misdirection, but it&rsquo;ll require you to be careful and tactical to do so&hellip; and of course if that doesn&rsquo;t work out, you can always draw your swords and go in fighting! So far I&rsquo;ve given it &ldquo;Sneak&rdquo; as a codename, but that&rsquo;s definitely a placeholder until I can flesh out more of the world.</p>
  382. <p>So far Sneak runs on PC &amp; Xbox, but I hope to add Switch and PS4 support within the next couple months. I&rsquo;m using a C# framework called MonoGame, which provides low-level graphics &amp; audio support across all these platforms. In order to write games for Switch or PS4, you need to apply to Nintendo &amp; Sony to get access to their platform-specific SDKs. So my first real milestone will be coming up with a compelling Game Design Doc &amp; gameplay videos so that they can (hopefully) be convinced that I&rsquo;m worth taking seriously. Wish me luck!</p>
  383. <p>Sony won&rsquo;t even talk to anyone unless they&rsquo;re a Real Business (&amp; Nintendo kinda wants you to be too), so as of&hellip; yesterday, I&rsquo;m officially the founder of SemiColin Games LLC (and, for now at least, the only member&hellip;)</p>
  384. <p>If you want to follow along, I have an extremely-placeholder website up at <a href="https://semicolin.games">semicolin.games</a> where you can sign up for Yet Another Newsletter if you like, and a Twitter account <a href="https://twitter.com/SemiColinGames">@SemiColinGames</a> that would appreciate a follow. I&rsquo;ll probably set up a devblog with an RSS feed too eventually, but that&rsquo;s not quite ready yet. When it is, I&rsquo;ll send a quick update here.</p>
  385. <p>I only got started in December &amp; a lot of my work so far has been on building infrastructure (and learning how to start a business), so I don&rsquo;t have any Extremely Compelling Gameplay Videos yet. Here&rsquo;s a short animated GIF for now. The <a href="https://twitter.com/mcmillen/status/1205164954728509440">bloopers on Twitter</a> might be more fun though. :)</p>
  386. <p><img alt="Animation of a pixel-art character swinging a sword" src="/media/20200209-sneak.gif"><br>
  387. (Art definitely not final!)</p>
  388. <p>Thanks for following along with me on this adventure! Hopefully my next update will come more quickly, and be less wordy! I&rsquo;ve wanted to make videogames since I was Literally A Kid, so I&rsquo;m quite excited to finally be doing that full-time, and to hopefully share something good with all of you. When I&rsquo;m at a stage where I want alpha testers, I&rsquo;ll definitely be asking here first.</p>
  389. <p>Thanks for your support!<br>
  390. ~ Colin (&amp; <a href="https://semicolin.games">SemiColin Games</a>)</p>
  391. ]]>
  392. </content>
  393. <updated>2020-02-09T12:00:00-04:00</updated>
  394. </entry>
  395. <entry>
  396. <title>Downvotes &amp; Dislikes Considered Harmful</title>
  397. <id>https://www.mcmillen.dev/blog/20210721-downvotes-considered-harmful.html</id>
  398. <link rel="alternate" href="https://www.mcmillen.dev/blog/20210721-downvotes-considered-harmful.html"/>
  399. <content type="html">
  400. <![CDATA[
  401. <h1 id="downvotes-dislikes-considered-harmful">Downvotes &amp; Dislikes Considered Harmful</h1>
  402. <p><em>Posted 2021-07-21.</em></p>
  403. <p>If you&rsquo;re letting users rank content, you probably <strong>don&rsquo;t need and don&rsquo;t want downvotes</strong>. Here&rsquo;s why.</p>
  404. <p>(This post inspired by news that Twitter is considering <a href="https://twitter.com/Sadcrib/status/1417913362999136257">adding &ldquo;Dislikes&rdquo; to Tweets</a>.)</p>
  405. <h2 id="background">Background</h2>
  406. <p>In my past life at Google, I was responsible for co-creating <a href="https://books.google.com/books?id=fEJ0AwAAQBAJ&amp;newbks=1&amp;newbks_redir=0&amp;lpg=PP83&amp;dq=memegen%20eric%20schmidt&amp;pg=PP83#v=onepage&amp;q=memegen%20eric%20schmidt&amp;f=false">Memegen</a>, a large &amp; influential Google-internal social network. Memegen lets Google employees create internal-only memes and allows users to upvote &amp; downvote the memes of others. Memegen&rsquo;s home page is the Popular page, which shows the most-upvoted memes of the past day.</p>
  407. <p>Adding downvotes to Memegen was my single greatest mistake.</p>
  408. <h2 id="the-problems-of-downvotes">The problems of downvotes</h2>
  409. <p>Any voting system where <em>most</em> posts mostly receive upvotes, but also allows downvotes, has a huge problem:</p>
  410. <blockquote>
  411. <p>No matter how you do the math, <strong>downvotes count more</strong> than upvotes do.</p>
  412. </blockquote>
  413. <p>Mathematically, it will always be comparatively easy for a vocal minority to bury any specific items that they don&rsquo;t want surfaced on the top-N posts page. This is true even if you&rsquo;re using a sophisticated ranking algorithm like <a href="https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Wilson_score_interval">Wilson score intervals</a> to rank posts (as Reddit &amp; many other sites do).</p>
  414. <p>Downvotes aim to solve the problem of filtering out low-quality <strong>content</strong>, but are too easily coopted by trolls to let them filter out <strong>people</strong> &mdash; often for bad reasons that have more to do with the identity of who&rsquo;s posting rather than the content of their posts.</p>
  415. <p>From the standpoint of attracting users, downvotes create another huge problem: someone whose first submission to a site gets downvoted to oblivion will feel bad about it and probably not come back to submit better stuff in the future.</p>
  416. <h2 id="what-does-a-downvote-actually-mean">What does a downvote actually <em>mean?</em></h2>
  417. <p>The other problem with downvotes is that it&rsquo;s unclear to everyone what they mean. Does a downvote mean that this particular post is:</p>
  418. <ol>
  419. <li>offensive or illegal and needs to be removed ASAP?</li>
  420. <li>a duplicate?</li>
  421. <li>just something you personally don&rsquo;t like?</li>
  422. <li>off-topic for the forum?</li>
  423. </ol>
  424. <p>As the creator of a social product, you need <strong>give people different buttons</strong> for these.</p>
  425. <p>Offensive or illegal posts (#1) shouldn&rsquo;t be handled by an algorithmic rating system. You need actual human moderators for that &mdash; and enough of them that they can review those reports in a timely manner. (I hope you&rsquo;re willing to train &amp; pay them well!)</p>
  426. <p>For duplicate posts (#2) it&rsquo;s nicer &amp; more informative if your software simply says &ldquo;hey, this submission is a duplicate of this other thing, why don&rsquo;t you all check out that post instead?&rdquo;</p>
  427. <p>#3 is solved by default &mdash; people can simply not vote for content they don&rsquo;t like.</p>
  428. <p>#4 is pretty much the same as #3 (but maybe a moderator should intervene if a user has a history of posting too many off-topic things, or if it&rsquo;s obviously spam).</p>
  429. <h2 id="how-to-actually-rank-posts">How to actually rank posts</h2>
  430. <p>Once you&rsquo;ve dispensed with the idea of downvotes, the main things a user cares about are: &ldquo;what are the best things that have been posted today?&rdquo; (or in the last hour / week / etc) or &ldquo;what are the best things since I last visited?&rdquo;</p>
  431. <p>On paper, the math is super simple: just count the number of upvotes for each item that was submitted in the relevant time period, and show the top N!</p>
  432. <p>It turns out that&rsquo;s it&rsquo;s actually a bit trickier to implement than something like a Wilson score interval, so here&rsquo;s some tips on how to do that.</p>
  433. <p>We need to store each vote and when it was cast, and then when it&rsquo;s time to compute the &ldquo;most popular in the last day&rdquo; page, you first select all the votes cast within the last day, and then count how many were for each post, and rank those.</p>
  434. <p>Doing this every time the user hits the homepage is clearly a terrible idea, so set up a cronjob to do it every 5 or 15 minutes or something. It&rsquo;s okay if the info is slightly out of date! Most users won&rsquo;t care or notice if it takes a few minutes for things to move around.</p>
  435. <p>How exactly to optimize this depends on the scale of your site, your storage architecture, a ton of other stuff, but for Memegen, every post had properties like <code>score_hour</code>, <code>score_day</code>, <code>score_month</code>, <code>score_alltime</code>. A mapreduce was responsible for updating these values every few minutes.</p>
  436. <p>Obviously you don&rsquo;t need to touch or compute anything for any post that got no votes since the last time you ran the updater. In the steady state, <em>most</em> of the posts in your system won&rsquo;t need any update.</p>
  437. <h2 id="conclusion">Conclusion</h2>
  438. <p>Downvotes are a blunt instrument for users to say &ldquo;I don&rsquo;t like this content&rdquo;.</p>
  439. <p>It&rsquo;s easy for small groups of trolls to misuse downvotes as a vehicle for harassing &amp; silencing groups of (often marginalized) people.</p>
  440. <p>Downvotes reduce engagement by scaring off first-time posters.</p>
  441. <p>Instead of adding downvotes to your site, build <em>specific</em> tools that handle specific kinds of unwanted posts.</p>
  442. <p>(This post is a distillation &amp; refinement of some thoughts originally posted in <a href="https://twitter.com/mcmillen/status/1310998579184574465?s=20">a Twitter thread</a> in September 2020.)</p>
  443. <h2 id="comments">Comments?</h2>
  444. <p>Feel free to reply to <a href="https://twitter.com/mcmillen/status/1418010991913230336">my post on Twitter</a> about this article. Thanks!</p>
  445. ]]>
  446. </content>
  447. <updated>2021-07-21T12:00:00-04:00</updated>
  448. </entry>
  449. </feed>