⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uofconstraint.h

📁 遗传算法vc++语言版源程序,台湾大学编写。
💻 H
字号:
#ifndef _UOFCONSTRAINT_
#define _UOFCONSTRAINT_
#include <vector>
#include "CommonUtility.h"
using namespace std;

//! Constraint Definition class
/*! This class confines the generaton of the corresponding parameters*/
class UOFConstraint : public UOFId
{
public:
	/** An enum type. 
     *  The selectable types for UOFConstraint
     */
	enum UOFCONSTAINT_TYPE
	{
		CT_FIX, /**< With this type, the corresponding parameter can't be modified  */
		CT_NUM, /**< With this type, the corresponding parameter can only be integer in the range between m_Min and m_Max  */
		CT_LIN, /**< With this type, the corresponding parameter will be generated in the range between by (m_Max-m_Min)/m_Steps* (random number from 0 to 1)*/
		CT_EXP, /**< With this type, the corresponding parameter will be generated in the range between by exponential step size*/
		CT_SET  /**< With this type, the corresponding parameter will be selected from m_Set*/
	};
	string	m_Name;	/**< the name of this constraint */
	int		m_Type;	/**< the type of this constraint */
	double	m_Max;	/**< the maximum value of this constraint */
	double	m_Min;	/**< the minimum value of this constraint */
	double	m_Steps;	/**< the number of steps of this constraint */
	double	m_DefVal;	/**< the default value of this constraint */
	vector<double>	m_Set;	/**< the candidate of this constraint */
	//! A constructor
	/*!	This is a constructor simply build up generate a object without initialization.*/

	UOFConstraint(){}
	//! A constructor that generates a object with initialization.
	/*!
	  \param n sets the m_Name.
	  \param t sets the m_Type.
	  \param d sets the m_DefVal.
	  \param max sets the m_Max.
	  \param min sets the m_Min.
	  \param steps sets the m_Steps.
	*/
	UOFConstraint(const char *n, int t, double d, double max, double min, double steps = 2.0)
	{
		m_Name = n; m_Type = t; m_DefVal = d; m_Max = max; m_Min = min; m_Steps = steps;
		if(m_Max<m_Min)
			swap(m_Max, m_Min);
	}
	//! A constructor that builds up generate a object with initialization.
	/*!
	  \param n sets the m_Name.
	  \param t sets the m_Type.
	  \param d sets the m_DefVal.
	  \param set sets the m_Set.
	*/
	UOFConstraint(const char *n, int t, double d, vector<double> set)
	{
		m_Name = n; m_Type = t; m_DefVal = d; m_Set = set;
	}

	//! This function returns a random value under this constraint. return cur if current m_Type is not surpportted.
	/*!
	  \param cur is not used currently
	*/
	double GetRandom(double cur)
	{
		double ss;
		switch(m_Type)
		{
		case CT_FIX:
			return m_DefVal;
		case CT_NUM:
			return (m_Min + (m_Max-m_Min) * ::RG.Random());
		case CT_LIN:
			ss = (m_Max-m_Min)/(m_Steps+1);
			return (m_Min + ss * (double)::RG.IRandom(0, (int)m_Steps));
		case CT_EXP:
			return cur;
		case CT_SET:
			return m_Set[::RG.IRandom(0, (int)m_Set.size()-1)];
		};
		return cur;
	}
	//!	This function tests if the input value exceed the constraint. If the m_Type is set to CT_SET, it returns false, otherwise it examines if cur is between m_Min and m_Max.
	/*!
	  \param cur is a double value.
	*/
	bool OverBound(double cur)
	{
		if(m_Type!=CT_SET)
			if(cur > m_Max || cur < m_Min)
				return true;
		return false;
	}
};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -