program kmeans_prb ! !******************************************************************************* ! !! KMEANS_PRB tests various KMEANS programs. ! implicit none ! call timestamp ( ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'KMEANS_PRB' write ( *, '(a)' ) ' Test various KMEANS programs.' call test ( 'points_100.xy' ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'KMEANS_PRB' write ( *, '(a)' ) ' Normal end of execution.' stop end subroutine test ( file_in_name ) ! !******************************************************************************* ! !! TEST_ALL tests all the codes a given dataset. ! implicit none ! integer cluster_num integer dim_num character ( len = * ) file_in_name integer i integer it_max real, allocatable, dimension ( :, : ) :: point integer point_num integer seed ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST_ALL' write ( *, '(a)' ) ' Read point data from "' // trim ( file_in_name ) // '".' ! ! Get the "width" of the file. ! call file_column_count ( file_in_name, dim_num ) write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Points are assumed to have dimension ', dim_num ! ! Get the "length" of the file. ! call points_count ( file_in_name, dim_num, point_num ) ! ! Now allocate some data. ! allocate ( point(dim_num,point_num) ) ! ! Read the coordinates. ! call points_read ( file_in_name, dim_num, point_num, point ) cluster_num = 5 seed = 12345678 call random_initialize ( seed ) it_max = 20 call test01 ( dim_num, point_num, cluster_num, point, it_max ) seed = 12345678 call random_initialize ( seed ) it_max = 20 call test02 ( dim_num, point_num, cluster_num, point, it_max ) seed = 12345678 call random_initialize ( seed ) it_max = 20 call test03 ( dim_num, point_num, cluster_num, point, it_max ) seed = 12345678 call random_initialize ( seed ) it_max = 20 call test04 ( dim_num, point_num, cluster_num, point, it_max ) seed = 12345678 call random_initialize ( seed ) it_max = 20 call test05 ( dim_num, point_num, cluster_num, point, it_max ) seed = 12345678 call random_initialize ( seed ) it_max = 20 call test06 ( dim_num, point_num, cluster_num, point, it_max ) seed = 12345678 call random_initialize ( seed ) it_max = 20 call test07 ( dim_num, point_num, cluster_num, point, it_max ) seed = 12345678 call random_initialize ( seed ) it_max = 20 call test08 ( dim_num, point_num, cluster_num, point, it_max ) deallocate ( point ) return end subroutine test01 ( dim_num, point_num, cluster_num, point, it_max ) ! !******************************************************************************* ! !! TEST01 tries out the HMEANS_01 routine. ! implicit none ! integer cluster_num integer dim_num integer point_num ! integer cluster(point_num) real cluster_center(dim_num,cluster_num) real cluster_energy(cluster_num) integer cluster_population(cluster_num) integer it integer it_max real point(dim_num,point_num) ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST01' write ( *, '(a)' ) ' Test the HMEANS_01 algorithm.' write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of iterations allowed is ', it_max ! ! Initialize the centers. ! call cluster_initialize_5 ( dim_num, point_num, cluster_num, point, & cluster_center ) call hmeans_01 ( dim_num, point_num, cluster_num, it_max, it, point, & cluster, cluster_center, cluster_population, cluster_energy ) write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of iterations taken is ', it call cluster_print_summary ( point_num, cluster_num, & cluster_population, cluster_energy ) if ( .false. ) then call cluster_write ( 'test01_clusters.xy', dim_num, point_num, & cluster_num, point, cluster, cluster_center ) end if return end subroutine test02 ( dim_num, point_num, cluster_num, point, it_max ) ! !******************************************************************************* ! !! TEST02 tries out the HMEANS_02 routine. ! implicit none ! integer cluster_num integer dim_num integer point_num ! integer cluster(point_num) real cluster_center(dim_num,cluster_num) real cluster_energy(cluster_num) integer cluster_population(cluster_num) integer it integer it_max real point(dim_num,point_num) ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST02' write ( *, '(a)' ) ' Test the HMEANS_02 algorithm.' write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of iterations allowed is ', it_max ! ! Initialize the centers. ! call cluster_initialize_5 ( dim_num, point_num, cluster_num, point, & cluster_center ) call hmeans_02 ( dim_num, point_num, cluster_num, it_max, it, point, & cluster, cluster_center, cluster_population, cluster_energy ) write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of iterations taken is ', it call cluster_print_summary ( point_num, cluster_num, & cluster_population, cluster_energy ) if ( .false. ) then call cluster_write ( 'test02_clusters.xy', dim_num, point_num, & cluster_num, point, cluster, cluster_center ) end if return end subroutine test03 ( dim_num, point_num, cluster_num, point, it_max ) ! !******************************************************************************* ! !! TEST03 tries out the KMEANS_01 routine. ! implicit none ! integer cluster_num integer dim_num integer point_num ! integer cluster(point_num) real cluster_center(dim_num,cluster_num) real cluster_energy(cluster_num) integer cluster_population(cluster_num) integer it integer it_max real point(dim_num,point_num) ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST03' write ( *, '(a)' ) ' Test the KMEANS_01 algorithm.' write ( *, '(a)' ) ' (Applied Statistics Algorithm #58)' write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of iterations allowed is ', it_max ! ! Initialize the centers. ! call cluster_initialize_5 ( dim_num, point_num, cluster_num, point, & cluster_center ) call kmeans_01 ( dim_num, point_num, cluster_num, it_max, it, point, & cluster, cluster_center, cluster_population, cluster_energy ) write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of KMEANS_01 iterations taken is ', it call cluster_print_summary ( point_num, cluster_num, & cluster_population, cluster_energy ) if ( .false. ) then call cluster_write ( 'test03_clusters.xy', dim_num, point_num, & cluster_num, point, cluster, cluster_center ) end if return end subroutine test04 ( dim_num, point_num, cluster_num, point, it_max ) ! !******************************************************************************* ! !! TEST04 tries out the KMEANS_02 routine. ! implicit none ! integer cluster_num integer dim_num integer point_num ! integer cluster(point_num) real cluster_center(dim_num,cluster_num) real cluster_energy(cluster_num) integer cluster_population(cluster_num) integer it integer it_max real point(dim_num,point_num) ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST04' write ( *, '(a)' ) ' Test the KMEANS_02 algorithm.' write ( *, '(a)' ) ' (Applied Statistics Algorithm #136)' write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of iterations allowed is ', it_max ! ! Initialize the centers. ! call cluster_initialize_5 ( dim_num, point_num, cluster_num, point, & cluster_center ) call kmeans_02 ( dim_num, point_num, cluster_num, it_max, it, point, & cluster, cluster_center, cluster_population, cluster_energy ) write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of iterations taken is ', it call cluster_print_summary ( point_num, cluster_num, & cluster_population, cluster_energy ) if ( .false. ) then call cluster_write ( 'test04_clusters.xy', dim_num, point_num, & cluster_num, point, cluster, cluster_center ) end if return end subroutine test05 ( dim_num, point_num, cluster_num, point, it_max ) ! !******************************************************************************* ! !! TEST05 tries out the KMEANS_03 routine. ! implicit none ! integer cluster_num integer dim_num integer point_num ! integer cluster(point_num) real cluster_center(dim_num,cluster_num) real cluster_energy(cluster_num) integer cluster_population(cluster_num) integer it integer it_max real point(dim_num,point_num) ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST05' write ( *, '(a)' ) ' Test the KMEANS_03 algorithm.' write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of iterations allowed is ', it_max ! ! Initialize the centers. ! call cluster_initialize_5 ( dim_num, point_num, cluster_num, point, & cluster_center ) call kmeans_03 ( dim_num, point_num, cluster_num, it_max, it, point, & cluster, cluster_center, cluster_population, cluster_energy ) write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of iterations taken is ', it call cluster_print_summary ( point_num, cluster_num, & cluster_population, cluster_energy ) if ( .false. ) then call cluster_write ( 'test05_clusters.xy', dim_num, point_num, & cluster_num, point, cluster, cluster_center ) end if return end subroutine test06 ( dim_num, point_num, cluster_num, point, it_max ) ! !******************************************************************************* ! !! TEST06 tries out the HMEANS_01 + KMEANS_01 routine. ! implicit none ! integer cluster_num integer dim_num integer point_num ! integer cluster(point_num) real cluster_center(dim_num,cluster_num) real cluster_energy(cluster_num) integer cluster_population(cluster_num) integer it integer it_max integer, parameter :: it_max0 = 3 real point(dim_num,point_num) ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST06' write ( *, '(a)' ) ' Test the HMEANS_01 + KMEANS_01 algorithm.' write ( *, '(a)' ) ' (Applied Statistics Algorithm #58)' write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of HMEANS_01 iterations allowed is ', it_max0 write ( *, '(a,i6)' ) ' Number of KMEANS_01 iterations allowed is ', it_max ! ! Initialize the centers. ! call cluster_initialize_5 ( dim_num, point_num, cluster_num, point, & cluster_center ) call hmeans_01 ( dim_num, point_num, cluster_num, it_max0, it, point, & cluster, cluster_center, cluster_population, cluster_energy ) write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of HMEANS_01 iterations taken is ', it call cluster_print_summary ( point_num, cluster_num, & cluster_population, cluster_energy ) call kmeans_01 ( dim_num, point_num, cluster_num, it_max, it, point, & cluster, cluster_center, cluster_population, cluster_energy ) write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of KMEANS_01 iterations taken is ', it call cluster_print_summary ( point_num, cluster_num, & cluster_population, cluster_energy ) if ( .false. ) then call cluster_write ( 'test06_clusters.xy', dim_num, point_num, & cluster_num, point, cluster, cluster_center ) end if return end subroutine test07 ( dim_num, point_num, cluster_num, point, it_max ) ! !******************************************************************************* ! !! TEST07 tries out the HMEANS_01 + KMEANS_02 routine. ! implicit none ! integer cluster_num integer dim_num integer point_num ! integer cluster(point_num) real cluster_center(dim_num,cluster_num) real cluster_energy(cluster_num) integer cluster_population(cluster_num) integer it integer it_max integer, parameter :: it_max0 = 3 real point(dim_num,point_num) ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST07' write ( *, '(a)' ) ' Test the HMEANS_01 + KMEANS_02 algorithm.' write ( *, '(a)' ) ' (Applied Statistics Algorithm #136)' write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of HMEANS_01 iterations allowed is ', it_max0 write ( *, '(a,i6)' ) ' Number of KMEANS_02 iterations allowed is ', it_max ! ! Initialize the centers. ! call cluster_initialize_5 ( dim_num, point_num, cluster_num, point, & cluster_center ) call hmeans_01 ( dim_num, point_num, cluster_num, it_max0, it, point, & cluster, cluster_center, cluster_population, cluster_energy ) write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of HMEANS_01 iterations taken is ', it call cluster_print_summary ( point_num, cluster_num, & cluster_population, cluster_energy ) call kmeans_02 ( dim_num, point_num, cluster_num, it_max, it, point, & cluster, cluster_center, cluster_population, cluster_energy ) write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of KMEANS_02 iterations taken is ', it call cluster_print_summary ( point_num, cluster_num, & cluster_population, cluster_energy ) if ( .false. ) then call cluster_write ( 'test07_clusters.xy', dim_num, point_num, & cluster_num, point, cluster, cluster_center ) end if return end subroutine test08 ( dim_num, point_num, cluster_num, point, it_max ) ! !******************************************************************************* ! !! TEST08 tries out the HMEANS_01 + KMEANS_03 routine. ! implicit none ! integer cluster_num integer dim_num integer point_num ! integer cluster(point_num) real cluster_center(dim_num,cluster_num) real cluster_energy(cluster_num) integer cluster_population(cluster_num) integer it integer it_max integer, parameter :: it_max0 = 3 real point(dim_num,point_num) ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST08' write ( *, '(a)' ) ' Test the HMEANS_01 + KMEANS_03 algorithm.' write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' Initialize by using a few steps of HMEANS_02:' write ( *, '(a,i6)' ) ' Number of HMEANS_02 iterations allowed is ', it_max0 write ( *, '(a,i6)' ) ' Number of KMEANS_03 iterations allowed is ', it_max ! ! Initialize the centers. ! call cluster_initialize_5 ( dim_num, point_num, cluster_num, point, & cluster_center ) ! ! Initialize the clusters. ! call hmeans_01 ( dim_num, point_num, cluster_num, it_max0, it, point, & cluster, cluster_center, cluster_population, cluster_energy ) write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of HMEANS_01 iterations taken is ', it call cluster_print_summary ( point_num, cluster_num, & cluster_population, cluster_energy ) call kmeans_03 ( dim_num, point_num, cluster_num, it_max, it, point, & cluster, cluster_center, cluster_population, cluster_energy ) write ( *, '(a)' ) ' ' write ( *, '(a,i6)' ) ' Number of KMEANS_03 iterations taken is ', it call cluster_print_summary ( point_num, cluster_num, & cluster_population, cluster_energy ) if ( .false. ) then call cluster_write ( 'test08_clusters.xy', dim_num, point_num, & cluster_num, point, cluster, cluster_center ) end if return end