• PUDN用户
    了解作者
  • Java
    开发工具
  • 2KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 13
    下载次数
  • 2010-04-17 06:46
    上传日期
Prisoner s Dilemma problem solved using genetic algorithms
DilemaPrisioneroGen.java.zip
  • DilemaPrisioneroGen.java
    5.9KB
内容介绍
public class DilemaPrisioneroGen { int memory; int fear; int populationSize = 3500; int geneSize = 16; short[][] population; short[][] newPopulation; int generations = 350; int[] fitness = new int[generations]; int[] ones = new int[generations]; int genFitness = 0; int onesInChallenge = 0; boolean startEvolution(){ population = new short[populationSize][geneSize]; newPopulation = new short[populationSize][geneSize]; for(int i = 0;i<populationSize;i++){ for(int x = 0; x<geneSize; x++){ population[i][x]=((short)(Math.round(Math.random()*1))); } } for(int i = 0; i<generations; i++){ onesInChallenge = 0; evalPopulation(i); ones[i] = onesInChallenge; for(int x = 0; x<populationSize; x++){ for(int y = 0; y<geneSize; y++){ System.out.print(String.valueOf(population[x][y]+0)); } System.out.println(); } } /*for(int i = 0; i<generations; i++){ System.out.println(fitness[i]); } System.out.println(); for(int i = 0; i<generations; i++){ System.out.println(ones[i]); }*/ return true; } void evalPopulation(int idGeneration){ //new population generated from two selected parents and all parents genFitness = 0; short[][] selected = new short[2][16]; for(int i = 0;i<(populationSize/2);i+=2){ selected =compete(population[i*2],population[(i*2)+1],population[(i*2)+2],population[(i*2)+3]); newPopulation[i]= selected[0]; newPopulation[i+1] = selected[1]; } fitness[idGeneration] = genFitness; int Parent1, Parent2, Parent3, Parent4; for(int i = 0;i<(populationSize/4);i++){ Parent1 = (int)(Math.random()*(populationSize/2)); Parent2 = (int)(Math.random()*(populationSize/2)); Parent3 = (int)(Math.random()*(populationSize/2)); Parent4 = (int)(Math.random()*(populationSize/2)); newPopulation[(populationSize/2)+(i*2)]=son1(newPopulation[Parent1],newPopulation[Parent2]); newPopulation[(populationSize/2)+(i*2)+1]=son2(newPopulation[Parent3],newPopulation[Parent4]); } population = newPopulation; } //Today no mutation is implemented short[] son2 (short[] parent1, short[] parent2){ short[] son = new short[parent1.length]; int partition = (int)(parent1.length*Math.random()); int i; for(i = 0; i<partition; i++){ if(Math.random()<0.002) son[i] = (short)(parent2[i]==0?1:0); else son[i] = parent2[i]; } for(; i<parent2.length; i++){ if(Math.random()<0.002) son[i] = (short)(parent1[i]==0?1:0); else son[i] = parent1[i]; } return son; } short[] son1 (short[] parent1, short[] parent2){ short[] son = new short[parent1.length]; int partition = (int)(parent1.length*Math.random()); int i; for(i = 0; i<partition; i++){ if(Math.random()<0.002) son[i] = (short)(parent1[i]==0?1:0); else son[i] = parent1[i]; } for(; i<parent2.length; i++){ if(Math.random()<0.002) son[i] = (short)(parent2[i]==0?1:0); else son[i] = parent2[i]; } return son; } short[][] compete(short[] prisioner1, short[] prisioner2,short[] prisioner3, short[] prisioner4){ short p1Turn0 = ((short)(Math.round(Math.random()*1))); short p2Turn0 = ((short)(Math.round(Math.random()*1))); short p1Turn1 = ((short)(Math.round(Math.random()*1))); short p2Turn1 = ((short)(Math.round(Math.random()*1))); short p3Turn0 = ((short)(Math.round(Math.random()*1))); short p4Turn0 = ((short)(Math.round(Math.random()*1))); short p3Turn1 = ((short)(Math.round(Math.random()*1))); short p4Turn1 = ((short)(Math.round(Math.random()*1))); short[][] selected = new short[2][16]; int p1Years = 0; int p2Years = 0; int p3Years = 0; int p4Years = 0; int plays = 100; for(int i = 0; i<plays; i++){ int currentTurn = 0; int last2Turns = p1Turn0*8+p2Turn0*4+p1Turn1*2+p2Turn1; currentTurn=prisioner1[last2Turns]*2+prisioner2[last2Turns]; switch(currentTurn){ case 0: p1Years +=1; p2Years +=1; break; case 1: p1Years +=0; p2Years +=5; break; case 2: p1Years +=5; p2Years +=0; break; default: p1Years +=1; p2Years +=1; break; } p1Turn0 = p1Turn1; p2Turn0 = p2Turn1; p1Turn1 = prisioner1[last2Turns]; p2Turn1 = prisioner2[last2Turns]; } for(int i = 0; i<plays; i++){ int currentTurn = 0; int last2Turns = p3Turn0*8+p4Turn0*4+p3Turn1*2+p4Turn1; currentTurn=prisioner3[last2Turns]*2+prisioner4[last2Turns]; switch(currentTurn){ case 0: p3Years +=1; p4Years +=1; break; case 1: p3Years +=0; p4Years +=5; break; case 2: p3Years +=5; p4Years +=0; break; default: p3Years +=1; p4Years +=1; break; } p3Turn0 = p3Turn1; p4Turn0 = p4Turn1; p3Turn1 = prisioner3[last2Turns]; p4Turn1 = prisioner4[last2Turns]; } int pair1 = p1Years>p2Years?p1Years-p2Years:p2Years-p1Years; int pair2 = p3Years>p4Years?p3Years-p4Years:p4Years-p3Years; genFitness += pair1; genFitness += pair2; for(int x = 0; x<geneSize; x++) onesInChallenge += prisioner1[x]; for(int x = 0; x<geneSize; x++) onesInChallenge += prisioner2[x]; //Seleccionamos al par con menor diferencia entre los jugadores if(pair1>pair2){ selected[0]=prisioner3; selected[1]=prisioner4; return selected; } else{ selected[0]=prisioner1; selected[1]=prisioner2; return selected; } } public static void main(String[] args){ //En cada turno 0 significa Guarda silencio y 1 significa delata al contrario. //Si p1Turn == 0 && p2Turn == 0 p1 y p2 ganan 3 puntos, //si p1Turn == 0 && p2Turn == 1, p1 gana 0 y p2 gana 5. //Si p1Turn == 1 y p2Turn == 0, p1 gana 5 y p2 gana 0. //Si p1Turn == 1 && p2Turn == 1, p1 y p2 ganan 1. DilemaPrisioneroGen prisioner = new DilemaPrisioneroGen(); prisioner.memory = 5; prisioner.fear = 1; prisioner.startEvolution(); //Generamos un numero x de soluciones al problema que competir�n unas con otras hasta obtener la mejor } }
评论
    相关推荐
    • matlabcnhelp.rar
      matlab中文帮助很难找的,快速下载
    • MobilePolice.rar
      移动警察,车牌识别,车牌定位系统源代码,已经运用在移动车载稽查系统中。
    • SVM(matlab).rar
      支持向量机(SVM)实现的分类算法源码[matlab]
    • svm.zip
      用MATLAB编写的svm源程序,可以实现支持向量机,用于特征分类或提取
    • Classification-MatLab-Toolbox.rar
      模式识别matlab工具箱,包括SVM,ICA,PCA,NN等等模式识别算法,很有参考价值
    • VC++人脸定位实例.rar
      一个经典的人脸识别算法实例,提供人脸五官定位具体算法及两种实现流程.
    • QPSK_Simulink.rar
      QPSK的Matlab/Simulink的调制解调仿真系统,给出接收信号眼图及系统仿真误码率,包含载波恢复,匹配滤波,定时恢复等重要模块,帮助理解QPSK的系统
    • LPRBPDemo2009KV.rar
      车牌识别,神经网络算法,识别率高达95%,识别时间低于80ms。
    • MODULATION.RAR
      这个源程序代码包提供了通信系统中BPSK,QPSK,OQPSK,MSK,MSK2,GMSK,QAM,QAM16等调制解调方式 用matlab的实现,以及它们在AWGN和Rayleigh信道下的通信系统实现及误码率性能
    • algorithms.rar
      十大算法论文,包括遗传算法,模拟退火,蒙特卡罗法等等,对于初学者很有帮助!!