I want the objects of this class:
public class Chromosome implements Runnable, Comparable<Chromosome> {
private String[] chromosome;
public double fitness;
private Random chromoGen;
public Chromosome(double[] candidate) {
super();
//encode candidate PER parameter; using Matrix as storage
chromosome = encode(candidate);
chromoGen = new Random();
}
//De-fault
public Chromosome() {
super();
chromoGen = new Random();
//de-fault genotype
chromosome = new String[6];
}
/**
* IMPLEMENTED
*/
public void run() {
//I hope leaving it empty works...
}
public int compareTo(Chromosome c) {
return (int) (fitness - c.fitness);
}
/**
* Fitness stored in chromosome!
*/
public void setFitness(ArrayList<double[]> target) {
fitness = FF.fitness(this, target);
}
public double getFitness() {
return fitness;
}
/**
* ENCODERS/DECODERS
*/
public String[] encode(double[] solution) {
//subtract 2^n from solution until you reach 2^-n
/**
* LENGTH: 51 BITS!!
*
* 1st BIT IS NEGATIVE/POSITIVE
*
* THE PRECISION IS [2^30 <-> 2^-20]!!!
*
* RANGE: -2.14748...*10^9 <-> 2.14748...*10^9
*/
String[] encoded = new String[6];
//PER PARAMETER
for (int j = 0; (j < 6); j++) {
encoded[j] = encode(solution[j]);
}
return encoded;
}
public String encode(double sol) {
/**
* THE PRECISION IS [2^30 <-> 2^-20]!!!
*/
double temp = sol;
String row = "";
//NEGATIVITY CHECK
if (temp < 0) {
//negative
row = "1";
} else {
//positive
row = "0";
}
//main seq.
for (int n = 30; (n > (-21)); n--) {
if ((temp - Math.pow(2, n)) >= 0) {
temp = temp - Math.pow(2, n);
row = row + "1";
} else {
row = row + "0";
}
}
return row;
}
public double decoded(int position) {
//returns UN-ENCODED solution
double decoded = 0.00;
char[] encoded = (chromosome[position]).toCharArray();
/**
* [n?][<--int:30-->][.][<--ratio:20-->]
*/
int n = 30;
for (int i = 1; (i < encoded.length); i++) {
if (encoded[i] == '1') {
decoded += Math.pow(2, n);
}
//next binary-place
n--;
}
//NEGATIVE??
if (encoded[0] == '1') {
decoded = ((-1) * decoded);
}
//Output
return decoded;
}
/**
* GETTERS
* ---------------\/--REDUNDANT!!
*/
public double getParameter(int parameter) {
//decoded solution
return decoded(parameter);
}
/**
* Used in E-algrm.
*/
public String getRow(int row) {
//encoded solution
return chromosome[row];
}
/**
* SETTERS
*/
public void setRow(String encoded, int row) {
chromosome[row] = encoded;
}
public void setRow(double decoded, int row) {
chromosome[row] = encode(decoded);
}
/**
* MUTATIONS
*/
public void mutate(double mutationRate) {
//range of: 51
double ran = 0;
int r;
char[] temp;
for (int m = 0; (m < 6); m++) {
temp = (chromosome[m]).toCharArray();
ran = chromoGen.nextDouble();
if (ran <= mutationRate) {
r = chromoGen.nextInt(51);
if (temp[r] == '1') {
temp[r] = '0';
} else {
temp[r] = '1';
}
}
//output
chromosome[m] = new String(temp);
}
}
}
...To be in SEPARATE threads; however I have no need for the method run()
; but when I try to do this:
child1 = new Chromosome();
(new Thread(child1)).start();
Still, the only thread I see at run-time is main()
.
So, how can I make it separate threads??
run
is method that is called bystart
when the Thread becomes activerun()
method because it defines what the runnable / thread should actually do.