package trueParetoFront;

import java.util.*;
import java.io.*;


public class TruePFGenerator implements Serializable
{
	public static void main(String[] args) 
	{
		String[] problemSet = {"DTLZ1",	"DTLZ2","DTLZ3","DTLZ4"};
		Integer[] mSet = {3, 5, 8, 10, 15};
		Integer[] sampleSize = {91, 210, 156, 275, 135};
		String curDir = System.getProperty("user.dir");

		for (int problemIndex=0;problemIndex<problemSet.length ;problemIndex++ )
		{
			String problemName = problemSet[problemIndex];
			for (int mIndex=0;mIndex<mSet.length ;mIndex++ )
			{
				Integer m = mSet[mIndex];
				Integer size = sampleSize[mIndex];
				Double[][] directionVector = new Double[size][m];
				String FILEPATH = curDir + "/" + "DirectionVector/DirectionVector" + m.toString() + "D_" + size.toString() + ".dat";
				String TRUEPFPATH = curDir + "/" + "trueParetoFront/" + problemName + "M" + m.toString()+ ".dat";
				loadFile(FILEPATH, directionVector);
				printTruePFToFile(TRUEPFPATH, problemName, directionVector);
			}
		}		
	}


		private static void loadFile(String filePath, Double[][] directionVector){
			try {
				FileInputStream fis = new FileInputStream(filePath);
				InputStreamReader isr = new InputStreamReader(fis);
				BufferedReader br = new BufferedReader(isr);
				int numberOfObjectives = 0;
				int i = 0;
				int j = 0;
				String aux = br.readLine();
				while (aux != null) {
					StringTokenizer st = new StringTokenizer(aux);
					j = 0;
					numberOfObjectives = st.countTokens();
					while (st.hasMoreTokens()) {
						Double value = (new Double(st.nextToken()))
								.doubleValue();
						directionVector[i][j] = value;
						j++;
					}
					aux = br.readLine();
					i++;
				}
				br.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	  private static void printTruePFToFile(String path, String problemName, Double[][] directionVector){

		try {
		  FileOutputStream fos   = new FileOutputStream(path)     ;
		  OutputStreamWriter osw = new OutputStreamWriter(fos)    ;
		  BufferedWriter bw      = new BufferedWriter(osw)        ;            

				if (problemName.equals("DTLZ1"))
				{
					for (int i=0;i<directionVector.length ;i++ )
					{
						for (int j=0;j<directionVector[0].length ;j++ )
						{
							bw.write( 0.5*directionVector[i][j] + " ");
						}
						bw.newLine();
					}
				}else{
					for (int i=0;i<directionVector.length ;i++ )
					{
						for (int j=0;j<directionVector[0].length ;j++ )
						{
							bw.write( directionVector[i][j]/norm(directionVector[i]) + " ");
						}
						bw.newLine();
					}					
				}
		  bw.close();
		}catch (IOException e) {
		  e.printStackTrace();
		}  

	  } 


	  	private static Double norm(Double[] z) {
			Double sum = 0.0;
			for (int i = 0; i < z.length; i++)
				sum += z[i] * z[i];
			return Math.sqrt(sum);
		}




}


