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

📄 problem5_1.h

📁 文化算法的实际应用
💻 H
字号:
#ifndef CAEP_PROBLEM_H
#define CAEP_PROBLEM_H

#include <cmath>
#include <iostream>
#include <algorithm>
#include "config.h"
#include "cell.h"

static const int  Pi = 3.1415926;
using namespace std;

namespace CAEP
{
	class Individual
	{
	private:

	public:
		float mValue;
		vector<float> mConstraints;
		float mViolation;
		bool mIsFeasible;
		Cell *mCell;
		int mVictoryCount;
		vector<float> mVariables;
		Configer &mConfiger;

		Individual(Configer &cfg):mConfiger(cfg),mConstraints(cfg.ConstraintCount),mVariables(cfg.VariableCount)
		{
		}
		
		static void Limit(vector<float>& low, vector<float>& high)
		{
			low[0] = -3.5;
			low[1] = 4.1;
			high[0] = 12.1;
			high[1] = 5.8;
			low [2] =0.0;
			high[2] = 1.0;
		}

		static void SetConfiger(Configer *cfg)
		{
			cfg->VariableCount = 3;
			cfg->ConstraintCount = 1;
			cfg->EqConstraintCount = 1;
			cfg->TestTreeCount = 5;
			cfg->MaxDepth = 5;
			cfg->TreeDim = 3;
			cfg->TreeNodeCount = 8;
			cfg->PopulationSize = 500;
			cfg->TopCount = 3;
			cfg->IsTrustIndividual = true;
		}

		void Evaluate()
		{
			int i;
			float s1 = 0.0, s2 = 0.0, s3 = 0.0;
			float delta = 0.001; // For the equality restrictions  


			/* Evaluate the objective function, and store its value in mValue .
			   Evaluate the constraints and store 1 in mIsFeasible if the point is feasible,
			   or 0 if it is infeasible.
			   Use the values stored in mVariables[i]. */


			//s1 = mVariables[0] * mVariables[0] + (mVariables[1] - 1) * (mVariables[1] - 1);

			s1 = 21.5 + mVariables[0]*sin(4 * Pi * mVariables[0]) + mVariables[1]*sin(20*Pi*mVariables[1]) + mVariables[2];
			mValue = -s1;

			mIsFeasible = true;
			mConstraints[0] = -1;

		}

		void Violation(vector<float> max)
		{
			int i;
			float v;

			if (mConfiger.IsTrustIndividual) {
				return;
			}

			mViolation = 0;

			for (i = 0; i < mConfiger.ConstraintCount; i++) {
				// Inequiality constraints 
				if (i < mConfiger.ConstraintCount - mConfiger.EqConstraintCount) {
					v = (mConstraints[i] > 0)? mConstraints[i]: 0;
				}
				// Equality constraints 
				else {
					v = fabs(mConstraints[i]);
				}

				if (v > max[i]) {
					max[i] = v;
				}
				mViolation += v/max[i];
			}
		}
	};
}

#endif

⌨️ 快捷键说明

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