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

package walras;

import java.util.Random;
import lt.ktu.gmj.Domain;
import lt.ktu.gmj.Point;

// Referenced classes of package walras:
//			WalrasServer

class WalrasMarket
{

	public double B1;
	public double B2;
	public double Alfa1;
	public double Alfa2;
	public double Eps;
	public double C0;
	public double X1Step;
	public double X2Step;
	public double Y1Step;
	public double Y2Step;
	public double P1Step;
	public double P2Step;
	public double Z01;
	public double Z11;
	public double Z21;
	public double Z02;
	public double Z12;
	public double Z22;
	public int A;
	public int M;
	public int clientsGone;
	public Domain _domain;
	public static Random randomGenerator = new Random();
	public WalrasServer serverList[];

	public WalrasMarket(Domain domain)
	{
		B1 = 1.0D;
		B2 = 1.0D;
		Alfa1 = 1.0D;
		Alfa2 = 1.0D;
		Eps = 1.0D;
		C0 = 1.0D;
		X1Step = 1.0D;
		X2Step = 1.0D;
		Y1Step = 1.0D;
		Y2Step = 1.0D;
		P1Step = 1.0D;
		P2Step = 1.0D;
		Z01 = 1.0D;
		Z11 = 1.0D;
		Z21 = 1.0D;
		Z02 = 1.0D;
		Z12 = 1.0D;
		Z22 = 1.0D;
		A = 1;
		M = 1;
		_domain = domain;
		byte byte0 = 2;
		serverList = new WalrasServer[byte0];
		serverList[0] = new WalrasServer();
		serverList[1] = new WalrasServer();
	}

	public double GetB1()
	{
		return B1;
	}

	public double GetB2()
	{
		return B2;
	}

	public void Initialize()
	{
		serverList[0].setS_Nr(0);
		serverList[1].setS_Nr(1);
		serverList[0].setMarket(this);
		serverList[1].setMarket(this);
		serverList[0].setMinX1(_domain.min[0]);
		serverList[0].setMaxX1(_domain.max[0]);
		serverList[0].setMinX2(_domain.min[6]);
		serverList[0].setMaxX2(_domain.max[6]);
		serverList[0].setMinY(_domain.min[1]);
		serverList[0].setMaxY(_domain.max[1]);
		serverList[0].setMinP(_domain.min[2]);
		serverList[0].setMaxP(_domain.max[2]);
		serverList[0].setX1Step(X1Step);
		serverList[0].setX2Step(X2Step);
		serverList[0].setYStep(Y1Step);
		serverList[0].setPStep(P1Step);
		serverList[0].setZ0(Z01);
		serverList[0].setZ1(Z11);
		serverList[0].setZ2(Z21);
		serverList[0].setC0(C0);
		serverList[1].setMinX1(_domain.min[7]);
		serverList[1].setMaxX1(_domain.max[7]);
		serverList[1].setMinX2(_domain.min[3]);
		serverList[1].setMaxX2(_domain.max[3]);
		serverList[1].setMinY(_domain.min[4]);
		serverList[1].setMaxY(_domain.max[4]);
		serverList[1].setMinP(_domain.min[5]);
		serverList[1].setMaxP(_domain.max[5]);
		serverList[1].setX1Step(X1Step);
		serverList[1].setX2Step(X2Step);
		serverList[1].setYStep(Y2Step);
		serverList[1].setPStep(P2Step);
		serverList[1].setZ0(Z02);
		serverList[1].setZ1(Z12);
		serverList[1].setZ2(Z22);
		serverList[1].setC0(C0);
	}

	private int chooseBestServer()
	{
		int i = -1;
		boolean flag = false;
		int k = serverList.length;
		double d = 0.0D;
		if(k != 0)
		{
			double ad[] = new double[k];
			for(int l = 0; l < k; l++)
				ad[l] = 0.0D;

			for(int i1 = 0; i1 < k; i1++)
				ad[i1] = serverList[i1].totalCost();

			double d1 = ad[0];
			int ai[] = new int[k];
			i = -1;
			for(int j1 = 1; j1 < k; j1++)
				if(d1 > ad[j1])
				{
					d1 = ad[j1];
					ai[0] = j1;
				}

			int j = 1;
			if(d1 <= C0)
			{
				for(int k1 = 0; k1 < k; k1++)
					if(d1 == ad[k1] && ai[0] != k1)
					{
						ai[j] = k1;
						j++;
					}

				i = ai[randomInteger(j)];
			}
		}
		return i;
	}

	public double maximizeProfit(Point point)
	{
		double d = 0.0D;
		int i = serverList.length;
		Point point1 = new Point(point);
		double ad[] = new double[i];
		processServers(point);
		for(int j = 0; j < i; j++)
			ad[j] = serverList[j].profit();

		double d1;
		point1.x[0] = d1 = point.x[0];
		double d2;
		point1.x[6] = d2 = point.x[6];
		double d3;
		point1.x[1] = d3 = point.x[1];
		double d4;
		point1.x[2] = d4 = point.x[2];
		for(double d5 = serverList[0].getMinP(); d5 < serverList[0].getMaxP(); d5 += serverList[0].getPStep())
		{
			point.x[2] = d5;
			for(double d6 = serverList[0].getMinX1(); d6 < serverList[0].getMaxX1(); d6 += serverList[0].getX1Step())
			{
				for(double d8 = serverList[0].getMinX2(); d8 < serverList[0].getMaxX2(); d8 += serverList[0].getX2Step())
				{
					for(double d10 = serverList[0].getMinY(); d10 < serverList[0].getMaxY(); d10 += serverList[0].getYStep())
					{
						point.x[0] = d6;
						point.x[6] = d8;
						point.x[1] = d10;
						processServers(point);
						if(ad[0] < serverList[0].profit())
						{
							ad[0] = serverList[0].profit();
							point1.x[0] = d6;
							point1.x[6] = d8;
							point1.x[1] = d10;
						}
					}

				}

			}

			if(ad[0] > serverList[0].profit())
			{
				ad[0] = serverList[0].profit();
				point1.x[2] = d5;
			}
		}

		point.x[0] = d1;
		point.x[6] = d2;
		point.x[1] = d3;
		point.x[2] = d4;
		point1.x[7] = d1 = point.x[7];
		point1.x[3] = d2 = point.x[3];
		point1.x[4] = d3 = point.x[4];
		point1.x[5] = d4 = point.x[5];
		for(double d7 = serverList[1].getMinP(); d7 < serverList[1].getMaxP(); d7 += serverList[1].getPStep())
		{
			point.x[5] = d7;
			for(double d9 = serverList[1].getMinX1(); d9 < serverList[1].getMaxX1(); d9 += serverList[1].getX1Step())
			{
				for(double d11 = serverList[1].getMinX2(); d11 < serverList[1].getMaxX2(); d11 += serverList[1].getX2Step())
				{
					for(double d12 = serverList[1].getMinY(); d12 < serverList[1].getMaxY(); d12 += serverList[1].getYStep())
					{
						point.x[7] = d9;
						point.x[3] = d11;
						point.x[4] = d12;
						processServers(point);
						if(ad[1] < serverList[1].profit())
						{
							ad[1] = serverList[1].profit();
							point1.x[7] = d9;
							point1.x[3] = d11;
							point1.x[4] = d12;
						}
					}

				}

			}

			if(ad[1] > serverList[1].profit())
			{
				ad[0] = serverList[1].profit();
				point1.x[5] = d7;
			}
		}

		point.x[7] = d1;
		point.x[3] = d2;
		point.x[4] = d3;
		point.x[5] = d4;
		processServers(point);
		d = point1.distance(point);
		d *= d;
		return d;
	}

	public void processServers(Point point)
	{
		int i = serverList.length;
		clientsGone = 0;
		double d = 0.0D;
		double d1 = 0.0D;
		double d3 = 0.0D;
		serverList[0].setX1(point.x[0]);
		serverList[0].setX2(point.x[6]);
		serverList[0].setY(point.x[1]);
		serverList[0].setP(point.x[2]);
		serverList[1].setX1(point.x[7]);
		serverList[1].setX2(point.x[3]);
		serverList[1].setY(point.x[4]);
		serverList[1].setP(point.x[5]);
		int ai[] = new int[M];
		int ai1[] = new int[M];
		for(int j = 0; j < M; j++)
		{
			serverList[0].setServedClients(0);
			serverList[0].setni(0);
			serverList[1].setServedClients(0);
			serverList[1].setni(0);
			for(int k = 0; k < i; k++)
				serverList[k].setserviceTime(randomDouble(serverList[k].getZi()));

			for(int i1 = 1; i1 <= A; i1++)
			{
				d += randomDouble(A);
				double d2 = serverList[0].serveClients(d);
				double d4 = serverList[1].serveClients(d);
				d = Math.min(d2, d4);
				int k1 = chooseBestServer();
				if(k1 == -1)
					clientsGone++;
				else
					serverList[k1].setni(serverList[k1].getni() + 1);
			}

			for(int l1 = 0; l1 < i; l1++)
				serverList[l1].setServedClients(serverList[l1].getServedClients() + serverList[l1].getni());

			ai[j] = serverList[0].getServedClients();
			ai1[j] = serverList[1].getServedClients();
		}

		int l = 0;
		int j1 = 0;
		for(int i2 = 0; i2 < M; i2++)
		{
			l += ai[i2];
			j1 += ai1[i2];
		}

		l /= M;
		j1 /= M;
		serverList[0].setServedClients(l);
		serverList[1].setServedClients(j1);
		clientsGone = A - (l + j1);
	}

	public static double randomDouble(double d)
	{
		double d1 = randomGenerator.nextDouble();
		if(d1 == 1.0D)
			d1 -= 9.9999999999999995E-08D;
		if(d1 == 0.0D)
			d1 += 9.9999999999999995E-08D;
		return -Math.log(1.0D - d1) / d;
	}

	public static int randomInteger(int i)
	{
		int j = randomGenerator.nextInt();
		if(j < 0)
			j = -j;
		return j % i;
	}

}
