人工智能五子棋(人工智能五子棋实验报告)

Mark wiens

发布时间:2022-09-05

人工智能五子棋(人工智能五子棋实验报告)

 

1 智能决策

1.1 博弈树模型算法

1.1.1 全局估算函数

此次项目中评估函数有两种:

(1)人为设定函数方法:根据人的经验,对一些特定的棋形在棋盘上进行检索。并且计数,最后赋予相应权值求和得到对棋盘的评价值。典型的棋形有活一活二活三冲四成龙。越接近于五子连珠的棋形应有越大的权重。由于先手优势,同一种棋形黑棋的权重的绝对值应略大于白棋的权重。博弈双方的权重的符号应相反。不失一般性,黑棋的权重设置为正。

(2)人工神经网络方法:用225个输入节点1个输出节点的神经网络对棋局进行评估。考虑到笔记本电脑计算条件本次实验中采用三层全连接网络结构,中间节点数为10。255个输入节点对应15*15棋盘上的所有点。如该点为黑棋输入为1,白棋输入-1,无子输入0。神经网络初始参数由高斯分布确定,再用遗传算法优化

1.1.2 极大极小算法

假设对弈的甲乙双方均为理性人,则对于甲走的每一步,乙都会选择下使得自己优势最大,甲的劣势最大的一步,甲下棋时亦然。例如在图6中在A棋局情况下,如果甲选择走B棋局, 那么乙将选择走对甲不利的E棋局;如果甲选择走 C棋局,乙将选择走G棋局;如果甲选择走D棋局, 那么乙将选择走K棋局……所以,综合来看,甲应该选择走B棋局,这样对自己最有利,这种算法就是极大极小算法。

​​图8 博弈树模型

1.2 博弈树模型算法优化与实现

1.2.1 增量分析法

在用人为设定函数计算棋局价值数值时,如果每次都遍历整个棋盘,则需要大量时间,事实上,只需计算出当前这一步落子在四个方向上的价值和未落子时该点四个方向上的价值之差再加上原来棋盘的价值及即可求得落子之后期盼的总价值

1.2.2 局部性原理

互联网小常识:入侵防护系统的分类:基于主机的入侵防护系统、基于网络的入侵防护系统(一般串联在防火墙与路由器之间)和应用入侵防护系统。

控制博弈树的层次和每层展开的节点数目,对反应速度和智能程度至关重要,根据人类棋手的经验,五子棋问题中,新下的棋与其余棋子的距离和该点的价值相关,距离越远,对整个棋盘影响越小,距离越近影响越大。这就是局部性原理,运用它可以选择有潜力的点进行展开,缩小搜索范围,提高搜索效率。

1.2.3 α-β剪枝

很多时候遍历整个博弈树是没有必要的,如图6中,在A棋局下轮到甲走棋,甲首先搜索叶子节点E=7,F=11,计算得到B 棋局的倒推值为7;然后,当甲继续搜索到节点G= 1时,发现C(C≤1)必定小于B。也就是说,走第二 个子节点C棋局肯定不如走第一个子节点曰棋局 有利,那么子节点C棋局往下的其它节点棋局(H、I)就不用再搜索了,此过程就称为α-β剪枝。,运用α-β剪枝可以加快搜索速度,且党棋局越明郎,搜索速度越快。

1.3 实验结果与分析

1.3.1 人为设定函数的机器棋手表现

表1 机器五子棋的测试结果

采用算法及优化手段

博弈树每层分支

博弈树层次

下棋速度

与普通棋手下胜算

基本的博弈树算法

几百

至多3层

很慢

约1成

互联网小常识:BGP协议的工作过程:打开分组、更新分组、保活分组、通知分组。其基本过程是BGP发言人发出打开分组,相邻的BGP发言人发送保活分组响应,然后定期发送(每30s)发送保活分组以确认连接,当网络拓扑结构或流量发生变化时,发送更新分组以更新,可以一次删除多个路由表项,但是每次只能增加一个路由表项。

加入优化2.2.1

7

增至5层

较慢

约3成

加入优化2.2.2

7

增至7层

约5成

加入优化2.2.3

7

增至9层

很快

8成多

1.3.1采用神经网络和遗传算法的机器棋手表现

运用遗传算法神经网络训练出的机器棋手表现极为不佳分析原因有以下两点:

(1)种群数量太小,基因多样性丢失:在种群数量20,突变率为0.1的实验中,实验进行到第28代后,每次棋局都是变得一模一样,主要原因是种群数量太小,次要原因是突变率太小

​​图9 种群20第28代出现多样性丢失

(2)训练代数太小,未学到智能性:在种群数量为100,突变率为0.3的实验中,经过长达两小时的训练到达第33代时,虽然没有出现基因多样性消失的现象,但机器棋手仍未表现出智能的迹象,主要原因是计算能力有限,短时间内训练代数太少,次要原因是网络初始参数不带有任何先天经验,从零开始学习比较费时

图10 种群100第33代仍未有智能表现

​​

代码一:人为设置函数

importtimeimportnumpyasnpimportcopyimportpygameimportsysflex1=[[0,1,0,0,0,0],[0,0,1,0,0,0],[0,0,0,1,0,0,],[0,0,0,0,1,0]]Flex1=[[0,-1,0,0,0,0],[0,0,-1,0,0,0],[0,0,0,-1,0,0,],[0,0,0,0,-1,0]]flex2=[[0,1,1,0,0,0],[0,0,1,1,0,0],[0,0,0,1,1,0],[0,1,0,1,0,0],[0,0,1,0,1,0],[0,1,0,0,1,0]]Flex2=[[0,-1,-1,0,0,0],[0,0,-1,-1,0,0],[0,0,0,-1,-1,0],[0,-1,0,-1,0,0],[0,0,-1,0,-1,0]]flex3=[[0,1,1,1,0,0],[0,1,1,0,1,0],[0,0,1,1,1,0],[0,1,0,1,1,0]]Flex3=[[0,-1,-1,-1,0,0],[0,-1,-1,0,-1,0],[0,0,-1,-1,-1,0],[0,-1,0,-1,-1,0]]flex4=[[0,1,1,1,1,0]]Flex4=[[0,-1,-1,-1,-1,0]]white_win=[[1,1,1,1,1,1],[1,1,1,1,1,0],[0,1,1,1,1,1,],[-1,1,1,1,1,1],[1,1,1,1,1,-1],[2,1,1,1,1,1],[1,1,1,1,1,2]]black_win=[[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,0],[0,-1,-1,-1,-1,-1,],[1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,1],[2,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,2]]block2=[[2,1,1,0,0,0,],[0,0,0,1,1,2],[-1,1,1,0,0,0,],[0,0,0,1,1,-1]]Block2=[[2,-1,-1,0,0,0,],[0,0,0,-1,-1,2],[1,-1,-1,0,0,0,],[0,0,0,-1,-1,1]]block3=[[2,1,1,1,0,0,],[0,0,1,1,1,2],[-1,1,1,1,0,0,],[0,0,1,1,1,-1],[2,1,1,0,1,0,],[0,1,0,1,1,2],[-1,1,1,0,1,0,],[0,1,0,1,1,-1]]Block3=[[2,-1,-1,-1,0,0,],[0,0,-1,-1,-1,2],[1,-1,-1,-1,0,0,],[0,0,-1,-1,-1,1],[2,-1,-1,0,-1,0,],[0,-1,0,-1,-1,2],[1,-1,-1,0,-1,0,],[0,-1,0,-1,-1,1]]block4=[[2,1,1,1,1,0,],[0,1,1,1,1,2],[-1,1,1,1,1,0,],[0,1,1,1,1,-1],[2,1,1,0,1,1],[2,1,0,1,1,1],[2,1,1,1,0,1]]Block4=[[2,-1,-1,-1,-1,0,],[0,-1,-1,-1,-1,2],[1,-1,-1,-1,-1,0,],[0,-1,-1,-1,-1,1],[2,-1,-1,0,-1,-1],[2,-1,0,-1,-1,-1],[2,-1,-1,-1,0,-1]]screen_size=(671,671)WIDTH=720HEIGHT=720width=44GRID_WIDTH=WIDTH//20WHITE=(255,250,255)BLACK=(0,0,0)GREEN=(0,0xff,0)RED=(0xff,0,0)bgcolor=(255,255,255)color=[BLACK,WHITE]pygame.init()icon=pygame.image.load("image/五子棋.jpeg")pygame.display.set_icon(icon)screen=pygame.display.set_mode(screen_size,0,32)pygame.display.set_caption(欢乐五子棋)clock=pygame.time.Clock()bg=pygame.image.load("image/checker.png")defdraw_check(surf):screen.fill(bgcolor)surf.blit(bg,(0,0))defdraw_checker(surf,x,y,color):ifcolor==1:执白棋pygame.draw.circle(surf,BLACK,((x)*width+27,(y)*width+26),20,0)ifcolor==-1:执黑棋pygame.draw.circle(surf,WHITE,((x)*width+27,(y)*width+26),20,0)defwinner_check(chessboard,ones_turn,board_size,chesser):counter_x,counter_y,counter_1,counter_2=0,0,0,0foriinrange(board_size):ifchessboard[ones_turn[0]][i]==chesser:counter_x+=1else:counter_x=0ifcounter_x==5:return1ifchessboard[i][ones_turn[1]]==chesser:counter_y+=1else:counter_y=0ifcounter_y==5:return1tip=ones_turn.index(max(ones_turn))iftip==0:i,j=ones_turn[0]-ones_turn[1],0else:i,j=0,ones_turn[1]-ones_turn[0]whilejandiifchessboard[i][j]==chesser:counter_1+=1else:counter_1=0ifcounter_1==5:return1i+=1j+=1ifones_turn[0]+ones_turn[1]>=board_size+1:i,j=ones_turn[0]+ones_turn[1]-(board_size+1),board_size+1else:i,j=0,ones_turn[0]+ones_turn[1]whilej>=0andi<=board_size+1:ifchessboard[i][j]==chesser:counter_2+=1else:counter_2=0ifcounter_2==5:return1i+=1j-=1return0defscore_count(windows,black_score,white_score):weight=[1,1,20,20,400,50000,10000,500000,]ex=0.3if windows in flex1:white_score+=weight[0]if windows in Flex1:black_score-=ex*weight[0]if windows in block2:white_score+=weight[1]if windows in Block2:black_score-=ex*weight[1]if windows in flex2:white_score+=weight[2]if windows in Flex2:black_score-=ex*weight[2]if windows in block3:white_score+=weight[3]if windows in Block3:black_score-=ex*weight[3]if windows in flex3:white_score+=weight[4]if windows in Flex3:black_score-=ex*weight[4]if windows in block4:white_score+=weight[5]if windows in Block4:black_score-=ex*weight[5]if windows in flex4:white_score+=weight[6]if windows in Flex4:black_score-=ex*weight[6]if windows in white_win:white_score+=weight[7]if windows in black_win:black_score-=ex*weight[7]returnblack_score,white_scoredefeval_chessboard(chessboard,ones_turn,old_score):ones_turn,old_scorewhite_score,black_score=0,0x y axisforiinrange(chessboard.shape[0]-5):windows=list(chessboard[i:i+6,ones_turn[1]])black_score,white_score=score_count(windows,black_score,white_score)windows=list(chessboard[ones_turn[0],i:i+6,])black_score,white_score=score_count(windows,black_score,white_score)tip=ones_turn.index(max(ones_turn))iftip==0:i,j=ones_turn[0]-ones_turn[1],0else:i,j=0,ones_turn[1]-ones_turn[0]whilej<=board_size-4andi<=board_size-4:windows.clear()forkinrange(6):windows.append(chessboard[i+k][j+k])black_score,white_score=score_count(windows,black_score,white_score)j+=1i+=1ifones_turn[0]+ones_turn[1]>=board_size+1:i,j=ones_turn[0]+ones_turn[1]-(board_size+1),board_size+1else:i,j=0,ones_turn[0]+ones_turn[1]whilej>=5andi<=board_size-4:windows.clear()forkinrange(6):windows.append(chessboard[i+k][j-k])black_score,white_score=score_count(windows,black_score,white_score)i+=1j-=1returnold_score+white_score+black_scoredefcompute_score(chessboard,ones_turn,old_score):t1=eval_chessboard(chessboard,ones_turn,old_score)ori_chessboard=copy.deepcopy(chessboard)ori_chessboard[ones_turn[0]][ones_turn[1]]=0t0=eval_chessboard(ori_chessboard,ones_turn,old_score)returnt1-t0+old_scorehow to avoid count twice?defupdate_boundary(boundary,i,j,size):sizemaxaxisfrom1width=2ifi==15:width=widthifboundary[0]==0:boundary[0],boundary[1]=j,jboundary[2],boundary[3]=i,iifboundary[2]>=i-width:ifi-width>=1:boundary[2]=i-widthelse:boundary[2]=1ifboundary[3]<=i+width:ifi+width<=size:boundary[3]=i+widthelse:boundary[3]=sizeifboundary[0]>=j-width:ifj-width>=1:boundary[0]=j-widthelse:boundary[0]=1ifboundary[1]<=j+width:ifj+width<=size:boundary[1]=j+widthelse:boundary[1]=sizereturnboundarydeftree_search(chessboard,boundary,search_degree,type,pre_step,pre_score,apha,beta):boundry=(l,r,t,d)boundry should be a tuple with four feature represent the boundary squarescore_list,position_list,back_score_list=[],[],[]ifsearch_degree==0:"""score_1=compute_score(chessboard,pre_step,pre_score) chessboard[pre_step[0]][pre_step[1]]=0 score_0=compute_score(chessboard,pre_step,pre_score)"""returncompute_score(chessboard,pre_step,pre_score),pre_stepmaybe?foriinrange(boundary[2],boundary[3]+1):forjinrange(boundary[0],boundary[1]+1):ifchessboard[i][j]==0:iftype==1:chessboard[i][j]=1tem_score=compute_score(chessboard,(i,j),pre_score)score_list.append((tem_score,i,j))else:chessboard[i][j]=-1tem_score=compute_score(chessboard,(i,j),pre_score)score_list.append((tem_score,i,j))chessboard[i][j]=0score_list.sort(key=lambdax:x[0],reverse=(type==1))forkinrange(8):i,j,tem_score=score_list[k][1],score_list[k][2],score_list[k][0]new_boundary=copy.deepcopy(boundary)new_boundary=update_boundary(new_boundary,i,j,chessboard.shape[0]-1)tem0,tem1=tree_search(chessboard,new_boundary,search_degree-1,-type,(i,j),tem_score,apha,beta)back_score_list.append(tem0)iftem0>apha:apha=tem0iftem0<beta:beta=tem0chessboard[i][j]=0ifbeta<=apha:returntem0,(i,j)iftype==1:returnmax(back_score_list),score_list[back_score_list.index(max(back_score_list))][1],\score_list[back_score_list.index(max(back_score_list))][2]iftype==-1:returnmin(back_score_list),score_list[back_score_list.index(min(back_score_list))][1],\score_list[back_score_list.index(min(back_score_list))][2]if __name__ =="_main_":if __name__ =="_main_":score=0score_max=0board_size=15needsboundaryfilledby2boundary=[0,0,0,0]flex1,Flex1,block2,Block2,flex2,Flex2,block3,Block3,flex3,Flex3,block4,Block4,w1,W1chance_count=np.zeros((1,board_size))chessboard=np.zeros((board_size+2,board_size+2))+2chessboard[1:-1,1:-1]-=2black_chess = eval(input())black_chess=1out=0while 1:foreventinpygame.event.get():ifevent.type==pygame.QUIT:sys.exit()ifevent.type==pygame.MOUSEBUTTONDOWN:pos=event.poshuman_turn1=(int(round((pos[0]-27)/width)),int(round((pos[1]-26)/width)))human_turn=human_turn1[0]+1,human_turn1[1]+1ifchessboard[human_turn[0]][human_turn[1]]==0:chessboard[human_turn[0]][human_turn[1]]=1boundary=update_boundary(boundary,human_turn[0],human_turn[1],board_size)ifwinner_check(chessboard,human_turn,board_size,1)==1:pygame.display.set_caption(哇,妈妈赢了,腻害腻害)out=1breakforiinrange(1,board_size+1):forjinrange(1,board_size+1):draw_checker(screen,i-1,j-1,chessboard[i][j])pygame.display.update()clock.tick(50)score=compute_score(chessboard,human_turn,score)score_max,robot_turn=tree_search(chessboard,boundary,2,-1,(0,0),score,-100000000,100000000)chessboard[robot_turn[0]][robot_turn[1]]=-1score=compute_score(chessboard,robot_turn,score)boundary=update_boundary(boundary,robot_turn[0],robot_turn[1],board_size)ifwinner_check(chessboard,robot_turn,board_size,-1)==1:print("robotwin")pygame.display.set_caption(承让承让了妈妈)out=1breakdraw_check(screen)foriinrange(1,board_size+1):forjinrange(1,board_size+1):draw_checker(screen,i-1,j-1,chessboard[i][j])pygame.display.update()ifout==1:time.sleep(3)sys.exit()clock.tick(50)

