package multinomad.algorithms;

import multinomad.config.Configuration;
import multinomad.config.LoadProperties;
import multinomad.individuals.DoubleIndividual;
import multinomad.individuals.Population;
import multinomad.individuals.Trajectory;
import multinomad.tools.CooperativeSelection;
import multinomad.tools.Nomad;
import multinomad.tools.Seeds;

// Exploring, Filtering, Clustering, Local search
public class EFCL implements Runnable{
	
	private Population solutions;
	private Population filteredPop;
	
public void run() {
		
	// Step 1: Explore the landscape with a large population
		Population pop = new Population(true);
		for(DoubleIndividual ind : pop) {
			System.out.println("Initial:\t\t"+ ind.asString());
		}
		
	// Step 2: Filter the best solutions (using cooperative selection)
		filteredPop = new Population(false);
		
		for(int i=0;i<pop.size();i++) {
			filteredPop.add(
					CooperativeSelection.select(pop)
					);
		}
		
		for(DoubleIndividual ind : filteredPop) {
			System.out.println("Filtered:\t\t"+ ind.asString());
		}
		
		
	// Step 4: Local search	
	/*---------------------------------------------------------------	
	  solutions = new Population(false);
	 

		for(DoubleIndividual ind : filteredPop) {
			// Performs a hill-climber 
			Trajectory trajectory = Nomad.callPyNomadCec2013(ind.getChr().asdouble(), 0.1, 1000);
			DoubleIndividual result = trajectory.getBest();
			result.setFitness(Configuration.f.evaluate(result.getChr().asdouble()));
			// Shows results
			System.out.println("Result:\t\t"+ result.asString());
			//System.out.println("[Trajectory] Elapsed time: "+(System.currentTimeMillis()-start));
			
			
			solutions.add(result);
		}

		Seeds seeds = Configuration.f.how_many_goptima(solutions, 0.01);
		
		System.out.println("Found "+seeds.getCount()+" optima");
	--------------------------------------------------------------------*/	
//		if (numberOfOptima == Configuration.comp.getNoGoptima(Configuration.indexf)) {
//			System.out.println("Success: "+ numberOfOptima + " optima were found");	
//		}else {
//			System.out.println("Failure: "+ numberOfOptima + " out of "+ Configuration.comp.getNoGoptima(Configuration.indexf) +" optima were found");	
//		}
		
	}

	public Population getSolutions() {
		return solutions;
	}
	
	public Population getFilteredPop() {
		return filteredPop;
	}
	
	public static void main(String[] args) {
		//System.out.println(Pattern.quote("["));
		LoadProperties lp = new LoadProperties(args,null);
		Configuration.setConfiguration(lp);
		EFCL efcl = new EFCL();
		Thread t1 =new Thread(efcl);  
		t1.start();		
	}
}
