comp10002 Foundations of Algorithms

comp10002 Foundations of Algorithms

Semester 2, 2024

Assignment 2

Learning Outcomes

In this project, you will demonstrate your understanding of dynamic memory and linked data structures (Chap- ter 10) and extend your program design, testing, and debugging skills.  You will learn about cellular automata, implement a tool for performing cellular automata computations, and use this tool to solve a practical problem.

Background

cellular automaton is a model of computation composed of cells, each in one of a finite number of states. For each cell, a set of cells called its neighborhood is defined. At the beginning of computation (time t=0), each cell is assigned a state.  The states of all its cells determine the state of the automaton.  The automaton progresses from its current state at time t to the next state at time t+1 by updating the states of its cells using an update rule.  This rule defines a relation between the neighborhood of a cell at time t and the state of the cell at time t+1. The rule is typically deterministic and is applied for each cell in the current state simultaneously.

In an elementary cellular automaton, cells are arranged in a one-dimensional array and each cell is in 0 or 1 state denoting on and off cells, respectively. An automaton of 31 cells, with cells at positions 0, 1, 12, 15, 18, and 29 on (counting from left to right starting from zero) and the other cells off, is shown below.

1100000000001001001000000000010

The neighborhood of a cell in an elementary cellular automaton comprises the cell itself and its two imme- diate neighbor cells. The update rule of an elementary automaton maps such possible neighborhoods of a cell to its states in the next time step. One such update rule is depicted below.

000  001  010  011  100  101  110  111

0      1      1      1      1       0       0      0

The top row lists all eight possible neighborhoods of a cell, with the cell in the middle being the cell of interest and the other two cells being its neighbors. For example, the 010 neighborhood denotes that the cell is on and both its immediate neighbors are off, whereas 110 is the neighborhood of an on cell with its left neighbor on and right neighbor off. The bottom row shows the state of the cell in the next time step for the corresponding neighborhoods.  For instance, a cell with neighborhood 010 stays on ( 1) in the next step, while a cell with neighborhood 110 switches off (0) in the next step.  A run of the first three computation steps of the example automaton using the example update rule is listed below; numbers at the start of each line denote time steps.

0:    1100000000001001001000000000010

1:    1010000000011111111100000000110

2:    1011000000110000000010000001100

3:    1010100001101000000111000011011

The gray background highlights eight applications of the update rule on cells at different positions at different time steps. For example, the neighborhood of the cell at position 12 at t=0 (010) determines the state of this cell at t=1 ( 1). As another example, the neighborhood of the cell at position 24 at t=0 (000) determines that this cell stays off at t=1 (0). When determining the neighborhoods of the edge cells (first and last cells in the array), the array is “wrapped” around. Specifically, the cell after the last one is the first cell, and the cell before the first one is the last cell. Hence, the neighborhood of the last cell at t=0 (position 30) is 101 determining that the the cell at position 30 at t=1 is off. Once cell states at time t are determined, one can compute cell states at time t+1.

There are 8 = 23 possible neighborhoods and 256 = 223  possible update rules defined for elementary cellular automata.  It is convenient to refer to each rule by its code, given as a number from 0 to 255.  If one traverses possible neighborhoods in order 111, 110, 101, 100, 011, 010, 001, 000 (in the descending order of the encoded binary numbers), writes out the corresponding next states in the same order, and interprets the obtained sequence of symbols as the binary representation of an integer, the resulting number is the code of the rule. For instance, the example update rule presented above results in the binary representation 00011110 of number 30.  Hence, this update rule is rule 30.  Many of the 256 rules are trivially equivalent, for example, by swapping roles of 0 and 1 or by reflection via the vertical axis. There are only 88 fundamentally different update rules.

Cellular automata have many applications, including studies of complex behaviors in nature (rule 30), prime numbers (rule 90),traffic flows (rule 184), and computability (rule 110). Artem and Alistair need a tool to study new ways of solving computational problems using cellular automata. Your task is to implement this tool.

Input Data

Your program should read input from stdin and write output to stdout.  The input will list instructions, one per input line, each terminating with “\n”, that is, one newline character.  The input specifies how to configure and execute an elementary cellular automaton and to collect basic statistics about the evolution of its states. The following file test0 .txt uses seven lines to specify an example input to your program.

1 31

2 30

** . . . . . . . . . .* . .* . .* . . . . . . . . . .* .

4 10

0,5

6 30,10

7

Lines 1–3 in the test0 .txt file specify a configuration of the automaton.  Line  1 defines the size of the au- tomaton, that is, the number of cells in the array.  Line 2 specifies the code of the update rule, whereas line 3 encodes the cell states, with “*” encoding on states and “ .” encoding off states.  Hence, line 3 specifies the example automaton discussed in the Background section. Line 4 specifies the number of time steps to evolve the automaton in Stage 1 of the program. Finally, lines 5 and 6 are instructions to analyze the evolution of states of cells at specific positions in the automaton in Stages 1 and 2 of the program, respectively.

The input will always follow the proposed format.  You can make your program robust by handling inputs that deviate from this format. However, such extensions to the program are not expected and will not be tested.

Output

The output your program should generate for the test0 .txt input file is in the test0-out .txt file and below.

==STAGE  0============================

0:  ** . . . . . . . . . .* . .* . .* . . . . . . . . . .* .

==STAGE  1============================

10 0:  ** . . . . . . . . . .* . .* . .* . . . . . . . . . .* .

11 1:  * .* . . . . . . . .********* . . . . . . . .** .

12 2:  * .** . . . . . .** . . . . . . . .* . . . . . .** . .

13 3:  * .* .* . . . .** .* . . . . . .*** . . . .** .**

14 4:  . .* .** . .** . .** . . . .** . .* . .** . .* .

15 5:  .** .* .*** .*** .* . .** .****** .****

16 6:  .* . .* .* . . .* . . .**** . .* . . . . . .* . . .

17 7:  ***** .** .*** .** . . .**** . . . .*** . . 18 8:  * . . . . .* . .* . . .* .* .** . . .* . .** . .**

19 9:  .* . . .****** .** .* .* .* .***** .*** .

20 10:  *** .** . . . . . .* . .* .* .* .* . . . . .* . .*

21 -------------------------------------

22 #ON=3  #OFF=3  CELL#0  START@5

23 ==STAGE  2============================

24 RULE:  184;  STEPS:  14 .

25 -------------------------------------

26 10:  *** .** . . . . . .* . .* .* .* .* . . . . .* . .* 27 11:  ** .** .* . . . . . .* . .* .* .* .* . . . . .* .* 28 12:  * .** .* .* . . . . . .* . .* .* .* .* . . . . .**

29 13:  .** .* .* .* . . . . . .* . .* .* .* .* . . . .**

30 14:  ** .* .* .* .* . . . . . .* . .* .* .* .* . . .* . 31 15:  * .* .* .* .* .* . . . . . .* . .* .* .* .* . . .*

32 16:  .* .* .* .* .* .* . . . . . .* . .* .* .* .* . .*

33 17:  * .* .* .* .* .* .* . . . . . .* . .* .* .* .* . .

34 18:  .* .* .* .* .* .* .* . . . . . .* . .* .* .* .* .

35 19:  . .* .* .* .* .* .* .* . . . . . .* . .* .* .* .*

36 20:  * . .* .* .* .* .* .* .* . . . . . .* . .* .* .* .

37 21:  .* . .* .* .* .* .* .* .* . . . . . .* . .* .* .*

38 22:  * .* . .* .* .* .* .* .* .* . . . . . .* . .* .* .

39 23:  .* .* . .* .* .* .* .* .* .* . . . . . .* . .* .*

40 24:  * .* .* . .* .* .* .* .* .* .* . . . . . .* . .* .

41 -------------------------------------

42 RULE:  232;  STEPS:  15 .

43 -------------------------------------

44 24:  * .* .* . .* .* .* .* .* .* .* . . . . . .* . .* .

45 25:  .* .* . . . .* .* .* .* .* .* . . . . . . . . . . .*

46 26:  * .* . . . . . .* .* .* .* .* . . . . . . . . . . . . .

47 27:  .* . . . . . . . .* .* .* .* . . . . . . . . . . . . . .

48 28:  . . . . . . . . . . .* .* .* . . . . . . . . . . . . . . .

49 29:  . . . . . . . . . . . .* .* . . . . . . . . . . . . . . . .

50 30:  . . . . . . . . . . . . .* . . . . . . . . . . . . . . . . .

51 31:  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

52 32:  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

53 33:  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

54 34:  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

55 35:  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

56 36:  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

57 37:  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

58 38:  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

59 39:  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

60 -------------------------------------

61 #ON=10  #OFF=20  CELL#30  START@10

62 -------------------------------------

63 10:  *** .** . . . . . .* . .* .* .* .* . . . . .* . .*

64 AT  T=10:  #ON/#CELLS  < 1/2

65 ==THE  END============================ 66

Stage 0 – ReadingAnalyzingand Printing Input Data (10/20 marks)

