<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.7">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2022-10-21T01:22:27+00:00</updated><id>/feed.xml</id><title type="html">Jeremy Feltracco</title><subtitle>Hopefully some signal in the noise.</subtitle><entry><title type="html">Docker container monitoring with ctop</title><link href="/utils/2022/10/20/ctop.html" rel="alternate" type="text/html" title="Docker container monitoring with ctop" /><published>2022-10-20T15:49:00+00:00</published><updated>2022-10-20T15:49:00+00:00</updated><id>/utils/2022/10/20/ctop</id><content type="html" xml:base="/utils/2022/10/20/ctop.html">&lt;p&gt;I can’t believe I’ve made it this long without encountering 
&lt;a href=&quot;https://ctop.sh&quot;&gt;ctop&lt;/a&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;brew install ctop&lt;/code&gt;). What is it?
Well just like top can show you general performance stats about your computer,
ctop lets you see the performance of your running containers.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/bcicen/ctop/blob/master/_docs/img/grid.gif?raw=true&quot; alt=&quot;ctop&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I found this massively helpful for figuring out where a performance
issue in my application was, easily seeing that my app server was maxing
out CPU while my DB was running under 20% load. Very useful tool, thanks &lt;a href=&quot;https://bradley.codes&quot;&gt;Bradley&lt;/a&gt;!&lt;/p&gt;</content><author><name></name></author><category term="utils" /><summary type="html">I can’t believe I’ve made it this long without encountering ctop (brew install ctop). What is it? Well just like top can show you general performance stats about your computer, ctop lets you see the performance of your running containers.</summary></entry><entry><title type="html">Filtering with Particles</title><link href="/filtering/2020/11/30/particle-filter.html" rel="alternate" type="text/html" title="Filtering with Particles" /><published>2020-11-30T02:49:00+00:00</published><updated>2020-11-30T02:49:00+00:00</updated><id>/filtering/2020/11/30/particle-filter</id><content type="html" xml:base="/filtering/2020/11/30/particle-filter.html">&lt;p&gt;I’ve written in past posts about designing a basic control system for a drone in a 2D, top-down
environment. Fundamentally, we found that if we know our position, and we know a target position
we are trying to track towards, we can often design a control law that accomplishes our goal
even with random wind and step changes in a state.&lt;/p&gt;

&lt;p&gt;There’s one fundamental assumption in designing these control systems however, the idea
that we know with some certainty what our own position is, and what the position of our target is!&lt;/p&gt;

&lt;h2 id=&quot;sensing&quot;&gt;Sensing&lt;/h2&gt;

&lt;p&gt;Say we’ve got a series of sensors on the ground and we’re trying to track an EM emitting
robot driving around. These are cheap passive sensors, meaning we only get a noisy bearing
towards the target and not a distance.&lt;/p&gt;

&lt;h2 id=&quot;challenge&quot;&gt;Challenge&lt;/h2&gt;

&lt;p&gt;The real problem here is that our bearing measurements are noisy. Without noise,
we could calculate the intersection between any two bearing lines and that would be the vehicle
position.&lt;/p&gt;

&lt;p&gt;With some noise, the intersection point between any two measurements is liable to be quite far
away from the actual robot position, making this approach infeasible.&lt;/p&gt;

&lt;h2 id=&quot;particle-filtering&quot;&gt;Particle Filtering&lt;/h2&gt;

&lt;p&gt;One big idea here is that while directly computing the robot position from several noisy bearings
is hard, it’s pretty easy to evaluate candidate robot positions in light of a new bearing measurement.
In other words, when picking where to eat, it’s easier to decide if a suggested restaurant is close to 
what you want than to directly pick the optimal restaurant from scratch.&lt;/p&gt;

&lt;p&gt;Given a sensor position, a bearing measurement, and a candidate robot position, we can compute the
expected bearing between the sensor and the candidate robot position first. This value can be
compared to the actual bearing measurement to generate a likelihood that the sensor measurement 
was produced by a robot in the candidate position. A big difference between the expected and observed
bearing means this candidate is unlikely and a small difference means the candidate is likely.&lt;/p&gt;

&lt;p&gt;Knowing this, we use the difference between the expected and observed measurements for a given
candidate position to compute a “score” for the measurement. One possible scoring function is
simply &lt;code class=&quot;highlighter-rouge&quot;&gt;1.0 / sqrt(abs(angleDiff))&lt;/code&gt;. When a small angle difference is found, this candidate gets
a large score. When a large angle difference is found, the score of this candidate (or “particle”)
trends towards 0.&lt;/p&gt;

&lt;h2 id=&quot;resampling&quot;&gt;Resampling&lt;/h2&gt;

&lt;p&gt;Given this list of particles with scores, we now look to create a new list of particles that
are generally closer to the target location than before. If we continue to build lists of particles
that are closer to the target, eventually the particle cloud should converge to the solution position.
In order to create this better list of particles, we can randomly resample particles (with replacement)
using scores as a probability of selection values.&lt;/p&gt;

&lt;p&gt;Just implementing this solution will cause a problem however. If we resample continuously we’ll
eventually get a completely homogenous list of particles closest to the target. Once the diversity
of the particles is down to 0, any change in the target position (or more generally the target model)
would not be caught by the filter.&lt;/p&gt;

&lt;p&gt;In order to solve this problem, we can add a small bit of gaussian noise to each particle per iteration.
This generates more candidate solutions, but generally close to the existing solutions, allowing
the filter to adapt to changes in the model while further exploring the statespace near existing
candidate solutions.&lt;/p&gt;

&lt;h2 id=&quot;demo&quot;&gt;Demo&lt;/h2&gt;

&lt;p&gt;Below you can see a simple particle filter essentially solving the SLAM problem for a small 2D world. Each
sensor is randomly selected to generate a measurement and the particle list is then updated based on that
measurement. You can see the basic filter operation by slowing down the simulation using the slider.&lt;/p&gt;

&lt;p&gt;Make sure to also push the simulation speed to max and click and drag to see the filter working live!&lt;/p&gt;

&lt;head&gt;
  &lt;script src=&quot;/assets/2020-11-29-particle-filter/main.js&quot;&gt;
  &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div class=&quot;slidecontainer&quot; style=&quot;display: flex; align-items: flex-end;&quot;&gt;
    &lt;label style=&quot;margin-right: 10px;&quot; for=&quot;rate-slider&quot;&gt;Rate&lt;/label&gt;
    &lt;input id=&quot;rate-slider&quot; type=&quot;range&quot; min=&quot;1&quot; max=&quot;100&quot; value=&quot;50&quot; /&gt;

    &lt;button id=&quot;restart-button&quot; style=&quot;margin-left: 10px; margin-right: 10px;&quot;&gt;Restart&lt;/button&gt;
  &lt;/div&gt;
  &lt;div style=&quot;display: flex; justify-content: center; padding-top: 15px; padding-bottom: 15px&quot;&gt;
    &lt;canvas width=&quot;640&quot; height=&quot;480&quot; tabindex=&quot;1&quot;&gt;&lt;/canvas&gt;
  &lt;/div&gt;
&lt;/body&gt;</content><author><name></name></author><category term="filtering" /><summary type="html">I’ve written in past posts about designing a basic control system for a drone in a 2D, top-down environment. Fundamentally, we found that if we know our position, and we know a target position we are trying to track towards, we can often design a control law that accomplishes our goal even with random wind and step changes in a state.</summary></entry><entry><title type="html">Moving to (Mac)Vim’s integrated terminal: some tiny tweaks</title><link href="/environment/2020/09/13/20-macvim-term.html" rel="alternate" type="text/html" title="Moving to (Mac)Vim’s integrated terminal: some tiny tweaks" /><published>2020-09-13T17:00:00+00:00</published><updated>2020-09-13T17:00:00+00:00</updated><id>/environment/2020/09/13/20-macvim-term</id><content type="html" xml:base="/environment/2020/09/13/20-macvim-term.html">&lt;p&gt;I’m trialing a move to MacVim, and with it, moving to Vim 8’s new built-in terminal
emulator.&lt;/p&gt;

&lt;p&gt;My purpose in doing this is to see how GUI Vim is looking these days and to see
if GUI Vim running a terminal emulator can actually compete with a terminal
emulator running Vim for my development uses.&lt;/p&gt;

&lt;h4 id=&quot;why-use-gui-vim&quot;&gt;Why use GUI Vim?&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;I like that tab management looks nice and I can re-order tabs easily&lt;/li&gt;
  &lt;li&gt;No dealing with terminal color limitations&lt;/li&gt;
  &lt;li&gt;Less keybinding weirdness, want to bind to Meta/Option/Alt? Easy, &lt;code class=&quot;highlighter-rouge&quot;&gt;set macmeta&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Proper bold/italics&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;why-have-a-terminal-inside-gui-vim&quot;&gt;Why have a terminal inside GUI Vim?&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Navigating terminal output using Vim bindings&lt;/li&gt;
  &lt;li&gt;Easily yank/put between buffers and terminal, no system clipboard involved&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Honestly this argument is kind of weak. If you like tinkering with your tools though,
carry on!&lt;/p&gt;

&lt;h3 id=&quot;launching-terminal&quot;&gt;Launching Terminal&lt;/h3&gt;

&lt;div class=&quot;language-vimscript 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;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;terminal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This launches a horizontal split with a terminal taking up precious vertical space.&lt;/p&gt;

&lt;p&gt;Let’s define some keybindings that are more comfortable. I use &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;space&amp;gt;&lt;/code&gt; as my leader,
and tend to group related commands under a prefix. I chose &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;leader&amp;gt;t&lt;/code&gt; to be my
&lt;em&gt;terminal&lt;/em&gt; prefix.&lt;/p&gt;

&lt;div class=&quot;language-vimscript 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;c&quot;&gt;&quot; terminal in tab&lt;/span&gt;
nnoremap &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;leader&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;tt &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;tab&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;term&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;CR&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;&quot; terminal in current window&lt;/span&gt;
nnoremap &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;leader&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;tc&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;term&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;++&lt;/span&gt;curwin&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;CR&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;&quot; terminal in vertical split&lt;/span&gt;
nnoremap &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;leader&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;tv &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;vert&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;term&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;CR&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;These cover my use cases pretty well, I never use horizontal split terminals.&lt;/p&gt;

&lt;h3 id=&quot;getting-out&quot;&gt;Getting Out&lt;/h3&gt;

&lt;p&gt;How do you get out of terminal windows? &lt;a href=&quot;https://vimhelp.org/terminal.txt.html&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;:help Terminal-Mode&lt;/code&gt;&lt;/a&gt; shows that
&lt;code class=&quot;highlighter-rouge&quot;&gt;CTRL-W&lt;/code&gt; basically works as expected. You can move to different windows,
tabs, or escape normal mode commands by prefixing with &lt;code class=&quot;highlighter-rouge&quot;&gt;CTRL-W&lt;/code&gt;. You can
escape to terminal normal mode using &lt;code class=&quot;highlighter-rouge&quot;&gt;CTRL-W N&lt;/code&gt;, but I found this annoying.&lt;/p&gt;

&lt;p&gt;If you don’t really need escape inside your terminal sessions you can bind
it to escape terminal mode (this is from the Vim docs!).&lt;/p&gt;

&lt;div class=&quot;language-vimscript 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;tnoremap&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;Esc&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;C&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;W&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;N
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;passing-through-special-keys&quot;&gt;Passing through special keys&lt;/h3&gt;

&lt;p&gt;The most annoying thing I find with these built-in terminals is how they
always seem to break shell bindings.&lt;/p&gt;

&lt;p&gt;I’ve learned bits and pieces of emacs-ish bindings available in common shells.
For example, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;M-b&amp;gt;&lt;/code&gt; goes backwards a word in most shells and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;M-f&amp;gt;&lt;/code&gt; goes forward
a word. Also, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;CTRL-W&amp;gt;&lt;/code&gt; deletes backwards one word.&lt;/p&gt;

&lt;p&gt;These are totally broken in Vim’s built-in terminal. Obviously &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;CTRL-W&amp;gt;&lt;/code&gt; gets
stolen by Vim since it is using &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;CTRL-W&amp;gt;&lt;/code&gt; to escape to normal mode.
In addition, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;M-b&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;M-f&amp;gt;&lt;/code&gt; will leave you frustrated with a bunch
of special characters like so &lt;code class=&quot;highlighter-rouge&quot;&gt;∫∫ƒƒƒ&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I don’t really have a great solution for &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;CTRL-W&amp;gt;&lt;/code&gt;, I don’t want to make
drastic changes to how Vim sets up these bindings by default, so I’m just
getting used to hitting &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;M-BS&amp;gt;&lt;/code&gt; to delete a word while avoiding
Vim’s bindings.&lt;/p&gt;

&lt;p&gt;I do have a solution for my &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;M-b&amp;gt;&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;M-f&amp;gt;&lt;/code&gt; woes though! Vim’s docs have
a &lt;a href=&quot;https://vimhelp.org/terminal.txt.html#terminal-special-keys&quot;&gt;little function definition&lt;/a&gt;
to help with passing special keys.&lt;/p&gt;

&lt;div class=&quot;language-vimscript 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;c&quot;&gt;&quot; Allow us to create &amp;lt;M-*&amp;gt; bindings&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;set&lt;/span&gt; macmeta
&lt;span class=&quot;c&quot;&gt;&quot; Remap to call send_term function&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;tmap&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;expr&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;M&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; SendToTerm&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;\&amp;lt;Esc&amp;gt;b&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;tmap&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;expr&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;M&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; SendToTerm&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;\&amp;lt;Esc&amp;gt;f&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
func SendToTerm&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;what&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;term_sendkeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;a:what&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;s1&quot;&gt;''&lt;/span&gt;
endfunc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This lets Vim actually pass the correct terminal escape code to get these
shortcuts working as expected again.&lt;/p&gt;

&lt;h3 id=&quot;thoughts&quot;&gt;Thoughts&lt;/h3&gt;

&lt;p&gt;I’m sure I’ll run into some more annoyances soon, but so far this is working
out pretty well for me, and there’s a clear path to adding additional
escape sequences for other broken shortcuts. I will say that being
able to navigate terminal output and rapidly yank/put has really been an
improvement.&lt;/p&gt;

&lt;h3 id=&quot;protip&quot;&gt;Protip&lt;/h3&gt;

&lt;p&gt;Want to know what keycode is being sent by a terminal to your application?
Run &lt;code class=&quot;highlighter-rouge&quot;&gt;cat&lt;/code&gt; then hit the key combination, you’ll see the keycode output.&lt;/p&gt;</content><author><name></name></author><category term="environment" /><summary type="html">I’m trialing a move to MacVim, and with it, moving to Vim 8’s new built-in terminal emulator.</summary></entry><entry><title type="html">From research to software engineering: things I learned</title><link href="/development/2020/08/14/web-dev.html" rel="alternate" type="text/html" title="From research to software engineering: things I learned" /><published>2020-08-14T17:00:00+00:00</published><updated>2020-08-14T17:00:00+00:00</updated><id>/development/2020/08/14/web-dev</id><content type="html" xml:base="/development/2020/08/14/web-dev.html">&lt;p&gt;Over the past few years I’ve had the interesting perspective of moving from a
research oriented embedded systems role, working with ROS and some
open source autopilots, to a full stack web engineer in a late-stage startup.&lt;/p&gt;

&lt;p&gt;I did a 180 degree change from research work to private company work, from embedded systems to web
development, from a team of research engineers to a team of sotware engineers, from a 
set of &lt;strong&gt;projects&lt;/strong&gt; to a set of &lt;strong&gt;products&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here are a few things that I would like to go back in time and yell at myself.&lt;/p&gt;

&lt;p&gt;They’re opinionated and generated from very limited experience, so please take all this with
a grain of salt.&lt;/p&gt;

&lt;h3 id=&quot;programming-languages&quot;&gt;Programming Languages&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Don’t decide to use a new language at work without team buy-in because you read a blog post.&lt;/li&gt;
  &lt;li&gt;If a simpler language like GoLang gives you a toolbox of features, C++ gives you a warehouse of arcane tools
some of which shoot nails in reverse. Try to generally use the same subset of features the rest of
your team uses and get team buy in before promoting some new gizmo you found.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;code-style&quot;&gt;Code Style&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Code style debates are a waste of time. Use an autoformatter and run a style check in CI.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;prioritization--shortcuts&quot;&gt;Prioritization + Shortcuts&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Decide if you want to do R&amp;amp;D or if you want to hack away on build systems (CMakeLists anyone?). If it’s the former,
try not to invent build systems - stick to what the docs and community suggest.&lt;/li&gt;
  &lt;li&gt;You can get surprisingly far committing generated files (ie protobuf generated code) to your git repo, it’s not always
worth investing tons of time tuning and perfecting build scripts to avoid committing a generated file.
Just make sure the generated file has a comment with instructions on how to regenerate it.&lt;/li&gt;
  &lt;li&gt;Same for libraries, committing a source library in a pinch is no great sin.&lt;/li&gt;
  &lt;li&gt;The worst feature you’ll ever build will start with a mandated implementation imposed on you (as opposed to a &lt;strong&gt;problem&lt;/strong&gt;)
and a lack of early and repeated feedback from those that have to use the feature.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;workstyle&quot;&gt;Workstyle&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Merge a little code into mainline everyday, especially when you’re starting out. Even if it’s just fixing a comment
or cleaning up some naming, it’s tangible and something to build on.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;git--reviews&quot;&gt;Git + Reviews&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;If you actually want someone to read your code in a review, make each commit a bite-sized chunk
with a clear goal. Don’t use &lt;code class=&quot;highlighter-rouge&quot;&gt;git commit -m &quot;...&quot;&lt;/code&gt;, use &lt;code class=&quot;highlighter-rouge&quot;&gt;git commit&lt;/code&gt; to bring up an editor and write a multi-line message
that a human can read. Don’t commit unrelated changes together.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;git rebase -i&lt;/code&gt; is your friend, re-order/squash commits to tell a &lt;strong&gt;story&lt;/strong&gt;, not just create a 
log of noisy nonsense.&lt;/li&gt;
  &lt;li&gt;5 small, well-scoped PRs are easier to review than 1 large PR.&lt;/li&gt;
  &lt;li&gt;It’s on the engineer trying to merge code to prove to the reviewer that it works. Explain why you think
it works - did you add automated tests, did you manually test, do you have any screenshots?&lt;/li&gt;
  &lt;li&gt;Code review is more about maintaining standards and team cohesion and less about catching subtle bugs.&lt;/li&gt;
  &lt;li&gt;Make sure it’s clear what parts of your PR feedback are &lt;strong&gt;blocking&lt;/strong&gt; and which parts are nit picks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;testing&quot;&gt;Testing&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Unit tests let you develop code locally, debug locally, and avoid deployment for testing. This lets you
develop code &lt;strong&gt;faster&lt;/strong&gt;, not &lt;strong&gt;slower&lt;/strong&gt;.&lt;/li&gt;
  &lt;li&gt;Newly written code without basic unit testing is definitely broken, it’ll just take another 15 minutes for you
to stage the whole environment manually just right to realize that it is in fact broken.&lt;/li&gt;
  &lt;li&gt;If you’re writing automated tests as you go, you almost never need a debugger.&lt;/li&gt;
  &lt;li&gt;Tests protect your code from your future self and from colleagues that lack full context.&lt;/li&gt;
  &lt;li&gt;Tests give you the confidence to make large code changes &lt;strong&gt;without&lt;/strong&gt; manually testing the change
(this gives you superpowers).&lt;/li&gt;
  &lt;li&gt;You can go overboard writing verbose tests. Make sure you’re testing core business logic and that
your tests communicate &lt;strong&gt;intent&lt;/strong&gt; over everything else.&lt;/li&gt;
  &lt;li&gt;Write lots of unit tests, write a small number of integration tests, and try to only very occasionally
manually test.&lt;/li&gt;
&lt;/ul&gt;</content><author><name></name></author><category term="development" /><summary type="html">Over the past few years I’ve had the interesting perspective of moving from a research oriented embedded systems role, working with ROS and some open source autopilots, to a full stack web engineer in a late-stage startup.</summary></entry><entry><title type="html">Dynamic programming - minimum edit distance</title><link href="/algorithms/2020/05/17/dp.html" rel="alternate" type="text/html" title="Dynamic programming - minimum edit distance" /><published>2020-05-17T05:00:00+00:00</published><updated>2020-05-17T05:00:00+00:00</updated><id>/algorithms/2020/05/17/dp</id><content type="html" xml:base="/algorithms/2020/05/17/dp.html">&lt;h1 id=&quot;minimum-edit-distance&quot;&gt;Minimum Edit Distance&lt;/h1&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;scince&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What is this word?&lt;/p&gt;

&lt;p&gt;Well, here are a few options:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;sense&lt;/code&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;seance&lt;/code&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;science&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If we’re a handy-dandy spell checker, how do we decide among them?&lt;/p&gt;

&lt;p&gt;We clearly need some sort of way to tell that words like &lt;code class=&quot;highlighter-rouge&quot;&gt;seance&lt;/code&gt; and
&lt;code class=&quot;highlighter-rouge&quot;&gt;science&lt;/code&gt; are closer to &lt;code class=&quot;highlighter-rouge&quot;&gt;scince&lt;/code&gt; than something like &lt;code class=&quot;highlighter-rouge&quot;&gt;sense&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One pretty natural idea is to see how many edits would be required to go
from &lt;code class=&quot;highlighter-rouge&quot;&gt;scince&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;sense&lt;/code&gt;. Or &lt;code class=&quot;highlighter-rouge&quot;&gt;seance&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;science&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let’s look at &lt;code class=&quot;highlighter-rouge&quot;&gt;sense&lt;/code&gt; first. One possible alignment is this.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;s c i n c e
s e _ n s e
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The edit distance between these words is the number of mismatched characters plus
the number of inserted gaps. Here we failed to line up 3 characters, so the
edit distance is 3.&lt;/p&gt;

&lt;p&gt;Of course we could make worse matches, for example:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;s c i n c e
s e n s e _
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here we managed to create an edit distance of 5 instead of 3. Clearly if we’re going to
use edit distance as our measure of similarity, we only want to consider the minimum
edit distance between two words.&lt;/p&gt;

&lt;p&gt;I hope this gives some good motivation for why we might want an algorithm to compute
minimum edit distance, now on to finding it!&lt;/p&gt;

&lt;h1 id=&quot;algorithmic-thinking&quot;&gt;Algorithmic Thinking&lt;/h1&gt;

&lt;p&gt;Let’s consider the general problem of finding the minimum edit distance between two strings,
&lt;code class=&quot;highlighter-rouge&quot;&gt;X&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Y&lt;/code&gt;, of lengths &lt;code class=&quot;highlighter-rouge&quot;&gt;n&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;m&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This problem seems pretty hard. In general there are many possible edit distances
we could compute between two strings and a simple algorithm here doesn’t come to mind.&lt;/p&gt;

&lt;p&gt;Well a super general strategy in problem solving is to take a complex problem, and break
it down into smaller problems. I can think of two main ways to make this problem
simpler.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Reduce the number of characters we consider in string &lt;code class=&quot;highlighter-rouge&quot;&gt;X&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Reduce the number of characters we consider in string &lt;code class=&quot;highlighter-rouge&quot;&gt;Y&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To start, let’s say we’ll only consider the first &lt;code class=&quot;highlighter-rouge&quot;&gt;i&lt;/code&gt; characeters in string &lt;code class=&quot;highlighter-rouge&quot;&gt;X&lt;/code&gt; and the
first &lt;code class=&quot;highlighter-rouge&quot;&gt;j&lt;/code&gt; characters in string &lt;code class=&quot;highlighter-rouge&quot;&gt;Y&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can express this as &lt;code class=&quot;highlighter-rouge&quot;&gt;X[1..i]&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Y[1..j]&lt;/code&gt;, note we’re being inclusive on our bounds here,
&lt;code class=&quot;highlighter-rouge&quot;&gt;X[1..1] == X[1]&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;X[1..0] == &quot;&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&quot;base-cases&quot;&gt;Base Cases&lt;/h1&gt;

&lt;p&gt;Is our problem easier yet? Not really, but we can express something new using &lt;code class=&quot;highlighter-rouge&quot;&gt;i&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;j&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We know that if &lt;code class=&quot;highlighter-rouge&quot;&gt;i&lt;/code&gt; is 0, then we’re comparing an empty string to another string with length
&lt;code class=&quot;highlighter-rouge&quot;&gt;j&lt;/code&gt;, so to go from empty string to a string of length &lt;code class=&quot;highlighter-rouge&quot;&gt;j&lt;/code&gt; will take a minimum of &lt;code class=&quot;highlighter-rouge&quot;&gt;j&lt;/code&gt; edits,
where each edit is just inserting another character in &lt;code class=&quot;highlighter-rouge&quot;&gt;Y[1..j]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The same argument applies the otherway around, if &lt;code class=&quot;highlighter-rouge&quot;&gt;j&lt;/code&gt; is 0, then &lt;code class=&quot;highlighter-rouge&quot;&gt;i&lt;/code&gt; is our minimum edit distance.&lt;/p&gt;

&lt;p&gt;What we’ve defined are base cases in this problem. They’re trivially solvable because the input
is insane, but they’re going to be a critical piece of our solution.&lt;/p&gt;

&lt;p&gt;To keep track of these base cases we’ll say that some table &lt;code class=&quot;highlighter-rouge&quot;&gt;T&lt;/code&gt;, (indexed by &lt;code class=&quot;highlighter-rouge&quot;&gt;i&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;j&lt;/code&gt;) stores
them. &lt;code class=&quot;highlighter-rouge&quot;&gt;T[i,0] = i&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;T[0,j] = j&lt;/code&gt;.&lt;/p&gt;

&lt;h1 id=&quot;recurrencerecurrencerecurrence&quot;&gt;Recurrence(Recurrence(Recurrence(……]&lt;/h1&gt;

&lt;p&gt;Now if we can define a way to go from the complicated problem, towards our base cases each step,
then after enough iteration, we’ll reach our base cases and know the solution of the hard problem.&lt;/p&gt;

&lt;p&gt;Following? If not hopefully walking through the logic here will help.&lt;/p&gt;

&lt;p&gt;Remember that table we defined to keep track of our base cases? Well we can use it to keep track
of all the intermediate solutions to smaller problems. Define &lt;code class=&quot;highlighter-rouge&quot;&gt;T[i,j]&lt;/code&gt; to be the minimum
edit distance between strings &lt;code class=&quot;highlighter-rouge&quot;&gt;X[1..i]&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Y[1..j]&lt;/code&gt;. Clearly, the solution to the overall
problem of a length &lt;code class=&quot;highlighter-rouge&quot;&gt;n&lt;/code&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;X&lt;/code&gt; string and length &lt;code class=&quot;highlighter-rouge&quot;&gt;m&lt;/code&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;Y&lt;/code&gt; string is then &lt;code class=&quot;highlighter-rouge&quot;&gt;T[n,m]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Okay, so we’ve got to express &lt;code class=&quot;highlighter-rouge&quot;&gt;T[i,j]&lt;/code&gt; as something simpler (since we need to move in the direction
of our base cases), so that means expressing &lt;code class=&quot;highlighter-rouge&quot;&gt;T[i,j]&lt;/code&gt; as a function of elements in the table
with smaller &lt;code class=&quot;highlighter-rouge&quot;&gt;i&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;j&lt;/code&gt; values.&lt;/p&gt;

&lt;p&gt;Consider these substrings.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;s c ?
s e ?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are three options to fill in the &lt;code class=&quot;highlighter-rouge&quot;&gt;?&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We use the next character from the X string &lt;code class=&quot;highlighter-rouge&quot;&gt;X[i]&lt;/code&gt; and leave a &lt;code class=&quot;highlighter-rouge&quot;&gt;_&lt;/code&gt; for the other string.&lt;/li&gt;
  &lt;li&gt;We use the next character from the Y string &lt;code class=&quot;highlighter-rouge&quot;&gt;Y[j]&lt;/code&gt; and leave a &lt;code class=&quot;highlighter-rouge&quot;&gt;_&lt;/code&gt; for the other string.&lt;/li&gt;
  &lt;li&gt;We use both &lt;code class=&quot;highlighter-rouge&quot;&gt;X[1]&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Y[1]&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;T[i,j]&lt;/code&gt; will have the value of the &lt;strong&gt;best&lt;/strong&gt; minimum edit distance when trying these three options out.&lt;/p&gt;

&lt;p&gt;Number 1 is basically just taking a loss by saying we’ll have to add a character into the &lt;code class=&quot;highlighter-rouge&quot;&gt;Y&lt;/code&gt;
string, so the best minimum edit distance it’ll produce is 1 plus the minimum edit distance between
the slightly shorter string &lt;code class=&quot;highlighter-rouge&quot;&gt;X[i-1]&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Y[j]&lt;/code&gt;. We can express this really elegantly as just
&lt;code class=&quot;highlighter-rouge&quot;&gt;T[i,j] = 1 + T[i-1,j]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Likewise, if we chose to use the &lt;code class=&quot;highlighter-rouge&quot;&gt;Y[j]&lt;/code&gt; character, &lt;code class=&quot;highlighter-rouge&quot;&gt;T[i,j] = 1 + T[i,j-1]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, there’s the option of using &lt;strong&gt;both&lt;/strong&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;X[i]&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Y[j]&lt;/code&gt;. Now this is the interesting case.&lt;/p&gt;

&lt;p&gt;If &lt;code class=&quot;highlighter-rouge&quot;&gt;X[i]&lt;/code&gt; is the same character as &lt;code class=&quot;highlighter-rouge&quot;&gt;Y[j]&lt;/code&gt;, then we haven’t increased the edit distance at all from the minimum
edit distance between &lt;code class=&quot;highlighter-rouge&quot;&gt;X[1..i-1]&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Y[1..j-1]&lt;/code&gt; (the prefixes of the current strings).&lt;/p&gt;

&lt;p&gt;If &lt;code class=&quot;highlighter-rouge&quot;&gt;X[i]&lt;/code&gt; is not the same character as &lt;code class=&quot;highlighter-rouge&quot;&gt;Y[j]&lt;/code&gt;, then just like the other cases, we increased the minimum
edit distance by 1.&lt;/p&gt;

&lt;p&gt;Now that we’ve expressed all the cases, we can create a general equation for &lt;code class=&quot;highlighter-rouge&quot;&gt;T[i,j]&lt;/code&gt; by just
taking the best result, so&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;T[i,j] = min{
    1 + T[i-1,j],
    1 + T[i,j-1],
    1 + T[i-1,j-1] if X[i] != Y[j] else T[i-1,j-1],
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;is-that-it&quot;&gt;Is That It?&lt;/h1&gt;

&lt;p&gt;Yes! We finally know how to solve the simplest cases and how to take a complicated problem and
reduce it to simpler cases.&lt;/p&gt;

&lt;p&gt;At this point we can write an algorithm for computing the minimum edit distance called &lt;code class=&quot;highlighter-rouge&quot;&gt;MinD&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;MinD(X: 1-&amp;gt;n, Y: 1-&amp;gt;m):
    for i = 1-&amp;gt;n: T[i,0] = i
    for j = 1-&amp;gt;m: T[0,j] = j

    for i = 1-&amp;gt;n
	for j = 1-&amp;gt;m
	    T[i,j] = min{
		1 + T[i-1,j],
		1 + T[i,j-1],
		1 + T[i-1,j-1] if X[i] != Y[j] else T[i-1,j-1],
	    }

    return T[n,m]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The critical piece to understand here is that every reference to our table in the loop body is
defined by the time we get around to computing it. Basically, our table gets filled row-by-row,
left-to-right, and we only look at previous row/column pairs. We can also see that this algorithm
has a running time of &lt;code class=&quot;highlighter-rouge&quot;&gt;O(n*m)&lt;/code&gt; as we have nested &lt;code class=&quot;highlighter-rouge&quot;&gt;n&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;m&lt;/code&gt; loops.&lt;/p&gt;

&lt;p&gt;Let’s walk through computing the minimum edit distance between &lt;code class=&quot;highlighter-rouge&quot;&gt;sense&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;scince&lt;/code&gt;, I’ve put
string &lt;code class=&quot;highlighter-rouge&quot;&gt;X&lt;/code&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;scince&lt;/code&gt;) along the rows and string &lt;code class=&quot;highlighter-rouge&quot;&gt;Y&lt;/code&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;sense&lt;/code&gt;) over the columns, along with indices
&lt;code class=&quot;highlighter-rouge&quot;&gt;i&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;j&lt;/code&gt; respectively.&lt;/p&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1, s&lt;/td&gt;
      &lt;td&gt;2, e&lt;/td&gt;
      &lt;td&gt;3, n&lt;/td&gt;
      &lt;td&gt;4, s&lt;/td&gt;
      &lt;td&gt;5, e&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1, s&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2, c&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3, i&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4, n&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5, c&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;6, e&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;base-cases-1&quot;&gt;Base cases&lt;/h3&gt;

&lt;p&gt;We can start by filling out our base cases, like so:&lt;/p&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1, s&lt;/td&gt;
      &lt;td&gt;2, e&lt;/td&gt;
      &lt;td&gt;3, n&lt;/td&gt;
      &lt;td&gt;4, s&lt;/td&gt;
      &lt;td&gt;5, e&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1, s&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2, c&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3, i&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4, n&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5, c&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;6, e&lt;/td&gt;
      &lt;td&gt;6&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;starting-to-fill&quot;&gt;Starting to fill&lt;/h3&gt;

&lt;p&gt;Now we start at &lt;code class=&quot;highlighter-rouge&quot;&gt;i=1&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;j=1&lt;/code&gt;, here we consider the inner loop
of the algorithm and see that since &lt;code class=&quot;highlighter-rouge&quot;&gt;X[i] == Y[j]&lt;/code&gt;, we can use
the minimum edit distance of &lt;code class=&quot;highlighter-rouge&quot;&gt;T[i-1,j-1]&lt;/code&gt;, so we get a value of 0.&lt;/p&gt;

&lt;p&gt;Next up, we have the case of &lt;code class=&quot;highlighter-rouge&quot;&gt;i=1&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;j=2&lt;/code&gt;. We can see
that &lt;code class=&quot;highlighter-rouge&quot;&gt;X[i] != Y[j]&lt;/code&gt; in this case, so we won’t be able to 
reuse the edit distance of &lt;code class=&quot;highlighter-rouge&quot;&gt;T[i-1,j-1]&lt;/code&gt;. Evaluating each
case in the minimum clause, we get the following values:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;1 + T[i-1,j] == 2 + 1 == 3
1 + T[i,j-1] == 0 + 1 == 1
1 + T[i-1,j-1] == 1 + 1 == 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;So the minimum edit distance of &lt;code class=&quot;highlighter-rouge&quot;&gt;se&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;s&lt;/code&gt; is 1.&lt;/p&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1, s&lt;/td&gt;
      &lt;td&gt;2, e&lt;/td&gt;
      &lt;td&gt;3, n&lt;/td&gt;
      &lt;td&gt;4, s&lt;/td&gt;
      &lt;td&gt;5, e&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1, s&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2, c&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3, i&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4, n&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5, c&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;6, e&lt;/td&gt;
      &lt;td&gt;6&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;result&quot;&gt;Result&lt;/h3&gt;

&lt;p&gt;After following the algorithm, you can see that we’ve solved for every
possible minimum edit distance for substrings &lt;code class=&quot;highlighter-rouge&quot;&gt;X[1..i]&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Y[1..j]&lt;/code&gt;,
until we finally reach the solution we want, &lt;code class=&quot;highlighter-rouge&quot;&gt;X[1..n]&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Y[1..m]&lt;/code&gt;.&lt;/p&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1, s&lt;/td&gt;
      &lt;td&gt;2, e&lt;/td&gt;
      &lt;td&gt;3, n&lt;/td&gt;
      &lt;td&gt;4, s&lt;/td&gt;
      &lt;td&gt;5, e&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1, s&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2, c&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3, i&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4, n&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5, c&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;6, e&lt;/td&gt;
      &lt;td&gt;6&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;&lt;strong&gt;3&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h1 id=&quot;reflecting&quot;&gt;Reflecting&lt;/h1&gt;

&lt;p&gt;What we just built is a dynamic programming algorithm. Programming here doesn’t really refer to 
our modern sense of computer programming, but instead is more akin to the word planning.&lt;/p&gt;

&lt;p&gt;When a plan is too complicated to work out immediately, then we try to think of a subproblem, 
that if solved would allow us to figure out the more complicated problem. In our case, that
subproblem was figuring out the minimum edit distance of a prefix of the input strings.&lt;/p&gt;

&lt;p&gt;This step of finding a subproblem to solve is the piece that requires some experimentation,
trying a specific definition of &lt;code class=&quot;highlighter-rouge&quot;&gt;T&lt;/code&gt; and seeing if it is possible to state a recurrence relation
between &lt;code class=&quot;highlighter-rouge&quot;&gt;T[i,j]&lt;/code&gt; and previously computed &lt;code class=&quot;highlighter-rouge&quot;&gt;T[i,j]&lt;/code&gt; values. If it’s not possible, then you
probably need to revisit your definition of &lt;code class=&quot;highlighter-rouge&quot;&gt;T&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you find this sort of stuff cool, you should do some searching on proofs by induction.
They’re heavily related to this concept but focus on proving certain properties by proving
a base case, then defining a recurrence relationship that moves you closer to the base case.&lt;/p&gt;

&lt;p&gt;So there you have it, dynamic programming is just a basic problem solving technique wrapped
in some fancy language.&lt;/p&gt;</content><author><name></name></author><category term="algorithms" /><summary type="html">Minimum Edit Distance</summary></entry><entry><title type="html">Entity component system</title><link href="/programming/2020/05/09/ecs.html" rel="alternate" type="text/html" title="Entity component system" /><published>2020-05-09T05:00:00+00:00</published><updated>2020-05-09T05:00:00+00:00</updated><id>/programming/2020/05/09/ecs</id><content type="html" xml:base="/programming/2020/05/09/ecs.html">&lt;p&gt;I’ve been playing with &lt;a href=&quot;https://github.com/amethyst/amethyst&quot;&gt;Amethyst&lt;/a&gt; a little bit. It’s a library
that leverages an Entity-Component-System Architecture (which I’ll refer to as ECS).&lt;/p&gt;

&lt;p&gt;I think ECS is interesting (even if you, like me, don’t intend to do serious game dev) because:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It’s completely unintuitive at first&lt;/li&gt;
  &lt;li&gt;It flies in the face of (college taught) object oriented design&lt;/li&gt;
  &lt;li&gt;It shows the difference between composition and inheritance&lt;/li&gt;
  &lt;li&gt;It can improve your software design&lt;/li&gt;
  &lt;li&gt;It can improve performance&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;entities&quot;&gt;Entities&lt;/h1&gt;

&lt;p&gt;Game &lt;strong&gt;entities&lt;/strong&gt; are the basic nouns in your game. For example, maybe you have a player, a block of terrain,
a sheep, an arrow.&lt;/p&gt;

&lt;p&gt;A key part of an ECS is that these entities are incredibly simple, they’re just an ID (like an integer).&lt;/p&gt;

&lt;p&gt;This is opposed to some other approaches that might have entities represented by a nested inheritance structure.&lt;/p&gt;

&lt;h1 id=&quot;components&quot;&gt;Components&lt;/h1&gt;

&lt;p&gt;Obviously you can’t do much without data associated with the identifier, so we add that through &lt;strong&gt;components&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Components are just dumb data. They do not contain logic. The most common component for a 2D game is likely
a position component which just contains an X-value and a Y-value.&lt;/p&gt;

&lt;p&gt;Now how do we find the position of a given entity? We can just use the entity ID as an index into
a big array of all positions containing several 100 position components.&lt;/p&gt;

&lt;h1 id=&quot;system&quot;&gt;System&lt;/h1&gt;

&lt;p&gt;We have a way to represent an entity and data about that entity, now we just need to add in logic.&lt;/p&gt;

&lt;p&gt;This is done through &lt;strong&gt;systems&lt;/strong&gt;. Systems are essentially just functions that can ask for groupings of
components to operate over. They can potentially mutate the components.&lt;/p&gt;

&lt;p&gt;For example, assume we have hundreds of &lt;code class=&quot;highlighter-rouge&quot;&gt;ball&lt;/code&gt; entities in our environment. Each ball has a &lt;code class=&quot;highlighter-rouge&quot;&gt;position&lt;/code&gt; and 
a &lt;code class=&quot;highlighter-rouge&quot;&gt;velocity&lt;/code&gt; component. We can also have a few static &lt;code class=&quot;highlighter-rouge&quot;&gt;box&lt;/code&gt; entities, which only have a &lt;code class=&quot;highlighter-rouge&quot;&gt;position&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A basic system for running movement simulation would request all components of type &lt;code class=&quot;highlighter-rouge&quot;&gt;position&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;velocity&lt;/code&gt;,
grouped by entity, and for each entity update the &lt;code class=&quot;highlighter-rouge&quot;&gt;velocity&lt;/code&gt; based on the effect of gravity, then update
&lt;code class=&quot;highlighter-rouge&quot;&gt;position&lt;/code&gt; based on the current &lt;code class=&quot;highlighter-rouge&quot;&gt;velocity&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This would mean only the &lt;code class=&quot;highlighter-rouge&quot;&gt;ball&lt;/code&gt; entities actually move, while the platforms stay static.&lt;/p&gt;

&lt;p&gt;If we assume each entity has a &lt;code class=&quot;highlighter-rouge&quot;&gt;collision_shape&lt;/code&gt; component and an &lt;code class=&quot;highlighter-rouge&quot;&gt;is_dead&lt;/code&gt; component, we can even
develop a system that requests groups of &lt;code class=&quot;highlighter-rouge&quot;&gt;(position, collision_shape, is_dead)&lt;/code&gt;, and checks if the collision
shapes collided. If they have, it can mark both collided objects as dead. Another renderer system
could read &lt;code class=&quot;highlighter-rouge&quot;&gt;is_dead&lt;/code&gt; and not render the entity if so.&lt;/p&gt;

&lt;h1 id=&quot;performance&quot;&gt;Performance&lt;/h1&gt;

&lt;p&gt;What did all this reorganizing buy us? Well, for one thing, if there’s anything computer architecture likes,
it’s predictable, sequential access patterns. Fundamentally this is because sequential iteration over a vector
makes it really easy to figure out where future data is going to come from. This allows your CPU to prefetch
data and prevent cache misses.&lt;/p&gt;

&lt;p&gt;Now, rather conveniently, all of our data is in these nice long sequential vectors.&lt;/p&gt;

&lt;h1 id=&quot;organization&quot;&gt;Organization&lt;/h1&gt;

&lt;p&gt;Another nice part of ECS is that adding and reusing functionality across entities is really easy. For example,
you likely will have an entity for your camera. Do you want a camera that moves according to physics rules you
implemented for a ball? Well, you can do that! Just slap a &lt;code class=&quot;highlighter-rouge&quot;&gt;velocity&lt;/code&gt; component on there and now you’ve got
your motion system running on your camera entity! Yes that’s a weird example, but it demonstrates the flexibility
inherent in this sort of architecture. Any entity can have any component with minimal refactor.&lt;/p&gt;</content><author><name></name></author><category term="programming" /><summary type="html">I’ve been playing with Amethyst a little bit. It’s a library that leverages an Entity-Component-System Architecture (which I’ll refer to as ECS).</summary></entry><entry><title type="html">Hough Transform</title><link href="/vision/2020/03/01/hough-transforms.html" rel="alternate" type="text/html" title="Hough Transform" /><published>2020-03-01T22:53:00+00:00</published><updated>2020-03-01T22:53:00+00:00</updated><id>/vision/2020/03/01/hough-transforms</id><content type="html" xml:base="/vision/2020/03/01/hough-transforms.html">&lt;h1 id=&quot;problem&quot;&gt;Problem&lt;/h1&gt;

&lt;p&gt;If I have an image like below, how do I actually figure out the slope and y-intercept of the line?&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/2020-03-01-hough-transforms/hough_input.png&quot; alt=&quot;Input&quot; title=&quot;Input&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As a human, we’d just recognize the line as an object, choose any two points along it,
and then we’ve got two x -&amp;gt; y mappings and two unknowns &lt;code class=&quot;highlighter-rouge&quot;&gt;y = m*x + b&lt;/code&gt;, so we can easily solve
for &lt;code class=&quot;highlighter-rouge&quot;&gt;m&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;b&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But how do we do this computationally?&lt;/p&gt;

&lt;h1 id=&quot;hough-transforms&quot;&gt;Hough Transforms&lt;/h1&gt;

&lt;p&gt;One approach is to just try every y-intecerpt and every slope, linearly spaced, and try to find one
that looks similar to the input image.&lt;/p&gt;

&lt;p&gt;Once you have this image attempt, you can find the intersection of your input image with this attempted
image, then calculate the sum of the resulting intersection. This gives you a score for how well a particular
&lt;code class=&quot;highlighter-rouge&quot;&gt;m&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;b&lt;/code&gt; fit the input data.&lt;/p&gt;

&lt;p&gt;If we do this many times, we can start to visualize what is called “Hough Space”, a space
defined by the parameters of the line, rather than &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;y&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/2020-03-01-hough-transforms/hough_single_line.png&quot; alt=&quot;Simple&quot; title=&quot;hough plot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;When looking at the Hough plot, the brightest spot corresponds to the true slope and y-intercept of
the input image’s line.&lt;/p&gt;

&lt;p&gt;You’ll also notice that there are large slightly brighter regions across the resulting Hough plot.
This is because other lines that don’t actually match, may still intercept the line in the original
image, so they’ll have a small, but non-zero score.&lt;/p&gt;

&lt;h1 id=&quot;mapping-between&quot;&gt;Mapping Between&lt;/h1&gt;

&lt;p&gt;Let’s look at a slightly more complicated example.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/2020-03-01-hough-transforms/hough_all.png&quot; alt=&quot;All&quot; title=&quot;hough all&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Here we have a shorter line with a different slope and y-intercept. You can still see this line’s
impact in the Hough plot, but since we’re scoring based on total number of matching pixels,
it will never get as bright as the longer line does.&lt;/p&gt;

&lt;p&gt;A final super interesting property here is the line-like slightly bright region in the top of the image.&lt;/p&gt;

&lt;p&gt;This line (in Hough space) corresponds to the point we have in regular XY space!&lt;/p&gt;

&lt;p&gt;Lines in XY space correspond to points in Hough space, and lines in Hough space correspond 
to points in XY space. This means that any point in the Hough line, corresponds to a line in XY space
that intersects the given point, nifty!&lt;/p&gt;

&lt;h1 id=&quot;looking-beyond&quot;&gt;Looking Beyond&lt;/h1&gt;

&lt;p&gt;This approach is the basis of OpenCV’s Hough* functions.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://docs.opencv.org/4.2.0/d9/db0/tutorial_hough_lines.html&quot;&gt;Hough Lines&lt;/a&gt; uses this technique on
images after extracting image edges to find lines. You’ll notice that it doesn’t use our classic
&lt;code class=&quot;highlighter-rouge&quot;&gt;y = m*x + b&lt;/code&gt; formula for a line, but instead uses a polar representation that avoids problems with vertical
lines.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://docs.opencv.org/4.2.0/dd/d1a/group__imgproc__feature.html#ga47849c3be0d0406ad3ca45db65a25d2d&quot;&gt;Hough Circles&lt;/a&gt;
also uses this approach. You can imagine that the “model” you search for in your image doesn’t just have
to be a line, but it could be a point and a radius.&lt;/p&gt;</content><author><name></name></author><category term="vision" /><summary type="html">Problem</summary></entry><entry><title type="html">Hacking up your own shell completion</title><link href="/environment/2020/01/18/build-your-own-shell-completion.html" rel="alternate" type="text/html" title="Hacking up your own shell completion" /><published>2020-01-18T10:41:00+00:00</published><updated>2020-01-18T10:41:00+00:00</updated><id>/environment/2020/01/18/build-your-own-shell-completion</id><content type="html" xml:base="/environment/2020/01/18/build-your-own-shell-completion.html">&lt;p&gt;In this post I’ll be walking through a simple hack I added to my shell that I think makes me more
productive (if nothing else it makes me happy).&lt;/p&gt;

&lt;p&gt;Note that all this applies to most common shells, but my examples will be using
&lt;a href=&quot;https://fishshell.com/&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;fish&lt;/code&gt;&lt;/a&gt; since that’s what I’ve been using as of late.&lt;/p&gt;

&lt;p&gt;I’ll also be talking about using &lt;a href=&quot;https://github.com/junegunn/fzf&quot;&gt;fzf&lt;/a&gt; as a part of this hack.
You can use &lt;code class=&quot;highlighter-rouge&quot;&gt;fzf&lt;/code&gt; or not with this idea, but I like the feel of it :).&lt;/p&gt;

&lt;h2 id=&quot;introducing-my-problems&quot;&gt;Introducing: My Problems&lt;/h2&gt;

&lt;p&gt;As a full-stack (frontend, backend, and firmware) software engineer, I use quite a
few tools to build, test, run, version control, and style check
the code I write.&lt;/p&gt;

&lt;p&gt;At my company we built this pretty simplistic go program that is really a collection of scripts
for building and testing out parts of our application.&lt;/p&gt;

&lt;p&gt;We’ll call the tool &lt;code class=&quot;highlighter-rouge&quot;&gt;doer&lt;/code&gt; for fun.&lt;/p&gt;

&lt;p&gt;Those things can be listed using the command &lt;code class=&quot;highlighter-rouge&quot;&gt;doer -list&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;No one really bothered making autocompletion scripts for 3+ shells used across the company 
for an internal tool, but I was having trouble remembering the exact abbreviations involved
in the command I wanted to run.&lt;/p&gt;

&lt;h2 id=&quot;script-it&quot;&gt;Script It!&lt;/h2&gt;

&lt;p&gt;Some people at my company use &lt;code class=&quot;highlighter-rouge&quot;&gt;fzf&lt;/code&gt;. For the uninitiated, &lt;code class=&quot;highlighter-rouge&quot;&gt;fzf&lt;/code&gt; is a program that lets you select
from multiple options using a fuzzy find search.&lt;/p&gt;

&lt;p&gt;The cool thing about &lt;code class=&quot;highlighter-rouge&quot;&gt;fzf&lt;/code&gt; is that you can send it a newline separated list of items,
and it’ll handle all the user interaction to select one and return the selected
item.&lt;/p&gt;

&lt;p&gt;Here’s an example of using &lt;code class=&quot;highlighter-rouge&quot;&gt;fzf&lt;/code&gt; to choose a line from a file and echo it.&lt;/p&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;
    &lt;script id=&quot;asciicast-jtcTgKoHx5VHCWwAS1oFVCx7p&quot; src=&quot;https://asciinema.org/a/jtcTgKoHx5VHCWwAS1oFVCx7p.js&quot; async=&quot;&quot;&gt;&lt;/script&gt;
&lt;/div&gt;

&lt;p&gt;I’ve often used &lt;code class=&quot;highlighter-rouge&quot;&gt;fzf&lt;/code&gt; as a file finder, you can simply run something like &lt;code class=&quot;highlighter-rouge&quot;&gt;git ls-files | fzf&lt;/code&gt;, and pass
the output to your editor, and you’ve already made a git aware fuzzy file finder, running this inside
your editor like &lt;a href=&quot;https://github.com/junegunn/fzf.vim&quot;&gt;fzf-vim&lt;/a&gt; makes this even more powerful.&lt;/p&gt;

&lt;p&gt;One quick solution to our &lt;code class=&quot;highlighter-rouge&quot;&gt;doer&lt;/code&gt; problem is to write a bash script like this:&lt;/p&gt;

&lt;div class=&quot;language-bash 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;c&quot;&gt;#!/bin/bash&lt;/span&gt;
doer &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;doer &lt;span class=&quot;nt&quot;&gt;-list&lt;/span&gt; | fzf&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here we just use command substitution to run &lt;code class=&quot;highlighter-rouge&quot;&gt;doer&lt;/code&gt;’s list command, pass it to &lt;code class=&quot;highlighter-rouge&quot;&gt;fzf&lt;/code&gt;, then run doer
with the result.&lt;/p&gt;

&lt;p&gt;Here’s it in action:&lt;/p&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;
    &lt;script id=&quot;asciicast-YDVpbyrXFKjiw1QSTe6L7ZJnf&quot; src=&quot;https://asciinema.org/a/YDVpbyrXFKjiw1QSTe6L7ZJnf.js&quot; async=&quot;&quot;&gt;&lt;/script&gt;
&lt;/div&gt;

&lt;p&gt;This works &lt;em&gt;really&lt;/em&gt; well, but unfortunately you lose shell history when you run this command.&lt;/p&gt;

&lt;p&gt;For example, lets say I finally figure out through the fuzzy completion version of &lt;code class=&quot;highlighter-rouge&quot;&gt;doer&lt;/code&gt; that I really
want to run &lt;code class=&quot;highlighter-rouge&quot;&gt;doer gen/other_thing&lt;/code&gt;, because it generates a file based on one I’m currently modifying.&lt;/p&gt;

&lt;p&gt;So I run the command once, fuzzy selecting my option, modify some code, then return to my shell
to quickly repeat the command I had just tried.&lt;/p&gt;

&lt;p&gt;After a quick &lt;code class=&quot;highlighter-rouge&quot;&gt;CTRL-P&lt;/code&gt; (or up arrow), or &lt;code class=&quot;highlighter-rouge&quot;&gt;CTRL-R&lt;/code&gt;, I find that the only thing in my 
history is &lt;code class=&quot;highlighter-rouge&quot;&gt;doer-fuzzy&lt;/code&gt;.
That means I have to fuzzy find my option again and rerun, and it means I don’t have a great history
of what I’ve run in the past. What a drag!&lt;/p&gt;

&lt;h2 id=&quot;maintaining-history&quot;&gt;Maintaining History&lt;/h2&gt;

&lt;p&gt;My first thought here was to somehow append the underlying command being run by &lt;code class=&quot;highlighter-rouge&quot;&gt;doer-fuzzy&lt;/code&gt; to my shell
history, but I quickly found out that this is hard, hacky, and no one really does this.&lt;/p&gt;

&lt;p&gt;So really what we want is to trigger argument completion in the shell, so that by the time I hit return
to trigger the command, the full command I’m interested in is on the shell already.&lt;/p&gt;

&lt;p&gt;Oh wait… that sounds like just normal shell completion, you the know, the tab-tab-tab-tab-tab approach.&lt;/p&gt;

&lt;p&gt;Okay fine, I guess I can add proper shell completion to the command using &lt;code class=&quot;highlighter-rouge&quot;&gt;fish&lt;/code&gt;’s built in &lt;code class=&quot;highlighter-rouge&quot;&gt;complete&lt;/code&gt; command,
add it to the repo, and set up an install script. But now that I think about it, I have several commands
I use that don’t support completion (not just &lt;code class=&quot;highlighter-rouge&quot;&gt;doer&lt;/code&gt;), or complete too much stuff for my 90% use case,
or just support so many options its a bit painful to autocomplete through tab key wear out.&lt;/p&gt;

&lt;p&gt;What if there was a way to strap an &lt;code class=&quot;highlighter-rouge&quot;&gt;fzf&lt;/code&gt; completion thing into my shell, that I fully control through
just a couple lines of &lt;code class=&quot;highlighter-rouge&quot;&gt;fish&lt;/code&gt; script, that doesn’t interfere with built-in completion, and allows me to 
quickly add bindings to any command I frequently run?&lt;/p&gt;

&lt;h2 id=&quot;introducing-personalized-fzf-completion&quot;&gt;Introducing Personalized FZF Completion!&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;fzf&lt;/code&gt; already sort of does this type of thing when you install the &lt;code class=&quot;highlighter-rouge&quot;&gt;fzf&lt;/code&gt; bindings to your shell.
Regardless of your currently typed command, you can hit &lt;code class=&quot;highlighter-rouge&quot;&gt;CTRL-T&lt;/code&gt; to search for a list of files in
your current directory. For example, lets say we’re trying to open a file in &lt;code class=&quot;highlighter-rouge&quot;&gt;nvim&lt;/code&gt;.&lt;/p&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;
    &lt;script id=&quot;asciicast-39bUMZz04ihhu4AzStO6ihKUF&quot; src=&quot;https://asciinema.org/a/39bUMZz04ihhu4AzStO6ihKUF.js&quot; async=&quot;&quot;&gt;&lt;/script&gt;
&lt;/div&gt;

&lt;p&gt;What if we could copy how &lt;code class=&quot;highlighter-rouge&quot;&gt;CTRL-T&lt;/code&gt; works, but replace the hardcoded &lt;code class=&quot;highlighter-rouge&quot;&gt;fzf&lt;/code&gt; command with a contextual one
that we control?&lt;/p&gt;

&lt;p&gt;Here’s a basic &lt;code class=&quot;highlighter-rouge&quot;&gt;fish&lt;/code&gt; script to accomplish the core part of this. Basically we just match
on any commands that are prefixed with &lt;code class=&quot;highlighter-rouge&quot;&gt;doer&lt;/code&gt;, and return an appropriate autocompletion function
for that command.&lt;/p&gt;

&lt;div class=&quot;language-bash 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;function &lt;/span&gt;get-completion-command
	&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; cmd &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;commandline&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	switch &lt;span class=&quot;nv&quot;&gt;$cmd&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'doer *'&lt;/span&gt;
			&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'doer -list'&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;return &lt;/span&gt;1
	end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This function gets called in a skeleton version of &lt;code class=&quot;highlighter-rouge&quot;&gt;fzf&lt;/code&gt;’s &lt;code class=&quot;highlighter-rouge&quot;&gt;CTRL-T&lt;/code&gt; command.&lt;/p&gt;

