📄 utilities.cs
字号:
using System;
using System.Collections;
namespace ClusterLibrary
{
/// <summary>
/// Summary description for Utils.
/// </summary>
public class Utilities
{
//=====================================
// Point 荤捞狼 芭府甫 备茄促
//=====================================
public static double Distance( Point pt1, Point pt2 ){
double result = 0;
try {
double temp = 0;
for ( int i = 0; i < pt1.Count ; i ++ ){
temp = (double) pt1[i] - (double) pt2[i];
result += (temp * temp);
temp = 0;
}
}
catch ( System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
}
return System.Math.Sqrt( result );
}
//====================================
// Point pt1, pt2 狼 芭府甫 t-norm栏肺 备茄促
//====================================
public static double Distance( Point pt1, Point pt2, double t ){
double result = 0;
try {
double temp = 0;
for ( int i = 0; i < pt1.Count ; i ++ ){
temp = System.Math.Abs( (double) pt1[i] - (double) pt2[i] );
result += System.Math.Pow( temp, t );
temp = 0;
}
}
catch ( System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
}
return System.Math.Pow( result, 1.0/t );
}
//=====================================
// Point狼 农扁甫 备茄促
//=====================================
public static double Distance( Point pt1 ){
double result = 0;
try {
for ( int i = 0; i < pt1.Count ; i ++ ){
result += (double) pt1[i] * (double) pt1[i];
}
}
catch ( System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
}
return System.Math.Sqrt( result );
}
public static double Distance( Point pt1, double t ){
double result = 0;
double temp = 0;
try {
for ( int i = 0; i < pt1.Count ; i ++ ){
temp = System.Math.Abs( (double)pt1[i] );
result += System.Math.Pow( temp, t );
}
}
catch ( System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
}
return System.Math.Pow( result, 1.0/t );
}
//=====================================
// Cluster 荤捞狼 芭府甫 备茄促
//=====================================
public static double Distance( Cluster cl1, Cluster cl2 ){
Point pt1 = cl1.Center();
Point pt2 = cl2.Center();
return Distance ( pt1, pt2 );
}
public static double Distance( Cluster cl1, Cluster cl2, double t ){
Point pt1 = cl1.Center();
Point pt2 = cl2.Center();
return Distance ( pt1, pt2, t );
}
//=====================================
// Cluster 郴狼 葛电 Point埃狼 芭府甫 备茄促
//=====================================
public static ArrayList IntraclusterDistance( Cluster cl1 ){
ArrayList resultList = new ArrayList();
Point pt1;
Point pt2;
for ( int i = 0; i < cl1.Count; i ++ ){
pt1 = (Point)cl1[i];
for ( int j = i + 1; j < cl1.Count; j ++ ){
pt2 = (Point)cl1[j];
resultList.Add( Distance( pt1, pt2 ) );
}
}
return resultList;
}
public static ArrayList IntraclusterDistance( Cluster cl1, double t ){
ArrayList resultList = new ArrayList();
Point pt1;
Point pt2;
for ( int i = 0; i < cl1.Count; i ++ ){
pt1 = (Point)cl1[i];
for ( int j = i + 1; j < cl1.Count; j ++ ){
pt2 = (Point)cl1[2];
resultList.Add( Distance( pt1, pt2, t ) );
}
}
return resultList;
}
//====================================
// Variance甫 备茄促
//====================================
public static Point Variance( Set clusters ){
Point center = clusters.Center();
Point temp;
Point result = new Point();
Cluster clTemp;
int number = 0;
for ( int i = 0; i < clusters.Count; i++ ){
clTemp = (Cluster) clusters[i];
number += clTemp.Count;
for ( int j = 0; j< clTemp.Count ; j++ ){
temp = (Point)clTemp[j] - center;
temp = temp ^ 2;
result += temp;
}
}
return result / (double) number;
}
public static Point Variance( Cluster cl ){
Point result = new Point();
Point center = cl.Center();
Point temp;
for ( int i = 0; i < cl.Count; i ++ ){
temp = (Point)cl[i] - center;
temp = temp ^ 2;
result = temp + result;
}
return result / cl.Count ;
}
//====================================
// 葛电 Cluster 尝府狼 芭府甫 备茄促
//====================================
public static ArrayList InterclusterDistance( Set clusters ){
ArrayList resultList = new ArrayList();
Point pt1;
Point pt2;
for ( int i = 0; i < clusters.Count; i ++ ){
pt1 = (Point)((Cluster)clusters[i]).Center();
for ( int j = i + 1; j < clusters.Count; j ++ ){
pt2 = (Point)((Cluster)clusters[j]).Center();
resultList.Add( Distance( pt1, pt2 ) );
}
}
return resultList;
}
public static ArrayList InterclusterDistance( Set clusters, double t ){
ArrayList resultList = new ArrayList();
Point pt1;
Point pt2;
for ( int i = 0; i < clusters.Count; i ++ ){
pt1 = (Point)((Cluster)clusters[i]).Center();
for ( int j = i + 1; j < clusters.Count; j ++ ){
pt2 = (Point)((Cluster)clusters[2]).Center();
resultList.Add( Distance( pt1, pt2, t ) );
}
}
return resultList;
}
//=====================================
// Max 蔼阑 备茄促
//=====================================
public static double Max( ArrayList alist ){
double result = 0;
try {
result = (double)alist[0];
for ( int i = 1; i < alist.Count ; i++ ){
if ( result < (double)alist[i] )
result = (double)alist[i];
}
}
catch (System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
}
return result;
}
public static double Max( double x, double y ){
if ( x>= y )
return x;
else
return y;
}
//=====================================
// Min 蔼阑 备茄促
//=====================================
public static double Min( ArrayList alist ){
double result = 0;
try {
result = (double)alist[0];
for ( int i = 1; i < alist.Count ; i++ ){
if ( result > (double)alist[i] )
result = (double)alist[i];
}
}
catch (System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
}
return result;
}
public static double Min ( double x, double y ){
if ( x <= y )
return x;
else
return y;
}
//======================================
// Riqt 蔼阑 备茄促
//======================================
/// <summary>
/// DBI
/// </summary>
/// <param name="u"></param>
/// <param name="i"></param>
/// <param name="q"></param>
/// <param name="t"></param>
/// <returns></returns>
public static double Riqt ( ClusterLibrary.Set u, int i, int q, int t){
return 0.0;
}
//===================================
// Within Cluster distance甫 备茄促
//===================================
/// <summary>
/// DBI
/// </summary>
/// <param name="x"></param>
/// <param name="v"></param>
/// <param name="q"></param>
/// <returns></returns>
public static double withInClusterD( ClusterLibrary.Cluster x, ClusterLibrary.Point v, int q){
int c = x.size();
double dis_temp = 0.0;
double result = 0.0;
for ( int i = 0; i < c; i++){
dis_temp = Distance((Point)x.getValueAt(i), v, 2);
result += ( System.Math.Pow( dis_temp, q ));
}
result = result / c;
return System.Math.Pow(result , 1.0/q );
}
/// <summary>
/// DBI
/// </summary>
/// <param name="x"></param>
/// <param name="q"></param>
/// <returns></returns>
public static double withInClusterD( ClusterLibrary.Cluster x, int q){
ClusterLibrary.Point vi = x.weight_center();
return withInClusterD(x, vi, q);
}
//====================================
// Inter Cluster Distance
//====================================
/// <summary>
/// Dunn
/// </summary>
/// <param name="S"></param>
/// <param name="T"></param>
/// <returns></returns>
public static double inter1(ClusterLibrary.Cluster S, ClusterLibrary.Cluster T){
int countS = S.Count;
int countT = T.Count;
ArrayList disList = new ArrayList();
try{
foreach ( Point ptS in S ){
foreach ( Point ptT in T ){
disList.Add( Distance( ptS, ptT ) );
}
}
return Min( disList );
}
catch( System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
return 0.0;
}
}
/// <summary>
/// Dunn
/// </summary>
/// <param name="S"></param>
/// <param name="T"></param>
/// <returns></returns>
public static double inter2(ClusterLibrary.Cluster S, ClusterLibrary.Cluster T){
int countS = S.Count;
int countT = T.Count;
ArrayList disList = new ArrayList();
try{
foreach ( Point ptS in S ){
foreach ( Point ptT in T ){
disList.Add( Distance( ptS, ptT ) );
}
}
return Max( disList );
}
catch( System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
return 0.0;
}
}
/// <summary>
/// Dunn
/// </summary>
/// <param name="S"></param>
/// <param name="T"></param>
/// <returns></returns>
public static double inter3(ClusterLibrary.Cluster S, ClusterLibrary.Cluster T){
int countS = S.Count;
int countT = T.Count;
double disTemp = 0.0;
try{
foreach ( Point ptS in S ){
foreach ( Point ptT in T ){
disTemp += Distance( ptS, ptT ) ;
}
}
return disTemp / ( countS * countT );
}
catch( System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
return 0.0;
}
}
/// <summary>
/// Dunn
/// </summary>
/// <param name="S"></param>
/// <param name="T"></param>
/// <returns></returns>
public static double inter4(ClusterLibrary.Cluster S, ClusterLibrary.Cluster T){
Point vs = S.Center();
Point vt = T.Center();
try{
return Distance( vs, vt );
}
catch( System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
return 0.0;
}
}
/// <summary>
/// Dunn
/// </summary>
/// <param name="S"></param>
/// <param name="T"></param>
/// <returns></returns>
public static double inter5(ClusterLibrary.Cluster S, ClusterLibrary.Cluster T){
Point vs = S.Center();
Point vt = T.Center();
int countS = S.Count;
int countT = T.Count;
double disTemp = 0.0;
try {
foreach ( Point ptS in S ){
disTemp += Distance ( ptS, vt );
}
foreach ( Point ptT in T ){
disTemp += Distance ( ptT, vs );
}
return disTemp / ( countS + countT );
}
catch ( System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
return 0.0;
}
}
/// <summary>
/// Dunn
/// </summary>
/// <param name="S"></param>
/// <param name="T"></param>
/// <returns></returns>
public static double inter6(ClusterLibrary.Cluster S, ClusterLibrary.Cluster T){
try {
return Max( HausdorffMetric( S, T ), HausdorffMetric( T, S ) );
}
catch ( System.Exception e ){
System.Console.WriteLine( e.StackTrace );
System.Console.WriteLine( e.ToString() );
return 0.0;
}
}
public static double HausdorffMetric( ClusterLibrary.Cluster S, ClusterLibrary.Cluster T ){
ArrayList disList = new ArrayList();
ArrayList minDisList = new ArrayList();
try{
foreach ( Point ptS in S ){
foreach ( Point ptT in T ){
disList.Add( Distance ( ptS, ptT ) );
}
minDisList.Add( Min( disList ) );
disList.Clear();
}
return Max ( minDisList );
}
catch ( System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
return 0.0;
}
}
//====================================
// Intra Cluster Distance
//====================================
/// <summary>
/// Dunn
/// </summary>
/// <param name="S"></param>
/// <returns></returns>
public static double intra1(ClusterLibrary.Cluster S){
ArrayList disList = new ArrayList();
int countS = S.Count;
Point x;
Point y;
try{
for ( int i = 0; i < countS; i ++ ){
x = (Point) S[i];
for ( int j = i + 1; j < countS; j ++ ){
y = (Point) S[j];
disList.Add ( Distance( x, y ) );
}
}
return Max ( disList );
}
catch ( System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
return 0.0;
}
}
/// <summary>
/// Dunn
/// </summary>
/// <param name="S"></param>
/// <returns></returns>
public static double intra2(ClusterLibrary.Cluster S){
int countS = S.Count;
double disTemp = 0.0;
Point x;
Point y;
if ( countS <= 1 )
return 0.0;
try{
for ( int i = 0; i < countS; i ++ ){
x = (Point) S[i];
for ( int j = i + 1; j < countS; j ++ ){
y = (Point) S[j];
disTemp += Distance( x, y );
}
}
return disTemp / ( countS * (countS - 1) );
}
catch ( System.Exception e ){
System.Console.WriteLine( e.StackTrace ) ;
System.Console.WriteLine( e.ToString() );
return 0.0;
}
}
/// <summary>
/// Dunn
/// </summary>
/// <param name="S"></param>
/// <returns></returns>
public static double intra3(ClusterLibrary.Cluster S){
int countS = S.Count;
Point v = S.Center();
double disTemp = 0.0;
try{
foreach( Point x in S ){
disTemp += Distance ( x, v );
}
return 2 * disTemp / countS;
}
catch ( System.Exception e ){
System.Console.WriteLine( e.StackTrace );
System.Console.WriteLine( e.ToString() );
return 0.0;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -