32leaves.net

Playing around with Markov algorithms

This is just something quick I wanted to for about a week now: an interpreter for Markov algorithms. So I wrote two of them, one in Ruby to get the idea how to do this and one in Javascript to have it on the web. Both are not particularly beautiful written but get the job done. Click this neat little link to get to the online version or have a look at the Ruby code (as you can see the production system and input word are “hard coded”). In both versions the production system removes the trailing zeros from the input word.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
E = 0x00
P = [
    { "a1" => "1a" },
    { "a0" => "0a" },
    { "0a" => "a" },
    { "a" => [E] },
    { E => "a" }
]
I = "0101101001110110000"


word = I
while true do
    ruleToRun = nil
    P.each {|rule|
        rule = rule.to_a[0]
        score = word.index(rule[0])
        if (!score.nil? || rule[0] == E) && ruleToRun.nil?
            ruleToRun = rule       
        end
    }
   
    break if ruleToRun.nil?
    shouldStopAfterThisRule = ruleToRun[1].is_a? Array
    ruleToRun[1] = ruleToRun[1][0] if shouldStopAfterThisRule
    oldword = word
    if ruleToRun[0] == E
        word = ruleToRun[1] + word;
    elsif ruleToRun[1] == E
        word = word.sub(ruleToRun[0], "")
    else
        word = word.sub(ruleToRun[0], ruleToRun[1])
    end
    puts "#{ruleToRun[0]} -> #{(shouldStopAfterThisRule ? "." : "")}#{ruleToRun[1].to_s}: #{oldword} => #{word}"
    break if shouldStopAfterThisRule
end
puts word

Simulating fluid dripping on a chain

This is some little afternoon project I did today. It’s basically a chain moved by a rotating cylinder. The simulation really respects friction, forces and is pretty close to reality except that the “fluid” is not really fluid but are really really small particles. Everything’s written in Java using JBox2D and Processing (by utilising the testbed, actually modifying the chain example). What’s cool is that basically every parameter can be set. Those parameters are:

  • chain ball radius The radius of an element within the chain. This influences the speed of which a ball becomes fully colored.
  • amount of chain balls The amount of balls on the chain (this can be set to close or wide, which may cause the simulation to be unrealistic
  • bearing radius The size of the bearings
  • bearing distance The distance of both bearings. Together with amount of chain balls this modifies the distance between the balls
  • motor speed The speed of the motor. Basically one could also modify the motors tourge, but it’s set to 10kNm per default so that it doesn’t matter
  • injected particles per second
  • particle size This radius directly changes the speed in which the chain balls become “colored”. Actually the percentage (ranged from 0..1) is calculated using the following equation

        \[\frac{2}{4\pi\cdot r^2} \cdot \vert P_{recv}\vert \cdot \mbox{size}(p)\]

    where

        \[r\]

    is the radius of this chain ball,

        \[P_{recv}\]

    is the set of received particles for this chain ball and

        \[\mbox{size}(p)\]

    is the particle radius

  • particle velocity The norm of the velocity the injector produces. The velocity is then

        \[v\cdot \begin{pmatrix}0\\-1\end{pmatrix}\]

This was a nice little project and I’m looking forward to play around with this stuff a little more (once I’ve finished playing around with my graph theory studies). Below is a video of the simulation with the following values set:

Parameter Value
chain ball radius 0.5 cm
amount of chain balls 50
bearing radius 5.5 cm
bearing distance 20 cm
motor speed 1.5 cm/s
injected particles per second 50 particles/s
particle size 0.01 cm
particle velocity 1 cm/s

Fork me on GitHub