# Activity 6: Help

## Representation of Patterns

• All patterns consist of repeating sequences of equally spaced "events" or rests, for example,
`X O X X O X X O X X O X X O X ... `
(a pattern with period 3)
`X O X X X O X O X X X O X O X X X O ... `
(a pattern with period 6).
• Each pattern is represented in terms of sequence with a length of 24 "micropulses", that is, the interval between the events in the pattern which are closest together. For example, the pattern
`X O X X O`
would appear as the sequence
`X O X X O X O X X O X O X X O X O X X O X O X X`
• In the working memory of the production system, the sequence is not represented explicitly. Rather it is represented as a sequences of clauses of the form
`(event ?pos),`
where ?pos is a number from 1 to 24, and
`(run ?start ?end),`
where ?start and ?end are numbers from 1 to 24. (event ?pos) indicates that there is an event (not a rest) at that position. (run ?start ?end) indicates that there is a run (a sequence of events with no breaks) from the ?start to the ?end positions. For example, the sequence
`X X X O O O O O X X X O O O O O X X X O O O O O`
would appear as
```(run 1 3)
(run 9 11)
(run 17 19)
(event 1)
(event 2)
(event 3)
(event 9)
(event 10)
(event 11)
(event 17)
(event 18)
(event 19)
```

## The Rules

• In the right-hand sides of rules, there are two sorts of actions: ADD, which adds a new clause to working memory, and DELETE, which removes a clause from working memory.
• Rules add or delete clauses in working memory which are of the form
(beat ?pos ?strength)
A clause of this type says that there is an accent at ?pos (a number from 1 to 24) with a particular strength (a number greater than or equal to 1).
• A rule which updates a "beat" clauses by increasing or decreasing the strength of the beat both adds a clause with the new strength and deletes the clause with the old strength.
• How rules are selected
• When a rule is selected, it is stored in a "used-rules" list together with the bindings of the variables that applied.
• On each cycle, the program finds all of the rules which match working memory (and for each rule, all of the ways it can match working memory). For each rule, there is a set of variable bindings that make it match.
• For each of the matching rules and variable bindings, the program finds how similar they were to the rules on the used-rules list. Each matching rule gets a score for how similar it is.
• The final score of the rule (and variable binding) is its similarity score minus the rule's strength.
• The selected rule and variable binding is the one with the lowest score. That is, it's the rule use which is most different from what happened before (low similarity score) and which has the highest priority (high strength). If two or more rules have the same score, one of them is chosen randomly. Because of this randomness, the program may differently if you run it more than once on the same input pattern.
• The rules themselves
• Runs: These rules add beats at the beginning and ends of runs of events.
```------------------------------------------
R U L E   run-beginning  (strength 2)
------------------------------------------
IF     (run ?start ?end)
AND
(not (beat ?start ?str))

------------------------------------------
R U L E   run-end  (strength 2)
------------------------------------------
IF     (run ?start ?end)
AND
(not (beat ?end ?str))

```
• Rests: This rule weakens a beat on a position where there is no event.
```------------------------------------------
R U L E   rest-beat  (strength 2)
------------------------------------------
IF     (beat ?pos ?str)
AND
(not (event ?pos))

THEN   DELETE (beat ?pos ?str)
AND
ADD (beat ?pos (- ?str 1))
```
• Consecutive beats: These rules cause the stronger of two consecutive to weaken the other. When the beats have equal strength, either one may be weakened.
```------------------------------------------
R U L E   consec-beats-a  (strength 2)
------------------------------------------
IF     (beat ?pos1 ?str1)
AND
(beat ?pos2 ?str2)
AND
(= (- ?pos2 ?pos1) 1)
AND
(>= ?str2 ?str1)

THEN   DELETE (beat ?pos1 ?str1)
AND
ADD (beat ?pos1 (- ?str1 1))

------------------------------------------
R U L E   consec-beats-b  (strength 2)
------------------------------------------
IF     (beat ?pos1 ?str1)
AND
(beat ?pos2 ?str2)
AND
(= (- ?pos2 ?pos1) 1)
AND
(>= ?str1 ?str2)

THEN   DELETE (beat ?pos2 ?str2)
AND
ADD (beat ?pos2 (- ?str2 1))
```
• Equally spaced beats: This rule causes three beats that are equally spaced (the interval must be two or more) to be strengthened.
```------------------------------------------
R U L E   equally-spaced  (strength 3)
------------------------------------------
IF     (beat ?pos1 ?str1)
AND
(beat ?pos2 ?str2)
AND
(beat ?pos3 ?str3)
AND
(= (- ?pos2 ?pos1) (- ?pos3 ?pos2))
AND
(> (- ?pos2 ?pos1) 1)

THEN   DELETE (beat ?pos1 ?str1)
AND
ADD (beat ?pos1 (+ ?str1 1))
AND
DELETE (beat ?pos2 ?str2)
AND
ADD (beat ?pos2 (+ ?str2 1))
AND
DELETE (beat ?pos3 ?str3)
AND
ADD (beat ?pos3 (+ ?str3 1))
```
• Intervals of 2 and 3: These rules look for two beats which are either two or three micropulses apart and place a beat either before the pair or after the pair, and separated from it by the same interval.
```------------------------------------------
R U L E   accent-2s-a  (strength 2)
------------------------------------------
IF     (beat ?pos1 ?str1)
AND
(beat ?pos2 ?str2)
AND
(= (- ?pos2 ?pos1) 2)
AND
(< ?pos2 (- ?seq-length 2))
AND
(not (beat (+ ?pos2 2) ?str3))

THEN   ADD (beat (+ ?pos2 2) 1)

------------------------------------------
R U L E   accent-2s-b  (strength 2)
------------------------------------------
IF     (beat ?pos1 ?str1)
AND
(beat ?pos2 ?str2)
AND
(= (- ?pos2 ?pos1) 2)
AND
(> ?pos1 2)
AND
(not (beat (- ?pos1 2) ?str3))

THEN   ADD (beat (- ?pos1 2) 1)

------------------------------------------
R U L E   accent-3s-a  (strength 2)
------------------------------------------
IF     (beat ?pos1 ?str1)
AND
(beat ?pos2 ?str2)
AND
(= (- ?pos2 ?pos1) 3)
AND
(< ?pos2 (- ?seq-length 3))
AND
(not (beat (+ ?pos2 3) ?str3))

THEN   ADD (beat (+ ?pos2 3) 1)

------------------------------------------
R U L E   accent-3s-b  (strength 2)
------------------------------------------
IF     (beat ?pos1 ?str1)
AND
(beat ?pos2 ?str2)
AND
(= (- ?pos2 ?pos1) 3)
AND
(> ?pos1 3)
AND
(not (beat (- ?pos1 3) ?str3))

THEN   ADD (beat (- ?pos1 3) 1)
```
• "Beats" with strength 0: This rule just removes any "beat" with strength less than or equal to zero. It has a high strength so that these "beats" go away before anything is done with them.
```------------------------------------------
R U L E   beat-0  (strength 5)
------------------------------------------
IF     (beat ?pos ?str)
AND
(< ?str 1)

THEN   DELETE (beat ?pos ?str)
```

## Running the Program

• Getting a silver or copper account
• To run the production system program, you will need an account on silver or copper.
• You will have to wait a few minutes for your account to be created.
• Logging on to silver or copper
• Running the program
• To start up the program, type the following at the prompt:
`scheme ~gasser/E105/ps.ss`
This will run 'scheme', which is the programming language in which the program is written and load the program that is in the file "~gasser/E105/ps.ss".
• You will see a menu like the following.
```WHAT NEXT? (Type the number of your choice followed by .)
[0]  Start over with a new pattern.
[1]  Run the production system.
[2]  Print rules.
[3]  Print working memory.
[4]  Interpret working memory.
[5]  Quit.

Pick an option:
```
You select an option by typing its number and then hitting CARRIAGE RETURN. The options mean
1. Create a new pattern for the production system and start with this pattern in working memory. You will see instructions for creating the pattern when you select this option.
2. Run the prodction system with the current working memory for 20 cycles (20 rule applications). If you have already run it with a given pattern, selecting this option causes it to run for 20 more cycles, further changing working memory.
3. Print out the rules.
4. Print out the current working memory.
5. Print out an interpreted version of working memory, one which translates the "run" and "beat" clauses into sequences which are easier to read.
6. Quit the program.
• Creating a new pattern
• When you select 0, you create a new periodic pattern. You type in only one cycle (period) of the pattern, using 1s for events and 0s for rests, with spaces between them and parentheses around the whole list.
• The program translates your pattern into a sequence of 24 points in time (events or rests) and translates this into a set of "event" and "run" clauses, which becomes the initial working memory.
• Running the production system
• When you select 1, you cause the production system to run for 20 cycles. You will see each rule which is selected along with the bindings of the variables and the actions that are taken when the rule is applied. Here is an example,
```C Y C L E   34
RULE SELECTED:
Rule equally-spaced
Bindings: ((?str3 3) (?pos3 23) (?str2 2) (?pos2 13) (?str1 1) (?pos1 3))
DELETING FROM WM: (beat 3 1)
ADDING TO WM: (beat 3 2)
DELETING FROM WM: (beat 13 2)
ADDING TO WM: (beat 13 3)
DELETING FROM WM: (beat 23 3)
ADDING TO WM: (beat 23 4)
```
• Since one or another rule will always apply, the production system would not halt without a time limit, but you can run it for as many repetitions of 20 cycles as you like.
• The program runs more slowly after about 15 cycles because the working memory, which must be completely checked for each rule, is significantly larger.
• Working memory
• Here is what the "interpreted" working memory display looks like
```I N T E R P R E T E D   W O R K I N G   M E M O R Y

1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Pattern sequence:
1  0  1  1  0  1  0  1  1  0  1  0  1  1  0  1  0  1  1  0  1  0  1  1

Beat strengths:
1  0  1  1  0  1  0  0  0  0  1  0  1  1  1  1  0  1  1  0  1  0  2  0
```
The first row of numbers just shows the positions in the sequence for convenience. The second row shows where there are events and rests. This does not change as the production system is run. The third row shows the strengths associated with beats in the different positions. This row changes as the production system is run and creates or deletes "beat" clauses.
• The transcript file
• As you run the program, it keeps a record of everything you see on the screen in a transcript file which is named for the date and time you start up the program.
• When you quit the program (by selecting option 5 and then "y" when asked whether you're sure you want to quit), you will return to the operating system on silver (UNIX). You can show any files you have in your home directory by typing "ls" followed by a carriage return.
• You should see a file named for the date and time, something like "29nov1532".
• You can transfer that file from silver to the Macintosh you are using in the following way.
• In the Netwrok menu, select "Do FTP command ..." (or use the COMMAND-f shortcut). This puts you in "ftp", where you can transfer files back and forth between silver and the Macintosh.
• To set the place on the Macintosh where you want the file to go, select "Set Transfer Directory ..." in the File menu. Go the directory (folder) where you want the file (you can put it on the Desktop if you want), and click on "Set Directory".
• Now transfer the file to the Macintosh by typing
`send `
where you replace by the name of the transcript file.
• Once the transcript file is on the Macintosh, you can read or edit it with a word processing program like SimpleText or WordPerfect or you can transfer it to your Student Locker account.

Last updated: 26 November 1995
URL: http://www.indiana.edu/~gasser/activity6_help.html