&lt;div class=&quot;language-bash 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;function &lt;/span&gt;fzf-smart-completion &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;List files and folders&quot;&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; commandline &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;__fzf_parse_commandline&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;dir&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$commandline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;1]
	&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; fzf_query &lt;span class=&quot;nv&quot;&gt;$commandline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;2]

	&lt;span class=&quot;c&quot;&gt;# use our cool new completion checker&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; FZF_CMD &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;get-completion-command&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; or &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;

	&lt;span class=&quot;c&quot;&gt;# fzf edge case and formatting (prevents fzf from taking up the whole screen)&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;FZF_HEIGHT 40%
	begin
	  &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-lx&lt;/span&gt; FZF_DEFAULT_OPTS &lt;span class=&quot;s2&quot;&gt;&quot;--height &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$FZF_HEIGHT&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; --reverse &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$FZF_DEFAULT_OPTS&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$FZF_CMD&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[2]&quot;&lt;/span&gt;
	  &lt;span class=&quot;nb&quot;&gt;eval&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$FZF_CMD&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;[1] | &quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;__fzfcmd&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' -m --query &quot;'&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$fzf_query&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'&quot;'&lt;/span&gt; | &lt;span class=&quot;k&quot;&gt;while &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;read&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; r&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;result &lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; end
	end
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-z&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
	  commandline &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; repaint
	  &lt;span class=&quot;k&quot;&gt;return
	else&lt;/span&gt;
	  &lt;span class=&quot;c&quot;&gt;# Remove last token from commandline.&lt;/span&gt;
	  commandline &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;
	end
	&lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;i &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$result&lt;/span&gt;
	  commandline &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;string escape &lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	  commandline &lt;span class=&quot;nt&quot;&gt;-it&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;' '&lt;/span&gt;
	end
	commandline &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; repaint
end

&lt;span class=&quot;nb&quot;&gt;bind&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-M&lt;/span&gt; insert &lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;t fzf-smart-completion
&lt;span class=&quot;nb&quot;&gt;bind&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\e&lt;/span&gt;t fzf-smart-completion
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this little bit of &lt;code class=&quot;highlighter-rouge&quot;&gt;fish&lt;/code&gt; scripting, we now have a pretty cool &lt;code class=&quot;highlighter-rouge&quot;&gt;ALT-T&lt;/code&gt; command that runs
our own fuzzy autocomplete like so.&lt;/p&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;
	&lt;script id=&quot;asciicast-5CFh2UxRWgHbuHjgR4PIKa38U&quot; src=&quot;https://asciinema.org/a/5CFh2UxRWgHbuHjgR4PIKa38U.js&quot; async=&quot;&quot;&gt;&lt;/script&gt;
&lt;/div&gt;

&lt;h1 id=&quot;extensions&quot;&gt;Extensions&lt;/h1&gt;

&lt;p&gt;This got me thinking, what other commands could benefit from this type of completion?&lt;/p&gt;

&lt;p&gt;One example is &lt;code class=&quot;highlighter-rouge&quot;&gt;go test&lt;/code&gt;, which for my use cases, should only run on source controlled files ending
in &lt;code class=&quot;highlighter-rouge&quot;&gt;_test.go&lt;/code&gt;. There’s no reason to autocomplete every file in my repo, just the test ones is perfect!&lt;/p&gt;

&lt;p&gt;The same idea applies to our frontend test runner, if I’m typing &lt;code class=&quot;highlighter-rouge&quot;&gt;yarn test&lt;/code&gt;, I probably
only want to see typescript files ending with our &lt;code class=&quot;highlighter-rouge&quot;&gt;.test&lt;/code&gt; convention.&lt;/p&gt;

&lt;div class=&quot;language-bash 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;function &lt;/span&gt;get-completion-command
	&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; cmd &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;commandline&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	switch &lt;span class=&quot;nv&quot;&gt;$cmd&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'doer *'&lt;/span&gt;
			&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'doer -list'&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'go test *'&lt;/span&gt;
			&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'git ls-files | grep _test.go'&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'yarn test *'&lt;/span&gt;
			&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'git ls-files | grep .test.ts'&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;return &lt;/span&gt;1
	end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can see the go tester in action here:&lt;/p&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;
	&lt;script id=&quot;asciicast-ScViakas3u43FxVbuzSHjOWEu&quot; src=&quot;https://asciinema.org/a/ScViakas3u43FxVbuzSHjOWEu.js&quot; async=&quot;&quot;&gt;&lt;/script&gt;
&lt;/div&gt;

&lt;p&gt;Another useful thing I’ve found is automating some routine &lt;code class=&quot;highlighter-rouge&quot;&gt;git&lt;/code&gt; commands.&lt;/p&gt;

&lt;p&gt;Obviously there exist quite a few &lt;code class=&quot;highlighter-rouge&quot;&gt;git&lt;/code&gt; UIs that try to allow you to use &lt;code class=&quot;highlighter-rouge&quot;&gt;git&lt;/code&gt; more quickly than the
CLI interface, but I have always come back to the CLI because frankly (1) there’s more documentation, and
(2) it doesn’t have performance issues on massive monorepos that I’ve seen in every &lt;code class=&quot;highlighter-rouge&quot;&gt;git&lt;/code&gt; UI (cough cough
&lt;a href=&quot;https://magit.vc/manual/magit/Performance.html#Performance&quot;&gt;magit&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Good &lt;code class=&quot;highlighter-rouge&quot;&gt;git&lt;/code&gt; autocomplete is awesome and likely used by everyone, but we can extend it using this same
interface.&lt;/p&gt;

&lt;p&gt;Simply adding the following gives you a &lt;code class=&quot;highlighter-rouge&quot;&gt;git add&lt;/code&gt; command that completes only changed files and
allows showing a toggleable preview of the file changes.&lt;/p&gt;

&lt;div class=&quot;language-bash 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;case&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'git add *'&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'git diff --name-only'&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'--bind='&lt;/span&gt;ctrl-space:toggle-preview&lt;span class=&quot;s1&quot;&gt;' --preview \'&lt;/span&gt;git diff &lt;span class=&quot;nt&quot;&gt;--color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;always &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\'&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;
	&lt;script id=&quot;asciicast-qakqDCZ4122BicEpje9SCXjtR&quot; src=&quot;https://asciinema.org/a/qakqDCZ4122BicEpje9SCXjtR.js&quot; async=&quot;&quot;&gt;&lt;/script&gt;
&lt;/div&gt;

&lt;h1 id=&quot;finish&quot;&gt;Finish&lt;/h1&gt;

&lt;p&gt;If you look hard enough at these scripts, you’ll find there are flaws or gaps in this completion system.&lt;/p&gt;

&lt;p&gt;A common example for me is my &lt;code class=&quot;highlighter-rouge&quot;&gt;git checkout&lt;/code&gt; completion. I simply list local branches as targets. This obviously
ignores a lot of different targets to the &lt;code class=&quot;highlighter-rouge&quot;&gt;git checkout&lt;/code&gt; command including commits, specific files, remote branches,
and tags, but for me, &amp;gt; 90% of the time I’m using it to just switch branches.&lt;/p&gt;

&lt;p&gt;On top of that, using this new framework doesn’t break or invalidate any other tool you have, I frequently
fall back to &lt;code class=&quot;highlighter-rouge&quot;&gt;fish&lt;/code&gt;’s &lt;code class=&quot;highlighter-rouge&quot;&gt;git&lt;/code&gt; completion when I’m doing something more specific, but I’m happy that
my most common access patterns are now a bit faster.&lt;/p&gt;

&lt;p&gt;This pattern also doesn’t add a layer of abstraction over the CLI tools you use, your shell history
still is useful, you use the same CLI tools as everyone else, you just hopefully save yourself a bit of typing.&lt;/p&gt;

&lt;p&gt;I’m quite sure this sort of thing would be pretty trivial to get working
in both &lt;code class=&quot;highlighter-rouge&quot;&gt;bash&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;zsh&lt;/code&gt; since they support &lt;code class=&quot;highlighter-rouge&quot;&gt;fzf&lt;/code&gt;’s &lt;code class=&quot;highlighter-rouge&quot;&gt;CTRL-T&lt;/code&gt; command as well. If I get a lot of long term
use out of this maybe I’ll try getting a similar idea running in a few shells and share those scripts.&lt;/p&gt;</content><author><name></name></author><category term="environment" /><summary type="html">In this post I’ll be walking through a simple hack I added to my shell that I think makes me more productive (if nothing else it makes me happy).</summary></entry><entry><title type="html">What is a control system anyway?</title><link href="/control/2020/01/12/simple-control-systems.html" rel="alternate" type="text/html" title="What is a control system anyway?" /><published>2020-01-12T06:30:00+00:00</published><updated>2020-01-12T06:30:00+00:00</updated><id>/control/2020/01/12/simple-control-systems</id><content type="html" xml:base="/control/2020/01/12/simple-control-systems.html">&lt;head&gt;
  &lt;script src=&quot;/assets/2020-01-02-simple-control-systems/onload.js&quot;&gt;
  &lt;/script&gt;
&lt;/head&gt;

&lt;p&gt;In this post I’ll be giving some intuition for how a basic control system for a drone
in a 2D world could work.&lt;/p&gt;

&lt;p&gt;This is purely for fun and explanations are aimed at providing some intuition for the problem space
rather than getting too far into implementation (I’ll get there later!).&lt;/p&gt;

&lt;p&gt;Let’s say we have a quadcopter and we’d like to write some
software that guides it towards a target location.
How do we go about doing that? Why is this even a difficult problem?&lt;/p&gt;

&lt;p&gt;We’ll start with a simple model for how an ideal drone might work ignoring the altitude
component for now. You can control it with your arrow keys below (make sure you click on the canvas first).&lt;/p&gt;

&lt;p&gt;As you get a feel for flying our little quadcopter, pay attention to what variable you’re actually
controlling (is it velocity or acceleration?)&lt;/p&gt;

&lt;body&gt;
  &lt;div style=&quot;display: flex; justify-content: center; padding-top: 15px; padding-bottom: 15px&quot;&gt;
    &lt;canvas id=&quot;inputCanvas&quot; tabindex=&quot;1&quot;&gt;&lt;/canvas&gt;
  &lt;/div&gt;
&lt;/body&gt;

&lt;p&gt;You aren’t directly controlling velocity, otherwise you’d be able to just let go of an arrow key and
immediately get the vehicle to stop.&lt;/p&gt;

&lt;p&gt;Instead you’re actually controlling the acceleration of the quadcopter, so if you build up too much
speed, then want to come to a stop, you have to accelerate opposite to your velocity.&lt;/p&gt;

&lt;p&gt;How exactly could we make this simulated quadcopter fly towards an arbitrary XY position?&lt;/p&gt;

&lt;h2 id=&quot;bang-bang&quot;&gt;Bang Bang&lt;/h2&gt;

&lt;p&gt;We can take the thermostat approach! What does your lovely thermostat do all day? Well, it works
really hard to try to keep a temperature reading in a small range of comfort.&lt;/p&gt;

&lt;p&gt;When the temperature is too high by some amount, it turns on cooling until the temperature
is back to optimal.&lt;/p&gt;

&lt;p&gt;If the temperature is too low, it kicks on heating until back to optimal.&lt;/p&gt;

&lt;p&gt;In our situation we’ll take this to mean always accelerating towards our target full blast.&lt;/p&gt;

&lt;p&gt;Lets see how this type of control system performs below (you can click or tap to move the target location).&lt;/p&gt;

&lt;body&gt;
  &lt;div style=&quot;display: flex; justify-content: center; padding-top: 15px; padding-bottom: 15px&quot;&gt;
    &lt;canvas id=&quot;bangBangCanvas&quot; width=&quot;640&quot; height=&quot;480&quot; tabindex=&quot;1&quot;&gt;&lt;/canvas&gt;
  &lt;/div&gt;
&lt;/body&gt;

&lt;p&gt;You’ll notice that this controller does get us into position eventually, but once its on the target
the acceleration vector will be jumping wildly in opposite directions.&lt;/p&gt;

&lt;p&gt;Now this might work in theory, but you can imagine hard and sudden changes in acceleration
commands could put a bunch of mechanical stress on your drone (and generally look a bit scary).&lt;/p&gt;

&lt;h2 id=&quot;stop-breaking-things&quot;&gt;Stop Breaking Things!&lt;/h2&gt;

&lt;p&gt;So how to we go about solving this problem?&lt;/p&gt;

&lt;p&gt;One idea is that small amounts of position error should really only cause an acceleration in 
&lt;strong&gt;proportion&lt;/strong&gt; to the amount of error.
So if we’re far away, we can punch it to get closer, and if we’re already close, we can just use a little
bit of acceleration to get there.&lt;/p&gt;

&lt;body&gt;
  &lt;div style=&quot;display: flex; justify-content: center; padding-top: 15px; padding-bottom: 15px&quot;&gt;
    &lt;canvas id=&quot;pCanvas&quot; width=&quot;640&quot; height=&quot;480&quot; tabindex=&quot;1&quot;&gt;&lt;/canvas&gt;
  &lt;/div&gt;
&lt;/body&gt;

&lt;p&gt;This is called a proportional controller.
You can see the acceleration vector just getting large when the drone is far away from the target,
and staying pretty small otherwise.&lt;/p&gt;

&lt;p&gt;This actually works pretty well! Sure, we overshoot quite a bit at the beginning, but things
eventually settle down and when we’re really close to the target we aren’t oscillating.&lt;/p&gt;

&lt;h2 id=&quot;speed-is-key&quot;&gt;Speed is Key&lt;/h2&gt;

&lt;p&gt;So at this point we’re more or less controlling our drone, and we aren’t frying motors or scaring
anyone, so what is there to work on next?&lt;/p&gt;

&lt;p&gt;Speed! Specifically settling time. We want our drone on its target as quickly as possible with minimal
overshoot.&lt;/p&gt;

&lt;p&gt;How do we go faster? Well if we think about it, it’s silly to only start slowing down &lt;strong&gt;after&lt;/strong&gt;
we pass the target.
We really need a way to teach the drone how to slow down preemptively.&lt;/p&gt;

&lt;p&gt;How do we know if we’re going too fast?&lt;/p&gt;

&lt;p&gt;One good way is to look at the &lt;strong&gt;change&lt;/strong&gt; in our error in the last timestep.&lt;/p&gt;

&lt;p&gt;If our error is smaller than last time, we know we are approaching the target, and if the error is
larger than last time, we know we are moving away from the target.&lt;/p&gt;

&lt;p&gt;So if the error last timestep minus the error this timestep is very large, that tells us we are approaching
the target too quickly and need to slow down.&lt;/p&gt;

&lt;p&gt;We can implement this by taking the proportional control value we calculated above, and just subtracting
this change in error. We can have separate (tunable) constants for the proportional part and this
error difference to adjust the controllers performance.&lt;/p&gt;

&lt;body&gt;
  &lt;div style=&quot;display: flex; justify-content: center; padding-top: 15px; padding-bottom: 15px&quot;&gt;
    &lt;canvas id=&quot;dCanvas&quot; width=&quot;640&quot; height=&quot;480&quot; tabindex=&quot;1&quot;&gt;&lt;/canvas&gt;
  &lt;/div&gt;
&lt;/body&gt;

&lt;p&gt;Whoa, that’s pretty good! We confidently accelerate up to our max speed, cruise for a bit, then slow
down before hitting our target, perfect!&lt;/p&gt;

&lt;p&gt;Before we move on, we can come up with a better name for this error difference we talked about.
It’s actually just a numerical derivative! Also, as a side note, since we’re taking the derivative of
our error, which happens to be a relative position, this derivative term is really calculating our velocity
for us, and preventing that velocity from getting too high!&lt;/p&gt;

&lt;h2 id=&quot;windy&quot;&gt;Windy&lt;/h2&gt;

&lt;p&gt;We’re about to declare victory when all of the sudden you start noticing the drone drifting away from
the target.&lt;/p&gt;

&lt;p&gt;Not by a ton, its not unstable, but there’s definitely a noticeable gap that isn’t going away. What’s up?&lt;/p&gt;

&lt;p&gt;Let’s think about the impact of a strong constant wind on our controller to the south-east.&lt;/p&gt;

&lt;p&gt;You can imagine your proportional controller will completely offset the wind once the drone is off
the target by a large enough margin, but there’s nothing in our control algorithm to offset the wind
to get us totally on target.&lt;/p&gt;

&lt;p&gt;We know our drone is powerful enough to handle the wind and stay on target, we just need something
monitoring the wind.&lt;/p&gt;

&lt;p&gt;One way of doing this is to keep track of historical error by keeping a sum of all errors in the past, and
just adding it into our control formula (with its own tunable constant).&lt;/p&gt;

&lt;p&gt;This error sum should grow larger and larger until it influences the controller enough to push the drone
onto the target location.&lt;/p&gt;

&lt;p&gt;We can show this down below, you can enable or disable this error accumulator using the below button,
and randomize the wind direction to see how adding this gain has affected our ability to control.&lt;/p&gt;

&lt;head&gt;
&lt;style&gt;
.u-button {
  border-radius: 5px;
  text-align: center;
  display: inline-block;
  font-size: 14px;
}
.u-button-enable {
  border-color: #2CA02C;
  border-radius: 5px;
  text-align: center;
  display: inline-block;
  font-size: 14px;
}
.u-button-off {
  border-color: #D62728;
  border-radius: 5px;
  text-align: center;
  display: inline-block;
  font-size: 14px;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;input type=&quot;submit&quot; class=&quot;u-button&quot; id=&quot;randomizeWind&quot; value=&quot;Randomize Wind&quot; /&gt;
  &lt;input type=&quot;submit&quot; class=&quot;u-button-off&quot; id=&quot;toggleIButton&quot; value=&quot;Toggle Integrator&quot; /&gt;
  &lt;input type=&quot;submit&quot; class=&quot;u-button-off&quot; id=&quot;windupPrevention&quot; value=&quot;Toggle Windup Prevention&quot; /&gt;
  &lt;div style=&quot;display: flex; justify-content: center; padding-top: 15px; padding-bottom: 15px&quot;&gt;
    &lt;canvas id=&quot;iCanvas&quot; width=&quot;640&quot; height=&quot;480&quot; tabindex=&quot;1&quot;&gt;&lt;/canvas&gt;
  &lt;/div&gt;
&lt;/body&gt;

&lt;p&gt;So obviously accumulating error (or integrating error) and passing that back through our error loop gets
rid of that bias issue we were seeing on just the proportional and derivative controller. Cool!&lt;/p&gt;

&lt;p&gt;You may have also noticed that we made our overshoot problem worse.&lt;/p&gt;

&lt;p&gt;Let’s say the drone is on the far left side of the world,
and the target is on the far right side of the world.
The drone will accelerate up to max acceleration just based on the proportional feedback, and the
drone will get to the target as fast as it can (I added a velocity limit in the simulation world), but
the whole time the vehicle is traveling, that error sum is just getting larger and larger.&lt;/p&gt;

&lt;p&gt;Once we finally get close to the target, our error sum is still going to be very large, and push us
past the target position. This effect lasts until the error sum balances back out to a value closer to 0.&lt;/p&gt;

&lt;p&gt;What we really wanted is just for integrator to kick in when proportional control wasn’t doing enough
to solve the control problem, not when the proportional controller already has things under control.&lt;/p&gt;

&lt;p&gt;So one naive way we can solve this integral windup problem is just by clearing the integrator sum when the controller
output hits maximum.&lt;/p&gt;

&lt;p&gt;This means we forget about what our integrator has learned during big transitions and let it build
back up once we’re close to the target.&lt;/p&gt;

&lt;p&gt;You can enable or disable windup prevention with the top right button!&lt;/p&gt;

&lt;h2 id=&quot;all-drone&quot;&gt;All D(r)one&lt;/h2&gt;

&lt;p&gt;What we just laid out is a classic control technique called PID control. P stands for proportional, I stands for
integral, and D stands for derivative.&lt;/p&gt;

&lt;p&gt;This controller, if tuned well, (and possibly with some domain specific tweaks) can handle just about
any typical control problem well enough, and serves as the basis for aircraft control, cruise control,
and a slew of robots and self-driving car applications.&lt;/p&gt;

&lt;p&gt;Hopefully you gained a bit of intuition about PID!&lt;/p&gt;</content><author><name></name></author><category term="control" /><summary type="html"></summary></entry></feed>