📄 problem3.h
字号:
#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)
{
int i;
for (i = 0; i < 2; i++) {
low[i] = -1.0;
high[i] = 1.0;
}
}
static void SetConfiger(Configer *cfg)
{
cfg->VariableCount = 2;
cfg->ConstraintCount = 1;
cfg->EqConstraintCount = 1;
cfg->TestTreeCount = 5;
cfg->MaxDepth = 5;
cfg->TreeDim = 3;
cfg->TreeNodeCount = 8;
cfg->PopulationSize = 20;
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);
mValue = s1;
mConstraints[0] = mVariables[1] - mVariables[0] * mVariables[0];
mIsFeasible = true;
if (fabs(mConstraints[0]) - delta > 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -