遗传算法求解可满足性问题代码
我的毕业设计,代码贴出来,希望大家给新手点建议,java的
Gene.java//获得指派序列
- Java code
package GAforSAT;import java.util.*;/* * 获得20条基础的基因指派序列.doneorchange * 20100430 */public class Gene { public static final int VariableLength = 20;// 变元集变元的长度,也就是基因的长度 public static final int GeneLength = 30;// 指派的个数,也就是指派集数组的长度 /* * @初步获得基因序列 @30条20位的二进制数 * @基因重复和取反的优化以后再做 */ public int[][] gene() { int[][] Gene = new int[GeneLength][VariableLength]; for (int i = 0; i < GeneLength; i++) { Random ran = new Random(); for (int j = 0; j < VariableLength; j++) { if (ran.nextBoolean()) { Gene[i][j] = 0; } else Gene[i][j] = 1; } } return Gene; } /* * test */// public static void main(String args[]) {// Gene gene = new Gene();// int[][] genee= gene.gene();// for (int i = 0; i < GeneLength; i++) {// for (int j = 0; j < VariableLength; j++) {// System.out.print(genee[i][j]);// }// System.out.println();// }//// }}GeneJudge.java //判断适应性
- Java code
package GAforSAT;/* * 用每条基因(指派)去判断每条子句.done * 获得每条基因的适应性大小存入数组 */public class GeneJudge { public int[] geneJudge(int[][] gene, int[][] cluseclump) { int[] GeneJudge = new int[gene.length];// 最终真值表 int[] cluse = new int[cluseclump[0].length];// 子句文字 int[] temp = new int[cluseclump[0].length];// 文字真值表 // 子句纵坐标 for (int i = 0; i < cluseclump.length; i++) { // 子句横坐标 for (int j = 0; j < cluseclump[0].length; j++) { cluse[j] = cluseclump[i][j];// 单个子句 } // 指派纵坐标 for (int k = 0; k < gene.length; k++) { for (int m = 0; m < cluseclump[0].length; m++) { if (cluse[m] < 0) { temp[m] =1^gene[k][-cluse[m]];//和1取异或,是0则值为1,是1则值为0 } else temp[m] = gene[k][cluse[m]]; } int tem=0;//临时真值 for(int n=0;n<cluseclump[0].length;n++){ tem+=temp[n]; } if (tem != 0) { GeneJudge[k]++; } } } return GeneJudge; } /* * test */// public static void main(String args[]) {// Gene test = new Gene();// int[][] test1 = test.gene();// for(int i=0;i<30;i++){// for(int j=0;j<20;j++){// System.out.print(test1[i][j]);// // }// System.out.println();// }// ClauseClump arr=new ClauseClump();// int[][]arrr=arr.getArray();// for(int i=0;i<100;i++){// for(int j=0;j<3;j++){// System.out.print(arrr[i][j]+"\t");// }// System.out.println();// }// GeneJudge test4 = new GeneJudge();// int[] tests = test4.geneJudge(test1, arrr);// for (int i = 0; i < tests.length; i++) {// System.out.println(tests[i]);// }// // }}Genetic.java//遗传
- Java code
package GAforSAT;import java.util.*;/* * 遗传.done * @liu * 对原始基因进行遗传处理获得新的更优指派 * 30条基础指派去掉10条 * 由其他25条遗传获得10条 * 20100503 */public class Genetic { public static final int GeneticNode = 25;// 指派变异时保留的指派数 /* * 获得经过适应性排序后适应性最高的25个基因 并通过遗传获得新的10条基因 */ public int[][] geneticGene(int[][] Gene, int[][] suitable) { int[][] geneticgene = new int[Gene.length][Gene[0].length]; // 取出适应性高的20条基因 for (int i = 0; i < GeneticNode; i++) { for (int j = 0; j < Gene[0].length; j++) { geneticgene[i][j] = Gene[suitable[i][1]][j]; } } // 遗传获得另外10条基因 for (int j = GeneticNode; j < Gene.length; j++) { Random ran = new Random(); int ran1 = ran.nextInt(Gene[0].length);// 找到分结点 int gene1 = ran.nextInt(Gene[0].length); int gene2 = ran.nextInt(Gene[0].length);// 找到两条母基因 // 新基因的前半段取gene1的前半段 for (int k = 0; k < ran1; k++) { geneticgene[j][k] = geneticgene[gene1][k]; } // 新基因的后半段取gene2的后半段 for (int m = ran1; m < Gene[0].length; m++) { geneticgene[j][m] = geneticgene[gene2][m]; } } return geneticgene; } /* * test */// public static void main(String args[]) {// Gene gg = new Gene();// int[][] test = gg.gene();// int[][] test1 = { { 80, 0 }, { 79, 1 }, { 78, 2 }, { 77, 3 },// { 76, 4 }, { 75, 5 }, { 74, 6 }, { 73, 7 }, { 72, 8 },// { 71, 9 }, { 70, 10 }, { 69, 11 }, { 68, 12 }, { 67, 13 },// { 66, 14 }, { 65, 15 }, { 64, 16 }, { 62, 18 }, { 61, 19 },// { 59, 20 }, { 58, 21 }, { 57, 22 }, { 56, 23 }, { 55, 24 },// { 54, 25 }, { 53, 26 }, { 51, 27 }, { 50, 28 }, { 49, 29 }, };// Genetic tt = new Genetic();// int[][] out = tt.geneticGene(test, test1);// for (int i = 0; i < 30; i++) {// for (int j = 0; j < 20; j++) {// System.out.print(out[i][j]);// }// System.out.println();// }// }}
Suitability.java//适应性判断
- Java code
package GAforSAT;public class Suitability { /* * 将指派的适应性按大小排序 冒泡排序.done * 20100430,20100503 */ public int[][] suitablelist(int[] suitability) { /* * 一维数组二维化 得到原基因的适应性和编号的二维数组 防止在排序过程中和原来的基因不对应 */ int length = suitability.length; int[][] suitable = new int[suitability.length][2]; for (int i = 0; i < length; i++) { suitable[i][0] = suitability[i]; suitable[i][1] = i; } /* * 冒泡排序 */ for (int i = length - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (suitable[j][0] < suitable[j + 1][0]) { // 实现值交换 int temp = suitable[j][0]; suitable[j][0] = suitable[j + 1][0]; suitable[j + 1][0] = temp; // 实现同步序号交换 int seq = suitable[j][1]; suitable[j][1] = suitable[j + 1][1]; suitable[j + 1][1] = seq; } } } return suitable; } /** * test * * @param args */// public static void main(String[] args) {// int[] ss = { 2, 34, 5, 23, 45 };// Suitability suit = new Suitability();// int[][] mm = suit.suitablelist(ss);// for (int i = 0; i < ss.length; i++) {// for (int j = 0; j < 2; j++) {// System.out.print(mm[i][j] + "\t");// }// System.out.println();// }//// }}[解决办法]
genetic algorithm is one of most inefficient non-determined algorithm for SAT problems.
[解决办法]
帮顶吧 头都看大了
[解决办法]
你没说你具体是啥问题
[解决办法]
楼主什么专业啊,这毕业设计很有个性
[解决办法]
代码 老多.
帮顶.