/*
 * Copyright 2007-2011 Tom Castle & Lawrence Beadle
 * Licensed under GNU Lesser General Public License
 * 
 * This file is part of EpochX: genetic programming software for research
 * 
 * EpochX is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * EpochX is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with EpochX. If not, see <http://www.gnu.org/licenses/>.
 * 
 * The latest version is available from: http://www.epochx.org
 */
package tileworld;

import java.awt.*;
import java.util.*;
import java.util.List;

import org.epochx.ge.model.GEModel;
import org.epochx.ge.representation.GECandidateProgram;
import org.epochx.representation.CandidateProgram;
import org.epochx.tools.ant.*;
import org.epochx.tools.eval.*;
import org.epochx.tools.grammar.Grammar;

import bsh.Interpreter;

/**
 * Abstract XGE model for ant trail problems in the Java language. This class
 * provides the general functionality of ant trail models, but does not itself
 * define a trail. Ant trail models for the Java language should extend this
 * class.
 */
public class modelGeneticEvolution extends GEModel {
TileworldMain a;
   int num;
	/**
	 * The grammar that defines valid solution space.
	 */
    
	public static final String GRAMMAR_STRING = "<code> ::= O.bT ( <opScalar> , <opScalar> , <opVector> , <opVector> ) \n"    
                + "<opVector> ::= O.bT ( <opScalar> , <opScalar> , <opVector> , <opVector> ) | O.bT ( nOfT , 2 , <opVector> , <opVector> ) | O.bT ( 1 , nOfT , <opVector> , <opVector> ) | O.vA( <opVector> , <opVector> )| O.sP ( <opScalar> , <opVector> ) | O.sP ( -1 , <opVector> ) | O.rot( <opVector> ) | O.iZ( <opVector>, <opVector>, <opVector>) | <vector> \n"
                        + "<opScalar> ::= O.n( <opVector> ) | O.dP ( <opVector> , <opVector> ) | O.sA ( <opScalar> , <opScalar> )| O.t ( <opScalar> , <opScalar> ) | O.bTS ( <opScalar> , <opScalar> , <opScalar> , <opScalar> ) | <scalar> \n"
                        + "<vector> ::= T1 | H1 | A1 | A2 | BD | prD | O.rV() \n"       
                        + "<scalar> ::= 0 | 1 | -1 | 2 | 0.5 | nOfT | dA2 | A2r | O.rS()| qT1[1] | qT1[2] | qH1[1]| qH1[2] \n";

	// Java interpreter for performing evaluation.
	private final Interpreter interpreter;



	
	public modelGeneticEvolution() {
		setGrammar(new Grammar(GRAMMAR_STRING));
                
		interpreter = new Interpreter();
                a=new TileworldMain();
                num=0;
	}


	@Override
	public double getFitness(CandidateProgram p) {
		GECandidateProgram program = (GECandidateProgram) p;
                
               /* if(p.getRandomSeed()==0) {
                    long rgenseed = System.currentTimeMillis();                    
                    p.setRandomSeed(rgenseed);
                }*/
                
                int score= a.runTest(program,interpreter);
                score+=a.runTest(program,interpreter);
                  //  System.out.println(num++ +" - score:"+score +" - lenght:"+program.getParseTreeDepth());
		// Calculate score.
		return score/2;
	}

}
