/*
 * SharedVars.h
 *
 *  Created on: Jul 27, 2015
 *      Author: jjpalacios
 */

#ifndef SRC_ECOBJECTS_SHAREDVARS_H_
#define SRC_ECOBJECTS_SHAREDVARS_H_

#include "../heading.h"
#include "../utils/Random.h"
#include "../utils/ParameterDB.h"

namespace clbDEB {

/**
 * This struct will keep those objects that are shared by all the
 * components of any evolutionary algorithm
 *
 * By having all those objects together, we won't need to pass them
 * one by one from one component to another.
 *
 * A clear example of one of this objects that are commonly shared is
 * the random number generator.
 *
 * @author jjpalacios
 *
 */
class SharedVars {
public:
	/**
	 * Random Number Generator
	 */
	Random * rng;

	/**
	 * Parameter database
	 */
	ParameterDB * parameters;

	/**
	 * Number of elements to tune
	 */
	int numElements;

	/**
	 * Minimum value allowed in each gene
	 */
	std::vector<double> minGene;

	/**
	 * Maximum value allowed in each gene
	 */
	std::vector<double> maxGene;

	/**
	 * Indicates if we are maximizing or minimizing the fitness function
	 */
	bool maximize;

	/**
	 * Default constructor
	 */
	SharedVars() {
		rng = NULL;
		parameters = NULL;
		maximize = false;
		numElements = 0;
	}

	/**
	 * Destructor
	 */
	~SharedVars() {
		delete rng;
		delete parameters;
	}

	/**
	 * Compares the fitness of two values given the "maximize" value
	 */
	template<typename T>
	bool hasBetterFitness(const T a, const T b) const {
		if(maximize) return a > b;
		else return a < b;
	}

	/**
	 * Returns the fitness value to use as a penalty for bad individuals
	 * For instance infeasible solutions or individuals penalized by clearing
	 */
	double penaltyFitness() const {
		if(maximize) return 0;
		else return Infd;
	}

	/**
	 * Check if a fitness value is a penalized value
	 */
	bool isPenalty(double fitness) const {
		if(maximize) return !(fitness > this->penaltyFitness());
		else return !(fitness < this->penaltyFitness());
	}
};


}

#endif /* SRC_ECOBJECTS_SHAREDVARS_H_ */
