| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1193 人关注过本帖
标题:量子进化算法程序问题
只看楼主 加入收藏
xiaolong0626
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-10-25
收藏
 问题点数:0 回复次数:0 
量子进化算法程序问题
这段量子进化算法程序参数设置:种群大小为30,量子位数目为18.想请教下中间红线加粗部分一大段关于qbits[i][]的代码是什么意思 起什么作用?
import *;
import java.util.Date;


/*
 * 始终用全局最优更新
 *
 * 种群大小为30
 * Max_Generations 最大进化代数为30代
 * maxRun 运行次数为30次
 */
public class Main_GlobalPSystemEvolver {
    double com=1/Math.sqrt(2.0);//把根号2分之1的值赋给com
    double[][] QBits=new double[60][18];//一个量子比特中有一对复数,种群有30代 ,需要60个复数
    double aQBit[][]=new double[2][18];//声明2个二维数组并初始化,初始值全部为0;
    int qbitCode[][]=new int[30][18];//种群大小30,量子位数目18,定义量子比特编码为整型
    int fitness[]=new int[30];//初始化一维数组,规定数组大小为30;
    int i=0,j=0;
    static int maxRun=30;
    static int SUCCESS=0;
    static int FAIL=0;
    static int totalGenerations=0;
    int currentGenerations=0;
    int Max_Generations=30;
    int BestFitness;
    int con_fitness;//历史最优适应值
    int con_individual[]=new int[18];
    int index;
    int randPN;
    double rand;
        
    String BestRule;
   
    Quantum_Update update;
    PSystem_FitnessFunction psf;
    PSystem_Individual psi=new PSystem_Individual();
    //PrintStream outputFitness = null;
   
    public Main_GlobalPSystemEvolver(){
        
    }
    //量子位初始化
    public void Initial(){
        for(i=0;i<60;i++){
            for(j=0;j<18;j++){
                rand=Math.random();//产生随机数,可返回0到1之间的一个随机数
                if (rand<0.5)
                    randPN=1;
                else
                    randPN=-1;
                QBits[i][j]=randPN*com;//随机为正负根号2分之1,为量子比特中两个分量的值
            }
        }
    }
    //量子观测
    public void QBitChange(double[][] bits){//定义了二维数组变量比特位:bits
        int k=0;
        for(i=0;i<bits.length;i++){//以行进行循环
            if (i%2==0){
                for(j=0;j<bits[i].length;j++){//以每行的列数循环
                    rand=Math.random();//两个for循环取得二维数组中的每一个值
                    if(rand<(bits[i][j]*bits[i][j]))
                        qbitCode[k][j]=0;
                    else
                        qbitCode[k][j]=1;//对量子比特进行二进制编码
                }
                k=k+1;
            }
            else
                continue;
        }
    }
    //确定性
    public void QBitDeterminate(int[][] qbits){//定义了二维数组qbits
        for(i=0;i<qbits.length;i++){
            if(qbits[i][0]+qbits[i][3]==2){//两个for循环取得二维数组中的每一个值
                rand=Math.random();//产生随机数
                if(rand<=0.5)
                    qbits[i][0]=0;
                else
                    qbits[i][0]=1;//二进制编码
                qbits[i][3]=1-qbits[i][0];   
            }
            

            if(qbits[i][1]+qbits[i][14]==2){
                rand=Math.random();
                if(rand<=0.5)
                    qbits[i][14]=0;
                else
                    qbits[i][14]=1;
                qbits[i][1]=1-qbits[i][14];
            }
            
            if(qbits[i][2]+qbits[i][15]==2){
                rand=Math.random();
                if(rand<=0.5)
                    qbits[i][15]=0;
                else
                    qbits[i][15]=1;
                qbits[i][2]=1-qbits[i][15];
            }
            
            if(qbits[i][4]+qbits[i][8]==2){
                rand=Math.random();
                if(rand<=0.5)
                    qbits[i][4]=0;
                else
                    qbits[i][4]=1;
                qbits[i][8]=1-qbits[i][4];
            }
            
            if(qbits[i][5]+qbits[i][6]+qbits[i][9]==3){
                rand=Math.random();
                if(rand<=1.0/3){
                    qbits[i][5]=1;
                    qbits[i][6]=0;
                    qbits[i][9]=0;
                }   
                else{
                    if(rand>2.0/3){
                        qbits[i][5]=0;
                        qbits[i][6]=0;
                        qbits[i][9]=1;
                    }
                    else{
                        qbits[i][5]=0;
                        qbits[i][6]=1;
                        qbits[i][9]=0;
                    }   
                }
            }
            else{
                if(qbits[i][5]+qbits[i][6]==2){
                    rand=Math.random();
                    if(rand<=0.5)
                        qbits[i][5]=0;
                    else
                        qbits[i][5]=1;
                    qbits[i][6]=1-qbits[i][5];
                }
                if(qbits[i][5]+qbits[i][9]==2){
                    rand=Math.random();
                    if(rand<=0.5)
                        qbits[i][5]=0;
                    else
                        qbits[i][5]=1;
                    qbits[i][9]=1-qbits[i][5];
                }
                if(qbits[i][6]+qbits[i][9]==2){
                    rand=Math.random();
                    if(rand<=0.5)
                        qbits[i][6]=0;
                    else
                        qbits[i][6]=1;
                    qbits[i][9]=1-qbits[i][6];
                }
            }
            
            if(qbits[i][7]+qbits[i][10]==2){
                rand=Math.random();
                if(rand<=0.5)
                    qbits[i][7]=0;
                else
                    qbits[i][7]=1;
                qbits[i][10]=1-qbits[i][7];
            }
            
            if(qbits[i][11]+qbits[i][16]+qbits[i][17]==3){
                rand=Math.random();
                if(rand<=1.0/3){
                    qbits[i][11]=1;
                    qbits[i][16]=0;
                    qbits[i][17]=0;
                }   
                else{
                    if(rand>2.0/3){
                        qbits[i][11]=0;
                        qbits[i][16]=0;
                        qbits[i][17]=1;
                    }
                    else{
                        qbits[i][11]=0;
                        qbits[i][16]=1;
                        qbits[i][17]=0;
                    }   
                }
            }
            else{
                if(qbits[i][11]+qbits[i][16]==2){
                    rand=Math.random();
                    if(rand<=0.5)
                        qbits[i][11]=0;
                    else
                        qbits[i][11]=1;
                    qbits[i][16]=1-qbits[i][11];
                }
                if(qbits[i][11]+qbits[i][17]==2){
                    rand=Math.random();
                    if(rand<=0.5)
                        qbits[i][11]=0;
                    else
                        qbits[i][11]=1;
                    qbits[i][17]=1-qbits[i][11];
                }
                if(qbits[i][16]+qbits[i][17]==2){
                    rand=Math.random();
                    if(rand<=0.5)
                        qbits[i][16]=0;
                    else
                        qbits[i][16]=1;
                    qbits[i][17]=1-qbits[i][16];
                }
            }
        }
    }

   

//获得一维数组arr[]中所有元素的最小值,返回最小值及其索引位置
    public int min(int[] arr){  
        int m=arr[0];
        for (i=1; i< arr.length; i++){
            if (arr[i]<m){
                m=arr[i];
                index=i;
            }
        }
        return m;
    }
   
