/*
 * Evaluation.java
 *
 * Created on Ketvirtadienis, 2007, Sausio 25, 17.07
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Main.algorithms;

import java.text.*;
import Main.*;


public class Evaluation extends AbstractAlgorithm
{

        public int indeksas;        
        private double[][] mMatrixXY;

	private AbstractAlgorithm mAlgorithm;

	public void setAlgorithm(AbstractAlgorithm algorithm)
	{
		mAlgorithm= algorithm;
	}

	public AbstractAlgorithm getAlgorithm()
	{
		return mAlgorithm;
	}

	public void setMatrixXY(double[][] matrix)
	{
		mMatrixXY= matrix;
	}

	public void solve(boolean calculateUV)
	{
		solve();
	}

        
	public void solve()
	{
	//	mLog.logn("----------------------------------");
	//	mLog.logn("EVALUATION BEGINS");

            
                mAlgorithm.calculateMatrixU();

		mAlgorithm.calculateMatrixV();
                
		double u= mAlgorithm.calculateU();
		double v= mAlgorithm.calculateV();
		double tmpU= mAlgorithm.U;
                double tmpV= mAlgorithm.V;

		//IFAlgorithm mAlgorithmTmp= new IFAlgorithm();
		SimpleSolver mAlgorithmTmp= new SimpleSolver();
                //MaxMinAlgorithm mAlgorithmTmp= new MaxMinAlgorithm();
		//SEAlgorithm mAlgorithmTmp= new SEAlgorithm();
		mAlgorithmTmp.setMatrix(mAlgorithm.getMatrix());
		/*mAlgorithmTmp.setMatrixU(mAlgorithm.getMatrixU());
		mAlgorithmTmp.setMatrixV(mAlgorithm.getMatrixV());*/

		mAlgorithm.reset();


	//	mLog.logn("-- MATRIX U");
		calculateMatrixU();
	//	outputMatrix(getMatrixU());

	//	mLog.logn("-- MATRIX V");
		calculateMatrixV();
	//	outputMatrix(getMatrixV());
                

		mAlgorithmTmp.setMatrixU(matrixU);
		mAlgorithmTmp.setMatrixV(matrixV);

		mAlgorithmTmp.solve(false);

		mAlgorithm= mAlgorithmTmp;

		result= mAlgorithmTmp.getResult();


		/*if(!mAlgorithm.isSolved())
		{
			mLog.logn("EVALUATION FAILED");
		}
		else*/
		{
			
                    
                        U= mAlgorithmTmp.U;
			V= mAlgorithmTmp.V;
                        
                        
			mLog.logn("U* = "+ Utility.Round(u));
			mLog.logn(" V* = "+ Utility.Round(v));
                        
                        mLog.logn("Umax = " + Utility.Round(U));
			mLog.logn(" Vmax = " + Utility.Round(V));
                        

			

			setSolved( ( (Math.abs(u-U)) <0.00000001) && ( (Math.abs(v-V))<0.00000001) );

			if(!mAlgorithmTmp.isSolved())
                        {                            
                            mLog.logn("EVALUATION EQUATION SOLVER FAILED"); 
                            indeksas3 = 0;
                        }

			if(isSolved())
			{
			       mLog.logn("TESTING CONDITIONS ARE TRUE");
                               mLog.logn("Umax <= U* AND Vmax <= V*");
                               mLog.logn("EQUILIBRIUM FOUND");
                               mLog.logn("----------------------------------");
                               indeksas3 = 5;
                               indeksas = 0;
                               //mLog.logn("Evalindeksas = " + indeksas);
                               
                                
			}
			else
			{
                            mLog.logn("TESTING CONDITIONS ARE FALSE: ");
                            mLog.logn("Umax > U* OR Vmax > V*");
                            mLog.logn("EQUILIBRIUM NOT FOUND");
                            indeksas = 10;
                           // mLog.logn("Evalindeksas = " + indeksas);
                            indeksas3 = 0;
                            indeksas2 = 0;
                            
                     
                           /* DSAAlgorithm mDSA = new DSAAlgorithm();
                            mDSA = new DSAAlgorithm();
                            mDSA.setMatrix((double[][]) matrix.clone());

                            mDSA.setMatrixU(getMatrixU());
                            mDSA.setMatrixV(getMatrixV());

                            mDSA.solve(isSolved());*/
                            
                           
			}
		}
                
	//	mLog.logn("EVALUATION ENDS");
	//	mLog.logn("----------------------------------");
	}
        
	        
        public void calculateMatrixU()
	{
		super.calculateMatrixU();

		for(int i= 0; i< matrixU.length; i++)
		{
			for(int j= 0; j< matrixU[i].length; j++)
			{
				matrixU[i][j]*= mMatrixXY[j][1];
			}
		}
	}

	public void calculateMatrixV()
	{
		super.calculateMatrixV();

		for(int i= 0; i< matrixV.length; i++)
		{
			for(int j= 0; j< matrixV[i].length; j++)
			{
				matrixV[i][j]*= mMatrixXY[i][0];
			}
		}
	}
}
