problem4.h

来自「文化算法的实际应用」· C头文件 代码 · 共 107 行

H
107
字号
#ifndef CAEP_PROBLEM_H
#define CAEP_PROBLEM_H

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

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] = low[1] = 0;
			high[0] = 2;
			high[1] = 3;
		}

		static void SetConfiger(Configer *cfg)
		{
			cfg->VariableCount = 2;
			cfg->ConstraintCount = 1;
			cfg->EqConstraintCount = 0;
			cfg->TestTreeCount = 5;
			cfg->MaxDepth = 5;
			cfg->TreeDim = 3;
			cfg->TreeNodeCount = 8;
			cfg->PopulationSize = 200;
			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 = -12 * mVariables[0]   - 7 * mVariables[1] + mVariables[1] * mVariables[1];

			mValue = s1;
			
			mConstraints[0] = mVariables[1] + 2 * mVariables[0] * mVariables[0] * mVariables[0] * mVariables[0] - 2;
			mIsFeasible = true;
			if(mConstraints[0] > 0)
			{
				mIsFeasible = false;
			}
		}

		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 + =
减小字号Ctrl + -
显示快捷键?