/******************************************************************************
* NOMAD -  Nonlinear Optimization for Mixed vAriables and Derivatives.        *
* Copyright (C) 2001-2004  Gilles Couture - Ecole Polytechnique,              *
*                          Charles Audet - Ecole Polytechnique,               *
*		           John Dennis - Rice University.                     *
*                                                                             *
* Author: Gilles Couture                                                      *
* École Polytechnique de Montréal - GERAD                                     *
* C.P. 6079, Succ. Centre-ville, Montréal (Québec) H3C 3A7 Canada             *
* Tel:  (514) 340-6053 x6928                                                  *
* Email: Gilles.Couture@gerad.ca                                              *
* Fax: (514) 340-5665                                                         *
*                                                                             *
* This program is free software; you can redistribute it and/or               *
* modify it under the terms of the GNU General Public License                 *
* as published by the Free Software Foundation. You can find the              *
* GNU General Public License at:                                              *
*             http://www.gnu.org/copyleft/gpl.html                            *
*                                                                             *
* This program 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 General Public License for more details.                                *
*                                                                             *
* You should have received a copy of the GNU General Public License           *
* along with this program; if not, write to the Free Software                 *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
*                                                                             *
* You can find information on the NOMAD software at:                          *
*             http://www.gerad.ca/NOMAD/                                      *
******************************************************************************/

// evaluator.h -- class definition for Evaluator. This class controls the data
// flow between the FunctionControl, Constraints and AugmentedPoint objects.

#ifndef _EVALUATOR_H_
#define _EVALUATOR_H_
#include <vector>
using namespace std;
#include "augmentedpoint.h"

class Constraints;
class Description;
class Filter;
class FunctionControl;
class Parameters;
class Statistics;
class SurrogateControl;

class Evaluator
{
  // Evaluator has the following variables:
  // - 'consecutive_fails' counts the number of consecutive failures, i.e.
  //   the number of consecutive function evaluations that did not find a
  //   better iterate.
  // - 'display_factor' is used for debugging.
  // - 'polldir_index' indicates which point in the 'list_of_points' structure
  //   had a lower truth function value.
  // - 'teval' records the number of truth evaluations when the last best
  //   iterate was found.
  // - A pointer to the starting point.
  // - Constant pointers to Description and Parameters.
  // - Pointers to the Constraints, Filter, FunctionControl and Statistics
  //   objects.
 private:
  int consecutive_fails, display_factor, polldir_index, teval;
  AugmentedPoint * starting_point;
  Constraints * constraints;
  const Description * const description;
  Filter * filter;
  FunctionControl * f_control;
  const Parameters * const parameters;
  Statistics * stats;

  // These class methods are described in "evaluator.cpp".
  int evaluateTruth(AugmentedPoint * ap, int & term_flag,
		    bool startingPoint = false);
  void updateStatistics(const int sent_from, const double fx_decrease);

 public:
  Evaluator(const Description * const des, const Parameters * const par);
  ~Evaluator();
  const AugmentedPoint * getFeasibleIncumbent();
  const AugmentedPoint * getInfeasibleIncumbent();
  bool feasible() const;
	bool isPrimaryFeasible() const;
  double getTruth() const;
  double * getLowerBounds() const;
	double * getUpperBounds() const;
  int evaluateStartingPoint(int & term_flag);
  int getNewTruthEvaluations() const;
  int getPermCacheHits() const;
  int getTempCacheHits() const;
  int getTruthEvaluations() const;
  int processThisList(std::vector<AugmentedPoint *> & list_of_points,
		      bool entire_list, int & term_flag, ofstream & fout,
		      ofstream & fgraph, ofstream & history,
		      const int sent_from = POLL);
  int * getProximityArray() const;
  void continueRun();
  void endProgram();
  void evaluateFlow(std::vector<AugmentedPoint *> & list_of_points);
  void fillWithBestPoint(double * coord);
  void isStartingPointFeasible();
  void linkWithSurrogate(SurrogateControl & surr) const;
  void setDisplay(const int display);
  void showFeasibleIncumbent() const;
  void showFeasibleIncumbent(ofstream & fout) const;
	void showFilter() const;
  void showInput(ofstream & fout) const;
  void showOutput(ofstream & fout) const;
  void updateInfeasibleIncumbent();
  void updateProximityArray() const;
  void writeHistoryLegend(ofstream & history);

  // Inline methods.
  // ---------------

  // This method returns the index of the point in the 'list_of_points'
  // structure (in 'process_this_list(...)' ) which was a new best iterate.
  int getPollDirIndex() const {return polldir_index;}
  // This method returns the truth evaluations counter value of the last time
  // that a new best iterate was found.
  int getTeval() const {return teval;}
};
#endif
