-
Notifications
You must be signed in to change notification settings - Fork 0
/
life3.go
378 lines (361 loc) · 7.95 KB
/
life3.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Package MAIN for proves the functions to include in a next package
// for implementation Cellular Automatas or Conway's Life
// This version is sequential without the characteristics of concurrency
package main
import (
"bufio"
"flag"
"fmt"
"log"
"math/rand"
"os"
"reflect"
"strconv"
"strings"
"time"
)
// Constants in CAPITALS
const (
M = 100 // Rows
N = 100 // Columns
H = 3 // History
T_SIMUL = 100 // Max. time of simulation (end if static condition or socillation)
X = 20 // Number of goroutines to create, and wait for calculate the next state of points
)
type Point struct {
x, y int
}
type World struct {
Matrix [H]map[Point]int
static bool // World is static?
T int // Actual time
X int // Number of Goroutines
}
var w World
var punts [X]chan<- Point
var sols [X]<-chan map[Point]int
// printw print world -> array of [][]cells in time t
func printw() {
m := map[Point]int{} // Map is empty set
m = w.Matrix[w.T]
fmt.Println("---")
for i := 0; i < M; i++ {
for j := 0; j < N; j++ {
if m[Point{i, j}] == 0 {
fmt.Print(".")
} else {
fmt.Print("*")
}
}
fmt.Println()
}
}
// randomw generate a random initial state
func randomw() {
var p Point
m := map[Point]int{}
// A 25% of cells are alive
for i := 0; i < (M * N / 4); i++ {
p.x = rand.Intn(M)
p.y = rand.Intn(N)
if m[p] == 0 {
m[p] = 1
}
}
w.Matrix[0] = m
}
// initw read a file .LIF and configure the world with it
func initw(f *os.File) bool {
var r *strings.Reader
var b byte
var x, y, oldy int
input := bufio.NewScanner(f)
input.Scan()
if input.Text() != "#Life 1.05" {
fmt.Fprintf(os.Stderr, "ERROR: The file for initialization the world is not a valid .LIF format\n")
return false
}
header:
// Read header of .LIF
for input.Scan() {
r = strings.NewReader(input.Text())
b, _ = r.ReadByte()
if b != '#' {
fmt.Println(input.Text())
} else {
b, _ = r.ReadByte()
switch b {
case 'D':
{
fmt.Println("Description")
}
case 'N':
{
fmt.Println("Rules Conway R 23/3")
}
case 'R':
{
fmt.Fprintf(os.Stderr, "ERROR: 'R' option not implemented\n")
return false
}
case 'P':
{
s := strings.Split(input.Text(), " ")
x, _ = strconv.Atoi(s[1])
y, _ = strconv.Atoi(s[2])
x += (M / 2)
y += (N / 2)
oldy = y
break header // Exit loop, now only blocks of position and cells
}
default:
{
fmt.Fprintf(os.Stderr, "ERROR: Option in header not implemented\n")
return false
}
}
}
}
var p Point
m := map[Point]int{}
// Read patterns and positions
for input.Scan() {
r = strings.NewReader(input.Text())
b, _ = r.ReadByte()
if b == '#' {
b, _ = r.ReadByte()
if b == 'P' {
s := strings.Split(input.Text(), " ")
x, _ = strconv.Atoi(s[1])
y, _ = strconv.Atoi(s[2])
x += (M / 2)
y += (N / 2)
oldy = y
} else {
fmt.Fprintf(os.Stderr, "ERROR: Expected Position or blocks not config parameters\n")
return false
}
} else {
p.x = x
for cells := int(r.Size()); cells > 0; cells-- {
p.y = y
switch b {
case '.':
{
//m[p] = 0
}
case '*':
{
m[p] = 1
}
default:
{
fmt.Fprintf(os.Stderr, "ERROR: Character not valid, only '.' or '*'\n")
return false
}
}
b, _ = r.ReadByte()
y++
}
}
x++
y = oldy
}
w.Matrix[0] = m
return true
// NOTE: ignoring potential errors from input.Err()
}
// oscilt2 compare Actual (t) = Past (t - 2) for know if the system is oscillator
func oscilt2(t int) bool {
oscil := true
if t < 2 {
return false
}
pt := (t - 2) % H // Past time
m_at := w.Matrix[w.T]
m_pt := w.Matrix[pt]
if !reflect.DeepEqual(m_at, m_pt) {
oscil = false
}
return oscil
}
//
func nextConcurrently() (chan<- Point, <-chan map[Point]int) {
c_punts := make(chan Point) // Can only read from
c_sol := make(chan map[Point]int) // Can only write to
go func() { // We launch the goroutine from inside the function.
var p Point
p_end := Point{M, N}
for {
for p = <-c_punts; p != p_end; p = <-c_punts {
// wait to change the actual time ;)
}
p_sol := map[Point]int{} // Map points alife
m_at := w.Matrix[w.T]
for p = <-c_punts; p != p_end; p = <-c_punts {
nxt := neighbours(m_at, p.x, p.y)
if nxt == 1 {
p_sol[p] = 1
}
if w.static && (nxt != m_at[p]) {
w.static = false
}
}
c_sol <- p_sol
}
}()
return c_punts, c_sol
}
// nextw compute for all the world the next state of the cells
func nextw() {
var paux Point
w.static = true
at := w.T // Actual time
nt := (at + 1) % H // Next time
m_at := w.Matrix[at]
m_nt := map[Point]int{}
m_s := map[Point]int{} // Partial solution offer by goroutine
m_v := map[Point]int{} // Map points for calculate the next state
for p, v := range m_at {
if v == 1 { // Only add the points for visit the cells alife and her neighbours
top, bottom, left, right := pneighbours(p.x, p.y)
paux = Point{top, left}
m_v[paux] = 1
paux = Point{top, p.y}
m_v[paux] = 1
paux = Point{top, right}
m_v[paux] = 1
paux = Point{p.x, left}
m_v[paux] = 1
paux = p
m_v[paux] = 1
paux = Point{p.x, right}
m_v[paux] = 1
paux = Point{bottom, left}
m_v[paux] = 1
paux = Point{bottom, p.y}
m_v[paux] = 1
paux = Point{bottom, right}
m_v[paux] = 1
}
}
i := 0
count := 0
for p, _ := range m_v {
if count < w.X {
punts[i] <- Point{M, N}
}
punts[i] <- p
i = (i + 1) % w.X
count++
}
for i = count; i < w.X; i++ {
punts[i] <- Point{M, N}
}
for i = 0; i < w.X; i++ {
punts[i] <- Point{M, N}
m_s = <-sols[i]
for k, v := range m_s {
m_nt[k] = v
}
}
w.Matrix[nt] = m_nt
}
// pneighbours return the positions of the neighbours of (i,j)
func pneighbours(i int, j int) (int, int, int, int) {
top := i - 1
bottom := i + 1
left := j - 1
right := j + 1
if top == -1 {
top = M - 1
}
if bottom == M {
bottom = 0
}
if left == -1 {
left = N - 1
}
if right == N {
right = 0
}
return top, bottom, left, right
}
// neighbours calculate the next state of the cells
func neighbours(m map[Point]int, i int, j int) int {
var nb int // number of neifhbours life
top, bottom, left, right := pneighbours(i, j)
nb = m[Point{top, left}] + m[Point{top, j}] + m[Point{top, right}]
nb += m[Point{i, left}] + m[Point{i, right}]
nb += m[Point{bottom, left}] + m[Point{bottom, j}] + m[Point{bottom, right}]
if (nb == 2) && (m[Point{i, j}] == 1) {
return 1
}
if nb == 3 {
return 1
}
return 0
}
// main function for run and test the implementation of the functions
func main() {
start := time.Now()
run := true
w.X = X
randomPtr := flag.Bool("random", false, "Initialize the world with a random state")
filePtr := flag.String("file", "name.lif", "File name .lif")
nGRPtr := flag.Int("x", X, "Number of goroutines for calculate next world")
flag.Parse()
switch {
case *randomPtr:
{
randomw()
}
case *filePtr != "name.lif":
{
f, err := os.Open(*filePtr)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
run = false
} else {
run = initw(f)
f.Close()
}
}
default:
{
fmt.Println("Use $life -h for help")
run = false
}
}
if *nGRPtr > X {
fmt.Fprintf(os.Stderr, "ERROR: The higher number of GoRoutines is %v\n", X)
run = false
} else if *nGRPtr < 1 {
fmt.Fprintf(os.Stderr, "Error: The minimal number of GoRoutines is 1\n")
run = false
}
if run {
w.X = *nGRPtr
fmt.Println("---X", w.X)
for i := 0; i < w.X; i++ {
punts[i], sols[i] = nextConcurrently()
}
for t := 0; t < T_SIMUL; t++ {
fmt.Println("Time:", t)
w.T = t % H
printw()
// Check the world before calculate the next
if oscilt2(t) {
fmt.Println("End simulation, the system is oscillator with period=2")
t = T_SIMUL
}
nextw()
if w.static {
fmt.Println("End simulation, the system is static.")
t = T_SIMUL
}
}
}
elapsed := time.Since(start)
log.Printf("Total time: %s", elapsed)
}