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

package lt.ktu.gmj.analysis;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import java.util.Vector;
import javax.swing.*;
import javax.swing.text.JTextComponent;
import lt.ktu.gmj.*;
import lt.monarch.charts.*;
import walras.LogAll;
import walras.WalrasModel;

// Referenced classes of package lt.ktu.gmj.analysis:
//			ResultFrame

public class WalrasProfit extends ResultFrame
	implements ChartPointProvider
{

	public JButton btn_smooth;
	public JButton btn_wiener;
	public JButton btn_undo;
	public JButton btn_refresh;
	public double y_in[];
	public double x_in[];
	public double y_out[];
	public int n;
	public int only_refresh;
	public int cur_point_nr;
	public double profx[];
	public double profy[];
	public double original_profy[];
	public JTextField st;
	public JLabel control1;
	public JLabel control2;
	public JLabel clserved1;
	public JLabel clserved2;
	public JLabel clgone;
	public JLabel bestiter;
	public JLabel curriter;
	public JLabel diff;
	public JLabel profit1;
	public JLabel profit2;
	public JLabel v1;
	public JLabel v2;
	public JLabel bestX11;
	public JLabel bestY1;
	public JLabel bestP1;
	public JLabel bestX22;
	public JLabel bestY2;
	public JLabel bestP2;
	public JLabel bestX12;
	public JLabel bestX21;
	private static final String CMD_SMOOTH = "Smooth";
	private static final String CMD_WIENER = "Wiener";
	private static final String CMD_UNDO = "Undo";
	private static final String CMD_REFRESH = "Refresh";
	protected Chart chart;
	protected int projection;
	protected ChartPointSeries series;
	protected JComboBox projectionChoice;
	private WalrasModel gWalrasModel;

	public WalrasProfit()
	{
		super("Server Profit");
		System.out.println("Walras Profit. (Nerijus Remeikis, 1999, Kaunas)");
		setWalrasModel(WalrasModel.getWalrasModel());
		only_refresh = 0;
		chart = new Chart();
		getContentPane().add("Center", chart);
		projectionChoice = new JComboBox();
		projectionChoice.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent actionevent)
			{
				projection = projectionChoice.getSelectedIndex();
				setupChart();
			}

		});
		getContentPane().add("South", projectionChoice);
		JPanel jpanel = new JPanel();
		JPanel jpanel1 = new JPanel();
		JPanel jpanel2 = new JPanel();
		JPanel jpanel3 = new JPanel();
		JPanel jpanel4 = new JPanel();
		jpanel.setLayout(new GridLayout(3, 1));
		jpanel1.setLayout(new GridLayout(1, 0, 0, 0));
		jpanel2.setLayout(new GridLayout(1, 0, 0, 0));
		jpanel3.setLayout(new GridLayout(1, 0, 0, 0));
		jpanel4.setLayout(new GridLayout(10, 1));
		jpanel.add("North", jpanel1);
		jpanel.add("North", jpanel2);
		jpanel.add("North", jpanel3);
		btn_smooth = new JButton("Smooth");
		btn_smooth.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent actionevent)
			{
				SmoothProfitCmd();
			}

		});
		jpanel4.add("North", btn_smooth);
		btn_wiener = new JButton("Wiener");
		btn_wiener.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent actionevent)
			{
				WienerFilterCmd();
			}

		});
		jpanel4.add("North", btn_wiener);
		btn_undo = new JButton("Undo");
		btn_undo.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent actionevent)
			{
				Undo();
			}

		});
		jpanel4.add("North", btn_undo);
		btn_refresh = new JButton("Refresh");
		btn_refresh.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent actionevent)
			{
				setupChart();
			}

		});
		jpanel4.add("North", btn_refresh);
		bestiter = new JLabel("0");
		curriter = new JLabel("0");
		diff = new JLabel("0");
		control1 = new JLabel("0");
		control2 = new JLabel("0");
		clserved1 = new JLabel("0");
		clserved2 = new JLabel("0");
		clgone = new JLabel("0");
		profit1 = new JLabel("0");
		profit2 = new JLabel("0");
		v1 = new JLabel("0");
		v2 = new JLabel("0");
		bestX11 = new JLabel("0");
		bestY1 = new JLabel("0");
		bestP1 = new JLabel("0");
		bestX22 = new JLabel("0");
		bestY2 = new JLabel("0");
		bestP2 = new JLabel("0");
		bestX12 = new JLabel("0");
		bestX21 = new JLabel("0");
		st = new JTextField("10", 6);
		curriter.setText(Integer.toString(getWalrasModel().curriter));
		bestiter.setText(Integer.toString(getWalrasModel().bestiter));
		diff.setText(Double.toString(getWalrasModel().bestf));
		control1.setText(Double.toString(getWalrasModel().bestcond1));
		control2.setText(Double.toString(getWalrasModel().bestcond2));
		clserved1.setText(Integer.toString(getWalrasModel().bestserved1));
		clserved2.setText(Integer.toString(getWalrasModel().bestserved2));
		clgone.setText(Integer.toString(getWalrasModel().bestgone));
		profit1.setText(Double.toString(getWalrasModel().bestu1));
		profit2.setText(Double.toString(getWalrasModel().bestu2));
		v1.setText(Double.toString(getWalrasModel().bestv1));
		v2.setText(Double.toString(getWalrasModel().bestv2));
		bestX11.setText(Double.toString(getWalrasModel().bestX11));
		bestY1.setText(Double.toString(getWalrasModel().bestY1));
		bestP1.setText(Double.toString(getWalrasModel().bestP1));
		bestX22.setText(Double.toString(getWalrasModel().bestX22));
		bestY2.setText(Double.toString(getWalrasModel().bestY2));
		bestP2.setText(Double.toString(getWalrasModel().bestP2));
		bestX12.setText(Double.toString(getWalrasModel().bestX12));
		bestX21.setText(Double.toString(getWalrasModel().bestX21));
		jpanel1.add("North", new JLabel("F(x)="));
		jpanel1.add("North", diff);
		jpanel1.add("North", new JLabel("Best it="));
		jpanel1.add("North", bestiter);
		jpanel1.add("North", new JLabel("Curr it="));
		jpanel1.add("North", curriter);
		jpanel1.add("North", new JLabel("Test1="));
		jpanel1.add("North", control1);
		jpanel1.add("North", new JLabel("Test2="));
		jpanel1.add("North", control2);
		jpanel1.add("North", new JLabel("S="));
		jpanel1.add("North", st);
		jpanel2.add("North", new JLabel("Served1="));
		jpanel2.add("North", clserved1);
		jpanel2.add("North", new JLabel("Served2="));
		jpanel2.add("North", clserved2);
		jpanel2.add("North", new JLabel("Gone="));
		jpanel2.add("North", clgone);
		jpanel2.add("North", new JLabel("u1="));
		jpanel2.add("North", profit1);
		jpanel2.add("North", new JLabel("u2="));
		jpanel2.add("North", profit2);
		jpanel2.add("North", new JLabel("v1="));
		jpanel2.add("North", v1);
		jpanel2.add("North", new JLabel("v2="));
		jpanel2.add("North", v2);
		jpanel3.add("North", new JLabel("X11="));
		jpanel3.add("North", bestX11);
		jpanel3.add("North", new JLabel("Y1="));
		jpanel3.add("North", bestY1);
		jpanel3.add("North", new JLabel("P1="));
		jpanel3.add("North", bestP1);
		jpanel3.add("North", new JLabel("X22="));
		jpanel3.add("North", bestX22);
		jpanel3.add("North", new JLabel("Y2="));
		jpanel3.add("North", bestY2);
		jpanel3.add("North", new JLabel("P2="));
		jpanel3.add("North", bestP2);
		jpanel3.add("North", new JLabel("X12="));
		jpanel3.add("North", bestX12);
		jpanel3.add("North", new JLabel("X21="));
		jpanel3.add("North", bestX21);
		getContentPane().add("North", jpanel);
		getContentPane().add("East", jpanel4);
	}

	public void CalculateOutput()
	{
		double ad[] = new double[8];
		ad[0] = getWalrasModel().bestX11;
		ad[1] = getWalrasModel().bestY1;
		ad[2] = getWalrasModel().bestP1;
		ad[3] = getWalrasModel().bestX22;
		ad[4] = getWalrasModel().bestY2;
		ad[5] = getWalrasModel().bestP2;
		ad[6] = getWalrasModel().bestX12;
		ad[7] = getWalrasModel().bestX21;
		if(projection == 0)
		{
			for(int i = 0; i < PointCnt(); i++)
			{
				profy[i] = getLogAll(i).profit1;
				profx[i] = i;
			}

			return;
		}
		if(projection == 1)
		{
			for(int j = 0; j < PointCnt(); j++)
			{
				profy[j] = getLogAll(j).profit2;
				profx[j] = j;
			}

			return;
		}
		int k = 0;
		for(double d = GetXMin(projection); d <= GetXMax(projection); d += GetXStep(projection))
		{
			ad[GetDomN(projection)] = d;
			getWalrasModel().ProcessServers(ad[0], ad[1], ad[2], ad[3], ad[4], ad[5], ad[6], ad[7]);
			profy[k] = getWalrasModel().getServerProfit(projection % 2);
			profx[k] = d;
			k++;
		}

	}

	public int GetDomN(int i)
	{
		if(i == 2)
			return 0;
		if(i == 3)
			return 7;
		if(i == 4)
			return 6;
		if(i == 5)
			return 3;
		if(i == 6)
			return 1;
		if(i == 7)
			return 4;
		if(i == 8)
			return 2;
		if(i == 9)
		{
			return 5;
		} else
		{
			System.out.println("ERROR: GetDomN");
			return 0;
		}
	}

	public double GetXMax(int i)
	{
		if(i == 0)
			return (double)(super.results.method().iterations() - 1);
		if(i == 1)
			return (double)(super.results.method().iterations() - 1);
		if(i == 2)
			return getWalrasModel().domain().max[0];
		if(i == 3)
			return getWalrasModel().domain().max[7];
		if(i == 4)
			return getWalrasModel().domain().max[6];
		if(i == 5)
			return getWalrasModel().domain().max[3];
		if(i == 6)
			return getWalrasModel().domain().max[1];
		if(i == 7)
			return getWalrasModel().domain().max[4];
		if(i == 8)
			return getWalrasModel().domain().max[2];
		if(i == 9)
			return getWalrasModel().domain().max[5];
		else
			return 0.0D;
	}

	public double GetXMin(int i)
	{
		if(i == 0)
			return 0.0D;
		if(i == 1)
			return 0.0D;
		if(i == 2)
			return getWalrasModel().domain().min[0];
		if(i == 3)
			return getWalrasModel().domain().min[7];
		if(i == 4)
			return getWalrasModel().domain().min[6];
		if(i == 5)
			return getWalrasModel().domain().min[3];
		if(i == 6)
			return getWalrasModel().domain().min[1];
		if(i == 7)
			return getWalrasModel().domain().min[4];
		if(i == 8)
			return getWalrasModel().domain().min[2];
		if(i == 9)
			return getWalrasModel().domain().min[5];
		else
			return 0.0D;
	}

	public double GetXStep(int i)
	{
		if(i == 0)
			return 1.0D;
		if(i == 1)
			return 1.0D;
		if(i == 2)
			return getWalrasModel().X1Step;
		if(i == 3)
			return getWalrasModel().X1Step;
		if(i == 4)
			return getWalrasModel().X2Step;
		if(i == 5)
			return getWalrasModel().X2Step;
		if(i == 6)
			return getWalrasModel().Y1Step;
		if(i == 7)
			return getWalrasModel().Y2Step;
		if(i == 8)
			return getWalrasModel().P1Step;
		if(i == 9)
			return getWalrasModel().P2Step;
		else
			return 0.0D;
	}

	public double GetYMax()
	{
		if(profy.length < 1)
			return 1.0D;
		double d = -9999D;
		for(int i = 0; i < profy.length; i++)
			if(profy[i] > d)
				d = profy[i];

		return d;
	}

	public double GetYMin()
	{
		if(profy.length < 1)
			return 0.0D;
		double d = 9999D;
		for(int i = 0; i < profy.length; i++)
			if(profy[i] < d)
				d = profy[i];

		return d;
	}

	public int PointCnt()
	{
		getWalrasModel().pointnum[0] = super.results.resultCount();
		getWalrasModel().pointnum[1] = super.results.resultCount();
		return getWalrasModel().pointnum[projection];
	}

	public void SmoothProfit()
	{
		for(int i = 0; i < n; i++)
		{
			double d = y_in[i];
			double d1 = y_in[i];
			double d2 = y_in[i];
			if(i - 1 > 0)
				d = y_in[i - 1];
			if(i + 1 < n)
				d2 = y_in[i + 1];
			double d3 = (d + d1 + d2) / 3D;
			y_out[i] = d3;
		}

	}

	public synchronized void SmoothProfitCmd()
	{
		n = Math.min(profx.length, profy.length);
		y_in = new double[n];
		y_out = new double[n];
		x_in = new double[n];
		for(int i = 0; i < n; i++)
			x_in[i] = profx[i];

		for(int j = 0; j < n; j++)
			y_in[j] = profy[j];

		for(int k = 0; k < n; k++)
			original_profy[k] = profy[k];

		SmoothProfit();
		for(int l = 0; l < n; l++)
			profy[l] = y_out[l];

		only_refresh = 1;
		setupChart();
		only_refresh = 0;
	}

	public synchronized void Undo()
	{
		for(int i = 0; i < original_profy.length; i++)
			profy[i] = original_profy[i];

		only_refresh = 1;
		setupChart();
		only_refresh = 0;
	}

	public void WienerFilter()
	{
		double ad[] = new double[n];
		double ad1[] = new double[n];
		double d = (new Double(st.getText())).doubleValue();
		double d1 = 1.0D;
		ad[0] = 1.0D;
		for(int i = 1; i < n; i++)
		{
			double d2 = Math.abs(x_in[i] - x_in[i - 1]);
			if(d == 0.0D)
				System.out.println("division by 0. (1)");
			ad[i] = (d * ad[i - 1] + d2 * d1) / d;
			d1 += ad[i];
		}

		double d3 = 0.0D;
		ad1[n - 1] = 1.0D;
		for(int j = n - 2; j >= 0; j--)
		{
			double d4 = Math.abs(x_in[j] - x_in[j + 1]);
			d3 += ad1[j + 1];
			if(d == 0.0D)
				System.out.println("division by 0. (2)");
			ad1[j] = (d * ad1[j + 1] + d4 * d3) / d;
		}

		for(int k = 0; k < n; k++)
		{
			double d5 = 0.0D;
			double d6 = 0.0D;
			double d7 = 0.0D;
			double d8 = 0.0D;
			for(int l = 0; l <= k; l++)
			{
				d5 += ad[l] * y_in[l];
				d6 += ad[l];
			}

			for(int i1 = k + 1; i1 < n; i1++)
			{
				d7 += ad1[i1] * y_in[i1];
				d8 += ad1[i1];
			}

			double d9 = d6 + (ad[k] * d8) / ad1[k];
			if(d9 == 0.0D)
				System.out.println("division by 0. (3)");
			if(ad1[k] == 0.0D)
				System.out.println("division by 0. (4)");
			y_out[k] = (d5 + (ad[k] * d7) / ad1[k]) / d9;
		}

	}

	public synchronized void WienerFilterCmd()
	{
		n = Math.min(profx.length, profy.length);
		y_in = new double[n];
		y_out = new double[n];
		x_in = new double[n];
		for(int i = 0; i < n; i++)
			x_in[i] = profx[i];

		for(int j = 0; j < n; j++)
			y_in[j] = profy[j];

		for(int k = 0; k < n; k++)
			original_profy[k] = profy[k];

		WienerFilter();
		for(int l = 0; l < n; l++)
			profy[l] = y_out[l];

		only_refresh = 1;
		setupChart();
		only_refresh = 0;
	}

	public boolean action(Event event, Object obj)
	{
		if(event.target == projectionChoice)
		{
			projection = projectionChoice.getSelectedIndex();
			setupChart();
		}
		if(event.target == btn_smooth)
			SmoothProfitCmd();
		if(event.target == btn_wiener)
			WienerFilterCmd();
		if(event.target == btn_undo)
			Undo();
		if(event.target == btn_refresh)
			setupChart();
		return false;
	}

	public void finalResult(ResultRepository resultrepository)
	{
		newProgressResult(resultrepository);
	}

	public LogAll getLogAll(int i)
	{
		if(getWalrasModel().mylog.size() > i)
			return (LogAll)getWalrasModel().mylog.elementAt(i);
		else
			return new LogAll(0.0D, 0.0D);
	}

	private WalrasModel getWalrasModel()
	{
		return gWalrasModel;
	}

	public synchronized void newLog(ResultRepository resultrepository)
	{
		chart.repaint();
	}

	public void newProgressResult(ResultRepository resultrepository)
	{
		curriter.setText(Integer.toString(getWalrasModel().curriter));
		bestiter.setText(Integer.toString(getWalrasModel().bestiter));
		diff.setText(Double.toString(getWalrasModel().bestf));
		control1.setText(Double.toString(getWalrasModel().bestcond1));
		control2.setText(Double.toString(getWalrasModel().bestcond2));
		clserved1.setText(Integer.toString(getWalrasModel().bestserved1));
		clserved2.setText(Integer.toString(getWalrasModel().bestserved2));
		clgone.setText(Integer.toString(getWalrasModel().bestgone));
		profit1.setText(Double.toString(getWalrasModel().bestu1));
		profit2.setText(Double.toString(getWalrasModel().bestu2));
		v1.setText(Double.toString(getWalrasModel().bestv1));
		v2.setText(Double.toString(getWalrasModel().bestv2));
		bestX11.setText(Double.toString(getWalrasModel().bestX11));
		bestY1.setText(Double.toString(getWalrasModel().bestY1));
		bestP1.setText(Double.toString(getWalrasModel().bestP1));
		bestX22.setText(Double.toString(getWalrasModel().bestX22));
		bestY2.setText(Double.toString(getWalrasModel().bestY2));
		bestP2.setText(Double.toString(getWalrasModel().bestP2));
		bestX12.setText(Double.toString(getWalrasModel().bestX12));
		bestX21.setText(Double.toString(getWalrasModel().bestX21));
		setupChart();
	}

	public synchronized void newResult(ResultRepository resultrepository)
	{
		curriter.setText(Integer.toString(getWalrasModel().curriter));
	}

	public ChartPoint point(int i)
	{
		cur_point_nr = i;
		return pointFromResult(super.results.resultAt(0));
	}

	public int pointCount()
	{
		return Math.min(profx.length, profy.length);
	}

	protected ChartPoint pointFromResult(Result result)
	{
		double d = profx[cur_point_nr];
		double d1 = profy[cur_point_nr];
		InvisibleDot invisibledot = new InvisibleDot(new Double(d), new Double(d1));
		return invisibledot;
	}

	public synchronized void prepare(ResultRepository resultrepository)
	{
		super.prepare(resultrepository);
		String as[] = {
			"1-st server profit (iteration)", "2-nd server profit (iteration)", "1-st server profit (x11)", "2-nd server profit (x21)", "1-st server profit (x12)", "2-nd server profit (x22)", "1-st server profit (y1)", "2-nd server profit (y2)", "1-st server profit (p1)", "2-nd server profit (p2)"
		};
		for(int i = 0; i < as.length; i++)
			projectionChoice.addItem(as[i]);

		setupChart();
		super.results.addObserver(this);
		show();
	}

	private void setWalrasModel(WalrasModel walrasmodel)
	{
		gWalrasModel = walrasmodel;
	}

	protected synchronized void setupChart()
	{
		Vector vector = new Vector();
		MathAxisMapper mathaxismapper = new MathAxisMapper();
		MathAxisMapper mathaxismapper1 = new MathAxisMapper();
		XAxis xaxis = new XAxis(mathaxismapper1);
		YAxis yaxis = new YAxis(mathaxismapper);
		series = new ConnectedSeries(this, xaxis, yaxis);
		Grid grid = new Grid(xaxis, yaxis);
		String as[] = {
			"U1", "U2"
		};
		yaxis.setLabel(as[projection % 2]);
		String as1[] = {
			"it", "it", "x11", "x21", "x12", "x22", "y1", "y2", "p1", "p2"
		};
		xaxis.setLabel(as1[projection]);
		if(only_refresh == 0)
		{
			profx = new double[PointCnt()];
			profy = new double[PointCnt()];
			original_profy = new double[PointCnt()];
			CalculateOutput();
		}
		cur_point_nr = 0;
		double d = GetYMin();
		double d1 = GetYMax();
		if(d == d1)
		{
			d--;
			d1++;
		}
		mathaxismapper.range.setRange(d, d1);
		mathaxismapper1.range.setRange(GetXMin(projection), GetXMax(projection));
		vector.addElement(series);
		xaxis.prepare();
		yaxis.prepare();
		chart.setup(vector);
		chart.setLeftAxis(yaxis);
		chart.setXAxis(xaxis);
		chart.setGrid(grid);
		chart.repaint();
	}
}