​​代码二:进化计算方法(建议尝试调整改进网络结构)

importtorchimportrandomimportcopyimportpygameimporttimechessboard=torch.randn((255,1))ensor=torch.Tensor(100,100,255,)torch.nn.init.normal_(ensor)screen_size=(671,671)WIDTH=720HEIGHT=720width=44GRID_WIDTH=WIDTH // 20WHITE=(255, 250, 255)BLACK=(0, 0, 0)GREEN=(0, 0xff, 0)RED=(0xff, 0, 0)bgcolor=(255,255,255)color=[BLACK,WHITE]pygame.init()screen_size=(671, 671)pygame.init()icon=pygame.image.load("image/五子棋.jpeg")pygame.display.set_icon(icon)screen=pygame.display.set_mode(screen_size, 0, 32)pygame.display.set_caption(欢乐五子棋)clock=pygame.time.Clock()bg=pygame.image.load("image/checker.png")fname="weight.txt"classF_c(torch.nn.Module):def__init__(self, input_size, hidden_size, num_classes):super(F_c,self).__init__()self.fc1=torch.nn.Linear(input_size, hidden_size,)self.sigmoid=torch.nn.Sigmoid()self.fc2=torch.nn.Linear(hidden_size, num_classes,)defforward(self, x):out=self.fc1(x)out=self.sigmoid(out)out=self.fc2(out)returnoutdefdraw_check(surf):screen.fill(bgcolor)surf.blit(bg,(0, 0))defdraw_checker(surf,x,y,color):ifcolor == 1:执白棋pygame.draw.circle(surf, BLACK, ((x) * width+27, (y) * width+26), 20, 0)ifcolor == -1:执黑棋pygame.draw.circle(surf, WHITE, ((x) * width+27, (y) * width+26), 20, 0)defwinner_check(chessboard,ones_turn,board_size,chesser):counter_x,counter_y,counter_1,counter_2=0,0,0,0fori in range(board_size):ifchessboard[ones_turn[0]][i]==chesser:counter_x+= 1else:counter_x=0ifcounter_x==5:return1ifchessboard[i][ones_turn[1]]==chesser:counter_y+= 1else:counter_y=0ifcounter_y==5:return1tip=ones_turn.index(max(ones_turn))iftip==0:i,j=ones_turn[0]-ones_turn[1],0else:i,j=0,ones_turn[1]-ones_turn[0]whilejifchessboard[i][j]==chesser:counter_1+=1else:counter_1=0ifcounter_1==5:return1i+=1j+=1ifones_turn[0]+ones_turn[1]>=board_size:i,j=ones_turn[0]+ones_turn[1]-(board_size-1),board_size-1else:i,j=0,ones_turn[0]+ones_turn[1]whilej>=0 and i<=board_size-1:ifchessboard[i][j]==chesser:counter_2+=1else:counter_2=0ifcounter_2==5:return1i+=1j-=1return0defupdate_boundary(boundary,i,j,size): size max axis from 1width=2ifboundary[0] == -1:boundary[0],boundary[1] = j, jboundary[2],boundary[3] = i, iifboundary[0]>=j-width:ifj-width>=0:boundary[0]=j-widthelse:boundary[1]=0ifboundary[1]<=j+width:ifj+widthboundary[1]=j+widthelse:boundary[1]=size-1ifboundary[2]>=i-width:ifi-width>=0:boundary[2]=i-widthelse:boundary[2]=0ifboundary[3]<=i+width:ifi+widthboundary[3]=i+widthelse:boundary[3]=size-1returnboundarydefcompute_score(chessboard,weight_tensor):the latter should be the object lof F_cchessboard_tensor=chessboard.clone().detach()chessboard_tensor=torch.cuda.FloatTensor(chessboard_tensor)chessboard_tensor=chessboard_tensor.to(device)chessboard=chessboard-2chessboard_tensor.resize_(225)ues torch.nnx=weight_tensor.forward(chessboard_tensor)returnxdeftree_search(chessboard,weight_tensor,boundary,pre_step,search_degree,type,apha,beta):boundry=(l,r,t,d)boundry should be a tuple with four feature represent the boundary squarescore_list,position_list, back_score_list = [], [], []ifsearch_degree == 0:returncompute_score(chessboard,weight_tensor), pre_step maybe?fori in range(boundary[2], boundary[3]+1 ):forj in range(boundary[0], boundary[1]+1 ):ifchessboard[i][j] == 2:iftype == 1:chessboard[i][j]=1tem_score=compute_score(chessboard,weight_tensor)score_list.append((tem_score,i, j))else:chessboard[i][j]=-1tem_score=compute_score(chessboard,weight_tensor)score_list.append((tem_score,i, j))chessboard[i][j]=2score_list.sort(key=lambda x: x[0], reverse=(type == 1))fork in range(min(8,len(score_list))):i,j, tem_score = score_list[k][1], score_list[k][2], score_list[k][0]new_boundary=copy.deepcopy(boundary)new_boundary=update_boundary(new_boundary, i, j, chessboard.shape[0] - 1)tem0,tem1 = tree_search(chessboard, weight_tensor,new_boundary,(i,j), search_degree - 1, -type, apha, beta)back_score_list.append(tem0)iftem0 > apha:apha=tem0iftem0 < beta:beta=tem0chessboard[i][j]=2ifbeta <= apha:returntem0, (i, j)iftype == 1:returnmax(back_score_list), (score_list[back_score_list.index(max(back_score_list))][1], \ score_list[back_score_list.index(max(back_score_list))][2])iftype == -1:returnmin(back_score_list), (score_list[back_score_list.index(min(back_score_list))][1], \ score_list[back_score_list.index(min(back_score_list))][2])defplay(c1,c2,generation):board_size=15chessboard=torch.zeros((15,15))+2c1_turn=(8,8)boundary=[-1]*4while1:chessboard[c1_turn[0]][c1_turn[1]]=1boundary=update_boundary(boundary, c1_turn[0], c1_turn[1], board_size)ifwinner_check(chessboard,c1_turn,board_size,1)==1:breakiftorch.max(chessboard) < 2:return1平局score 是不同的score2,c2_turn=tree_search(chessboard,c2,boundary,(0, 0),2,-1, -100000000, 100000000)chessboard[c2_turn[0]][c2_turn[1]]=-1boundary=update_boundary(boundary, c2_turn[0], c2_turn[1], board_size)ifwinner_check(chessboard,c2_turn,board_size,-1)==1:breakscore1,c1_turn=tree_search(chessboard,c1,boundary,(0, 0),2,1, -100000000, 100000000)draw_check(screen)fori in range(board_size):forj in range(board_size):draw_checker(screen,i , j , chessboard[i][j])pygame.display.update()pygame.display.set_caption("N0.{}match".format(generation))ifwinner_check(chessboard,c2_turn,board_size,-1) == 1:print("whitewin")return1else:print("blackwin")return0defcompetation(population,generation):l=len(population)len应为双数promoted[i],promoted[i+1]i=0promoted=[]print(l)whileix=population[i]f=play(population[i],population[i+1],generation)promoted.append(population[i+f])i+=2returnpromoted还要先交配 再 选出其余一半子代defreproduction(p,q):x=random.randint(0,p.fc1.weight.shape[0]*p.fc1.weight.shape[1])y=random.randint(0, p.fc1.weight.shape[0] * p.fc1.weight.shape[1])a=p.fc1.weightb=p.fc2.weightp.fc1.weight[0,x:y]=q.fc1.weight[0,x:y]q.fc1.weight[0,x:y]=a[0,x:y]x=random.randint(0, p.fc2.weight.shape[0] * p.fc2.weight.shape[1])y=random.randint(0, p.fc2.weight.shape[0] * p.fc2.weight.shape[1])p.fc2.weight[0,x:y]=q.fc2.weight[0,x:y]q.fc2.weight[0,x:y]=b[0,x:y]print(q.fc2.weight[0,x:y])returnp,qhow to initialize stochastic netdefgenetic_algrothim(population,generation):weight_tensor is object listifgeneration==0:torch.save(population[0],"net_params.pkl")print("ok")returnpopulationto get the best onemutation_rate=0.3how to initialize stochastic netpromoted=competation(population,generation)needs objectpopulation+= promotedl=len(promoted)print(len(promoted))fori in range(len(promoted),1):promoted[i],promoted[i+1]=reproduction(promoted[i],promoted[i+1])fori in range(l):promoted.append(population[random.randint(0,len(population)-1)])mutationfori in promoted:forj in range(i.fc1.weight.shape[1]):ifrandom.random() < mutation_rate:i.fc1.weight[0,j]+= random.random()forj in range(i.fc2.weight.shape[1]):ifrandom.random() < mutation_rate:i.fc2.weight[0,j]+= random.random()torch.save(population[0],"net_params0.pkl")torch.save(population[1],"net_params1.pkl")torch.save(population[2],"net_params2.pkl")torch.save(population[3],"net_params3.pkl")answer=genetic_algrothim(promoted,generation-1)returnanswerdevice=torch.device(cuda if torch.cuda.is_available() else cpu)a=torch.zeros(225)population,weight=[],[]fori in range(100):people=F_c(225,10,1)torch.manual_seed(i)torch.nn.init.normal_(people.fc1.weight,mean=0,std=1)torch.nn.init.normal_(people.fc2.weight,mean=0,std=1)people.to(device)population.append(people)weight=torch.tensor(weight)whileTrue:forevent in pygame.event.get():ifevent.type == pygame.QUIT:pygame.quit()print(weight)new_weight=genetic_algrothim(population,10000)print(new_weight)

互联网小常识:VLAN Trunk技术是交换机与交换机之间、交换机与路由器之间存在的一条物理链路,而在这一条物理链路上要传输多个VLAN信息的一种技术。VLAN Trunk采用帧标签的方式,每个帧标签指定一个唯一的VLAN ID。划分VLAN的方法分为基于端口、MAC地址、第三层协议类型或地址。

免责声明:本站所有信息均搜集自互联网,并不代表本站观点,本站不对其真实合法性负责。如有信息侵犯了您的权益,请告知,本站将立刻处理。联系QQ:1640731186