Can you make it more efficient and paraphrase it in Java import java.lang.Math; import java.util.*; import javax.xml.stream.events.EndDocument; public class KiK { private static int boardSize = 12; private static int rand; public static Integer [] randomGenotype() { Integer [] genotype = new Integer [boardSize]; for(int i=0; i rand1) { for(int i = rand2; i > rand1+1; i-- ) { int temp = genotype[i-1]; genotype[i-1] = genotype[i]; genotype[i] = temp; } } else { for(int i = rand1; i > rand2+1; i-- ) { int temp = genotype[i-1]; genotype[i-1] = genotype[i]; genotype[i] = temp; } } } return genotype; } public static Integer[][] recombine(Integer[] parent0, Integer[] parent1) { Integer [][] children = new Integer [2][boardSize]; int HALFWAY= boardSize/2; for(int i=0; i
Can you make it more efficient and paraphrase it in Java
import java.lang.Math;
import java.util.*;
import javax.xml.stream.events.EndDocument;
public class KiK
{
private static int boardSize = 12;
private static int rand;
public static Integer [] randomGenotype()
{
Integer [] genotype = new Integer [boardSize];
for(int i=0; i<genotype.length; i++)
{
genotype[i] = 0;
}
rand = (int)(Math.random()*boardSize +1);
for(int i=0; i<genotype.length; i++)
{
while(checkArray(genotype, rand))
{
rand = (int)(Math.random()*boardSize +1);
}
genotype[i] = rand;
}
return genotype;
}
private static boolean checkArray(Integer[] arr, int num)
{
for(int i=0; i<arr.length; i++)
{
if(arr[i] == num) {return true;}
}
return false;
}
public static Integer[] insertMutate(Integer[] genotype, double p)
{
double pVal = Math.random();
if(pVal <= p)
{
int rand1 = (int)(Math.random()*boardSize);
int rand2 = (int)(Math.random()*boardSize);
while(rand1 == rand2)
{
rand1 = (int)(Math.random()*boardSize);
rand2 = (int)(Math.random()*boardSize);
}
if(rand2 > rand1)
{
for(int i = rand2; i > rand1+1; i-- )
{
int temp = genotype[i-1];
genotype[i-1] = genotype[i];
genotype[i] = temp;
}
}
else
{
for(int i = rand1; i > rand2+1; i-- )
{
int temp = genotype[i-1];
genotype[i-1] = genotype[i];
genotype[i] = temp;
}
}
}
return genotype;
}
public static Integer[][] recombine(Integer[] parent0, Integer[] parent1)
{
Integer [][] children = new Integer [2][boardSize];
int HALFWAY= boardSize/2;
for(int i=0; i<boardSize; i++)
{
children[0][i] = 0;
children[1][i] = 0;
}
for(int i=0; i<HALFWAY; i++)
{
children[0][i] = parent0[i];
children[1][i] = parent1[i];
}
for(int i=HALFWAY; i<boardSize; i++)
{
int index = HALFWAY;
while(checkArray(children[0], parent1[index]))
{
index++;
if(index == boardSize) {index = 0;}
}
children[0][i] = parent1[index];
index = HALFWAY;
while(checkArray(children[1], parent0[index]))
{
index++;
if(index == boardSize) {index = 0;}
}
children[1][i] = parent0[index];
}
return children;
}
private static int isThreatened(Integer [] genotype, int index)
{
int count = 0;
int val = genotype[index];
for(int i = 1; i<= index; i++)
{
int threat = genotype[index-i];
if(threat == val) count++;
if(threat == val - i) count++;
if(threat == val + i) count++;
}
return count;
}
public static int measureFitness(Integer [] genotype)
{
int fitness = (int) (0.5 * boardSize * (boardSize - 1));
for(int i=0 ; i<boardSize; i++)
{
for(int j=0; j< isThreatened(genotype, i); j++)
{
fitness--;
}
}
return fitness;
}
}
In this question, it is asked to improve a given java code.
For improvements, better comments are used for understanding.
Better functions are used in the java program to do tasks like filling of array more efficiently.
Step by step
Solved in 3 steps