OpenCores
URL https://opencores.org/ocsvn/ca_prng/ca_prng/trunk

Subversion Repositories ca_prng

[/] [ca_prng/] [trunk/] [src/] [model/] [ca_prng.py] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 joachim
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
#=======================================================================
4
#
5
# ca_prng.py
6
# ---------
7
# Fast and simple ca_prng conformant cellular automata model in
8
# Python. This model is actually implented as a general 1D CA class
9
# and the rule and size of the CA array is provided as parameters.
10
# 
11
# 
12
# Author: Joachim Strömbergson
13
# Copyright (c) 2008, Kryptologik
14
# All rights reserved.
15
#
16
# Redistribution and use in source and binary forms, with or without
17
# modification, are permitted provided that the following conditions
18
# are met:
19
#     * Redistributions of source code must retain the above copyright
20
#       notice, this list of conditions and the following disclaimer.
21
# 
22
#     * Redistributions in binary form must reproduce the above
23
#       copyright notice, this list of conditions and the following
24
#       disclaimer in the documentation and/or other materials
25
#       provided with the distribution.
26
#
27
# THIS SOFTWARE IS PROVIDED BY Kryptologik ''AS IS'' AND ANY EXPRESS
28
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30
# ARE DISCLAIMED. IN NO EVENT SHALL Kryptologik BE LIABLE FOR ANY
31
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
33
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
#
39
#=======================================================================
40
 
41
#-------------------------------------------------------------------
42
# Python module imports.
43
#-------------------------------------------------------------------
44
import sys
45
import math
46
import optparse
47
 
48
 
49
#-------------------------------------------------------------------
50
# class CellularAutomata()
51
# 
52
# This class implements a 1D cellular automata. The class expects
53
# to be initalized with an array of arbitrary length with initial
54
# cell state values (0 or 1) as well as an array with update rules.
55
# 
56
# The update rule is expected to contain eight values (0 or 1)
57
# that define the update value for a cell given by the current
58
# state of the cell and two nearest neighbours. Note that nearest
59
# neighbour is calculated with wrap around, that is the cell
60
# array is treated as a ring.
61
#-------------------------------------------------------------------
62
class CellularAutomata():
63
    def __init__(self, rule, init_state, verbosity):
64
        self.my_rule = rule
65
        self.my_state = init_state
66
        self.verbose = verbosity
67
 
68
 
69
    def print_state(self):
70
        """Print the current state of the cellular automata."""
71
        print self.my_state
72
 
73
 
74
    def update_ca_state(self):
75
        """Update the cells in the cellular automata array."""
76
 
77
        # Create a new CA array to store the updated state.
78
        new_state = [x for x in range(len(self.my_state))]
79
 
80
        # For each cell we extract three consequtive bits from the
81
        # current state and use wrap around at the edges.
82
        for curr_bit in range(len(self.my_state)):
83
            if curr_bit == 0:
84
                bit_left = self.my_state[-1]
85
                bit_mid = self.my_state[0]
86
                bit_right = self.my_state[1]
87
            elif curr_bit == (len(self.my_state) - 1):
88
                bit_left = self.my_state[(curr_bit - 1)]
89
                bit_mid = self.my_state[curr_bit]
90
                bit_right = self.my_state[0]
91
            else:
92
                bit_left = self.my_state[(curr_bit - 1)]
93
                bit_mid = self.my_state[curr_bit]
94
                bit_right = self.my_state[(curr_bit + 1)]
95
 
96
            # Use the extraxted bits to calculate an index for
97
            # the update rule array and update the cell.
98
            rule_index = 4 * bit_left + 2 * bit_mid + bit_right
99
            if self.verbose:
100
                print "rule_index = %d " % rule_index
101
            new_state[curr_bit] = self.my_rule[rule_index]
102
 
103
        # Replace the old state array with the new array.
104
        self.my_state = new_state
105
 
106
 
107
#-------------------------------------------------------------------
108
# main()
109
#
110
# Main function.
111
#-------------------------------------------------------------------
112
def main():
113
    # Create an update rule array. This is Wolframs rule 30.
114
    # We also create a CA array with a given init state.
115
    my_update_rules = [0, 1, 1, 1, 1, 0, 0, 0]
116
    my_init_state = [1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0]
117
 
118
    # Create a CA object.
119
    my_ca = CellularAutomata(my_update_rules, my_init_state, False)
120
 
121
    # Run a few iterations printing the state before each update.
122
    for iteration in range(1000):
123
        my_ca.print_state()
124
        my_ca.update_ca_state()
125
 
126
 
127
#-------------------------------------------------------------------
128
# __name__
129
# Python thingy which allows the file to be run standalone as
130
# well as parsed from within a Python interpreter.
131
#-------------------------------------------------------------------
132
if __name__=="__main__":
133
    # Run the main function.
134
    sys.exit(main())
135
 
136
#=======================================================================
137
# EOF ca_prng.py
138
#=======================================================================

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.