    public void Run(){
      
        int iGenerations=0;
        
        Initial();
        QBitChange(QBits);
        QBitDeterminate(qbitCode);
        
        
        //获得各个个体的适应值
        for(i=0;i<qbitCode.length;i++){
            psf=new PSystem_FitnessFunction(qbitCode[i]);
            fitness[i]=psf.fitnessValue;
            
        }
        
            
        BestFitness=min(fitness);        
        con_fitness=BestFitness;
        for(i=0;i<18;i++){
            con_individual[i]=qbitCode[index][i];
        }   
        
        while(iGenerations<(Max_Generations-1)&&BestFitness>0){
            iGenerations=iGenerations+1;
            
            for (j=0; j<30;j++){
                aQBit[0]=QBits[2*j];
                aQBit[1]=QBits[2*j+1];
                //量子更新
                update=new Quantum_Update(con_individual,qbitCode[j],aQBit,con_fitness,fitness[j]);
                QBits[2*j]=update.newQBit[0];
                QBits[2*j+1]=update.newQBit[1];
            }
            QBitChange(QBits);
            QBitDeterminate(qbitCode);
            for(i=0;i<qbitCode.length;i++){
                //fileLength=fil.length();
                psf=new PSystem_FitnessFunction(qbitCode[i]);
                fitness[i]=psf.fitnessValue;
            }
            
            
            BestFitness=min(fitness);
            if(BestFitness<=con_fitness){
                con_fitness=fitness[index];
                for(i=0;i<18;i++){
                    con_individual[i]=qbitCode[index][i];
                }
            }
        }
        
        //获取最优规则集
        BestRule=psi.getRule(con_individual);
        currentGenerations=iGenerations;
        totalGenerations+=currentGenerations;
   
    }
   
    public static void main(String args[]){
        Main_GlobalPSystemEvolver evo=new Main_GlobalPSystemEvolver();
        Date d1 = new Date();
        System.out.println(d1.toString());
        
        PrintStream outputFile = null;
        try {
            outputFile = new PrintStream(new FileOutputStream(
                    "D:\\java\\workspace\\SquareOfFour\\Results.txt", true));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        
        for(int r=0;r<maxRun;r++){
            
            evo.Run();
            
            if (evo.BestFitness==0)
                SUCCESS++;
            else
                FAIL++;
            outputFile.println("===================================");
            outputFile.println("BEGIN RUN:\t"+(r+1)+"\nGenerations="+evo.currentGenerations+"\tBestFitness="+
                    evo.BestFitness+"\nBestRule:\n"+evo.BestRule);
            
        }
        
        Date d2 = new Date();
        int seconds = (int) ((d2.getTime() - d1.getTime()) / 1000);
        
        outputFile.println("\nTotalGenerations:"+totalGenerations+"\nSuccess:"+SUCCESS+"\nFail:"+
                FAIL+"\nDuration: " + (seconds / 60) + " minutes "+ (seconds % 60) + " seconds \n");
        
        outputFile.close();
        
    }

}
搜索更多相关主题的帖子: public double import 比特 
2016-10-25 23:07
快速回复:量子进化算法程序问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044120 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved