# How can I produce a summation function from this set of production rules for a grammar?

I am not entirely sure if the title is the correct way to phrase what is occurring. There is a recurring process which I decided to attempt to model using production rules similar to those used in a context-free grammar.

The real world process is a production line. It takes 1 unit, after 2 weeks, produces 1 unit, after 4 weeks, that unit will produce another unit - and also 2 of the starting units. After 4 weeks the "another unit" will produce "that unit" - and also 3 of the starting units. This continues semi indefinitely, but the range I am looking at is roughly 52 weeks.

I came up with this model of production rules

$S\rightarrow A$

$A\rightarrow S^2B$

$B\rightarrow S^3A$

I attempted to expand this so that I could formulate a generating function and solve for n=52 (52 being the 1 year mark).

$S$,$A$,$S^2B$,$S^5A$,$S^7B$, ...

However, I was unable to determine if I was taking the correct approach to solving this situation. I believe that an error here lies in the fact that the duration of time is ignored. Namely that from $S\rightarrow A$ 2 weeks elapse, from $A\rightarrow S^2B$ 4 weeks elapse, and from $B\rightarrow S^3A$ 4 weeks elapse.

In order to better understand this situation, I decided to code an example so I could at least see the totals. However, it did not lend itself to producing a summation function.

So, given all this, how is it possible to come up with a summation function instead of having to manually iterate through it like this c# approach does? Or, is there a similar/alternate approach to modeling this which will allow an easier summation process?

Please feel free to correct any misunderstandings I may have.

edits

For example:

Week 1: We have 1 unit (S). It sits for 2 weeks until it is ready to become 1 of the next type(A).

Week 3: The first unit is now here. It sits for 4 weeks until it is ready to become 1 of the next type(A). At that time, it also becomes 2 of the first type(S).

Week 7: The unit from Week 3 has now become 2 of the first type of unit(S). It also created 1 of the second type(A). The 2 units of type S will now sit for 2 weeks until they make 2 units of type A. The 1 unit of type A will now sit for 4 weeks until it makes 1 unit of type B and 2 units of type S.

Week 9: The 2 units of type S from week 7 have now sat for 2 weeks. Now they have become 2 units of type A. These two units of type A will sit for 4 weeks and produce 4 units of type S and 2 units of type B.

Week 11: The 1 unit of type A from week 7 has now sat for 4 weeks and has now made 1 unit of type B and 2 units of type S. The 1 unit of type B will sit for 4 weeks and then make 1 unit of type A and 3 units of type S. The 2 units of type S will now sit for 2 weeks and make 2 units of type A.

Also, the 2 units of type S from week 9 have now sat for 2 weeks and become 2 units of type A. These two units will sit for 4 weeks and become 2 units of type B and will also become 6 units of type S.

etc.

This can perhaps be represented by a series of recurring relations

$s(n) = a(n) + b(n) + c(n)$

$a(n) = 2*b(n-4) + 3*c(n-4)$

$b(n) = a(n-2) + c(n-4)$

$c(n) = b(n-4)$

where

$s(n) = a(n) = b(n) = c(n) = 0$ for $n < 1$

$a(1) = 1$

OK, so the question now has a set of recurrence relations. To solve these, you can plug them in to each other until you get a single recurrence relation. First, substitute $c(n)=b(n-4)$ to get rid of all of the $c$'s: $$a(n) = 2 b(n-4) + 3 b(n-8)$$ $$b(n) = a(n-2) + b(n-8).$$

Now substitute in to get rid of the $a$'s:

$$b(n) = 2 b(n-6) + 3 b(n-10) + b(n-8).$$

So, you've gotten down to a single recurrence relation for the $b$'s. Solve this using standard methods; see Solving or approximating recurrence relations for sequences of numbers for details. Then, once you have a solution for the $b$'s, you can reverse the substitutions and obtain a solution for the $a$'s, $c$'s, and finally $s$'s.

If you want to find an exact equation for the $b$'s (rather than an asymptotic estimate), the method of generating functions will probably be useful. You'll have to find the roots of a 10th-degree polynomial ($x^{10} - 2 x^4 - x^2 - 3$, I think), and the solution will likely be an expression of the form $c_1 r_1^n + \dots + c_{10} r_{10}^n$ where $r_1,\dots,r_{10}$ are the roots and $c_1,\dots,c_{10}$ are the appropriate constants. This might get a bit messy, but it should be doable.

Or: You can ask Wolfram Alpha to solve the recurrence relation for you. Sometimes it is able. Unfortunately, for this one it seems to have failed: output.

I'm not sure I understand the problem, but it sounds like rabbits: once rabbits get old enough, they breed and give birth to baby rabbits (which some time later again breed and give birth to yet more rabbits, etc.) -- treating the rabbits as immortal. It sounds like we could treat each unit as a rabbit. The classic Fibonnaci sequence arises out of a situation like this.

Have you tried to write a recurrence relation? Let $f(n)$ denote the number of units after $n$ weeks; can you express $f(n)$ as some simple function of $f(n-1),f(n-2),\dots,f(1)$? If so, there are standard techniques for solving recurrence relations.

Update: Here is an attempt at an recurrence relation, based upon the description of the process you gave. Let $s(n)$ denote the number of units of type S that newly appeared on week $n$, and $a(n)$ the number of units of type A that newly appeared on week $n$ (neither of these include any units that were previously created). If I understand your rules, it's something vaguely along these lines:

• $a(n) = s(n-2) + a(n-4)$,since a newly created unit of type S sits for 2 weeks, then becomes one of type A; and a newly created unit of type A sits for 4 weeks, then becomes one of type A (and 2 of type S)

• $s(n) = 2 a(n-2)$ (since a newly created unit of type A sits for 4 weeks, then becomes two of type S (and one of type A)

• $s(1)=1$, $a(1)=1$, and $s(n)=a(n)=0$ for $n<0$.

If this is correct, you can solve these mutual recurrence relations using standard relationships.

I suspect these recurrence relationships might not be exactly what you had in mind, because the rules that I stated might not accurately capture exactly the process you had in mind, but I'm not sure how to fix them, because I still don't understand the rules of your process. I suggest you start by writing down more clearly the rules of the process, then try to define recurrence relations like these. I suspect it's all clear in your mind how the process works, but you haven't figured out how to write it down very clearly yet. The process of figuring out how to write it down clearly and precisely forces you to think carefully about the process and will be beneficial to you. So, I suggest you take some time to do that, then edit your question accordingly.

• I like your example of rabbits, because this is similar to that indeed. A recurrence relation sounds like the end result of what I was after, but I could not come up with one for this situation. Jul 10 '13 at 19:46
• OK, thanks @TravisJ. Perhaps you could edit the question to clarify the second paragraph of the question? I don't quite understand the rules or the process here: I read what you wrote, but I'm not quite getting it yet. Perhaps you could give an example showing what happens for the first five or so weeks (say), naming each unit separately (so I can see how/why each unit was produced)?
– D.W.
Jul 10 '13 at 19:50
• Please see my edit with an example of the first 11 weeks of this pattern. Obviously as the week numbers grow, so does the complexity of the explanation. Jul 10 '13 at 20:05
• Thank you for the edit and your suggestions. It is very close to the situation outlined, and I made an edit to the end of my question with what I believe to be a closer match. I believe that it properly tracks the numbers I was getting from my c# template. Do you know the name of the approach I would take to create a summation for the relationships? Jul 10 '13 at 22:42

In order to count properly, you need the grammar to terminate, i.e. generate words. This still allows you to properly model "semi-indefinite", that is arbitrarily long production runs. So maybe use this grammar which assumes that production may end at any point in time:

\qquad\begin{align} S &\to A \mid \varepsilon \\ A &\to S^2 B \mid \varepsilon \\ B &\to S^3 A \mid \varepsilon \end{align}

Now we want to count

• the number of produced units (by type S, A, B) and
• the number of weeks used.

We can apply the straight-forward translation and add a parameter for weeks:

\qquad\begin{align} S(w,x,y,z) &= x z^2 A(w,x,y,z) + 1 \\ A(w,x,y,z) &= w^2 y z^4 B(w,x,y,z) + 1 \\ B(w,x,y,z) &= w^3 x z^4 S(w,x,y,z) + 1 \end{align}

Note how we count units of type S, A and B in the exponents of $w$, $x$ and $y$, respectively. The exponent of $z$ tracks the number of weeks. Now solve the equation system for $S(\_)$.

Is this the generating function for the quantity you wanted? Let's see: $[w^k x^m y^n z^j]S(w,x,y,z)$ is the number of linear interleavings of production so that $k$ S-units, $m$ A-units and $n$-B units are produced over the course of $j$ weeks.

I guess that in your setting (which sounds like 3D-printer reproduction) you assume that all available units produce in parallel. In this case, the framework of formal grammars and generating functions in general as used here won't be of help since it can only deal with additive measures.

Instead, we can set up a (multi-dimensional) set of interleaved recurrences:

\qquad\begin{align*} S_0 &= S_1 = (1,0,0) \\ S_n &= (1,0,0) + A_{n-2} \\ \phantom{.} \\ A_0 &= A_1 = A_2 = A_3 = (0,1,0) \\ A_n &= (0,1,0) + 2S_{n-4} + B_{n-4} \\ \phantom{.} \\ B_0 &= B_1 = B_2 = B_3 = (0,0,1) \\ B_n &= (0,0,1) + 3S_{n-4} + A_{n-4} \end{align*}

Addition $+$ denotes component-wise addition. Now, if $S_n = (s,a,b)$ then you have (at most) $s$ objects of type $S$, $a$ of type $A$ and $b$ of type $B$ after $n$ weeks.

By the way, $S_{52} = (73018, 55866, 22659)$. You better get a large greenhouse.

• +1, Well written answer, thanks. I like the breakdown of the variables to exponents. Each production will happen in parallel as you correctly assume (it is for plant populations). You stated that as a result of being parallel, then these grammars and generating functions will not be of help. Is there a different framework I can use to model this? Jul 11 '13 at 18:52
• @TravisJ Of course; you gave recurrences yourself, and those can even be solved using generating functions. Maybe tree grammars can be useful, have to think about that.
– Raphael
Jul 12 '13 at 11:30
• @TravisJ I thought about using tree grammars. It is easy and intuitive to set one up for your problem but I don't know of a way to translate it into a (useful) generating function. I added a solution using bare recurrences, though. My machine is still working on a closed-form solution.
– Raphael
Jul 30 '13 at 13:35
• The suggestion of using tree grammars led me to researching more about the type of approach I was using. I had thought that my model for this grammar was a linked list but as it turns out it is more of a directed graph. Most of the reason for attempting to produce a solution to these types of grammars is because it allows for predictions to be done easier. As it stands, and from what I have read, it seems that it is not possible to reverse a directed graph which contains a cycle. Anyway, thanks for your input here. :) Aug 1 '13 at 15:51