/*
 * Copyright (c) 2017, Peter Frank Perroni (pfperroni@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * Additionally, if used for any scientific purpose, the following reference
 * must be cited:
 * 
 * Peter Frank Perroni, Daniel Weingaertner, and Myriam Regattieri Delgado.
 * 2017. Estimating Stop Conditions of Swarm Based Stochastic Metaheuristic
 * Algorithms. In Proceedings of GECCO '17, Berlin, Germany, July 15-19, 2017,
 * pg 43-50. DOI: http://dx.doi.org/10.1145/3071178.3071326
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * @mainpage Convergence Stabilization Modeling operating in Online Mode
 * 
 * CSMOn ( formely called of C'MOn! ) is an automated method to estimate
 * the best moment to stop swarm iterations based on the analysis of the
 * convergence behavior presented during optimization, aiming to provide
 * an effective balance between saving fitness evaluations and keeping
 * the optimization quality.
 * The convergence analysis is performed through a sequence of linear
 * regressions using exponential and log-like curves.
 *
 * @date 04/Mar/2017
 * @author Peter Frank Perroni (pfperroni@gmail.com)
 */

#ifndef CSMON_HPP_
#define CSMON_HPP_

#include <vector>
#include <cmath>
#include "ISearch.hpp"

using namespace std;

/**
 * @brief A point representing the number of evaluations and the respective fitness value.
 * 
 * @date 04/Mar/2017
 * @author Peter Frank Perroni (pfperroni@gmail.com)
 */
typedef struct _point{
	int x;
	double y;
	_point(int _x, int _y){
		x = _x, y = _y;
	}
}t_point;

/**
 * @brief Convergence Stabilization Modeling operating in Online Mode.
 * 
 * @date 04/Mar/2017
 * @author Peter Frank Perroni (pfperroni@gmail.com)
 */
class CSMOn{
	ISearch *search;
	bool autoRelaxation;
	int M, s;
	double R, minEstimatedFit;
	vector<t_point> gb;

	double decayE();
	double decayL();
	double alphaE(int p1, int p2);
	double alphaP(int p1, int p2);

public:
	CSMOn(ISearch *search, int M, double R, double minEstimatedFit);
	virtual ~CSMOn();
	void run();
	void getBest(int nBest);
	int adjustExp(double r);
	int adjustLog(double r, int pT);
	int getNEvals();
	double getFitness();
	int getBestPos(double *x);

};

#endif /* CSMON_HPP_ */