test_binary_genotype.cpp
来自「free library of genetic algorithms」· C++ 代码 · 共 91 行
CPP
91 行
#include <iostream>#include <stdlib.h>#include <cpplibga/genotype/Binary.h>int main(void){ { typedef cpplibga::genotype::Binary<1> GT; GT b,c; if(GT::size()!=1){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} b.setGene(0,true); if(b.getGene(0)!=true){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} b.setGene(0,false); if(b.getGene(0)!=false){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} b.invertGene(0); if(b.getGene(0)!=true){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} c=b; if(!(c==b)){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} } { typedef cpplibga::genotype::Binary<13> GT; GT b,c; if(GT::size()!=13){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} for(unsigned long t=0;t<1000;t++) { unsigned long i=t%13; bool v=rand()<RAND_MAX/2; b.setGene(i,v); if(b.getGene(i)!=v){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} b.invertGene(i); if(b.getGene(i)==v){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} } c=b; if(!(c==b)){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} for(unsigned long t=0;t<1000;t++) { unsigned long i=t%13; bool v=b.getGene(i); b.setGene(i,!v); if(c==b){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} b.invertGene(i); if(!(c==b)){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} b.invertGene(i); b.setGene(i,v); if(!(c==b)){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} } } { typedef cpplibga::genotype::Binary<128> GT; GT b,c; if(GT::size()!=128){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} for(unsigned long t=0;t<1000;t++) { unsigned long i=t%128; bool v=rand()<RAND_MAX/2; b.setGene(i,v); if(b.getGene(i)!=v){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} b.invertGene(i); if(b.getGene(i)==v){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} } c=b; if(!(c==b)){std::cout<<"Error at line "<<__LINE__<<std::endl;return -1;} } std::cout<<"Ok"<<std::endl; return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?