The first version of your program should read the automaton configuration from input, construct a representation of the initial state of the automaton at t=0, and print basic information about the automaton to the output.  The first eight lines in the test0-out .txt file correspond to the output your program should generate in Stage 0 based on the first three input lines in the test0 .txt file. Line 1 of the output prints the Stage 0 header. Lines 2 and 3 print the size of the automaton and the code of the update rule, respectively.  Lines 5 and 6 visualize the update rule using the principles explained in the Background section. Line 8 prints the automaton state at t=0. Use "%4d:" format specifier to printf the time step, and use “*” and “ .” characters to printf on and off cell states, respectively. Finally, lines 4 and 7 print the delimiter lines of 37 “-” characters.

You should not make assumptions about the maximum number of cells requested from the input for the automaton your program should construct. Use dynamic memory and data structures of your choice, for example, arrays or linked lists, to store the history of states of the automaton.

Stage 1 – Execute Automaton (16/20 marks)

In Stage 1, your program should execute the automaton configured in Stage 0 for the requested number of time steps and print the number of on and off states a particular cell has been at in the requested period of execution. Lines 9 to 22 in the test0-out .txt file correspond to the output your program should generate in this stage based on the instructions at lines 4 and 5 in the test0 .txt input file.  Line 4 in the test0 .txt file specifies the number of time steps the automaton should execute starting from the initial state (t=0),while line 5, using format x,y, requests to count and print the number of on and off states of the cell at position x in the automaton starting from time step t=y and to the last so far executed time step. Hence, the instructions in the test0 .txt file request to evolve the initial state of the automaton for ten time steps and count the number of observed on and off states of the cell at position zero starting from time step t=5 up to and including time step t=10.

The output of Stage 1 should start with the corresponding header; see line 9 in the test0-out .txt file. The subsequent lines, lines 10 to 20 in the test0-out .txt file, print the automaton states in chronological order they were observed, starting with the initial state (t=0) to the last computed state (t=10).  Again, use “*” and “ .” characters to denote on and off cell states and the "%4d:" format specifier to format the output of time steps.  The output line that follows all the printed states should be the delimiter line of 37 “-” characters;  see line 21 in the test0-out .txt output file. Next, your program should print the counts of on and off states of the requested cell using the "#ON=%d  #OFF=%d  CELL#%d  START@%d\n" format specifier; see line 22 in the output.

You should not make assumptions neither about the maximum number of time steps that can be requested to evolve the automaton nor about the requested cell position or time step to start counting the cell states.

Stage 2 – Solve Practical Problem (20/20 marks)

Given an array of zeros and ones (bits), the density classification problem consists in deciding if the array contains more zeros or more ones. Fuk [1] showed that elementary cellular automata based on rules 184 and 232 could solve this problem perfectly. Specifically, given an automaton of k cells, one should first execute the automaton for n = ⌊(k − 2)/2⌋ time steps using update rule 184 and then evolve the resulting state form. =  ⌊(k − 1)/2⌋ steps using update rule 232; ⌊x⌋ denotes the largest integer less than or equal to x. If all the cells of the resulting automaton are on, then the state of the original automaton has more on states.  In contrast, if all the cells of the resulting automaton are off, then the state of the original automaton has more off states.  Finally, if cells in the resulting automaton exhibit an alternating sequence of on and off states (regardless of the state of the cell at position zero in the array), then the state of the original automaton has the same number of on and off cells.

In Stage 2, your program should apply the procedure ofFuk described above to solve the density classifica- tion problem for the state of the automaton computed for the last time step in Stage 1 (t=10 for the test0 .txt input file). The output of Stage 2 should start with the corresponding header; see line 23 in the test0-out .txt file. In the subsequent lines, your program should print the evolution of the automaton using rules 184 and 232, with rules applied in the correct order for the correct number of time steps. Lines 24 to 60 in the test0-out .txt file print this output for the automaton resulting from Stage 1. Lines 24 and 42 inform. about the changes in the update rule and the number of time steps the new rules are applied, while lines 25, 41, 43, and 60 are the delimiter lines of 37 “-” characters.

The subsequent line (line 61 in test0-out.txt) in the output should print the number of on and off states for the cell at position starting from the time step requested on line 6 in the test0 .txt input file.  Finally, after the delimiter line (line 62 in test0-out.txt), your program should print the automaton for which the  density classification problem was solved (line 63) and the answer to the problem (line 64).  As all the cells in  the automaton at t=39 are off, there are more off cells than on cells in the automaton at t=10. Replace the “<” symbol in the result message (line 64) with “=” or “>”, as required by other inputs to your tool.

Stage 3 – Have Fun (no marks for fun as fun is your reward)

Search for other solutions to interesting practical problems using elementary cellular automata, implement them, and share them with the class via the discussion board.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值