// 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:   KnapSackGen.java

package lt.ktu.gmj.tasks;

import java.io.*;
import java.net.*;
import java.util.Random;
import java.util.StringTokenizer;
import javax.swing.JApplet;
import lt.ktu.HostNameRetriever;
import lt.ktu.gmj.*;
import lt.ktu.gmj.propertySheet.*;

// Referenced classes of package lt.ktu.gmj.tasks:
//			Genetic, KnapSackGenDomain, KnapGenWeightProvider, KnapIterationsProvider, 
//			KnapGenDataUrlProvider

public class KnapSackGen
	implements Task
{

	private double norm_point[];
	private int number_of_objects;
	public double total_weight;
	public int Iterations;
	public int Initialpopulation;
	private Genetic G;
	private float object_cost[];
	private float object_weight[];
	private double Wealth[];
	private Random rnd;
	public String data_url;
	private String data_url_old;
	private KnapSackGenDomain mydomain;

	private void input()
		throws MalformedURLException, IOException
	{
		int num = 0;
		number_of_objects = 0;
		URL tmyurl = new URL(data_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];
		Wealth = new double[Initialpopulation];
		G = new Genetic(number_of_objects, Initialpopulation);
		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;
				num++;
			}

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

	private double calculation(Point pt)
	{
		Initialpopulation = (int)pt.x[1] >= 2 ? (int)pt.x[1] : 2;
		Wealth = new double[Initialpopulation];
		G = new Genetic(number_of_objects, Initialpopulation);
		G.generate();
		for(int i = 0; i < Iterations; i++)
		{
			evaluate();
			G.reproduction(Wealth);
			for(int j = 0; j < Initialpopulation; j++)
			{
				double prob = rnd.nextDouble();
				if(prob <= pt.x[0])
					G.cross(j);
				else
					G.mutation(j);
			}

		}

		evaluate();
		double cost = 0.0D;
		for(int i = 0; i < Initialpopulation; i++)
			if(cost < Wealth[i])
				cost = Wealth[i];

		return -cost;
	}

	private void evaluate()
	{
		for(int i = 0; i < Initialpopulation; i++)
		{
			double sum_cost = 0.0D;
			double sum_weight = 0.0D;
			for(int j = 0; j < number_of_objects; j++)
				if(G.Population[i][j] == 1)
				{
					sum_cost += object_cost[j];
					sum_weight += object_weight[j];
				}

			if(sum_weight <= total_weight)
				Wealth[i] = sum_cost;
			else
				Wealth[i] = 0.0D;
		}

	}

	public Domain domain()
	{
		return mydomain;
	}

	public KnapSackGen()
	{
		total_weight = 100D;
		Iterations = 10;
		Initialpopulation = 10;
		rnd = new Random();
		data_url_old = new String();
		mydomain = new KnapSackGenDomain();
		URL url = HostNameRetriever.getApplet().getCodeBase();
		data_url = new String(url.toString() + "/norveda.txt");
	}

	public void customize(PropertyManager manager)
	{
		manager.add(new DoubleProperty("Total Weight", new KnapGenWeightProvider(this), 10D, 10000D));
		manager.add(new IntProperty("Iterations", new KnapIterationsProvider(this), 10, 10000));
		manager.add(new UrlProperty("URL of data file", new KnapGenDataUrlProvider(this)));
	}

	public IResult f(Point pt)
	{
		if(!data_url.equals(data_url_old))
			try
			{
				input();
				data_url_old = new String(data_url);
			}
			catch(Exception e)
			{
				System.out.println("!!!-Bad data: " + e);
			}
		return new SimpleResult(calculation(pt));
	}
}
