~mu = 0.9; //mutation rate ~charset.curdle(0.9) ~charset = (0..255).select{|x| x.asAscii.isAlpha}.collect{|x| x.asAscii}; //a character set for rules ~mating_pool = 4.collect{ "tHlcqbunKdrifgojIPEDLXwxmNGMQVJRahZBFUkWsOeAyCpTYzvS" }; //axiom ~gen_no = 160; //generation number //create an initial gene pool ~gene_pool = 8.collect{ ~mating_pool.collect{|mom| mom.curdle(~mu).scramble.flatten.clump(13) } }.flatten; //rulesets should remain the same for testing purposes ~rsetz = Dictionary.new!3; k = 0; ~rsetz.do{|player, i| 4.do{|page, j| ~rsetz[i].add(~charset[k].asString -> j); k = k + 1; } }; //***start here for non-initial generations*** //make new population of games ( ~generate = Routine({ loop({ ~pop = ~gene_pool.collect{|dna| TDAT_Game.new(dna, [], 10); }; wait(0.01); //fill each game with players ~pop.do{|game| ~rsetz.do{|rulz| TDAT_Player.new(game, rulz, 0, 0); }; game.initMatrix; game.initStops; }; //define routines ~routines = ~pop.collect{|game| game.players.collect{|player, index| Routine({ loop({ player.turn; wait((1/128).exprand(1/256)); }) }); } }; wait(0.01); //play them so we can start testing games out ~routines.do{|game| game.do{|player| player.play; } }; //***loop this 32 times to make a generation*** wait(0.01); ~tally = Routine({ var tests; tests = 0; ~generation_score = []!~score.size; ~pop.do{|game| game.turns = 0; game.players.choose.jumpTo((3.rand)); }; loop({ wait(1); if(tests < 32, { if(~pop.collect{|game| game.players.collect{|player| player.stopped; }.every{|x| x} }.every{|x| x}, { ~score = ~pop.collect{|game| game.turns }; ~generation_score.do{|gameRunningTotal, index| ~generation_score[index] = gameRunningTotal ++ [~score[index]]; }; tests = tests + 1; ~pop.do{|game| game.turns = 0; game.players.choose.jumpTo((3.rand)); }; ~generation_score[0].size.postln; ~score.postln; }); }, { "done".postln; ~gen_score_avgs = ~generation_score.collect{|gRT| gRT.mean }; ~mating_pool_indeces = ~gen_score_avgs.order[28..]; ~mating_pool = ~gene_pool[~mating_pool_indeces]; ~mating_pool = ~mating_pool.collect{|game| game.flatten}; ~new_gene_pool = ~mating_pool.collect{|mom| mom.clump(13) } ++ (7.collect{ ~mating_pool.collect{|mom| mom.curdle(~mu).scramble.flatten.clump(13) } }.flatten); nil.alwaysYield; }); }); }); ~tally.play(SystemClock); ~begun = false; while({~tally.isPlaying}, { wait(0.01); }); ~routines.do{|game| game.do{|player| player.stop}}; ~file = File("/Users/josephmariglio/Documents/Classes/Spring09/tdat_demo/b/genes/"++~gen_no.asString, "w"); ~file.write(~gene_pool.asCompileString); ~file.write(~gen_score_avgs.asCompileString); ~file.close; ~gene_pool = ~new_gene_pool; ~gen_no = ~gen_no + 1; "next generation: ".post; ~gen_no.postln; }) }); ) ~generate.play(SystemClock); ~generate.stop