Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

Genetic algorithm help!

$
0
0
This is my code
and this is a picture to help me explain. The idea is to find the minimum cost between two bins. Values from the matrix are put into either bin0 or bin1 based on the Cij of the population matrix being a 0 or a 1. I then have to find the minimum resulting cost of the arcs between the two bins. the weights of the arcs are the values of the matrix which we read in, shown in the picture.

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 int total[] = new int[size];
	static final double mutationRate = .7;
	static final double crossoverRate= .8;
	static final int tournamentSize = size;
	static final int runTimes = 10;
	static int timesRan = 1;

	public static void main(String[] args){

		int cost[] = null;
		int penalty[] = null;

		long time0 = System.currentTimeMillis();
		for(int i=0; i<=runTimes; i++){
			timesRan++;
			matrix = getInput();
			fillPopulation(population);

			cost = calcCost(matrix, population);
			penalty = getPenalty(matrix,population,cost);

			mutate(population);
			crossOver(population);

			tournamentSelection(population);

		}

		for(int i=0;i<size;i++){
			total[i] = cost[i] + penalty[i];
			System.out.println(total[i]);
			}

		long time1 = System.currentTimeMillis();

		System.out.println("Total time to run " + runTimes + " is: " + (time1-time0)/60 + " seconds");
	}

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

	int randi = (int)(Math.random()*size);
	int newPopulation[][] = new int[size*timesRan][size];
	for(int i=0;i<tournamentSize;i++){
		for(int j=0; j<tournamentSize; j++){
				newPopulation[i][j] = population[randi][j];  		//copies a random row into the new population
				mutate(newPopulation);
				crossOver(newPopulation);
			}
		}
	//printArray2DI(newPopulation);
	calcCost(matrix,newPopulation);
	}

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

	for(int m=0;m<size;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);
		return cost;
	}

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

	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{
		FileReader input = new FileReader("input2.txt");
		BufferedReader in = new BufferedReader(input);
		text = in.readLine();

		while(text != null){

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

			text = in.readLine();
			}//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{
		FileReader input = new FileReader("input2.txt");
		BufferedReader in = new BufferedReader(input);
		text = in.readLine();
		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]);
					}

			text = in.readLine();
			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
}




Viewing all articles
Browse latest Browse all 2703

Trending Articles