package search.mcts.selection;

import java.util.concurrent.ThreadLocalRandom;

import other.state.State;
import search.mcts.MCTS;
import search.mcts.nodes.BaseNode;

/**
 * Progressive Bias, as described in "Progressive Strategies for 
 * Monte-Carlo Tree Search" by Chaslot et al.
 * 
 * Assumes that a heuristic function has been defined inside the MCTS object.
 * 
 * @author Dennis Soemers
 */
public final class ProgressiveBias implements SelectionStrategy
{
	
	//-------------------------------------------------------------------------
	
	/** Exploration constant */
	protected double explorationConstant;
	
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor with default value sqrt(2.0) for exploration constant
	 */
	public ProgressiveBias()
	{
		this(Math.sqrt(2.0));
	}
	
	/**
	 * Constructor with parameter for exploration constant
	 * @param explorationConstant
	 */
	public ProgressiveBias(final double explorationConstant)
	{
		this.explorationConstant = explorationConstant;
	}
	
	//-------------------------------------------------------------------------

	@Override
	public int select(final MCTS mcts, final BaseNode current)
	{
		assert(mcts.heuristics() != null);
		
		int bestIdx = -1;
        double bestValue = Double.NEGATIVE_INFINITY;
        int numBestFound = 0;
        
        final double parentLog = Math.log(Math.max(1, current.sumLegalChildVisits()));
        final int numChildren = current.numLegalMoves();
        final State state = current.contextRef().state();
        final int moverAgent = state.playerToAgent(state.mover());
        final double unvisitedValueEstimate = 
        		current.valueEstimateUnvisitedChildren(moverAgent);

        for (int i = 0; i < numChildren; ++i) 
        {
        	final BaseNode child = current.childForNthLegalMove(i);
        	final double exploit;
        	final double explore;
        	final double heuristicScore;

        	if (child == null)
        	{
        		exploit = unvisitedValueEstimate;
        		explore = Math.sqrt(parentLog);
        		heuristicScore = unvisitedValueEstimate;
        	}
        	else
        	{
        		exploit = child.exploitationScore(moverAgent);
        		final int numVisits = child.numVisits() + child.numVirtualVisits();
        		explore = Math.sqrt(parentLog / numVisits);
        		
        		// No idea what kind of weight we should use, just guessing 10.0 for now based on nothing
        		heuristicScore = (10.0 * child.heuristicValueEstimates()[moverAgent]) / numVisits;
        	}

        	final double ucb1Value = exploit + explorationConstant * explore + heuristicScore;

        	if (ucb1Value > bestValue)
        	{
        		bestValue = ucb1Value;
        		bestIdx = i;
        		numBestFound = 1;
        	}
        	else if 
        	(
        		ucb1Value == bestValue 
        		&& 
        		ThreadLocalRandom.current().nextInt() % ++numBestFound == 0
        	)
        	{
        		bestIdx = i;
        	}
        }
        
        return bestIdx;
	}
	
	//-------------------------------------------------------------------------
	
	@Override
	public int backpropFlags()
	{
		return 0;
	}
	
	@Override
	public int expansionFlags()
	{
		return MCTS.HEURISTIC_INIT;
	}
	
	@Override
	public void customise(final String[] inputs)
	{
		if (inputs.length > 1)
		{
			// we have more inputs than just the name of the strategy
			for (int i = 1; i < inputs.length; ++i)
			{
				final String input = inputs[i];
				
				if (input.startsWith("explorationconstant="))
				{
					explorationConstant = Double.parseDouble(
							input.substring("explorationconstant=".length()));
				}
				else
				{
					System.err.println("Progressive Bias ignores unknown customisation: " + input);
				}
			}
		}
	}
	
	//-------------------------------------------------------------------------

}
