灰色关联分析法(GRA)-C++实现

目录

1. 前言

2. 理论基础

3. 代码 


1. 前言

        最近小白在跟进一个项目,其中运用到了灰色关联分析法用来分析样本数据。同时在阅读了一些原理资料后,设计了一个模块化的C++程序(其实也不算模块化啦),关于原理方面,我在这里就不过多的赘述了,想要了解原理的同学,可以移步到下面的链接去了解。接下来我就把我编写的算法程序分享给大家。

2. 理论基础

链接:原理链接1、原理链接2

3. 代码 

(1).h文件:

#pragma once
#include 
#include 
#include 
#include 

using namespace std;

class GRA
{
public:
	GRA();
	void SetTargetSequence(vector target_);//设置目标序列
	void SetIndexSequence(vector> index_);//设置样本序列
	void SetWeight(vector w);//设置权重
	double PolarityMin(vector targetIndex_);//数据标准化
	double PolarityMax(vector targetIndex_);//数据标准化
	double IndexMax(vector targetIndex_);//数据标准化
	double IndexMin(vector targetIndex_);//数据标准化
	void ForwardProcessing(vector &index_, int num);//数据正向化
	void MaximumDimensionless(vector &index_);//数据标准化
	void MinimumDimensionless(vector& index_);//数据标准化
	void PushIndex(vector index_);
	vector> TransPose(vector> index_);//转置
	void GreyRelationCompute();//灰色关联度计算
	vector GetGreyRelation();//输出关联度向量
	vector> GetRelationMatrix();//输出关联度矩阵(用于检测数据计算准确性)


private:
	vector weight;//权重
	double row;
	vector targetSequence;//目标序列
	vector> indexSequence;//样本序列
	vector> relationMatrix;//关联度矩阵
	vector relation;//关联度向量

};

(2).cpp文件:

#include 
#include 
#include "GRA.h"

using namespace std;

GRA::GRA()
{
	row = 0.5;
	
}

void GRA::SetTargetSequence(vector target_)
{
	targetSequence = target_;
}

void GRA::SetIndexSequence(vector> index_)
{
	indexSequence = index_;
}

void GRA::SetWeight(vector w)
{
	weight = w;
}

double GRA::PolarityMax(vector targetIndex_)
{
	double tmp = fabs(targetSequence[0] - targetIndex_[0]);
	for (int i = 0; i < targetIndex_.size(); i++)
	{
		if (tmp < (fabs(targetSequence[i] - targetIndex_[i])))
		{
			tmp = fabs(targetSequence[i] - targetIndex_[i]);
		}
	}
	return tmp;

}

double GRA::PolarityMin(vector targetIndex_)
{
	double tmp = fabs(targetSequence[0] - targetIndex_[0]);
	for (int i = 0; i < targetIndex_.size(); i++)
	{
		if (tmp > (fabs(targetSequence[i] - targetIndex_[i])))
		{
			tmp = fabs(targetSequence[i] - targetIndex_[i]);
		}
	}
	return tmp;

}

double GRA::IndexMax(vector targetIndex_)
{
	double tmp = targetIndex_[0];
	for (int i = 0; i < targetIndex_.size(); i++)
	{
		if (tmp < targetIndex_[i])
		{
			tmp = targetIndex_[i];
		}
		else
		{
			continue;
		}
	}
	return tmp;
}

double GRA::IndexMin(vector targetIndex_)
{
	double tmp = targetIndex_[0];
	for (int i = 0; i < targetIndex_.size(); i++)
	{
		if (tmp > targetIndex_[i])
		{
			tmp = targetIndex_[i];
		}
		else
		{
			continue;
		}
	}
	return tmp;
}

void GRA::ForwardProcessing(vector &index_,int num)
{
	double Max = IndexMax(index_);
	for (int i = 0; i < index_.size(); i++)
	{
		index_[i] = fabs(targetSequence[num] - index_[i]);
	}
	return;
}

void GRA::MaximumDimensionless(vector& index_)
{
	double MAX = IndexMax(index_);
	double MIN = IndexMin(index_);
	for (int i = 0; i < index_.size(); i++)
	{
		index_[i] = (index_[i] - MIN) / (MAX - MIN);
	}
	return;
}


void GRA::MinimumDimensionless(vector& index_)
{
	double MAX = IndexMax(index_);
	double MIN = IndexMin(index_);
	for (int i = 0; i < index_.size(); i++)
	{
		index_[i] = (MAX - index_[i]) / (MAX - MIN);
	}
	return;
}

void GRA::PushIndex(vector index_)
{
	indexSequence.push_back(index_);
}

vector> GRA::TransPose(vector> index_)
{
	vector> result;
	for (int j = 0; j < index_[0].size(); j++)
	{
		vector tmp;
		for (int i = 0; i < index_.size(); i++)
		{
			tmp.push_back(index_[i][j]);
		}
		result.push_back(tmp);
	}
	return result;
}

void GRA::GreyRelationCompute()
{
	vector> Index = TransPose(indexSequence);//indexSequence 4x7,index 7x4
	vector tmp_1, tmp_2;
	double memberReslution_1, memberReslution_2;
	for (int i = 0; i < Index.size(); i++)
	{
		double member_1 = PolarityMin(Index[i]);
		
		double member_2 = PolarityMax(Index[i]);
		
		tmp_1.push_back(member_1);
		tmp_2.push_back(member_2);
	}
	memberReslution_1 = IndexMin(tmp_1);
	memberReslution_2 = IndexMax(tmp_2);
	for (int i = 0; i < Index.size(); i++)
	{
		vector re;
		/*re.push_back(i);*/
		for (int j = 0; j < Index[i].size(); j++)
		{
			re.push_back((memberReslution_1 + row * memberReslution_2) / (fabs(targetSequence[j] - Index[i][j]) + (row * memberReslution_2)));
		}
		relationMatrix.push_back(re);
	}
	double tmp = 0;
	for (int i = 0; i < relationMatrix.size(); i++)
	{
		for (int j = 0; j < relationMatrix[i].size(); j++)
		{
			
			tmp += weight[j] * relationMatrix[i][j];
		}
		tmp = tmp / weight.size();
		relation.push_back(tmp);
		tmp = 0;
	}
}

vector GRA::GetGreyRelation()
{
	return relation;
}
vector> GRA::GetRelationMatrix()
{
	return relationMatrix;
}

(3)测试文件:
 

#include 
#include 
#include "GRA.h"
#include 

using namespace std;

void main()
{
	vector index_1, index_2, index_3, index_4, index_5, index_6, index_7;//样本数
	index_1.push_back(0);
	index_1.push_back(1);
	index_1.push_back(0.173);
	index_1.push_back(0.168);

	index_2.push_back(0);
	index_2.push_back(1);
	index_2.push_back(0.212);
	index_2.push_back(0.022);

	index_3.push_back(0);
	index_3.push_back(1);
	index_3.push_back(0.248);
	index_3.push_back(0.083);

	index_4.push_back(0);
	index_4.push_back(1);
	index_4.push_back(0.222);
	index_4.push_back(0.118);

	index_5.push_back(0);
	index_5.push_back(1);
	index_5.push_back(0.199);
	index_5.push_back(0.051);

	index_6.push_back(0);
	index_6.push_back(1);
	index_6.push_back(0.188);
	index_6.push_back(0.06);

	index_7.push_back(0);
	index_7.push_back(1);
	index_7.push_back(0.204);
	index_7.push_back(0.035);

	vector> index;
	for (int i = 0; i < index_1.size(); i++)
	{
		vector tmp;
		tmp.push_back(index_1[i]);
		tmp.push_back(index_2[i]);
		tmp.push_back(index_3[i]);
		tmp.push_back(index_4[i]);
		tmp.push_back(index_5[i]);
		tmp.push_back(index_6[i]);
		tmp.push_back(index_7[i]);
		index.push_back(tmp);
	}
	cout << index.size() << ", " << index[0].size() << endl;
	GRA A;
	vector target;
	target.push_back(1);
	target.push_back(1);
	target.push_back(1);
	target.push_back(1);
	
	A.SetTargetSequence(target);
	A.ForwardProcessing(index[0], 0);
	A.MinimumDimensionless(index[2]);
	A.MinimumDimensionless(index[3]);
	vector weight(4, 1);
	A.SetWeight(weight);
	A.SetIndexSequence(index);
	A.GreyRelationCompute();
	vector> relation = A.GetRelationMatrix();
	for (int i = 0; i < relation.size(); i++)
	{
		for (int j = 0; j < relation[i].size(); j++)
		{
			cout << relation[i][j] << ",\t";
		}cout << endl;
	}
	vector path = A.GetGreyRelation();
	for (int i = 0; i < path.size(); i++)
	{
		cout << path[i] << ",\t";
	}
	cout << endl;

	system("pause");
	return;
}

你可能感兴趣的:(C++,个人项目,c++,开发语言,算法)