Prisoner's dilemma
No preview image
Model was written in NetLogo 6.2.2
•
Viewed 99 times
•
Downloaded 5 times
•
Run 0 times
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
Comments and Questions
Please start the discussion about this model!
(You'll first need to log in.)
Click to Run Model
extensions [rnd] breed [sheng s];;0 saint cooperate with every one;;圣徒俱乐部 跟任何人都合作 breed [huoban h];;1 buddies only cooperate with buddied member;;伙伴俱乐部 只跟自己阵营的人合作 breed [boji b];;4 fight club only cooperate with other club member;;搏击俱乐部 只跟外人合作 breed [youda y];;6 youda cooperate with no one ;;犹大俱乐部 跟任何人都不合作 turtles-own [ fitness ;;be careful we have 256 bits ,and each bit range from [0 1 4 6](using global rulers which means Any two numbers add up to be unique ) ga-bits ;;4 bits range from [0 1 2 3] ;;we have 6 bits and the first bit means 2 round before which club we choose ,the second bit means which bit our opponent choose . ;;why we use [0 1 2 3], because is more simply to calculate the history-number ,0->saint 1->buddies 2->fight club 3->youda club-bits ;; to see if this round we have been palyed or not isgambled ;;256 bits also range from [0 1 4 6] next-club-bits;;64位 ;; history-number = 64 * item 0 club-bits + 16 * item 1 club-bits + 4 * item 2 club-bits + 1 * item 3 club-bits history-number max-fitness ;; 4 * 256 bits ,if our clu-bits is [0 0 0 1] then ourhistory-number is 1,if our this round is 0(saint) then we choose 0 *256 + 1(1 means history-number) ,if this round we are 1(buddies) then we choose 1*256 + 1 and so on payoff ;; 4 * 256 bits, and the rule is equal to payoff ,if our club-bits is [0 0 0 1] .if this round is 4(fight-club),then our step[2*256 + 1]++,if this round we are youda ,then step[3*256 + 1]++ step;记录这个历史已经走了多少次 ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;if this round A is youda(6) and club-bits is [0 0 1 2] which means the first round we are saint ,our opponent is saint;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;and the second round we are buddies and our opponent is fight club ,and if the third round A's opponent B is saint(0) and club-bits is [ 1 2 3 0];;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;A club-bits [0 0 1 2]->[1 2 3 0] B club-bits[1 2 3 0]->[3 0 0 3];;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;and A history-number is 6 ,ga-bits[6] means which club we are chooseing now ,and then we use next-club-bits[6] to cover ga-bits[6] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;and next-club-bits[6] covered and base on the weight : payoff[x * 256 + 6]/sum( payoff[ 0 * 256 + 6]+payoff[1 * 256 + 6]+payoff[2 * 256 + 6]+payoff[ 3* 256 + 6]);;;;;;;;; ;;;;;and now our history-number=1*64+2*16+3*4+0*1 = 108 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; globals[ co_xunhuan; test co test test-list forget_payoff ] to setup clear-all ;;change shape set-default-shape sheng "person doctor" set-default-shape boji "person soldier" set-default-shape huoban "person service" set-default-shape youda "person lumberjack" ;;set ga-bits's mutate_probability set mutate_probability 0.1 ;;this is nextclub's mutate_probability set nextclub_mutate_probability 0.25 ;;set each club member let pop floor( population / 4) set test 0 set test-list [1 2 3 4 5] set forget_payoff 0 create-sheng pop[ setxy random-pxcor random-pycor set ga-bits n-values 256 [one-of [0 1 4 6]];;我们得找到相应的那个位置去修改为0 set next-club-bits n-values 256 [one-of [0 1 4 6]] set club-bits n-values 4 [one-of [0 1 2 3]];从3个历史变成2个历史吧 let number (64 * item 0 club-bits + 16 * item 1 club-bits + 4 * item 2 club-bits + 1 * item 3 club-bits) ;list 不再是list[1] 而是 item 1 list set ga-bits replace-item number ga-bits 0 set isgambled 0 ;; which mean if isgambled is 0 set history-number number set size 2 set payoff n-values (256 * 4 ) [ 1 ] set step n-values (256 * 4) [1] ] create-huoban pop[ setxy random-pxcor random-pycor set ga-bits n-values 256 [one-of [0 1 4 6]] set next-club-bits n-values 256 [one-of [0 1 4 6]] set club-bits n-values 4 [one-of [0 1 2 3]] let number 64 * item 0 club-bits + 16 * item 1 club-bits + 4 * item 2 club-bits + 1 * item 3 club-bits ;list 不再是list[1] 而是 item 1 list set ga-bits replace-item number ga-bits 1 set isgambled 0 set history-number number set size 2 set payoff n-values (256 * 4 ) [ 1 ] set step n-values (256 * 4) [1] ] create-boji pop[ setxy random-pxcor random-pycor set ga-bits n-values 256 [one-of [0 1 4 6]] set next-club-bits n-values 256 [one-of [0 1 4 6]] set club-bits n-values 4 [one-of [0 1 2 3]] let number 64 * item 0 club-bits + 16 * item 1 club-bits + 4 * item 2 club-bits + 1 * item 3 club-bits ;list 不再是list[1] 而是 item 1 list set ga-bits replace-item number ga-bits 4 set isgambled 0 set history-number number set size 2 set payoff n-values (256 * 4 ) [ 1 ] set step n-values (256 * 4) [1] ] create-youda pop[ setxy random-pxcor random-pycor set ga-bits n-values 256 [one-of [0 1 4 6]] set next-club-bits n-values 256 [one-of [0 1 4 6]] set club-bits n-values 4 [one-of [0 1 2 3]] let number 64 * item 0 club-bits + 16 * item 1 club-bits + 4 * item 2 club-bits + 1 * item 3 club-bits ;list 不再是list[1] 而是 item 1 list set ga-bits replace-item number ga-bits 6 set isgambled 0 set history-number number set size 2 set payoff n-values (256 * 4 ) [ 1 ] set step n-values (256 * 4) [1] ] reset-ticks end to calculate-fitness ;do nothing end to gamble let opponent one-of other turtles-here ; cuz the turtles move randomly,if remain only 2-4 turtles not gamble ,which will waste lots of times if we wait this 2-4 turtles to meet each other ; if we have 200 pop ,and 20 people not played yet . ;we first set buf = self when we find the opponent's isgambled =0 and != buf ,then we match this two to gamble let buf self ;if self b87 let count_remain 0 if count turtles with [isgambled = 0] < population / 10[ ask turtles with [isgambled = 0 ][ ;myself ;b87 ;self s52 if buf != self[ set count_remain count_remain + 1 if count_remain = 1 [ set opponent self ;将最后一个置为opponent ] ] ] ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 圣和圣 0和0 (0,0);;;;;;; ;;; saint and saint (0 0) ;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ifelse opponent = nobody[ ;if the current turtle location meet no turtle ; then do nothing ] [ ;base on ga-bits[history-number]to get the opponent breed(club) let opp_history item [history-number] of opponent [ga-bits] of opponent;拿到对手当前的阵营 我们更新历史的时候要注意 由于我们ga-bits中是0146 对应的是0123 注意【ga-bits] of opponent ;if 4 then -> 2 mean fight club if 6 then -> 3 mean youda if opp_history > 1[set opp_history opp_history / 2] ;base on ga-bits[history] get my breed(club) let self_history item history-number ga-bits if self_history > 1 [set self_history self_history / 2] ;;to get the current_history-number payoff let self_history_payoff (self_history * 256) + history-number ;当前payoff对应的位置 let opp_history_payoff (opp_history * 256) + ([history-number] of opponent) ;;to get the current history-number step let self_step item ((self_history * 256) + history-number) step ;得到当前历史走过几步 let opp_step item opp_history_payoff [step] of opponent;大致意思是先拿到opponent的payoff的对应位置 再映射到step 那个对应位置就要1024里的那个 比如第一轮就是拿到1; ;;;;; test 测试啦 if(test = 0 and [breed] of opponent = sheng and [history-number] of opponent < 30 and (history-number < 30 and fitness > 0 and isgambled = 0 and [fitness] of opponent > 0 and [isgambled] of opponent = 0))[ set test 1 print("opponent info") print("对手 club-bit") print [club-bits] of opponent print("对手ga-bits") print [ga-bits] of opponent print("对手当前的step") print [step] of opponent print("对手当前的payoff") print [payoff] of opponent print("对手当前的ga 步数") print [history-number] of opponent print("对手当前的payoff步数") print [opp_history_payoff] of opponent print("对手当前历史的步数") print [opp_step] of opponent print [fitness] of opponent print("myself info") print("本人的阵营") print breed print("本人的history-number") print history-number print("本人当前的club-bits") print club-bits print ("本人当前的payoff-历史") print self_history_payoff print ("本人目前的ga") print ga-bits print( "本人当前的历史走过的步数") print self_step print fitness print isgambled print [isgambled] of opponent ] if[item history-number ga-bits] of opponent + item history-number ga-bits = 0 and cheack-playbefore([isgambled] of opponent )(isgambled)[ ;;设置已经玩过了 ;;set we have been played before set isgambled 1 ;; cuz me and opponent are saint ,so our fitness should + 4 set fitness fitness + 4 ;;;比如我们当前阵营为圣 我们的历史是圣圣圣圣 我们比赛完 然后更新我的历史 我们新的阵营由新的历史对应的ga-bits提供 ;;;然后更新历史前要注意下一次遇到相同的圣圣圣圣 我们此时阵营的选择从next-club相应位置中获取 ;;;then we should change our currently ga-bits[history-number] ,and use next-club-bits[history-number] to cover it set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) ;;还有这个时候就得更新我们的step和我们的payoff 比如此时我们在100号的ga基因 阵营是0 self_history_payoff就是0 * 256 + 100 ;;before we change our history-number we should change our payoff[breed * 256 + history-number] and step[breed * 256 + history-number] ;; in payoff ,we use the [(old payoff * old step)+ fitness ]/step + 1 set payoff replace-item self_history_payoff payoff ((item self_history_payoff payoff * self_step + 4 ) / (self_step + 1));(payoff原有的值 * 步数 + 当前得分(这轮获得的fitness) )/(步数 + 1) ;; set old step + 1 set step replace-item self_history_payoff step (self_step + 1);这一步就要更新我们的步数了 let numbers 0 let step_buf[];[[123 1][234 2][... 3][... 4]...] ;; below code goal is to change next-club-bits[history-number] base on the weight of the [x * 256 + history-number] repeat 4[ set step_buf lput list item(256 * numbers + history-number) payoff numbers step_buf set numbers numbers + 1 ] ;;if our history-number is 100 then we get [[payoff[0* 256+100],0] [payoff[1*256 +100],1] [payoff[2*256+100],2][payoff[3*256 +100],3]] ;; base on weight(payoff) we can get the 0 1 2 3 , ;;for example [100,0] [1,1][1,2][1,3] base on weight we safely choose [100,0] and take the 0 out,which means we get saint ,and then change next-club-bits[history-number] = 0 let step_number weight step_buf let new_club item 1 step_number ;注意我们这里拿的是0123 我们得将其变回0 1 4 6 ;;be careful : next_club is 0 1 2 3 ,and we should change it to 0 1 4 6 if(new_club > 1)[ set new_club new_club * 2 ] set next-club-bits replace-item history-number next-club-bits new_club ;下面我们得更新club-bits ; and then we should change club-bits ;; for example [1 2 2 0] -> [2 0 1 3] let number 0 repeat 2[ set club-bits replace-item number club-bits item (number + 2) club-bits set number number + 1 ] set club-bits replace-item 2 club-bits 0 ;self_history set club-bits replace-item 3 club-bits 0 ;opp_history ;finally base on the new club-bits ,we change our history-number set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) ;change our breed base on the ga-bits[history-number] if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ;对对手进行大致相同的操作 ;;the opponent do the same job... ask opponent [set isgambled 1 set fitness fitness + 4 set ga-bits replace-item history-number ga-bits (item history-number next-club-bits);这里将下一次的选择刻入ga-dna中 set payoff replace-item opp_history_payoff payoff ((item opp_history_payoff payoff * opp_step + 4 ) / (opp_step + 1));(payoff原有的值 * 步数 + 当前得分(这轮获得的fitness) )/(步数 + 1) set step replace-item opp_history_payoff step (opp_step + 1);这一步就要更新我们的步数了 let numberss 0 let step_buff[];[[123 1][234 2][... 3][... 4]...] repeat 4[ set step_buff lput list item(256 * numberss + history-number) payoff numberss step_buff set numberss numberss + 1 ] let step_numbers weight step_buff let new_clubs item 1 step_numbers ;比如[2.5 0]我们拿0 if(new_clubs > 1)[ set new_clubs new_clubs * 2 ] set next-club-bits replace-item history-number next-club-bits new_clubs ;到此我们先将next-club-bits[history-number]放入了ga-bits[history-number]里面 然后根据payoff的表的权重选出我们下一轮的策略,然后将选出的阵营放入next-club-bits let coun 0 repeat 2[ set club-bits replace-item coun club-bits item (coun + 2) club-bits set coun coun + 1 ] set club-bits replace-item 2 club-bits 0;opp_history set club-bits replace-item 3 club-bits 0;self_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ] ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 圣和伙伴 0和1 (0,6) ;;;; ;;;saint and buddies(0 , 6) ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; if[item history-number ga-bits] of opponent + item history-number ga-bits = 1 and cheack-playbefore([isgambled] of opponent )(isgambled)[ set isgambled 1 let opponent_fitness 0 let fitness_buf 0 ;the only difference is that we should find out who is the saint and appapparently the other one is buddies ifelse breed = sheng [ set fitness_buf 0 set fitness fitness + fitness_buf set opponent_fitness opponent_fitness + 6 ] [ set fitness_buf 6 set fitness fitness + fitness_buf set opponent_fitness 0 ] set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item self_history_payoff payoff ((item self_history_payoff payoff * self_step + fitness_buf ) / (self_step + 1)) set step replace-item self_history_payoff step (self_step + 1) let numbers 0 let step_buf[] repeat 4[ set step_buf lput list item(256 * numbers + history-number) payoff numbers step_buf set numbers numbers + 1 ] let step_number weight step_buf let new_club item 1 step_number if(new_club > 1)[ set new_club new_club * 2 ] set next-club-bits replace-item history-number next-club-bits new_club let number 0 repeat 2[ set club-bits replace-item number club-bits item (number + 2) club-bits set number number + 1 ] set club-bits replace-item 2 club-bits self_history set club-bits replace-item 3 club-bits opp_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ask opponent [set isgambled 1 set fitness fitness + opponent_fitness set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item opp_history_payoff payoff ((item opp_history_payoff payoff * opp_step + opponent_fitness ) / (opp_step + 1)) set step replace-item opp_history_payoff step (opp_step + 1) let numberss 0 let step_buff[] repeat 4[ set step_buff lput list item(256 * numberss + history-number) payoff numberss step_buff set numberss numberss + 1 ] let step_numbers weight step_buff let new_clubs item 1 step_numbers if(new_clubs > 1)[ set new_clubs new_clubs * 2 ] set next-club-bits replace-item history-number next-club-bits new_clubs let coun 0 repeat 2[ set club-bits replace-item coun club-bits item (coun + 2) club-bits set coun coun + 1 ] set club-bits replace-item 2 club-bits opp_history set club-bits replace-item 3 club-bits self_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ] ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 圣和搏击 0和4 (4,4) ;;;;; ;;;saint and fight club(4 , 4) ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; if[item history-number ga-bits] of opponent + item history-number ga-bits = 4 and cheack-playbefore([isgambled] of opponent )(isgambled)[ set isgambled 1 set fitness fitness + 4 set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item self_history_payoff payoff ((item self_history_payoff payoff * self_step + 4 ) / (self_step + 1)) set step replace-item self_history_payoff step (self_step + 1) let numbers 0 let step_buf[] repeat 4[ set step_buf lput list item(256 * numbers + history-number) payoff numbers step_buf set numbers numbers + 1 ] let step_number weight step_buf let new_club item 1 step_number if(new_club > 1)[ set new_club new_club * 2 ] set next-club-bits replace-item history-number next-club-bits new_club let number 0 repeat 2[ set club-bits replace-item number club-bits item (number + 2) club-bits set number number + 1 ] set club-bits replace-item 2 club-bits self_history set club-bits replace-item 3 club-bits opp_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ask opponent [set isgambled 1 set fitness fitness + 4 set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item opp_history_payoff payoff ((item opp_history_payoff payoff * opp_step + 4 ) / (opp_step + 1)) set step replace-item opp_history_payoff step (opp_step + 1) let numberss 0 let step_buff[] repeat 4[ set step_buff lput list item(256 * numberss + history-number) payoff numberss step_buff set numberss numberss + 1 ] let step_numbers weight step_buff let new_clubs item 1 step_numbers if(new_clubs > 1)[ set new_clubs new_clubs * 2 ] set next-club-bits replace-item history-number next-club-bits new_clubs let coun 0 repeat 2[ set club-bits replace-item coun club-bits item (coun + 2) club-bits set coun coun + 1 ] set club-bits replace-item 2 club-bits opp_history set club-bits replace-item 3 club-bits self_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ] ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 圣和犹大 0和6 (0,6) ;;; ;;;saint and youda (0 , 6) ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; if[item history-number ga-bits] of opponent + item history-number ga-bits = 6 and cheack-playbefore([isgambled] of opponent )(isgambled)[ set isgambled 1 let fitness_buf 0 let opponent_fitness 0 ifelse breed = sheng [ set fitness_buf 0 set fitness fitness + fitness_buf set opponent_fitness 6 ] [set fitness_buf 6 set fitness fitness + fitness_buf set opponent_fitness 0 ] set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item self_history_payoff payoff ((item self_history_payoff payoff * self_step + fitness_buf ) / (self_step + 1)) set step replace-item self_history_payoff step (self_step + 1) let numbers 0 let step_buf[] repeat 4[ set step_buf lput list item(256 * numbers + history-number) payoff numbers step_buf set numbers numbers + 1 ] let step_number weight step_buf let new_club item 1 step_number if(new_club > 1)[ set new_club new_club * 2 ] set next-club-bits replace-item history-number next-club-bits new_club let number 0 repeat 2[ set club-bits replace-item number club-bits item (number + 2) club-bits set number number + 1 ] set club-bits replace-item 2 club-bits self_history set club-bits replace-item 3 club-bits opp_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ask opponent [set isgambled 1 set fitness fitness + opponent_fitness set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item opp_history_payoff payoff ((item opp_history_payoff payoff * opp_step + opponent_fitness ) / (opp_step + 1)) set step replace-item opp_history_payoff step (opp_step + 1) let numberss 0 let step_buff[] repeat 4[ set step_buff lput list item(256 * numberss + history-number) payoff numberss step_buff set numberss numberss + 1 ] let step_numbers weight step_buff let new_clubs item 1 step_numbers if(new_clubs > 1)[ set new_clubs new_clubs * 2 ] set next-club-bits replace-item history-number next-club-bits new_clubs let coun 0 repeat 2[ set club-bits replace-item coun club-bits item (coun + 2) club-bits set coun coun + 1 ] set club-bits replace-item 2 club-bits opp_history set club-bits replace-item 3 club-bits self_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ] ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 伙伴和伙伴 1和1 (4,4) ;;; ;;;buddies and buddies(4,4) ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; if[item history-number ga-bits] of opponent + item history-number ga-bits = 2 and cheack-playbefore([isgambled] of opponent )(isgambled)[ set isgambled 1 set fitness fitness + 4 set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item self_history_payoff payoff ((item self_history_payoff payoff * self_step + 4 ) / (self_step + 1)) set step replace-item self_history_payoff step (self_step + 1) let numbers 0 let step_buf[] repeat 4[ set step_buf lput list item(256 * numbers + history-number) payoff numbers step_buf set numbers numbers + 1 ] let step_number weight step_buf let new_club item 1 step_number if(new_club > 1)[ set new_club new_club * 2 ] set next-club-bits replace-item history-number next-club-bits new_club let number 0 repeat 2[ set club-bits replace-item number club-bits item (number + 2) club-bits set number number + 1 ] set club-bits replace-item 2 club-bits 1 set club-bits replace-item 3 club-bits 1 set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ask opponent [set isgambled 1 set fitness fitness + 4 set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item opp_history_payoff payoff ((item opp_history_payoff payoff * opp_step + 4 ) / (opp_step + 1)) set step replace-item opp_history_payoff payoff (opp_step + 1) let numberss 0 let step_buff[] repeat 4[ set step_buff lput list item(256 * numberss + history-number) payoff numberss step_buff set numberss numberss + 1 ] let step_numbers weight step_buff let new_clubs item 1 step_numbers if(new_clubs > 1)[ set new_clubs new_clubs * 2 ] set next-club-bits replace-item history-number next-club-bits new_clubs let coun 0 repeat 2[ set club-bits replace-item coun club-bits item (coun + 2) club-bits set coun coun + 1 ] set club-bits replace-item 2 club-bits 1 set club-bits replace-item 3 club-bits 1 set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ] ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 伙伴和搏击 1和4 (6,0) ;;;;; ;;; buddies and fight club(6 , 0) ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; if[item history-number ga-bits] of opponent + item history-number ga-bits = 5 and cheack-playbefore([isgambled] of opponent )(isgambled)[ set isgambled 1 let opponent_fitness 0 let fitness_buf 0 ifelse breed = huoban [ set fitness_buf 6 set fitness fitness + fitness_buf set opponent_fitness opponent_fitness + 0 ] [ set fitness_buf 0 set fitness fitness + fitness_buf set opponent_fitness opponent_fitness + 6 ] set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item self_history_payoff payoff ((item self_history_payoff payoff * self_step + fitness_buf ) / (self_step + 1)) set step replace-item self_history_payoff step (self_step + 1) let numbers 0 let step_buf[] repeat 4[ set step_buf lput list item(256 * numbers + history-number) payoff numbers step_buf set numbers numbers + 1 ] let step_number weight step_buf let new_club item 1 step_number if(new_club > 1)[ set new_club new_club * 2 ] set next-club-bits replace-item history-number next-club-bits new_club let number 0 repeat 2[ set club-bits replace-item number club-bits item (number + 2) club-bits set number number + 1 ] set club-bits replace-item 2 club-bits self_history set club-bits replace-item 3 club-bits opp_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ask opponent [set isgambled 1 set fitness fitness + opponent_fitness set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item opp_history_payoff payoff ((item opp_history_payoff payoff * opp_step + opponent_fitness ) / (opp_step + 1));(payoff原有的值 * 步数 + 当前得分(这轮获得的fitness) )/(步数 + 1) set step replace-item opp_history_payoff step (opp_step + 1);这一步就要更新我们的步数了 let numberss 0 let step_buff[] repeat 4[ set step_buff lput list item(256 * numberss + history-number) payoff numberss step_buff set numberss numberss + 1 ] let step_numbers weight step_buff let new_clubs item 1 step_numbers if(new_clubs > 1)[ set new_clubs new_clubs * 2 ] set next-club-bits replace-item history-number next-club-bits new_clubs let coun 0 repeat 2[ set club-bits replace-item number club-bits item (coun + 2) club-bits set coun coun + 1 ] set club-bits replace-item 2 club-bits opp_history set club-bits replace-item 3 club-bits self_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ] ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 伙伴和犹大 1和6 (1,1) ;;; ;;;buddies and youda (1 , 1) ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; if[item history-number ga-bits] of opponent + item history-number ga-bits = 7 and cheack-playbefore([isgambled] of opponent )(isgambled)[ set isgambled 1 set fitness fitness + 1 let opponent_fitness 0 set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item self_history_payoff payoff ((item self_history_payoff payoff * self_step + 1 ) / (self_step + 1)) set step replace-item self_history_payoff step (self_step + 1) let numbers 0 let step_buf[] repeat 4[ set step_buf lput list item(256 * numbers + history-number) payoff numbers step_buf set numbers numbers + 1 ] let step_number weight step_buf let new_club item 1 step_number if(new_club > 1)[ set new_club new_club * 2 ] set next-club-bits replace-item history-number next-club-bits new_club let number 0 repeat 2[ set club-bits replace-item number club-bits item (number + 2) club-bits set number number + 1 ] set club-bits replace-item 2 club-bits self_history set club-bits replace-item 3 club-bits opp_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ask opponent [set isgambled 1 set fitness fitness + 1 set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item opp_history_payoff payoff ((item opp_history_payoff payoff * opp_step + 1) / (opp_step + 1)) set step replace-item opp_history_payoff step (opp_step + 1) let numberss 0 let step_buff[] repeat 4[ set step_buff lput list item(256 * numberss + history-number) payoff numberss step_buff set numberss numberss + 1 ] let step_numbers weight step_buff let new_clubs item 1 step_numbers if(new_clubs > 1)[ set new_clubs new_clubs * 2 ] set next-club-bits replace-item history-number next-club-bits new_clubs let coun 0 repeat 2[ set club-bits replace-item coun club-bits item (coun + 2) club-bits set coun coun + 1 ] set club-bits replace-item 2 club-bits opp_history set club-bits replace-item 3 club-bits self_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ] ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 搏击和搏击 4和4 (1,1) ;;; ;;;fight club and fight club(1 , 1) ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; if[item history-number ga-bits] of opponent + item history-number ga-bits = 8 and cheack-playbefore([isgambled] of opponent )(isgambled)[ set isgambled 1 set fitness fitness + 1 set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item self_history_payoff payoff ((item self_history_payoff payoff * self_step + 1 ) / (self_step + 1)) set step replace-item self_history_payoff step (self_step + 1) let numbers 0 let step_buf[] repeat 4[ set step_buf lput list item(256 * numbers + history-number) payoff numbers step_buf set numbers numbers + 1 ] let step_number weight step_buf let new_club item 1 step_number if(new_club > 1)[ set new_club new_club * 2 ] set next-club-bits replace-item history-number next-club-bits new_club let number 0 repeat 2[ set club-bits replace-item number club-bits item (number + 2) club-bits set number number + 1 ] set club-bits replace-item 2 club-bits self_history set club-bits replace-item 3 club-bits opp_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ask opponent [set isgambled 1 set fitness fitness + 1 set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item opp_history_payoff payoff ((item opp_history_payoff payoff * opp_step + 1 ) / (opp_step + 1)) set step replace-item opp_history_payoff step (opp_step + 1) let numberss 0 let step_buff[] repeat 4[ set step_buff lput list item(256 * numberss + history-number) payoff numberss step_buff set numberss numberss + 1 ] let step_numbers weight step_buff let new_clubs item 1 step_numbers if(new_clubs > 1)[ set new_clubs new_clubs * 2 ] set next-club-bits replace-item history-number next-club-bits new_clubs let coun 0 repeat 2[ set club-bits replace-item coun club-bits item (coun + 2) club-bits set coun coun + 1 ] set club-bits replace-item 2 club-bits opp_history set club-bits replace-item 3 club-bits self_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ] ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 搏击和犹大 4和6 (0,6) ;;;; ;;;fight club and youda(0,6) ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; if[item history-number ga-bits] of opponent + item history-number ga-bits = 10 and cheack-playbefore([isgambled] of opponent )(isgambled)[ set isgambled 1 let opponent_fitness 0 let fitness_buf 0 ifelse breed = boji[ set fitness_buf 0 set fitness fitness + fitness_buf set opponent_fitness 6 ] [ set fitness_buf 6 set fitness fitness + fitness_buf set opponent_fitness opponent_fitness + 0 ] set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item self_history_payoff payoff ((item self_history_payoff payoff * self_step + fitness_buf ) / (self_step + 1)) set step replace-item self_history_payoff step (self_step + 1) let numbers 0 let step_buf[] repeat 4[ set step_buf lput list item(256 * numbers + history-number) payoff numbers step_buf set numbers numbers + 1 ] let step_number weight step_buf let new_club item 1 step_number if(new_club > 1)[ set new_club new_club * 2 ] set next-club-bits replace-item history-number next-club-bits new_club let number 0 repeat 2[ set club-bits replace-item number club-bits item (number + 2) club-bits set number number + 1 ] set club-bits replace-item 2 club-bits self_history set club-bits replace-item 3 club-bits opp_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ask opponent [set isgambled 1 set fitness fitness + opponent_fitness set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item opp_history_payoff payoff ((item opp_history_payoff payoff * opp_step + opponent_fitness ) / (opp_step + 1)) set step replace-item opp_history_payoff step (opp_step + 1) let numberss 0 let step_buff[] repeat 4[ set step_buff lput list item(256 * numberss + history-number) payoff numberss step_buff set numberss numberss + 1 ] let step_numbers weight step_buff let new_clubs item 1 step_numbers if(new_clubs > 1)[ set new_clubs new_clubs * 2 ] set next-club-bits replace-item history-number next-club-bits new_clubs let coun 0 repeat 2[ set club-bits replace-item coun club-bits item (coun + 2) club-bits set coun coun + 1 ] set club-bits replace-item 2 club-bits opp_history set club-bits replace-item 3 club-bits self_history set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ] ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 犹大和犹大 6和6 (1,1) ;;; ;;;youda and youda (1 , 1) ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; if[item history-number ga-bits] of opponent + item history-number ga-bits = 12 and cheack-playbefore([isgambled] of opponent )(isgambled)[ set isgambled 1 set fitness fitness + 1 set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item self_history_payoff payoff ((item self_history_payoff payoff * self_step + 1 ) / (self_step + 1)) set step replace-item self_history_payoff step (self_step + 1) let numbers 0 let step_buf[] repeat 4[ set step_buf lput list item(256 * numbers + history-number) payoff numbers step_buf set numbers numbers + 1 ] let step_number weight step_buf let new_club item 1 step_number if(new_club > 1)[ set new_club new_club * 2 ] set next-club-bits replace-item history-number next-club-bits new_club let number 0 repeat 2[ set club-bits replace-item number club-bits item (number + 2) club-bits set number number + 1 ] set club-bits replace-item 2 club-bits 3 set club-bits replace-item 3 club-bits 3 set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ask opponent [set isgambled 1 set fitness fitness + 1 set ga-bits replace-item history-number ga-bits (item history-number next-club-bits) set payoff replace-item opp_history_payoff payoff ((item opp_history_payoff payoff * opp_step + 1 ) / (opp_step + 1)) set step replace-item opp_history_payoff step (opp_step + 1);这一步就要更新我们的步数了 let numberss 0 let step_buff[] repeat 4[ set step_buff lput list item(256 * numberss + history-number) payoff numberss step_buff set numberss numberss + 1 ] let step_numbers weight step_buff let new_clubs item 1 step_numbers ;比如[2.5 0]我们拿0 if(new_clubs > 1)[ set new_clubs new_clubs * 2 ] set next-club-bits replace-item history-number next-club-bits new_clubs let coun 0 repeat 2[ set club-bits replace-item coun club-bits item (coun + 2) club-bits set coun coun + 1 ] set club-bits replace-item 2 club-bits 3 set club-bits replace-item 3 club-bits 3 set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ] ] ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;结束了我们的一轮的交换行为;;; ;;;over one round match ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;; end ;;; to get history-number base on club-bits to-report get_history_number[z0 z1 z2 z3] report 64 * z0 + 16 * z1 + 4 * z2 + 1 * z3 end to-report cheack-playbefore [x z] ifelse x + z > 0 [report false] [;set x 1 这里我们想直接赌过的直接在判断的时候就将 isgamebled 设为 1 但行不通 我们先不理 先在代码主体写吧 虽然冗余 但至少清晰 后续再修改 ;set z 1 不过还有个思路是把turtle对象扔进来 但他的效率会低 就不这样做 ; print x ;print z report true] end to play ask turtles[ ;;move move ;;gamble gamble ] if count turtles with [isgambled = 0] = 0[ set co co + 1 ;假如我们要10次交换才进行ga操作 set co_xunhuan co_xunhuan + 1 set forget_payoff forget_payoff + 1 ask turtles [ set isgambled 0 ] reset_game ] ;;;which means ten round we set a tick and generate-generation if co = 10[ set co 0 ;print [fitness] of max-one-of turtles[fitness] tick generate-generation ] end ;each round we reset the x-cor and y-cor ; each 50 round we reset step payoff and fitness to reset_game ask turtles[ setxy random-pxcor random-pycor if co_xunhuan = 10[ set fitness 0 set co_xunhuan 0 if forget_payoff = 50[ set payoff n-values (256 * 4 ) [ 1 ] set step n-values (256 * 4) [1] ] ] ] end to generate-generation ;g;mean old-generation was fixed and cover the whole turtles let old-generation turtles with [true] let crossover_number floor ((population * crossover_probability) / 2) ;假如我们配种的几率为0。8那么我们将进行40轮次 因为我们一轮次产生2个子代 然后剩下的20 我们从我们的父代去抽取然后进行变异 repeat crossover_number[;crossover_number[ let parents [] ;remember we get two ruetles set parents select old-generation;注意我们里面装的2个turtl 而不是我们的gp let child [] set child crossover (item 0 parents)(item 1 parents) ask item 0 parents [hatch 1 [set ga-bits item 0 item 0 child set next-club-bits item 0 item 1 child set payoff item 0 item 2 child set step item 0 item 3 child set fitness 0 ;;;由于我们还要变异,孩子的阵营由变异去操作就行了 ;;;due to we also need to mutate ,we dont set breed fitst set club-bits n-values 4 [one-of [0 1 2 3]] set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ] ] ask item 1 parents [hatch 1 [set ga-bits item 1 item 0 child set next-club-bits item 1 item 1 child set payoff item 1 item 2 child set step item 1 item 3 child set fitness 0 set club-bits n-values 4 [one-of [0 1 2 3]] set history-number get_history_number(item 0 club-bits)(item 1 club-bits)(item 2 club-bits)(item 3 club-bits) if item history-number ga-bits = 0 [set breed sheng] if item history-number ga-bits = 1 [ set breed huoban] if item history-number ga-bits = 4 [ set breed boji] if item history-number ga-bits = 6 [set breed youda] ] ] ] ;in repeat - 1 is to randomly delete one turtle and use max-one-of to hatch a new one repeat (population - crossover_number * 2 ) - 1 [ ask max-one-of(n-of floor(population / 5) old-generation) [fitness] [hatch 1 ] ] ;before in repeat - 1 ,now we hatch a new one to make sure the whole number of turtle is not change ask max-one-of old-generation [fitness][hatch 1] ask old-generation [ die ] if crossover_probability + mutate_probability > 1[ set mutate_probability (1 - crossover_probability) ] ask turtles[ set fitness 0 mutate ] end ;这里是选择过程 我们使用轮盘转法 ;selection we use tournament to-report select [population_] let parent1 rnd:weighted-one-of population_ [fitness] let parent2 rnd:weighted-one-of population_ [fitness] ;let parent1 max-n-of ;let parent1 max-one-of (n-of floor (population / 30) population_)[fitness] ;let parent2 max-one-of (n-of floor (population / 30) population_)[fitness] ;return two turtle report list parent1 parent2 end to mutate let random-value random-float 100.0 let current_club_buff item history-number ga-bits set ga-bits map [ bi -> ifelse-value (random-float 1.0 < mutate_probability ) ;each bits(256 bits in ga-bits) we may mutate or do nothing [ one-of remove bi [0 1 4 6] ] [ bi ] ] ga-bits ;remember no matter how drastic the mutate is, club-bits not change (means history-number not change) ; so we should change ga-bits[history-number] back base on the current breed ;;这里的意思是 我们前面将ga-bits全进行变异操作了 现在我们将ga-bits的现在club改回来 set ga-bits replace-item history-number ga-bits current_club_buff ;do the same mutate in next-club-bits set next-club-bits map [ bi -> ifelse-value (random-float 1.0 < nextclub_mutate_probability ) [ one-of remove bi [0 1 4 6] ] [ bi ] ] next-club-bits ;we also mutate the payoff set payoff map[payoff_ -> ifelse-value( random-float 1.0 < payoff_mutate_probalility ) ;which means we have 50% to mutate : +0.05 or -0.05 [(payoff_ + random-float 0.1) - 0.05];这里的意思有一半的几率+ 一半的几率减 然后幅度也不大 在【-0.1,0.1]之间 [payoff_] ] payoff end ;cross over ;1. ga-bits ;2. next-club-bits ;3. payoff ;4. step to-report crossover [turtle1 turtle2] let ga1 [ga-bits] of turtle1 let ga2 [ga-bits] of turtle2 let club3 [next-club-bits] of turtle1 let club4 [next-club-bits] of turtle2 ;If we have n positions to swap ;we create a list with n numbers ; and delete the duplicates ; and we lput 0 and rput 256 let cross_point n-values crossover_point [1 + random 254] set cross_point remove-duplicates cross_point let buf nobody ;由于 交叉的节点可以共用 所以我们不用创建club的交叉位点 while [ length(cross_point) < crossover_point ][ set buf n-values (crossover_point - length(cross_point)) [1 + random 254] set cross_point (sentence cross_point buf) set cross_point remove-duplicates cross_point ] set cross_point fput 0 cross_point set cross_point lput 256 cross_point let cross_point_sort sort cross_point ;print cross_point_sort let child1 nobody let child2 nobody let child_1 nobody let child_2 nobody let child_main1 [] let child_main2 [] let child_club1 nobody let child_club2 nobody let child_club_1 nobody let child_club_2 nobody let child_club_main1 [] let child_club_main2 [] let count_ 0 ;;注意 生下的孩子 不一定要跟父母一个阵营 ;; child's breed may not equal to parent's breed while [count_ < length(cross_point_sort) - 1 ][ ;set child1 ifelse ((count_ mod length(cross_point_sort)) = 0)[ ;;;;;;;;;;;;;;;;;;;;;;;; ;;;ga-bits's crossover;;; ;;;;;;;;;;;;;;;;;;;;;;;; set child1 (sentence (sublist ga1 item count_ cross_point_sort item (count_ + 1) cross_point_sort));这里就是交换2个ga的染色体 产生第一个parent的ga set child_1 (sentence (sublist ga2 item count_ cross_point_sort item (count_ + 1 ) cross_point_sort)) set child_main1 (sentence child_main1 child1) set child_main2 (sentence child_main2 child_1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;nextclub-bits's crossover;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; set child_club1 (sentence (sublist club3 item count_ cross_point_sort item (count_ + 1) cross_point_sort));这里就是交换next-club-bits的信息 set child_club_1 (sentence (sublist club4 item count_ cross_point_sort item (count_ + 1 ) cross_point_sort)) set child_club_main1 (sentence child_club_main1 child_club1) set child_club_main2 (sentence child_club_main2 child_club_1) ] [ ;;for example before we get ga1[0.....3] then we combine ga2[3....8] and then we combine ga1[8....23] and so on ... set child2 (sentence(sublist ga2 item count_ cross_point_sort item ( count_ + 1 ) cross_point_sort)) ;;the next child is ga2[0..3] then combine ga1[3.....8] and then combine ga2[8...23] and so on .... set child_2 (sentence (sublist ga1 item count_ cross_point_sort item (count_ + 1 ) cross_point_sort)) set child_main1 (sentence child_main1 child2) set child_main2(sentence child_main2 child_2) set child_club2 (sentence(sublist club4 item count_ cross_point_sort item ( count_ + 1 ) cross_point_sort)) set child_club_2 (sentence (sublist club3 item count_ cross_point_sort item (count_ + 1 ) cross_point_sort)) set child_club_main1 (sentence child_club_main1 child_club2) set child_club_main2(sentence child_club_main2 child_club2) ] set count_ count_ + 1 ] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;上面是对ga和next-club的交换 用到256的list 下面我们要对step和payoff进行操作;;; ;;;now we foucus on the payoff and step ,now we have 4 * 256 bits ;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; let payoff_point n-values (crossover_point * 4) [1 + random 1022] set payoff_point remove-duplicates payoff_point let payoff_buf nobody while [ length(payoff_point) < (crossover_point * 4) ][ set payoff_buf n-values (crossover_point * 4 - length(cross_point)) [1 + random 1022] set payoff_point (sentence payoff_point payoff_buf) set payoff_point remove-duplicates payoff_point ] let payoff_point_sort sort payoff_point set payoff_point_sort fput 0 payoff_point_sort set payoff_point_sort lput 1024 payoff_point_sort let pay1 [payoff] of turtle1;;得到ga1的 let pay2 [payoff] of turtle2 let step3 [step] of turtle1 let step4 [step] of turtle2 let child_payoff1 nobody let child_payoff2 nobody let child_payoff_1 nobody let child_payoff_2 nobody let child_main_payoff1 [] let child_main_payoff2 [] let child_step1 nobody let child_step2 nobody let child_step_1 nobody let child_step_2 nobody let child_step_main1 [] let child_step_main2 [] let count_p 0 while [count_p < length(payoff_point_sort) - 1 ][ ifelse ((count_p mod length(payoff_point_sort)) = 0)[ ;;;;;;;;;;;;;;;;;;;;;;;; ;;;payoff的crossover;;; ;;;;;;;;;;;;;;;;;;;;;;;; set child_payoff1 (sentence (sublist pay1 item count_p payoff_point_sort item (count_p + 1) payoff_point_sort)) set child_payoff_1 (sentence (sublist pay2 item count_p payoff_point_sort item (count_p + 1 ) payoff_point_sort)) set child_main_payoff1 (sentence child_main_payoff1 child_payoff1) set child_main_payoff2 (sentence child_main_payoff2 child_payoff_1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; step 的 crossover ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; set child_step1 (sentence (sublist step3 item count_p payoff_point_sort item (count_p + 1) payoff_point_sort)) set child_step_1 (sentence (sublist step4 item count_p payoff_point_sort item (count_p + 1 ) payoff_point_sort)) set child_step_main1 (sentence child_step_main1 child_step1) set child_step_main2 (sentence child_step_main2 child_step_1) ] [ set child_payoff2 (sentence(sublist pay2 item count_p payoff_point_sort item ( count_p + 1 ) payoff_point_sort)) set child_payoff_2 (sentence (sublist pay1 item count_p payoff_point_sort item (count_p + 1 ) payoff_point_sort)) set child_main_payoff1 (sentence child_main_payoff1 child_payoff2) set child_main_payoff2(sentence child_main_payoff2 child_payoff_2) set child_step2 (sentence(sublist step4 item count_p payoff_point_sort item ( count_p + 1 ) payoff_point_sort)) set child_step_2 (sentence (sublist step3 item count_p payoff_point_sort item (count_p + 1 ) payoff_point_sort)) set child_step_main1 (sentence child_step_main1 child_step2) set child_step_main2(sentence child_step_main2 child_step2) ] set count_p count_p + 1 ] ;delete ; to check our cross over function ;这下面是测试看我们的step和payoff是否有正确的crossover ;print child_main_payoff1 ;print child_main_payoff2 ;print child_step_main1 ;print child_step_main2 ;print pay1 ;print pay2 ;print step3 ;print step4 ;print (sum(step3)) ;print(".....") ;print child_main1 ;print child_club_main1 let ga_child list child_main1 child_main2 let club_child list child_club_main1 child_club_main2 let pay_off_child list child_main_payoff1 child_main_payoff2 let step_child list child_step_main1 child_step_main2 let re list ga_child club_child ;[ [[ga1][ga2]] [[club1][club2]] ] let re1 list pay_off_child step_child let re2 sentence re re1 report re2 end to move rt random 50 lt random 50 fd 1 end to-report isgamed_number ;let number_1 count turtles with [isgambled = 1] ;report number_1 report 1 end to-report weight [pop] let p1 rnd:weighted-one-of-list pop [[p] -> first p] report p1 end ;;to see how many 0 in all turtles's ga-bits to-report leng0 let len0 0 ask turtles[ set len0 len0 + length filter[gg -> gg = 0] ga-bits ] report len0 end to-report leng1 let len1 0 ask turtles[ set len1 len1 + length filter[gg -> gg = 1] ga-bits ] report len1 end to-report leng4 let len4 0 ask turtles[ set len4 len4 + length filter[gg -> gg = 4] ga-bits ] report len4 end to-report leng6 let len6 0 ask turtles[ set len6 len6 + length filter[gg -> gg = 6] ga-bits ] report len6 end
There is only one version of this model, created almost 2 years ago by Kevin LIu.
This model does not have any ancestors.
This model does not have any descendants.