/******************************************************************************
* 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/                                      *
******************************************************************************/

// functioncontrol.h -- class definition for FunctionControl. This class
// controls access to the cache and the user provided functions (truth and
// surrf).

#ifndef _FUNCTIONCONTROL_H_
#define _FUNCTIONCONTROL_H_

class AugmentedPoint;
class CacheControl;
class Description;
class GeneralConstraints;
class Parameters;
class SurrFunction;
class SurrogateControl;
class TruthFunction;

class FunctionControl
{
  // FunctionControl has the following variables:
  // - a counter of function evaluations (truth evaluations + cache hits).
  // - a counter of truth evaluations.
  // - a few strings, used for calling black boxes.
  // - a pointer to the CacheManager object.
  // - a pointer to the GeneralConstraints object.
  // - a pointer to the SurrogateManager object.
  // - pointers to the TruthFunction and SurrFunction objects, used when
  //   there's no black box in the problem.
 private:
  char * input_file, * output_file, * surrogate_box, * truth_box;
  int display_factor, new_truth_evaluations, truth_evaluations;
  CacheControl * cache_control;
  const Description * const description;
  GeneralConstraints * gen_cons;
  const Parameters * const parameters;
  SurrFunction * surr_f;
  SurrogateControl * surr_control;
  TruthFunction * truth;

  // The class methods are described in "functioncontrol.cpp".
  char * createBoxString(char * file, const char * box);
  double giveMeTruth(AugmentedPoint & ap, int & term_flag);
  void createBlackBoxesAndFileStrings();
  void openInputFile(const double * coord);

 public:
  FunctionControl(const Description * const des, const Parameters * const par);
  ~FunctionControl();
  bool evaluateTruth(AugmentedPoint & ap, int & term_flag,
		     bool startingPoint = false);
  int getPermCacheHits() const;
  int getTempCacheHits() const;
  void closeCaches() const;
  void continueRun();
  void evaluateFlow(AugmentedPoint & ap);
  void fillWithBestPoint(double * coord, double * incumbent);
  void linkWithSurrogate(SurrogateControl & surr);
  void setDisplay(const int display);
  void showInput(ofstream & fout) const;
  void showOutput(ofstream & fout) const;
  void writeHistoryLegend(ofstream & history);

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

  // Returns the number of 'new' truth evaluations.
  int getNewTruthEvaluations() const {return new_truth_evaluations;}
  // Returns the number of truth evaluations.
  int getTruthEvaluations() const {return truth_evaluations;}
};
#endif
