// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   KnapGen2GenTask.java

package lt.ktu.gmj.tasks;

import drm.genetics.*;
import drm.genetics.advanced.BestSelCrossover;
import drm.genetics.advanced.BestSelMutation;
import drm.genetics.simple.*;
import java.io.*;
import java.net.*;
import java.util.Random;
import java.util.StringTokenizer;

// Referenced classes of package lt.ktu.gmj.tasks:
//			KnapGen2Individual

public class KnapGen2GenTask extends AbstractTask
{

	protected int indCount;
	protected float object_cost[];
	protected float object_weight[];
	protected double object_ratio[];
	protected int number_of_objects;
	protected double total_weight;

	public KnapGen2GenTask(String url, double total_weight, int mutationType, double percentMutating, int mutationLevel, 
			int crossoverType, int crossoverStyle, int crossoverLevel)
	{
		indCount = 2;
		try
		{
			this.total_weight = total_weight;
			input(url);
			minWeight = (1.0D / 0.0D);
			switch(mutationType)
			{
			case 0: // '\0'
				mutation = new SingleGeneMutation();
				break;

			case 1: // '\001'
				mutation = new SimpleMutation(percentMutating);
				break;

			case 2: // '\002'
				mutation = new BestSelMutation(percentMutating, mutationLevel);
				break;
			}
			switch(crossoverType)
			{
			case 0: // '\0'
				crossover = new SimpleCrossover();
				break;

			case 1: // '\001'
				crossover = new BestSelCrossover(crossoverLevel);
				break;
			}
			switch(crossoverStyle)
			{
			case 0: // '\0'
				crossover.setType(1);
				break;

			case 1: // '\001'
				crossover.setType(2);
				break;

			case 2: // '\002'
				crossover.setType(-1);
				break;
			}
		}
		catch(Exception ex)
		{
			System.err.println("Failed on Genetics task initialization");
			ex.printStackTrace();
		}
	}

	public Individual[] createIndividuals()
	{
		Random random = new Random(12345L);
		if(indCount < 2)
			indCount = 2;
		int size = 3;
		Gene genom[] = new Gene[size];
		Individual inds[] = new Individual[indCount];
		for(int i = 0; i < indCount; i++)
		{
			for(int j = 0; j < size; j++)
				genom[j] = new DoubleGene(random.nextDouble(), 0.0D, 1.0D);

			KnapGen2Individual uin = new KnapGen2Individual(object_cost, object_weight, object_ratio, number_of_objects, total_weight);
			uin.setGenome(genom);
			inds[i] = uin;
		}

		return inds;
	}

	private void input(String url)
		throws MalformedURLException, IOException
	{
		int num = 0;
		number_of_objects = 0;
		URL tmyurl = new URL(url);
		System.out.println("Reading data from: " + tmyurl.getHost() + tmyurl.getFile());
		URLConnection tmycon = tmyurl.openConnection();
		System.out.println("Data type: " + tmycon.getContentType() + " with size: " + tmycon.getContentLength());
		InputStream tis = tmycon.getInputStream();
		BufferedReader tids = new BufferedReader(new InputStreamReader(tis));
		tids.readLine();
		String data_line;
		while((data_line = tids.readLine()) != null) 
		{
			StringTokenizer st = new StringTokenizer(data_line);
			st.nextToken();
			st.nextToken();
			number_of_objects += Integer.valueOf(st.nextToken()).intValue();
			num++;
		}
		System.out.println("Object types: " + num + " and total ammount: " + number_of_objects);
		tids.close();
		tis.close();
		object_cost = new float[number_of_objects];
		object_weight = new float[number_of_objects];
		object_ratio = new double[number_of_objects];
		URLConnection mycon = tmyurl.openConnection();
		InputStream is = mycon.getInputStream();
		BufferedReader ids = new BufferedReader(new InputStreamReader(is));
		ids.readLine();
		num = 0;
		while((data_line = ids.readLine()) != null) 
		{
			StringTokenizer st = new StringTokenizer(data_line);
			float f1 = Float.valueOf(st.nextToken()).floatValue();
			float f2 = Float.valueOf(st.nextToken()).floatValue();
			int k = Integer.valueOf(st.nextToken()).intValue();
			for(int i = 0; i < k; i++)
			{
				object_weight[num] = f1;
				object_cost[num] = f2;
				object_ratio[num] = f2 / f1;
				num++;
			}

		}
		ids.close();
		is.close();
		System.out.println("Data ready...");
	}
}
