Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.48 KB | None | 0 0
  1. public class CMeansAlgorithm3 {
  2. private static int fuzzyness = 2;
  3.  
  4. private final Map<Double, Species> integerClusterHashMap = new HashMap<Double, Species>();
  5.  
  6. /// Array containing all points used by the algorithm
  7. private List<Job> points;
  8.  
  9. /// Gets or sets membership matrix
  10. public double[][] U;
  11.  
  12. /// Algorithm precision
  13. private double eps = Math.pow(10, -5);
  14.  
  15. /// Gets or sets objective function
  16. private double J;
  17.  
  18. /// Gets or sets log message
  19. public String log;
  20.  
  21. private List<Species> clusterList;
  22.  
  23. public CMeansAlgorithm3(List<Job> points, int clusterSize){
  24. this.points = points;
  25. clusterList = initialiseCentroids(points, clusterSize);
  26. U = new double[points.size()][clusterList.size()];
  27. calculateClusterMembershipValues();
  28. recalculateClusterIndexes();
  29. }
  30.  
  31. private void calculateClusterMembershipValues() {
  32. // Iterate through all points to create initial U matrix
  33. for (int i = 0; i < points.size(); i++) {
  34. Job p = points.get(i);
  35. double sum = 0.0;
  36.  
  37. for (int j = 0; j < clusterList.size(); j++) {
  38. Cluster c = clusterList.get(j);
  39. double diff = Math.sqrt(Math.pow(p.getMidpointX() - c.getCentroid().getX(), 2.0) + Math.pow(p.getMidpointY() - c.getCentroid().getY(), 2.0));
  40. U[i][j] = (diff == 0) ? eps : diff;
  41. sum += U[i][j];
  42. }
  43.  
  44. double sum2 = 0.0;
  45. for (int j = 0; j < clusterList.size(); j++) {
  46. U[i][j] = 1.0 / Math.pow(U[i][j] / sum, 2.0 / (fuzzyness - 1.0));
  47. sum2 += U[i][j];
  48. }
  49.  
  50. for (int j = 0; j < clusterList.size(); j++) {
  51. U[i][j] = U[i][j] / sum2;
  52. }
  53. }
  54. }
  55.  
  56. /// Recalculates cluster indexes
  57. private void recalculateClusterIndexes() {
  58. for (int i = 0; i < points.size(); i++) {
  59. double max = -1.0;
  60. Job p = points.get(i);
  61.  
  62. for (int j = 0; j < clusterList.size(); j++) {
  63. max = U[i][j] > max ? U[i][j] : max;
  64. // if (max < U[i][j]) {
  65. // max = U[i][j];
  66. // p.setClusterIndex((max == 0.5) ? 0.5 : j);
  67. // }
  68. }
  69. p.setClusterIndex(max);
  70. }
  71. }
  72.  
  73. /// Perform a complete run of the algorithm until the desired accuracy is achieved.
  74. /// For demonstration issues, the maximum Iteration counter is set to 20.
  75. /// Algorithm accuracy
  76. /// The number of steps the algorithm needed to complete
  77. public List<Species> run(double accuracy) {
  78. int k = 0;
  79. int maxIterations = 100;
  80.  
  81. do {
  82. k++;
  83. J = calculateObjectiveFunction();
  84. calculateClusterCentroids();
  85. step();
  86. double Jnew = calculateObjectiveFunction();
  87.  
  88. if (Math.abs(J - Jnew) < accuracy) break;
  89. }
  90. while (maxIterations > k);
  91.  
  92. assignJobsToClusters();
  93. return clusterList;
  94. }
  95.  
  96. /// Calculate the objective function
  97. /// The objective function as double value
  98. private double calculateObjectiveFunction() {
  99. double Jk = 0;
  100.  
  101. for (int i = 0; i < this.points.size();i++) {
  102. for (int j = 0; j < clusterList.size(); j++) {
  103. Jk += Math.pow(U[i][j], this.fuzzyness) * Math.pow(this.calculateEuclidDistance(points.get(i), clusterList.get(j)), 2);
  104. }
  105. }
  106. return Jk;
  107. }
  108.  
  109. private List<Species> initialiseCentroids(final List<Job> dataSet, final int speciesSize) {
  110. final List<Species> clusterList = new ArrayList<Species>();
  111. final List<Integer> uniqueIndexes = ToolBox.uniqueIndexes(dataSet.size(), speciesSize);
  112.  
  113. for (int i=0; i< uniqueIndexes.size(); i++){
  114. final int randomIndex = uniqueIndexes.get(i);
  115. final Species species = new Species(i);
  116. final Centroid centroid = new Centroid(dataSet.get(randomIndex).getMidpointX(), dataSet.get(randomIndex).getMidpointY(), i);
  117. species.setCentroid(centroid);
  118. speciesList.add(species);
  119. }
  120. return clusterList;
  121. }
  122.  
  123. /// Perform one step of the algorithm
  124. public void step() {
  125. for (int c = 0; c < clusterList.size(); c++) {
  126. for (int h = 0; h < points.size(); h++) {
  127. double top;
  128. top = calculateEuclidDistance(points.get(h), clusterList.get(c));
  129. if (top < 1.0) top = eps;
  130.  
  131. // sumTerms is the sum of distances from this data point to all clusters.
  132. double sumTerms = 0.0;
  133.  
  134. for (int ck = 0; ck < clusterList.size(); ck++) {
  135. double thisDistance = calculateEuclidDistance(points.get(h), clusterList.get(ck));
  136. if (thisDistance < 1.0) thisDistance = eps;
  137. sumTerms += Math.pow(top / thisDistance, 2.0 / (fuzzyness - 1.0));
  138.  
  139. }
  140. // Then the membership value can be calculated as...
  141. U[h][c] = (1.0 / sumTerms);
  142. }
  143. }
  144.  
  145. recalculateClusterIndexes();
  146. }
  147.  
  148. /// Calculates Euclid distance between point and centroid
  149. /// Point
  150. /// Centroid
  151. /// Calculated distance
  152. private double calculateEuclidDistance(Job p, Species c) {
  153. return ToolBox.calculateDistance(p.getMidpointX(), p.getMidpointY(), c.getCentroid().getX(), c.getCentroid().getY());
  154. }
  155.  
  156. /// Calculates the centroids of the clusters
  157. private void calculateClusterCentroids() {
  158. for (int j = 0; j < clusterList.size(); j++) {
  159. Species c = clusterList.get(j);
  160. double uX = 0.0;
  161. double uY = 0.0;
  162. double membershipSum = 0.0;
  163.  
  164. for (int i = 0; i < points.size(); i++) {
  165. Job p = points.get(i);
  166.  
  167. double uu = Math.pow(U[i][j], this.fuzzyness);
  168. uX += uu * p.getMidpointX();
  169. uY += uu * p.getMidpointY();
  170. membershipSum += uu;
  171. }
  172.  
  173. c.setMembershipSum(membershipSum);
  174. c.getCentroid().setX(((uX / membershipSum)));
  175. c.getCentroid().setY(((uY / membershipSum)));
  176.  
  177. log += String.format("Cluster Centroid: (" + c.getCentroid().getX() + "; " + c.getCentroid().getY() + ")");
  178. }
  179. }
  180.  
  181. private void assignJobsToClusters(){
  182. for (final Cluster cluster : clusterList){
  183. if (!integerClusterHashMap.containsKey(cluster.getMembershipSum()))
  184. integerClusterHashMap.put(cluster.getMembershipSum(), cluster);
  185. }
  186.  
  187. for (Job job : points){
  188. final double clusterIndex = job.getClusterIndex();
  189. Species c = integerSpeciesHashMap.get(clusterIndex);
  190.  
  191. if (c != null) {
  192. c.add(job);
  193. }
  194. }
  195. }
  196.  
  197. package f;
  198. import java.io.File;
  199. import java.io.FileNotFoundException;
  200. import java.util.Scanner;
  201.  
  202. /**
  203. *
  204. * @author p2csc13002
  205. */
  206. public class F{
  207.  
  208. static Double data_set[][]=new Double[20000][100];
  209. static Double diff[][]=new Double[20000][100];
  210. static Double eud[][]=new Double[20000][1000];
  211. static Double intial_centroid[][]=new Double[300][400];
  212. static Double new_center[][]=new Double[300][400];
  213. static int num = 0;
  214. static int row=4;//rows in Your DataSet here i use iris dataset
  215.  
  216. static int cnum;
  217. static int itc=0;
  218. static int checker=1;
  219.  
  220.  
  221. private static void readFile() throws FileNotFoundException
  222. {
  223. Scanner scanner = new Scanner(new File("E:/aa.txt"));
  224. scanner.useDelimiter(System.getProperty("line.separator"));
  225. //scanner.useDelimiter(",");
  226. int lineNo = 0;
  227. while (scanner.hasNext())
  228. {
  229. parseLine(scanner.next(),lineNo);
  230. lineNo++;
  231. System.out.println();
  232. }
  233. // System.out.println("total"+num); PRINT THE TOTAL
  234. scanner.close();
  235. }
  236. //read file is copey to the data_set
  237. public static void parseLine(String line,int lineNo)
  238. { itc=0;
  239. Scanner lineScanner = new Scanner(line);
  240. lineScanner.useDelimiter(",");
  241. for(int col=0;col<row;col++)
  242. {
  243. Double arry=lineScanner.nextDouble();
  244. data_set[num][col]=arry; ///here read data set is assign the variable data_set
  245. }
  246. num++;
  247.  
  248. }
  249. public static void init()
  250. {
  251. for(int i=0;i<num;i++)
  252. {
  253. data_set[i][row]=0.0;
  254. data_set[i][row+1]=0.0;
  255. }
  256. }
  257. public static void print()
  258. {
  259. double re=0;
  260. double a=0;
  261.  
  262.  
  263. if(itc==0)
  264. {
  265.  
  266. System.out.println("ENTER K");
  267. Scanner sc=new Scanner(System.in);
  268. cnum=sc.nextInt(); //enter the number of cenroid
  269. System.out.println("centroid");
  270. for(int i=0;i<cnum;i++)
  271. {
  272. for(int j=0;j<row;j++)
  273. {
  274.  
  275. intial_centroid[i][j]=data_set[i][j]; //// CENTROID ARE STORED IN AN intial_centroid variable
  276. System.out.print(intial_centroid[i][j]);
  277. }
  278. System.out.println();
  279. }
  280.  
  281. }
  282. else
  283. {
  284.  
  285. for(int i=0;i<cnum;i++)
  286. {
  287. for(int j=0;j<row;j++)
  288. {
  289. intial_centroid[i][j]=new_center[i][j]; //// CENTROID ARE STORED IN AN intial_centroid variable
  290. System.out.print(intial_centroid[i][j]);
  291. }
  292. System.out.println();
  293. }
  294.  
  295.  
  296. }
  297.  
  298. for(int i=0;i<num;i++)
  299. {
  300. for(int j=0;j<cnum;j++)
  301. {
  302. re=0;
  303. for(int k=0;k<row;k++)
  304. {
  305. a= (intial_centroid[j][k]-data_set[i][k]);
  306. //System.out.println(a);
  307. a=a*a;
  308. re=re+a; // store the row sum
  309.  
  310. }
  311.  
  312. diff[i][j]= Math.sqrt(re);// find the squre root
  313. System.out.println(diff[i][j]);
  314.  
  315. }
  316. }
  317.  
  318. }
  319. public static void s()
  320. {
  321.  
  322. double b,c;
  323. for(int i=0;i<num;i++)
  324. {
  325. for(int j=0;j<cnum;j++)
  326. {
  327. c=0.0;
  328. b=0.0;
  329. for(int k=0;k<cnum;k++)
  330. {
  331. if(diff[i][k]==0)
  332. {
  333. b=0;
  334. }
  335. if(diff[i][k]!=0)
  336. {
  337. b=diff[i][j]/diff[i][k];
  338. }
  339. c=c+b;
  340. }
  341. if(c==0)
  342. {
  343. eud[i][j]=0.0;
  344. }
  345. else
  346. {
  347. eud[i][j]=1/c;
  348. }
  349. }
  350.  
  351.  
  352. }
  353. double a=0;
  354. for(int i=0;i<num;i++)
  355. {
  356. a=0;
  357. for(int j=0;j<cnum;j++)
  358. {
  359. a=a+eud[i][j];
  360. System.out.print(eud[i][j]+" ");
  361. }
  362. System.out.print("total "+a);
  363. System.out.println();
  364. }
  365. double aaa;
  366. int counter=0;
  367. for(int i=0;i<num;i++)
  368. {counter=0;
  369. aaa=eud[i][0];
  370. for(int j=0;j<cnum;j++)
  371. {
  372. if(aaa<=eud[i][j])
  373. {
  374. aaa=eud[i][j];
  375. counter=j;
  376. }
  377. }
  378. if(itc%2==0)
  379. {
  380. data_set[i][row]=(double)counter;
  381. }
  382.  
  383. if(itc%2==1)
  384. {
  385. data_set[i][row+1]=(double)counter;
  386. }
  387. }
  388. for(int i=0;i<num;i++)
  389. {
  390. for(int j=0;j<=row+1;j++)
  391. {
  392. System.out.print(data_set[i][j]+", ");
  393.  
  394. }
  395. System.out.println();
  396. }
  397. }
  398.  
  399. public static void newcenter()
  400. {
  401. itc++;
  402. double a=0.0;
  403. double c=0.0;
  404. double d=0.0;
  405. double f=0.0;
  406. for(int k=0;k<cnum;k++)
  407. {
  408. for(int j=0;j<row;j++)
  409. {
  410. a=0.0;
  411. d=0.0;
  412.  
  413. c=0.0;
  414. f=0.0;
  415. for(int i=0;i<num;i++)
  416. {
  417. //System.out.print("edu"+eud[i][k]);
  418. a=eud[i][k];
  419. a=a*a;
  420. c=c+a;
  421. //System.out.println("data"+data_set[i][j]);
  422. d=a*data_set[i][j];
  423. f=f+d;
  424. }
  425. new_center[k][j]=f/c;
  426. System.out.println("centroid new "+new_center[k][j]);
  427. // j=row+5;
  428. // k=cnum+5;
  429.  
  430. }
  431. }
  432. }
  433.  
  434.  
  435. public static void print11()
  436. {
  437. System.out.println();
  438. System.out.println();
  439. System.out.println();
  440. System.out.println("----OUTPUT----");
  441. int c=0;
  442. int a=0;
  443. for(int i=0;i<cnum;i++)
  444. {
  445. System.out.println("---------CLUSTER-"+i+"-----");
  446. a=0;
  447. for(int j=0;j<num;j++)
  448. {
  449. if(data_set[j][row]==i)
  450. {a++;
  451. for(int k=0;k<row;k++)
  452. {
  453.  
  454. System.out.print(data_set[j][k]+" ");
  455. }
  456. c++;
  457. System.out.println();
  458. }
  459. //System.out.println(num);
  460.  
  461. }
  462. System.out.println("CLUSTER INSTANCES="+a);
  463.  
  464.  
  465. }
  466. System.out.println("TOTAL INSTANCE"+c);
  467. }
  468.  
  469.  
  470. public static void check()
  471. {
  472.  
  473. checker=0;
  474. for(int i=0;i<num;i++)
  475. {
  476. //System.out.println("hii");
  477. if(Double.compare(data_set[i][row],data_set[i][row+1]) != 0)
  478.  
  479. {
  480. checker=1;
  481. //System.out.println("hii " + i + " " + data_set[i][4]+ " "+data_set[i][4]);
  482. break;
  483. }
  484. System.out.println();
  485. }
  486.  
  487. }
  488.  
  489. public static void main(String[] args) throws FileNotFoundException {
  490. readFile();
  491. //
  492. init();
  493. while(checker!=0)
  494. //for(int i=0;i<5;i++)
  495. {
  496.  
  497. print();
  498. s();
  499. newcenter();
  500. check();
  501.  
  502. }
  503. print11();
  504. }
  505. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement