/*
Modified BSD License

This file is a part of Statistical package and originates from:
http://sf.net/p/enjomitchsorbit

Copyright (c) 2014, Szymon "Enjo" Ender
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "BinSearchOpti2D.hpp"
#include "BinSearchOpti2DSubject.hpp"
#include "GeneralMath.hpp"
#include <cmath>
#include <utility>

using namespace EnjoLib;

BinSearchOpti2D::BinSearchOpti2D(const Point & minArgs, const Point & maxArgs, double eps)
{
    this->m_minArgs = minArgs;
    this->m_maxArgs = maxArgs;
    this->m_eps = eps;
    // Bound binary seach should finish in log2(n) iterations. Let's allow for max 2 logs.
	GeneralMath gm;
    const double numSlices = gm.round( (maxArgs-minArgs).len() / eps);
    this->m_maxIter = 2 * gm.round(gm.Log2(numSlices));
}

BinSearchOpti2D::~BinSearchOpti2D()
{
}

Result<Point> BinSearchOpti2D::Run( BinSearchOpti2DSubject & subj ) const
{
//    GeneralMath gm;
    Point mid;
    double valMid;
    bool valMidDirty = true; // Caching
    int i = 0;
    Point a = m_minArgs;
    Point b = m_maxArgs;
    bool bmaxIter = false;
    do
    {   // Cut the argument in slices until the value (value) is below threshold (binary search)
        Point ab = Point(a.x, b.y);
        Point ba = Point(b.x, a.y);
        mid = (a + b) / 2; // Midpoint
        Point botLeft = (a + mid) / 2;
        Point topRigh = (b + mid) / 2;
        Point botRigh = (ba + mid) / 2;
        Point topLeft = (ab + mid) / 2;
        double valBotLeft = subj.UpdateGetValue(botLeft);
        double valTopRigh = subj.UpdateGetValue(topRigh);
        double valBotRigh = subj.UpdateGetValue(botRigh);
        double valTopLeft = subj.UpdateGetValue(topLeft);
        if (valMidDirty)
            valMid = subj.UpdateGetValue(mid);
        if (valBotLeft < valTopRigh && valBotLeft < valBotRigh && valBotLeft < valTopLeft && valBotLeft <= valMid)
        {
            b = mid; // Narrow right border
            valMidDirty = true;
        }
        else if (valTopRigh < valBotLeft && valTopRigh < valBotRigh && valTopRigh < valTopLeft && valTopRigh <= valMid)
        {
            a = mid; // Narrow left border
            valMidDirty = true;
        }
        else if (valBotRigh < valBotLeft && valBotRigh < valTopRigh && valBotRigh < valTopLeft && valBotRigh <= valMid)
        {
            a.x = mid.x;
            b.y = mid.y;
            valMidDirty = true;
        }
        else if (valTopLeft < valBotLeft && valTopLeft < valTopRigh && valTopLeft < valBotRigh && valTopLeft <= valMid)
        {
            b.x = mid.x;
            a.y = mid.y;
            valMidDirty = true;
        }
        else // Narrow both borders, because the answer is inside
		{
			a = botLeft;
			b = topRigh;
			// value of the middle point stays the same and doesn't need to be recalculated
		}
        bmaxIter = ++i == m_maxIter;
    } while( (b-a).len()/2 > m_eps && ! bmaxIter ); // Continue searching, until below threshold
    //sprintf(oapiDebugString(), "i = %d, maxi = %d, arg = %lf, value = %lf, pdiff = %lf",i, m_maxIter, arg, diff, prevDiff);
    if ( bmaxIter )
        return Result<Point>(m_maxArgs, false);
    else
        //return Result<double>(arg, subj.IsValid( arg, value ));
        return Result<Point>(mid, true);
}
