Skip to content

Instantly share code, notes, and snippets.

@sugiartocokrowibowo
Created August 11, 2018 17:08
Show Gist options
  • Save sugiartocokrowibowo/745ddf58fa1fcf701e0a44a4f2e1e79d to your computer and use it in GitHub Desktop.
Save sugiartocokrowibowo/745ddf58fa1fcf701e0a44a4f2e1e79d to your computer and use it in GitHub Desktop.
package ga.turbin;
public class Chromosome {
//input
int n_baris;
int n_kolom;
double uo;
private int[] n_turbin_col;
public int n_turbin;
double p_turbin;
//private double[] sigma_p_col;
private double sigmaP;
private double cost;
double ct = 0.88;
double r = 40;
double h = 60;
double zo = 0.33;
double x = 200;
private double[][] X = null;
private double[][] U = null;
private double[][] P = null;
//output
public double fitness;
public int[][] values;
public Chromosome(int n_baris, int n_kolom, double uo, double p_turbin) {
super();
this.n_baris = n_baris;
this.n_kolom = n_kolom;
this.uo = uo;
this.p_turbin = p_turbin;
this.values = new int[n_baris][n_kolom];
//generateChromosome();
}
public Chromosome(int n_baris, int n_kolom, double uo, double p_turbin, int[][] values) {
super();
this.n_baris = n_baris;
this.n_kolom = n_kolom;
this.uo = uo;
this.p_turbin = p_turbin;
this.values = values.clone();
}
public void generateChromosome() {
for (int i = 0; i < this.values.length; i++) {
for (int j = 0; j < this.values[i].length; j++) {
this.values[i][j] = random();
}
}
evaluate();
}
public Chromosome[] crossover(Chromosome p, int[] mask) {
Chromosome[] result = new Chromosome[2];
Chromosome p1 = this;
Chromosome p2 = p;
//int n_baris = p1.n_baris;
//int n_kolom = p1.n_kolom;
//double uo = p1.uo;
//double p_turbin = p1.p_turbin;
result[0] = new Chromosome(n_baris, n_kolom, uo, p_turbin);
result[1] = new Chromosome(n_baris, n_kolom, uo, p_turbin);
int k = 0;
for (int i = 0; i < p1.values.length; i++) {
for (int j = 0; j < p1.values[i].length; j++) {
if (mask[k] == 0) {
result[0].values[i][j] = p1.values[i][j];
result[1].values[i][j] = p2.values[i][j];
} else {
result[0].values[i][j] = p2.values[i][j];
result[1].values[i][j] = p1.values[i][j];
}
k++;
}
}
result[0].evaluate();
result[1].evaluate();
return result;
}
public void evaluate() {
//validate
this.X = null;
this.U = null;
this.P = null;
boolean isValid = true;
if (this.values == null || this.values.length != this.n_baris || this.values[0].length != this.n_kolom) {
isValid = false;
}
if (isValid) {
this.n_turbin = 0;
this.X = new double[this.n_baris][this.n_kolom];
double a = 0.5 * (1 - Math.sqrt(1.0 - ct));
double y = h / zo;
double alpha = 0.5 / Math.log(y);
double R1 = r * Math.sqrt((1.0 - a) / (1.0 - 2.0 * a));
this.U = new double[this.n_baris][this.n_kolom];
this.P = new double[this.n_baris][this.n_kolom];
this.sigmaP = 0;
//Hutung: n_turbin, X, U, P
for (int i = 0; i < this.values.length; i++) {
boolean isTurbinTerdepan = true;
int step = 0;
for (int j = 0; j < this.values[i].length; j++) {
step++;
if (this.values[i][j] == 1) {
this.n_turbin += 1;
if (isTurbinTerdepan) {
isTurbinTerdepan = false;
// //alternatif 1
// if(j==0){
// this.U[i][j] = uo;
// }else{
// this.X[i][j] = (step) * this.x;
// this.U[i][j] = uo * (1 - ((2 * a) / Math.pow((1 + (alpha * (X[i][j] / R1))), 2)));
// }
// //end of alternatif 1
//alternatif 2
this.U[i][j] = uo;
//end of alternatif 2
step = 0;
} else {
//step++;
this.X[i][j] = step * this.x;
step = 0;
this.U[i][j] = uo * (1 - ((2 * a) / Math.pow((1 + (alpha * (X[i][j] / R1))), 2)));
}
P[i][j] = this.p_turbin * Math.pow(this.U[i][j], 3);
this.sigmaP += P[i][j];
} else {
X[i][j] = 0;
U[i][j] = 0;
P[i][j] = 0;
}
}
}
//Hitung Cost
double kt = -0.00174;
this.cost = this.n_turbin * ((2.0 / 3.0) + ((1.0 / 3.0) * Math.exp(kt * (Math.pow(this.n_turbin, 2.0)))));
//Hitung Fitness
this.fitness = this.cost / this.sigmaP;
}
}
// public void evaluate() {
// //validate
// this.X = null;
// this.U = null;
// this.P = null;
// boolean isValid = true;
// if (this.values == null || this.values.length != this.n_baris || this.values[0].length != this.n_kolom) {
// isValid = false;
// }
//
// if (isValid) {
// this.n_turbin = 0;
// this.X = new double[this.n_baris][this.n_kolom];
//
// double a = 0.5 * (1 - Math.sqrt(1.0 - ct));
// double y = h / zo;
// double alpha = 0.5 / Math.log(y);
// double R1 = r * Math.sqrt((1.0 - a) / (1.0 - 2.0 * a));
// this.U = new double[this.n_baris][this.n_kolom];
//
// this.P = new double[this.n_baris][this.n_kolom];
// this.sigmaP = 0;
//
// //Hutung: n_turbin, X, U, P
// for (int i = 0; i < this.values.length; i++) {
// boolean isTurbinTerdepan = true;
// int step = 0;
// for (int j = 0; j < this.values[i].length; j++) {
// if (this.values[i][j] == 1) {
// this.n_turbin += 1;
// if (isTurbinTerdepan) {
// isTurbinTerdepan = false;
// this.U[i][j] = uo;
// } else {
// step++;
// this.X[i][j] = step * this.x;
// step = 0;
// this.U[i][j] = uo * (1 - ((2 * a) / Math.pow((1 + (alpha * (X[i][j] / R1))), 2)));;
// }
// P[i][j] = this.p_turbin * Math.pow(this.U[i][j], 3);
// this.sigmaP += P[i][j];
// } else {
// X[i][j] = 0;
// U[i][j] = 0;
// P[i][j] = 0;
// }
// }
// }
//
// //Hitung Cost
// double kt = -0.00174;
// this.cost = this.n_turbin * ((2.0 / 3.0) + ((1.0 / 3.0) * Math.exp(kt * (Math.pow(this.n_turbin, 2.0)))));
//
// //Hitung Fitness
// this.fitness = this.cost / this.sigmaP;
// }
// }
public void cetakChromosom() {
System.out.println("Chromosome ");
for (int i = 0; i < this.values.length; i++) {
for (int j = 0; j < this.values[i].length; j++) {
System.out.print(this.values[i][j] + " ");
}
System.out.println();
}
System.out.println("Fitness Chromosome: " + this.fitness + "\n");
}
public void cetakNTurbinCol() {
for (int j = 0; j < this.n_turbin_col.length; j++) {
System.out.print(this.n_turbin_col[j] + " ");
}
System.out.println();
}
public void cetakColomDouble(double[] col) {
for (int j = 0; j < col.length; j++) {
System.out.print(col[j] + " ");
}
System.out.println();
}
private int random() {
int result = 0;
if (Math.random() < 0.5) {
result = 1;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment