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

package walras;

import java.io.PrintStream;
import java.util.Vector;
import lt.ktu.gmj.*;
import lt.ktu.gmj.propertySheet.*;

// Referenced classes of package walras:
//			LogAll, WalrasDomain, WalrasMarket, WalrasModelAProvider, 
//			WalrasModelAlfa1Provider, WalrasModelAlfa2Provider, WalrasModelB1Provider, WalrasModelB2Provider, 
//			WalrasModelC0Provider, WalrasModelEpsProvider, WalrasModelMProvider, WalrasModelP1StepProvider, 
//			WalrasModelP2StepProvider, WalrasModelX1StepProvider, WalrasModelX2StepProvider, WalrasModelY1StepProvider, 
//			WalrasModelY2StepProvider, WalrasModelZ01Provider, WalrasModelZ02Provider, WalrasModelZ11Provider, 
//			WalrasModelZ12Provider, WalrasModelZ21Provider, WalrasModelZ22Provider, WalrasServer, 
//			sLayerLogo

public class WalrasModel
	implements Task
{

	public WalrasDomain domain;
	public WalrasMarket market;
	private int firstTime;
	private static final sLayerLogo _WalrasLogo = new sLayerLogo();
	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 pointnum[];
	public static WalrasModel gWalrasModel = null;
	public Vector mylog;
	public int ibestiter;
	public int curriter;
	public double bestX11;
	public double bestY1;
	public double bestP1;
	public double bestX22;
	public double bestY2;
	public double bestP2;
	public double bestX12;
	public double bestX21;
	public double bestv1;
	public double bestv2;
	public double bestu1;
	public double bestu2;
	public double bestf;
	public double bestcond1;
	public double bestcond2;
	public int bestiter;
	public int bestserved1;
	public int bestserved2;
	public int bestgone;
	public double bestdiff;

	public WalrasModel()
	{
		domain = new WalrasDomain();
		B1 = 16D;
		B2 = 16D;
		Eps = 0.01D;
		C0 = 20D;
		X1Step = 1.0D;
		X2Step = 1.0D;
		Y1Step = 1.0D;
		Y2Step = 1.0D;
		P1Step = 1.0D;
		P2Step = 1.0D;
		Z01 = 8D;
		Z11 = 0.20000000000000001D;
		Z21 = 0.20000000000000001D;
		Z02 = 8D;
		Z12 = 0.20000000000000001D;
		Z22 = 0.20000000000000001D;
		A = 50;
		M = 1;
		mylog = new Vector();
		bestdiff = 9999D;
		firstTime = 1;
		gWalrasModel = this;
	}

	public double GetB1()
	{
		return B1;
	}

	public double GetB2()
	{
		return B2;
	}

	public void ProcessServers(double d, double d1, double d2, double d3, double d4, double d5, double d6, 
			double d7)
	{
		Point point = new Point(domain());
		point.x[0] = d;
		point.x[1] = d1;
		point.x[2] = d2;
		point.x[3] = d3;
		point.x[4] = d4;
		point.x[5] = d5;
		point.x[6] = d6;
		point.x[7] = d7;
		market.processServers(point);
	}

	public void customize(PropertyManager propertymanager)
	{
		propertymanager.add(new DoubleProperty("B1 (stock of first server resource)", new WalrasModelB1Provider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("B2 (stock of second server resource)", new WalrasModelB2Provider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("Alfa1 (interest of credit)", new WalrasModelAlfa1Provider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("Alfa2 (yield of deposit)", new WalrasModelAlfa2Provider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("Eps (nonlinearity stabilizing  resource charges)", new WalrasModelEpsProvider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("C0 (critical cost, when customer leaves service system)", new WalrasModelC0Provider(this), -1000D, 1000D));
		propertymanager.add(new IntProperty("A (average number of clients per time unit)", new WalrasModelAProvider(this), 1, 1000));
		propertymanager.add(new IntProperty("M (number of conditional time units)", new WalrasModelMProvider(this), 1, 1000));
		propertymanager.add(new DoubleProperty("Z01 (efficiency of first server)", new WalrasModelZ01Provider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("Z11 (efficiency of first server resource when used by first  server)", new WalrasModelZ11Provider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("Z21 (efficiency of second  server resource when used by first sever)", new WalrasModelZ21Provider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("Z02 (efficiency of second server)", new WalrasModelZ02Provider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("Z12 (efficiency of first  server resource when used by second sever)", new WalrasModelZ12Provider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("Z22 (efficiency of second server resource when used by second  server)", new WalrasModelZ22Provider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("X1Step (step of change of resource x1)", new WalrasModelX1StepProvider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("X2Step (step of change of resource x2)", new WalrasModelX2StepProvider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("Y1Step (step of change of first server service charge)", new WalrasModelY1StepProvider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("Y2Step (step of change of second server service charge)", new WalrasModelY2StepProvider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("P1Step (step of change of charge of first server  resource x11)", new WalrasModelP1StepProvider(this), -1000D, 1000D));
		propertymanager.add(new DoubleProperty("P2Step (step of change of charge of second server  resource x22)", new WalrasModelP2StepProvider(this), -1000D, 1000D));
	}

	public Domain domain()
	{
		return domain;
	}
	public IResult f(Point point)
	{
		if(firstTime == 1)
		{
			pointnum = new int[10];
			for(int i = 0; i < 10; i++)
				pointnum[i] = 0;

			for(double d1 = domain().min[0]; d1 <= domain().max[0]; d1 += X1Step)
				pointnum[2] = pointnum[2] + 1;

			for(double d3 = domain().min[7]; d3 <= domain().max[7]; d3 += X1Step)
				pointnum[3] = pointnum[3] + 1;

			for(double d5 = domain().min[6]; d5 <= domain().max[6]; d5 += X2Step)
				pointnum[4] = pointnum[4] + 1;

			for(double d7 = domain().min[3]; d7 <= domain().max[3]; d7 += X2Step)
				pointnum[5] = pointnum[5] + 1;

			for(double d8 = domain().min[1]; d8 <= domain().max[1]; d8 += Y1Step)
				pointnum[6] = pointnum[6] + 1;

			for(double d9 = domain().min[4]; d9 <= domain().max[4]; d9 += Y2Step)
				pointnum[7] = pointnum[7] + 1;

			for(double d10 = domain().min[2]; d10 <= domain().max[2]; d10 += P1Step)
				pointnum[8] = pointnum[8] + 1;

			for(double d11 = domain().min[5]; d11 <= domain().max[5]; d11 += P2Step)
				pointnum[9] = pointnum[9] + 1;

			market = new WalrasMarket(domain);
			market.B1 = B1;
			market.B2 = B2;
			market.Alfa1 = Alfa1;
			market.Alfa2 = Alfa2;
			market.Eps = Eps;
			market.C0 = C0;
			market.A = A;
			market.M = M;
			market.Z01 = Z01;
			market.Z11 = Z11;
			market.Z21 = Z21;
			market.Z02 = Z02;
			market.Z12 = Z12;
			market.Z22 = Z22;
			market.X1Step = X1Step;
			market.X2Step = X2Step;
			market.Y1Step = Y1Step;
			market.Y2Step = Y2Step;
			market.P1Step = P1Step;
			market.P2Step = P2Step;
			market.Initialize();
			curriter = -1;
			firstTime = 0;
			System.out.println("-----------------------------------------");
			System.out.println("BEST RESULTS");
			System.out.println("-----------------------------------------");
		}
		double d = market.maximizeProfit(point);
		double d2 = (double)Math.round(d * 1000D) / 1000D;
		curriter = curriter + 1;
		mylog.addElement(new LogAll((double)Math.round(market.serverList[0].profit() * 1000D) / 1000D, (double)Math.round(market.serverList[1].profit() * 1000D) / 1000D));
		if(d < bestdiff)
		{
			bestdiff = d;
			bestf = d2;
			bestX11 = (double)Math.round(market.serverList[0].getX1() * 1000D) / 1000D;
			bestY1 = (double)Math.round(market.serverList[0].getY() * 1000D) / 1000D;
			bestP1 = (double)Math.round(market.serverList[0].getP() * 1000D) / 1000D;
			bestX22 = (double)Math.round(market.serverList[1].getX2() * 1000D) / 1000D;
			bestY2 = (double)Math.round(market.serverList[1].getY() * 1000D) / 1000D;
			bestP2 = (double)Math.round(market.serverList[1].getP() * 1000D) / 1000D;
			bestX12 = (double)Math.round(market.serverList[0].getX2() * 1000D) / 1000D;
			bestX21 = (double)Math.round(market.serverList[1].getX1() * 1000D) / 1000D;
			bestv1 = (double)Math.round(market.serverList[0].getvi() * 1000D) / 1000D;
			bestv2 = (double)Math.round(market.serverList[1].getvi() * 1000D) / 1000D;
			bestu1 = (double)Math.round(market.serverList[0].profit() * 1000D) / 1000D;
			bestu2 = (double)Math.round(market.serverList[1].profit() * 1000D) / 1000D;
			double d4 = (market.serverList[0].getX1() + market.serverList[1].getX1()) - market.GetB1();
			d4 = (double)Math.round(d4 * 1000D) / 1000D;
			double d6 = (market.serverList[0].getX2() + market.serverList[1].getX2()) - market.GetB2();
			d6 = (double)Math.round(d6 * 1000D) / 1000D;
			bestcond1 = d4;
			bestcond2 = d6;
			bestiter = curriter;
			bestserved1 = market.serverList[0].getServedClients();
			bestserved2 = market.serverList[1].getServedClients();
			bestgone = market.clientsGone;
			System.out.println("-----------------------------------------");
			System.out.print("iter= ");
			System.out.println(bestiter);
			System.out.print("f= ");
			System.out.println(bestf);
			System.out.print("X11= ");
			System.out.println(bestX11);
			System.out.print("Y1= ");
			System.out.println(bestY1);
			System.out.print("P1= ");
			System.out.println(bestP1);
			System.out.print("X22= ");
			System.out.println(bestX22);
			System.out.print("Y2= ");
			System.out.println(bestY2);
			System.out.print("P2= ");
			System.out.println(bestP2);
			System.out.print("X12= ");
			System.out.println(bestX12);
			System.out.print("X21= ");
			System.out.println(bestX21);
			System.out.println();
			System.out.print("v1= ");
			System.out.println(bestv1);
			System.out.print("v2= ");
			System.out.println(bestv2);
			System.out.print("u1= ");
			System.out.println(bestu1);
			System.out.print("u2= ");
			System.out.println(bestu2);
			System.out.print("cond1= ");
			System.out.println(bestcond1);
			System.out.print("cond2= ");
			System.out.println(bestcond2);
			System.out.print("served1= ");
			System.out.println(bestserved1);
			System.out.print("served2= ");
			System.out.println(bestserved2);
			System.out.print("gone= ");
			System.out.println(bestgone);
		}
		//return d;
		return new SimpleResult(d);
	}

	public double getProfitsMax()
	{
		double d = -9999D;
		for(int i = 0; i < mylog.size(); i++)
		{
			if(((LogAll)mylog.elementAt(i)).profit1 > d)
				d = ((LogAll)mylog.elementAt(i)).profit1;
			if(((LogAll)mylog.elementAt(i)).profit2 > d)
				d = ((LogAll)mylog.elementAt(i)).profit2;
		}

		return d;
	}

	public double getProfitsMin()
	{
		double d = 9999D;
		for(int i = 0; i < mylog.size(); i++)
		{
			if(((LogAll)mylog.elementAt(i)).profit1 < d)
				d = ((LogAll)mylog.elementAt(i)).profit1;
			if(((LogAll)mylog.elementAt(i)).profit2 < d)
				d = ((LogAll)mylog.elementAt(i)).profit2;
		}

		return d;
	}

	public double getServerProfit(int i)
	{
		return market.serverList[i].profit();
	}

	public WalrasMarket getWalrasMarket()
	{
		return market;
	}

	public static WalrasModel getWalrasModel()
	{
		return gWalrasModel;
	}

}
