📄 gaainversion.java
字号:
public class GaaInversion {
int gaType;
int inversionType;
double inversionRate;
public GaaInversion (GaaProblem problem) {
gaType = problem.gaType;
inversionType = problem.inversionType;
inversionRate = problem.inversionRate;
}
public String inversion(String chrom) {
String s = doInversion(chrom, inversionRate);
return(s);
}
public String inversion(String chrom, double rate) {
String s = doInversion(chrom, rate);
return(s);
}
public String doInversion(String chrom, double rate) {
int i, pos1, pos2, temp;
String s1, s2, s3, s="";
if (GaaMisc.flip(rate)) {
pos1 =(int) Math.floor((Math.random()*(chrom.length()-1)));
pos2 =(int) Math.floor((Math.random()*(chrom.length()-1)));
if (pos1 > pos2) {
temp = pos2;
pos2 = pos1;
pos1 = temp;
}
if ((pos2-pos1) > 1) {
try{
s1 = chrom.substring(0,pos1);
s2 = chrom.substring(pos1,pos2);
s3 = chrom.substring(pos2);
s2 = invertString(s2);
s = s1.concat(s2);
s = s.concat(s3);
}
catch (Exception e) {
GaaAction.deb.debug("GaaInversion Inversion error: "+e.toString());
}
}
else
s = chrom;
}
else
s = chrom;
return s;
}
String invertString(String txt) {
int i, size;
size = txt.length();
StringBuffer sbfrom = new StringBuffer(txt);
StringBuffer sbto = new StringBuffer(txt);
for (i=0;i<size;i++) {
sbto.setCharAt(i,sbfrom.charAt(size-i-1));
}
return(sbto.toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -