Swarm Intelligence algorithm--the optimal route behavior simulation of slime bacteria searching for food 2

Source: Internet
Author: User
Tags rand
After searching some data, it is found that the theoretical support has cellular automata, in which the diffusion-limited condensation model in fractal theory is more appropriate to the network of slime bacteria.

Adjusted over and over again. Now buy a network of slime bacteria. As shown in figure:
The first diagram is a simulation process
The second figure is the enlargement of the final graph



There are still a couple of assumptions
1, the vitality of the bacteria, with the expansion, vitality reduced
2, with the expansion of the entire range of bacteria in the expansion, randomly generated some points of touch, and then feel dot Generate network
The growth principle of feeling point is the most likely to grow from the place where the vitality is low to the high life, until it grows into a place where there is a network, or the core of the slime fungus.

Insufficient places:
In the simulation behavior, the cell is used as a small square, so there will be some diagonal lines in the graph.


Note:
The picture uses JPG, which is 200*200 pixel. Loaded is a full white picture, behind the black dots for the program to simulate the

Source code is as follows
Package Main
Import ("FMT" "Image" "image/jpeg"//"IO" "Image/color" "Math/rand" "OS" "Path" "Runtime" "Sync" "Time")
Func Main () {runtime. Gomaxprocs (1)
For {select {}}
}
var max_x int = var max_y int = 200
var lock *sync. Rwmutex var im image. Image var allpart []parts var wg *sync. Waitgroup
Type Parts []*part
Type part struct {x int y int en int black bool walked bool}
Func Newpart (x, y int) *part {p: = part{x, y, 0, False, false} return &p}
Func initimage () error {f, err: = OS. Open ("200.jpg") if Err!= nil {fmt. PRINTLN (ERR) return err} defer f.close () IM, err = jpeg. Decode (f) If Err!= nil {fmt. PRINTLN (ERR) return err} return nil}
Func Print () {tick: = time. Tick (1 * time. Second) Count: = 1 for {select {case <-tick:func () {p: = path. Join ("./worm", FMT. Sprintf ("%d.jpg", count)) lock. Lock () F, _: = OS. OpenFile (P, os.) O_rdwr|os. O_create, 0x666) jpeg. Encode (f, IM, nil) f.close () lock. Unlock () count++} ()
} } }
Func init () {initimage () WG = &sync. waitgroup{} lock = &sync. rwmutex{} Allpart = make ([]parts, Max_y, max_y) for y: = 0; Y < max_y; y++ {Allpart[y] = Parts ([]*part, max_x, max_x)}
For y: = 0; Y < max_y; y++ {for x: = 0; x < max_x + x + + {Allpart[y][x] = Newpart (x, y)}}
Allpart[max_y/2][max_x/2].en = Allpart[max_y/2][max_x/2].black = True allpart[max_y/2][max_x/2].walked = True Setblack (MAX_X/2, MAX_Y/2)

For I: = 0; I < 500; i++ {rand. Seed (Rand. Int63 ()) Y1: = Int (rand. INT31 ())% max_y x1: = Int (rand. INT31 ())% max_x if X1 = = Max_x/2 && y1 = max_y/2 {Continue} Allpart[y1][x1].black = True Setblack (x1, y1)}
For y: = 0; Y < max_y; y++ {for x: = 0; x < max_x + + + {WG. ADD (1)}} for Y: = 0; Y < max_y; y++ {for x: = 0 x < max_x + + + {go Allpart[y][x].run ()}}
Go Print ()
}
Func geten (x, y int) int {if x < 0 | | Y < 0 {return-1} if x >= max_x | | y >= max_y {return-1}
Return Allpart[y][x].en}
Func setblack (x, y int) {p: = im. ( *image. YCBCR) if! (Image. Point{x, y}. In (P.rect)) {return} Yi: = P.yoffset (x, y) ci: = P.coffset (x, y) P.y[yi], P.cb[ci], p.cr[ci] = color. RGBTOYCBCR (0, 0, 0)}
Func (P *part) run () {WG. Done () WG. Wait ()
TICK: = time. Tick (1 * time. Second)
For {select {case <-tick:if p.walked {return}
Func () {
Other: = Make (Map[*part]int) Max_node: = 0 Otherblack: = 0
Getmax: = Func (xi, Yi int) {en: = Geten (xi, yi) if en > 0 {other[allpart[yi][xi]] = en if allpart[yi][xi].black {oth erblack++} if en > max_node {max_node = en} getmax (p.x-1, p.y-1) Getmax (p.x, p.y-1) Getmax (p.x+1, p.y-1)
Getmax (p.x-1, p.y) Getmax (p.x+1, P.Y)
Getmax (p.x-1, p.y+1) Getmax (p.x, p.y+1) Getmax (p.x+1, p.y+1)
If P.en = = 0 {if Max_node > 0 {p.en = max_node-1} return}
If P.black {//maxwalk ensum: = 0 havewalked: = 0 Nextother: = Make (Map[*part]int) for NP, en: = range other {if en > P.en {NEXTOTHER[NP] = en ensum + en if np.walked {havewalked++}}}
If Len (nextother) <= 1 {for NP, en: = range other {if en = = p.en {NEXTOTHER[NP] = en ensum + en if np.walked {have walked++}}}
If Len (nextother) = = 0 {for NP, en: = range other {if en < p.en {NEXTOTHER[NP] = en ensum + en if np.walked {havew alked++}}}
If havewalked > 0 {p.walked = True return}
If Ensum > 0 {rand. Seed (Rand. INT63 ()) RN: = Int (rand. INT31 ())% Ensum for NP, _: = Range Nextother {rn = Rn-np.en if rn <= 0 {np.black = True Setblack (np.x, NP.Y) P.wal Ked = True Return}}}
} }() } } }

Hao Haohua

QQ 29185807 long Moon Lonely Road

April 1, 2015

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.