博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
群体智能算法-黏菌寻找食物最优路线行为模拟 2
阅读量:4033 次
发布时间:2019-05-24

本文共 3807 字,大约阅读时间需要 12 分钟。

经过查找一些资料,发现目前比较好的理论支撑有元胞自动机,其中的分形理论中的扩散限制凝聚模型与黏菌的网络比较贴切。

经过反复的调整。现在购机了一个黏菌的网络。如图:
第一个图为模拟的过程
第二图为最终图的放大
其中依然有有几个假设
1、黏菌的生命力,随着扩展,生命力降低
2、随着扩展完之后,黏菌在整个扩展范围内,随机生成一些感触点,然后感触点生成网络
     感触点的生长原则,则是最大可能从生命力低的地方向生命力高的地方生长,直至生长到了有网络的地方,或者是黏菌的核心。
不足的地方:
在模拟行为中采用的是元胞为小方块,所以会出现图中的一些对角线的直线。
备注:
其中图片采用的是jpg,为200*200像素。加载的是一个全白的图片,后面的黑点为程序模拟出来的
源码如下

 

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 = 200

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(make([]*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 = 200

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; x++ {

wg.Add(1)

}

}

for y := 0; y < max_y; y++ {

for x := 0; x < max_x; 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 {

otherblack++

}

}

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 {

havewalked++

}

}

}

}

 

if len(nextother) == 0 {

for np, en := range other {

if en < p.en {

nextother[np] = en

ensum += en

if np.walked {

havewalked++

}

}

}

}

 

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.walked = true

return

}

}

}

 

}

}()

}

}

}

 

 

 龚浩华

qq 29185807 月牙寂 道长

2015年4月1日 

如果你觉得本文对你有帮助,可以转到你的朋友圈,让更多人一起学习。

第一时间获取文章,可以关注本人公众号:月牙寂道长,也可以扫码关注

 

你可能感兴趣的文章
SVN服务器的配置
查看>>
Value '0000-00-00' can not be represented as java.sql.Date错误修改
查看>>
配置PHP+mssql环境的一些常见问题及解决方案
查看>>
JSP使用SmartUpload上传图片
查看>>
JSP 获得项目所在物理路径
查看>>
只能看不能改的Select
查看>>
'umi' 不是内部或外部命令
查看>>
Jetty 和 Tomcat 之争,到底孰强孰弱
查看>>
Tomcat 的类加载机制与 JVM 有何不同
查看>>
高并发之限流算法:计数器、漏桶、令牌桶
查看>>
Tomcat 之 server.xml 优化配置
查看>>
消息中间件:谈一谈 RocketMQ 的技术架构
查看>>
微服务统一认证,OAuth2 的认证流程
查看>>
Dubbo性能有多强,来看下官方的性能测试报告
查看>>
Kafka的常用使用场景:从初级到高级,你用到了几个
查看>>
阿里技术团队推荐:Dubbo 服务化最佳实践
查看>>
Nginx 限流常用模块:限制并发和IP访问频率
查看>>
OpenResty 高性能服务器,单机可达10K
查看>>
RocketMQ的十二个特性,你都知道吗「上」
查看>>
RocketMQ的十二个特性,你都知道吗「下」
查看>>