Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Genetic Algorithm Cost Fitness Function help
New Posts  All Forums:Forum Nav:

# Genetic Algorithm Cost Fitness Function help

Hello,

I am writing a genetic algorithm and I need help with my fitness algorithm. Basically I have a 2D population array of 1's and 0's and a 2D array which I need to perform calculations on, the Matrix read in is an adjacency matrix and the goal is to put the elements of the matrix into two bins then to calculate the minimum number of interconnects.

So far this is my code for this algorithm and I'm just stuck.
Warning: Spoiler! (Click to show)
Code:
``````import java.io.*;
import javax.swing.*;
import java.util.*;

public class Lab02Test{

static final int size = getSize();
static int matrix[][] = new int[size][size];
//static int population[][] = new int[size][size];
static int population[][] = {
{0,1,0,0},
{0,1,0,1},
{0,0,0,1},
{1,1,0,1}
};
static final double mutationRate = .7;
static final double crossoverRate= .8;

public static void main(String[] args){
matrix = getInput();
//fillPopulation(population);

calcCost(matrix, population);
System.out.println();

int penalty[] = getPenalty(matrix, population);

printArray2DI(matrix);
System.out.println();

printArray2DI(population);

//mutate(population);

//crossOver(population);
}

public static void calcCost(int[][] matrix, int[][] population){
int cost[] = new int[size];

for(int m=0;m<1;m++){
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
System.out.println(m + "\t" + i + "\t" + j);
if(population[i][j] == 1){
cost[m] += matrix[i][j];
}
}//end for 3
}// end for 2
}//end for 1
printArrayI(cost);
}

public static int[] getPenalty(int[][] matrix, int[][] population){

int penalty[] = new int[size];
int bin0 = 0;
int bin1 = 0;

for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
if(population[i][j] == 1){
bin1++;
}else{
bin0++;
}//end else
}//end for 2
penalty[i] = calcPenalty(bin1,bin0);
//System.out.println("Bin0 " + bin0 + " Bin1 " + bin1);
bin1=0;
bin0=0;
}//end for 1

//printArrayI(penalty);
return penalty;
}

public static int calcPenalty(int bin0, int bin1){
int num = Math.abs(2*(bin0-bin1));
return num;
}

public static void fillPopulation(int pop[][]){

for(int i=0; i<size;i++){
for(int j =0;j<size;j++){
double rand = Math.random();
if(rand > .5){
population[i][j] = 1;
}else{
population[i][j] = 0;
}
}
}
}//end fillPopulation

public static void mutate(int[][] population){

for(int i=0; i<size;i++){
for(int j=0; j<size;j++){
double rand = Math.random();
if(rand > mutationRate){
if(population[i][j] == 1){
population[i][j] = 0;
}else{
population[i][j] = 1;
}
}
}
}
}

public static void crossOver(int[][] population){

int temp;
int split = (int)(Math.random()*(size/2));

for(int i=0; i<split; i++){
for(int j=0; j<split;j++){
temp = population[i][j];
population[i][j] = population[i+1][j+1];
population[i+1][j+1] = temp;
}
}
}

public static void printArrayI(int array[]){
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}//end printArray

public static void printArray2DI(int array[][]){
for(int i=0;i<array.length;i++){
for(int j=0;j<array.length;j++){
System.out.print(array[i][j]);
}
System.out.println();
}
System.out.println();
}// end printArray2DI

public static int getSize(){
int size = 0;
String text;

try{

while(text != null){

//System.out.println(text);
size++;

}//end while

}catch(IOException e){
System.out.println(e);
}//end try catch

return size;

}//end getSize

public static int[][] getInput(){

String text;
String tokens[];
int array[][] = new int[size][size];

try{
int row = 0;

while(text != null){

//System.out.println(text);
tokens = text.split("\\s+");

for(int j=0;j<size;j++){
array[row][j] = parseInt(tokens[j]);
}

row++;
}//end while

//printArray(array);

}catch(IOException e){
System.out.println(e);
}//end try catch

return array;

}//end getInput

public static int parseInt(String string){
int number;

//System.out.println(string);

try{
number = Integer.parseInt(string);
}catch(NumberFormatException e){
number = -1;
}

return number;
}//end parseInt
}```
```

If you could even just show me some pseudo code or a few words of wisdom that would be great. I've tried google but to no avail.
 My Rig (13 items)
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced
 My Rig (13 items)
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced
I see your problem. You declared populations as a 2D array, but tried to put four dimensions in it.
Edited by cuad - 2/17/13 at 6:40pm
 sinep (11 items)
CPUMotherboardGraphicsRAM
i7 930 GA-X58A-UD3R MSI GTX 650Ti Corsair XMS3
Hard DriveOSMonitorKeyboard
Seagate 4TB + Crucial M4 64-bit Windows 7 Home Premium Asus VG248QE </3 Sidewinder X4
Wheel Mouse Optical Sideways Puretrak Talent Focusrite Scarlett 2i2 + Red Sony MDRZX300
 sinep (11 items)
CPUMotherboardGraphicsRAM
i7 930 GA-X58A-UD3R MSI GTX 650Ti Corsair XMS3
Hard DriveOSMonitorKeyboard
Seagate 4TB + Crucial M4 64-bit Windows 7 Home Premium Asus VG248QE </3 Sidewinder X4
Wheel Mouse Optical Sideways Puretrak Talent Focusrite Scarlett 2i2 + Red Sony MDRZX300
im using 3 for loops one to keep track of where I am over all and the inner two to compare the elements of the array, that was intentional. I just cant seem to get the right response.
 My Rig (13 items)
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced
 My Rig (13 items)
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced
New Posts  All Forums:Forum Nav:
Return Home
Back to Forum: Coding and Programming
• Genetic Algorithm Cost Fitness Function help
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Genetic Algorithm Cost Fitness Function help