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

import java.io.*;
import java.util.Random;
import lt.ktu.gmj.*;
import lt.ktu.gmj.propertySheet.IntProperty;
import lt.ktu.gmj.propertySheet.PropertyManager;

public class Tvarka
	implements Task
{

	public static final String C_LANGAS = "X";
	public static Tvarka gTvarka = null;
	public final int maxMokytoju = 64;
	public final int maxPamoku = 7;
	public final int maxDienu = 5;
	public final int maxInfo = 3;
	public int eMo;
	public int sMo;
	public String Mokytojai[][];
	public String Mokytojai0[][];
	private int Sdiena;
	private int sdiena;
	private int firsttime;
	public int K;
	public String data_url;
	private int kel;
	private int kelintas;
	private int kuris;
	private double eval;
	private double eval0;
	private int perstatymai;
	private int pos;
	public int number_of_variables;
	private double norm_point[];
	private double norm_eval[];
	private double n_eval[];
	private Random rnd;
	private TvarkaDomain domain;

	public Tvarka()
	{
		System.out.println("Tvarka. (Ignas Namajunas, 1999, Kaunas)");
		eMo = 64;
		sMo = 38;
		Mokytojai = new String[eMo][sMo];
		Mokytojai0 = new String[eMo][sMo];
		K = 2;
		data_url = new String(GlobalTools.getCodeBase() + "mokytojai.txt");
		number_of_variables = 1;
		rnd = new Random();
		domain = new TvarkaDomain();
		System.out.println("Tvarka() begin");
		System.out.println(data_url);
		gTvarka = this;
	}

	private void Atgal(int i, int j, String s, Point point)
	{
		boolean flag = false;
		for(int l = 1; l < sMo; l++)
			if(Mokytojai[i][l].equals("Q"))
			{
				Mokytojai[i][l] = s;
				Mokytojai[i][j] = "X";
				int k = l;
			}

		perstatymas1_d(i, j, s, point);
	}

	private void Dar_langas(int i, int j, Point point)
	{
		String s = new String("");
		if(sdiena <= 5)
			switch(sdiena)
			{
			default:
				break;

			case 1: // '\001'
				String s1 = casas(i, j, "P");
				if(s1.equals(""))
				{
					sdiena++;
					Dar_langas(i, j, point);
				}
				tikrink(i, j, s1, point);
				break;

			case 2: // '\002'
				String s2 = casas(i, j, "A");
				if(s2.equals(""))
				{
					sdiena++;
					Dar_langas(i, j, point);
				}
				tikrink(i, j, s2, point);
				break;

			case 3: // '\003'
				String s3 = casas(i, j, "T");
				if(s3.equals(""))
				{
					sdiena++;
					Dar_langas(i, j, point);
				}
				tikrink(i, j, s3, point);
				break;

			case 4: // '\004'
				String s4 = casas(i, j, "K");
				if(s4.equals(""))
				{
					sdiena++;
					Dar_langas(i, j, point);
				}
				tikrink(i, j, s4, point);
				break;

			case 5: // '\005'
				String s5 = casas(i, j, "N");
				tikrink(i, j, s5, point);
				break;
			}
	}

	private void NewInput()
		throws IOException
	{
		InputStreamReader inputstreamreader = GlobalTools.getInputStreamReader(data_url);
		char ac[] = new char[3];
		inputstreamreader.read(ac, 0, 3);
		String s = new String(ac);
		inputstreamreader.close();
		boolean flag;
		if(s.compareTo("Nr.") == 0)
		{
			flag = true;
			System.out.println("File open: Assuming NEW file format.");
		} else
		{
			flag = false;
			System.out.println("File open: Assuming OLD file format.");
		}
		boolean flag1;
		boolean flag2;
		if(flag)
			flag1 = flag2 = true;
		else
			flag1 = flag2 = false;
		inputstreamreader = GlobalTools.getInputStreamReader(data_url);
		BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
		if(flag1)
			bufferedreader.readLine();
		StreamTokenizer streamtokenizer = new StreamTokenizer(bufferedreader);
		streamtokenizer.resetSyntax();
		streamtokenizer.whitespaceChars(0, 32);
		streamtokenizer.wordChars(33, 255);
		for(int i = 0; i < eMo; i++)
		{
			if(flag2)
				streamtokenizer.nextToken();
			for(int j = 0; j < sMo; j++)
			{
				streamtokenizer.nextToken();
				if(streamtokenizer.ttype == -3)
					Mokytojai[i][j] = streamtokenizer.sval;
			}

		}

		bufferedreader.close();
		inputstreamreader.close();
		System.out.println("Data ready...");
	}

	private void Tikrinti_priest(String s, int i, int j, Point point)
	{
		int k = 0;
		for(int l = 1; l < eMo; l++)
		{
			for(int i1 = 1; i1 < sMo; i1++)
				if(Mokytojai[l][i1].equals(s) && l != i && Sdiena < 5)
				{
					Sdiena++;
					Atgal(i, j, s, point);
					k++;
				} else
				if(Mokytojai[l][i1].equals(s) && l != i && Sdiena >= 5)
				{
					perstatymas2(i, s, point);
					k++;
				}

		}

		if(k == 0)
			skaiciuok(point);
	}

	private String casas(int i, int j, String s)
	{
		String s1 = new String("");
		String s2 = new String("");
		boolean flag = false;
		for(int l = 3; l < sMo - 1; l++)
			if(Mokytojai[i][l].startsWith(s) && Mokytojai[i][l + 1].equals("W"))
			{
				s1 = Mokytojai[i][j];
				Mokytojai[i][j] = "X";
				int k = (l - 2) % 7;
				String s3 = s.concat(String.valueOf(k));
				Mokytojai[i][l + 1] = s3.concat(s1.substring(1, s1.length()));
			}

		return s1;
	}

	private String casasD(int i, int j, String s)
	{
		String s1 = new String("");
		String s2 = new String("");
		int k = (j - 3) % 7;
		for(int l = 3; l < sMo - 1; l++)
			if(Mokytojai[i][l].startsWith(s) && !Mokytojai[i][l + 1].startsWith(s) && !Mokytojai[i][l].equals("W"))
			{
				s1 = Mokytojai[i][l];
				String s3 = s.concat(String.valueOf(k));
				Mokytojai[i][j] = s3.concat(Mokytojai[i][l].substring(1, Mokytojai[i][l].length()));
				Mokytojai[i][l] = "Q";
			}

		return s1;
	}

	public void customize(PropertyManager propertymanager)
	{
		propertymanager.add(new IntProperty("K  number of repetitions", new TvarkaIntProvider(this), 1, 100));
		propertymanager.add(new UrlProperty("URL of data file", new TvarkaDataUrlProvider(this)));
	}

	public Domain domain()
	{
		return domain;
	}

	public IResult f(Point point)
	{
		if(firsttime == 0)
		{
			try
			{
				NewInput();
			}
			catch(IOException ioexception)
			{
				GlobalTools.showError(ioexception);
				return new SimpleResult(-1D);
			}
			firsttime = 1;
			eval0 = 1.7976931348623157E+308D;
		}
		kel = -1;
		skaiciuok(point);
		return new SimpleResult(eval0);
	}

	protected void finalize()
		throws Throwable
	{
		System.out.println("Tvarka::finalize() begin");
		gTvarka = null;
		super.finalize();
	}

	private int geriausias()
	{
		int i = 0;
		for(int j = 0; j < eMo; j++)
		{
			for(int k = 3; k < sMo; k++)
				if(Mokytojai[j][k].equals("X"))
					i++;

		}

		return i;
	}

	public static Tvarka getTvarka()
	{
		System.runFinalization();
		return gTvarka;
	}

	private void norm()
	{
		int i = 0;
		for(int j = 0; j < 10; j++)
			i = (int)((double)i + norm_eval[j]);

		for(int k = 0; k < 10; k++)
			norm_eval[k] = norm_eval[k] / (double)i;

	}

	private void perstatymas1(int i, int j, Point point)
	{
		int k = (j - 3) % 7;
		String s = new String("");
		for(int l = 3; l < sMo; l++)
			if(Mokytojai[i][l].startsWith("P") && !Mokytojai[i][l + 1].startsWith("P") && !Mokytojai[i][l].startsWith("W"))
			{
				Mokytojai[i][j] = Mokytojai[i][l];
				Mokytojai[i][l] = "Q";
				String s1 = "P".concat(String.valueOf(k));
				Mokytojai[i][j] = s1.concat(Mokytojai[i][j].substring(1, Mokytojai[i][j].length()));
				Tikrinti_priest(Mokytojai[i][j], i, j, point);
			}

	}

	private void perstatymas1_d(int i, int j, String s, Point point)
	{
		boolean flag = false;
		String s1 = new String("");
		if(Sdiena <= 5)
			switch(Sdiena)
			{
			case 2: // '\002'
				String s2 = casasD(i, j, "A");
				Tikrinti_priest(s2, i, j, point);
				break;

			case 3: // '\003'
				String s3 = casasD(i, j, "T");
				Tikrinti_priest(s3, i, j, point);
				break;

			case 4: // '\004'
				String s4 = casasD(i, j, "K");
				Tikrinti_priest(s4, i, j, point);
				break;

			case 5: // '\005'
				String s5 = casasD(i, j, "N");
				Tikrinti_priest(s5, i, j, point);
				break;
			}
		else
			perstatymas2(i, s, point);
	}

	private void perstatymas2(int i, String s, Point point)
	{
		for(int j = 1; j < eMo; j++)
		{
			for(int k = 3; k < sMo; k++)
				if(Mokytojai[j][k].equals(s) && i != j)
					Dar_langas(j, k, point);

		}

	}

	private void skaiciuok(Point point)
	{
		kel++;
		double d = rnd.nextDouble();
		if(kel < eMo * K - 1)
		{
			Sdiena = 1;
			sdiena = 1;
			if(d <= point.x[0])
				tikrinimas(point);
			else
				skaiciuok(point);
		}
		eval = geriausias();
		System.out.print("eval  ");
		System.out.println(eval);
		if(eval < eval0)
		{
			eval0 = eval;
			System.out.print("eval0 ");
			System.out.println(eval0);
			synchronized(Mokytojai0)
			{
				for(int i = 0; i < eMo; i++)
				{
					for(int j = 0; j < sMo; j++)
						Mokytojai0[i][j] = Mokytojai[i][j];

				}

			}
		}
	}

	private void tikrinimas(Point point)
	{
		int i = 0;
		double d = kel / eMo;
		double d1 = Math.floor(d);
		int j = (int)d1;
		kelintas = kel - j * eMo;
		if(kelintas <= eMo - 1)
		{
			for(int k = 0; k < sMo; k++)
				if(Mokytojai[kelintas][k].equals("X"))
				{
					i++;
					perstatymas1(kelintas, k, point);
				}

		}
		if(i == 0)
			skaiciuok(point);
	}

	private void tikrink(int i, int j, String s, Point point)
	{
		int k = 0;
		if(sdiena < 5)
		{
			for(int l = 1; l < eMo; l++)
			{
				for(int i1 = 3; i1 < sMo; i1++)
					if(Mokytojai[l][i1].equals(s) && i != l && sdiena < 5)
					{
						sdiena++;
						k++;
						Dar_langas(i, j, point);
					}

			}

		}
		if(k == 0)
			skaiciuok(point);
	}

}
