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

package Main.algorithms;

import Main.*;

public class QSEAlgorithm extends SEAlgorithm {
	DSAAlgorithm mDSA;

	boolean pureStrategy = false;

	public void solve() {
		solve(true);
	}

	public void solve(boolean calculateUV) {
            indeksas3 = 5;
            indeksas5 = 1;
            if (indeksas2 == 0){
		mLog.logn("----------------------------------");
		mLog.logn("QSEA BEGINS");
            }

		if (calculateUV) {
			if (indeksas2 == 0)
                            mLog.logn("-- MATRIX U");
			calculateMatrixU();
			if (indeksas2 == 0)
                            outputMatrix(getMatrixU());
			if (indeksas2 == 0)
                            mLog.logn("-- MATRIX V");
			calculateMatrixV();
			if (indeksas2 == 0)
                            outputMatrix(getMatrixV());
		}

		result = new double[matrix.length][2];
		fillMatrix(result, -1);

		indexes = new int[matrix.length];
		fillIndexes(indexes);

		indexesClone = (int[]) indexes.clone();

		pureStrategy = false;
		boolean solved = false;
		mTempMatrix = (double[][]) matrix.clone();
               if (indeksas2 == 0){
                    mIFA = new IFAlgorithm();
                    mDSA = new DSAAlgorithm();
               }
		mDSA.setMatrix(mTempMatrix);

		mDSA.setMatrixU(getMatrixU());
		mDSA.setMatrixV(getMatrixV());
                
                mDSA.solve(solved);
		if (mDSA.getResult() == null || mDSA.getResult().length == 0) {
			solved = false;
		} else {
			result = mDSA.getResult();
			// fillRestOfRezult(mDSA.getResult());
			// setMatrixU(mDSA.getMatrixU());
			// setMatrixV(mDSA.getMatrixV());
			// break;
			solved = true;

			U = mDSA.U;
			V = mDSA.V;

			pureStrategy = true;
		}
		mIFA.setMatrix(mTempMatrix);
		mIFA.setMatrixU(getMatrixU());
		mIFA.setMatrixV(getMatrixV());

		while (!solved) {

			mIFA.solve(solved);
			double[][] tempRezult = mIFA.getResult();
	//		mLog.logn("-- Throwing negative --");
			try {
				solved = !throwNegative(tempRezult);
			} catch (Exception e) {
				mLog.logn("--QSEA ALGORITHM FAILED");
				setSolved(false);
				return;
			}

			if (!solved) {
	//			mLog.logn("-- Setting reduced matrix U --");
				mIFA.setMatrixU(reduceMatrixUV(indexesClone, getMatrixU()));
	//			mLog.logn("-- Setting reduced matrix V --");
				mIFA.setMatrixV(reduceMatrixUV(indexesClone, getMatrixV()));
			} else {
				setMatrixU(mIFA.getMatrixU());
				setMatrixV(mIFA.getMatrixV());
	//			mLog.logn("-- Marging results --");
				// result= mIFA.getResult();
				fillRestOfRezult(indexesClone, tempRezult);

				U = mIFA.U;
				V = mIFA.V;
			}
		}
		// while(!solved);
		setSolved(true);

	/*	if (indeksas2 == 0){
                    mLog.logn("QSEA ENDS");
                    mLog.logn("----------------------------------");
                }*/
                indeksas2 +=1;

	}
        
	public boolean isPureStrategy() {
		return pureStrategy;
	}

}
