Demo entry 6754798

吴熙炜

   

Submitted by anonymous on Jul 22, 2018 at 09:49
Language: C++. Code size: 5.7 kB.

/*
	游戏类别模糊聚类处理及贴近度计算 
*/ 
#include<bits/stdc++.h>
#define FM Fuzzy_Matrix
using namespace std;
int n,m;
class Matrix_Fault{}; 
class Fuzzy_Matrix{
	friend ostream &operator << (ostream &os,FM &A) {
		for (int i = 0;i<A.n;++i)
		  for (int j = 0;j<A.m;++j)
		   os << A.s[i][j] << (j == A.m ? '\n' : ' ');
		puts("");
		return os;
	}
	friend istream &operator >> (istream &is,FM &A) {
		for (int i = 0;i<A.n;++i)
		   for (int j = 0;j<A.m;++j)
		     is >> A.s[i][j];
		return is;
	}
	friend bool operator == (FM A,FM B)	{
		if (A.n!=B.n || A.m!=B.m) return false;
		for (int i = 0;i<A.n;++i)
		   for (int j = 0;j<A.m;++j)
		      if (A.s[i][j]!=B.s[i][j]) return false;
		return true;
	}
	private:	
		int n,m;
		double **s;
	public :
		FM (int n = 0,int m = 0) : n(n),m(m) {
			s = new double *[n];
			for (int i = 0;i<n;++i) s[i] = new double [m];
			for (int i = 0;i<n;++i)
			  for (int j = 0;j<m;++j)
			     s[i][j] = 0;
		}
		FM (const FM &A) {
			if (this == &A) return;
			for (int i = 0;i<n;++i) delete [] s[i];
			delete [] s;
			n = A.n;m = A.m;
			s = new double *[n];
			for (int i = 0;i<n;++i) s[i] = new double [m];
			for (int i = 0;i<n;++i)
			  for (int j = 0;j<m;++j) 
			     s[i][j] = A.s[i][j]; 
		}
		FM operator * (FM A) {
			if (m != A.n) throw Matrix_Fault(); 
			FM C(n,A.m);
			for (int i = 0;i<n;++i)
			  for (int j = 0;j<A.m;++j)
			    for (int k = 0;k<m;++k)
			      C.s[i][j] = max(C.s[i][j],min(s[i][k],A.s[k][j]));
			return C;
		}
		~FM() {
			for (int i = 0;i<n;++i) delete [] s[i];
			delete [] s;
		}
		FM &operator = (const FM &A) {
			for (int i = 0;i<n;++i) delete [] s[i];
			delete [] s;
			n = A.n;m = A.m;
			s = new double *[n];
			for (int i = 0;i<n;++i) s[i] = new double [m];
			for (int i = 0;i<n;++i)
			  for (int j = 0;j<m;++j)
			    s[i][j] = A.s[i][j];
			return *this;
		}
		void Min_Max_Standardize () {
			double Min = 1e60,Max = 0;
			for (int j = 0;j<m;++j)
			{
				Min = 1e60;Max = 0;
				for (int i = 0;i<n;++i)
					Min = min(Min,s[i][j]),Max = max(Max,s[i][j]);
				for (int i = 0;i<n;++i)
					s[i][j] = (s[i][j] - Min) / (Max - Min);
			}	    
		}
		FM pow(FM A,int n) {
			FM B = A;
			for (n--;n;n>>=1) 
			{
				if (n & 1) B = B * A;
				A = A * A;
			}
			return B;
		}
		/*
			type = 1:Max & Min 
				X[i][j] = sigema(min(s[i][k],s[j][k])/(sigema(max(s[i][k],s[j][k]));
			type = 2: Arithmetic mean & Min
				X[i][j] = 2 * sigema(min(s[i][k],s[j][k])) / sigema(s[i][k] + s[j][k]);
			type = 3: Geometric mean & Min
				X[i][j] = sigema(min(s[i][k],s[j][k])) /  sigema(sqrt(s[i][k] * s[j][k]));
			type = 4: Absolute value & Index
				X[i][j] = exp(-sigema(abs(s[i][k] - s[j][k])));
			type = 5: Included angle cosine method  
				X[i][j] = sigema(s[i][k] * s[j][k]) / sqrt(sigema(s[i][k] ^ 2) * sigema(s[j][k] ^ 2));
			type = 6: Correlation coefficent
				X[i][j] = abs(sigema((s[i][k] - AVG(s[i])) * (s[j][k] - AVG(s[j]))) / sqrt(sigema((s[i][k] - AVG(s[i])) ^ 2 * sigema((s[j][k] - AVG(s[j])) ^ 2)); 
		*/ 
		FM Type1()	{
			FM X(n,n);
			for (int i = 0;i<n;++i)
			  for (int j = 0;j<n;++j)
			  {
			  	double fz = 0,fm = 0;
  	for (int k = 0;k<m;++k) fz+=min(s[i][k],s[j][k]),fm+=max(s[i][k],s[j][k]);
			  	X.s[i][j] = fz / fm;
			  }
			return X;
		}
		FM Type2()	{
			FM X(n,n);
			for (int i = 0;i<n;++i)
			  for (int j = 0;j<n;++j)
			  {
			  	double fz = 0,fm = 0;
			  	for (int k = 0;k<m;++k) fz += min(s[i][k],s[j][k]),fm += s[i][k] + s[j][k]; 
			  	X.s[i][j] = 2 * fz / fm;
			  }
			return X;
		}
		FM Type3()	{
			FM X(n,n);
			for (int i = 0;i<n;++i)
			  for (int j = 0;j<n;++j)
			  {
			  		double fz = 0,fm = 0;
			  		for (int k = 0;k<m;++k) fz += min(s[i][k],s[j][k]),fm += sqrt(s[i][k] * s[j][k]);
			  		X.s[i][j] = fz / fm;
			  } 
			return X;
		}
		FM Type4()	{
			FM X(n,n);
			for (int i = 0;i<n;++i)
			  for (int j = 0;j<n;++j)
			  {
			  		double num = 0;
			  		for (int k = 0;k<m;++k) num -= abs(s[i][k] - s[j][k]);
					X.s[i][j] = exp(num); 
			  }
			return X;
		}
		FM Type5()	{
			FM X(n,n);
			for (int i = 0;i<n;++i)
			  for (int j = 0;j<n;++j)
			  {
			  		double fz = 0,fm1 = 0,fm2 = 0;
			  		for (int k = 0;k<m;++k) fz += s[i][k] * s[j][k],fm1 += s[i][k] * s[i][k],fm2 += s[j][k] * s[j][k];
					X.s[i][j] = fz / sqrt(fm1 * fm2); 
			  }
			return X;
		}
		FM Type6()	{
			FM X(n,n);
			double AVG[n];
			for (int i = 0;i<n;++i)
			{
				AVG[i] = 0;
				for (int j = 0;j<m;++j) AVG[i] += s[i][j];
				AVG[i] /= m; 
			}  
			for (int i = 0;i<n;++i)
			  for (int j = 0;j<n;++j)
			  {
			  		double fz = 0,fm1 = 0,fm2 = 0;
			  		for (int k = 0;k<m;++k) 
					  	fz += (s[i][k] - AVG[i]) * (s[j][k] - AVG[j]),fm1 += (s[i][k] - AVG[i]) * (s[i][k] - AVG[i]),fm2 += (s[j][k] - AVG[j]) * (s[j][k] - AVG[j]);
					X.s[i][j] = fabs(fz) / sqrt(fm1 * fm2);			  
			  }
			return X;
		}
		FM Fuzzy_Similar_Matrix(int type) {	
			switch(type){
				case 1: return Type1();
				case 2: return Type2();
				case 3: return Type3();
				case 4: return Type4();
				case 5: return Type5();
				case 6: return Type6();
				default: throw Matrix_Fault(); 
			}
		}
		FM Fuzzy_Equal_Matrix(int type) {
			FM R = Fuzzy_Similar_Matrix(type);
			return pow(R,n);
		}
		FM Min_Max_Close_Degree(FM &A) {
			FM p(n,1);
			for (int i = 0;i<n:++i)
			{
				double fz = 0,fm = 0;
				for (int j = 0;j<m;++j) fz+=min(A.s[0][j],s[i][j]),fm += max(A.s[0][j],s[i][j]);
				p.s[i][0] = fz / fm;
			}
			return p;
		}
};
int main()
{ 	
	return 0;
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).