Skip to content

Instantly share code, notes, and snippets.

@h4ckm03d
Created September 15, 2014 09:49
Show Gist options
  • Save h4ckm03d/fe5a515957ed10dabdd6 to your computer and use it in GitHub Desktop.
Save h4ckm03d/fe5a515957ed10dabdd6 to your computer and use it in GitHub Desktop.
Simple implementation fuzzy c-means
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestFuzzy
{
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
StringBuilder log = new StringBuilder();
//define paramater
const int totalAttribut = 4;
const int k = 10; //total data
const int cluster = 2;
int pembobot = 2;
int maxIterasi = 50;
int currentIteration = 0;
double Pold = 0;
double Pnew = 0;
double E = 1e-5;
double current_error = 0;
double[,] data = new double[k, totalAttribut]{
{35,63,0,1},
{38,69,21,2},
{43,58,52,2},
{42,63,1,1},
{49,60,1,1},
{54,65,23,2},
{57,64,9,1},
{59,62,35,2},
{61,59,0,1},
{62,65,19,2}};
//Random random = new Random();
//double[,] U = new double[k, cluster];
//for (int i = 0; i < k; i++)
//{
// for (int j = 0; j < cluster; j++)
// {
// U[i, j] = random.NextDouble();
// }
//}
double[,] U = new double[k, cluster]
{
{0.38385,0.50822},
{0.09969,0.57784},
{0.33976,0.27027},
{0.08088,0.16942},
{0.46496,0.28391},
{0.00256,0.53164},
{0.16114,0.25857},
{0.81044,0.10977},
{0.37751,0.09454},
{0.18675,0.3892}
};
double[,] Uw = new double[k, cluster];
do
{
for (int i = 0; i < k; i++)
{
for (int j = 0; j < cluster; j++)
{
Uw[i, j] = Math.Pow(U[i, j], pembobot);
}
}
double[,] V = new double[cluster, totalAttribut];
double up;
double down;
log.AppendLine("iterasi "+currentIteration);
for (int i = 0; i < cluster; i++)
{
for (int j = 0; j < totalAttribut; j++)
{
up = 0;
down = 0;
for (int _k = 0; _k < k; _k++)
{
up += (Uw[_k, i] * data[_k, j]);
down += Uw[_k, i];
}
V[i, j] = up / down;
Console.WriteLine("V[{0},{1}] = {2}", i, j, V[i, j]);
log.AppendFormat("V[{0},{1}] = {2}\r\n", i, j, V[i, j]);
}
}
double[,] Dik = new double[cluster, k];
double d_val;
for (int i = 0; i < cluster; i++)
{
for (int _k = 0; _k < k; _k++)
{
d_val = 0;
for (int j = 0; j < totalAttribut; j++)
{
d_val += Math.Pow(data[_k, j] - V[i, j], 2);
}
Dik[i, _k] = Math.Sqrt(d_val);
Console.WriteLine("D[{0},{1}] = {2}", i, _k, Dik[i, _k]);
log.AppendFormat("D[{0},{1}] = {2}\r\n", i, _k, Dik[i, _k]);
}
}
//hitung fungsi objective
for (int i = 0; i < cluster; i++)
{
for (int _k = 0; _k < k; _k++)
{
Pnew += Uw[_k, i] * Math.Pow(Dik[i, _k], 2);
}
}
Console.WriteLine("Function Objective - {0} = {1}", currentIteration, Pnew);
log.AppendFormat("Function Objective - {0} = {1}\r\n", currentIteration, Pnew);
//update matrix U
double newU;
int power = 2 / (pembobot - 1);
for (int _k = 0; _k < k; _k++)
{
for (int i = 0; i < cluster; i++)
{
newU = 0;
for (int j = 0; j < cluster; j++)
{
newU += Math.Pow(Dik[i, _k] / Dik[j, _k], power);
}
U[_k, i] = Math.Pow(newU, -1);
Console.WriteLine("U[{0},{1}] = {2}", i, _k, U[_k, i]);
log.AppendFormat("U[{0},{1}] = {2}\r\n", i, _k, U[_k, i]);
}
}
current_error = Math.Abs(Pnew - Pold);
sb.AppendLine(string.Format("P{0} = {1}, P{2} = {3}, P{4} - P{5}={6}", currentIteration, Pold, currentIteration + 1, Pnew, currentIteration, currentIteration + 1, current_error));
Pold = Pnew;
Pnew = 0;
if (current_error < E)
break;
currentIteration++;
} while (currentIteration < maxIterasi);
Console.WriteLine(sb.ToString());
System.IO.File.WriteAllText("result.txt", log.ToString() + "\r\n==============\r\n" + sb.ToString());
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment