-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlcvt_dataset.cpp
2822 lines (2585 loc) · 70.9 KB
/
lcvt_dataset.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# include <cstdlib>
# include <cmath>
# include <ctime>
# include <iostream>
# include <iomanip>
# include <fstream>
# include <cstring>
using namespace std;
int main ( void );
char ch_cap ( char c );
bool ch_eqi ( char c1, char c2 );
int ch_to_digit ( char c );
double cluster_energy ( int dim_num, int n, double cell_generator[],
int sample_num, int sample_function_cvt, int *seed );
void cvt_iteration ( int m, int n, double generator[], int sample_num,
int sample_function_cvt, int *seed, double *change_l2 );
int find_closest ( int m, int n, double x[], double generator[] );
int i4_max ( int i1, int i2 );
int i4_min ( int i1, int i2 );
void i4_to_halton ( int seed, int base[], int ndim, double r[] );
void lcvt_write ( int dim_num, int n, int seed_start, int sample_function_init,
char* file_in_name, int sample_function_cvt, int sample_num, int cvt_it,
double cvt_energy, int latin_it, double latin_energy, double cell_generator[],
char *file_out_name );
int prime ( int n );
double r8_epsilon ( );
double r8_uniform_01 ( int *seed );
void r8mat_latinize ( int m, int n, double table[] );
double *r8table_data_read ( char *input_filename, int m, int n );
int *r8vec_sort_heap_index_a ( int n, double a[] );
void region_sampler ( int m, int n, int n_total, double x[],
int sample_function, bool reset, int *seed );
bool s_eqi ( char *s1, char *s2 );
int s_len_trim ( char* s );
double s_to_r8 ( char *s, int *lchar, bool *error );
bool s_to_r8vec ( char *s, int n, double rvec[] );
void timestamp ( );
void tuple_next_fast ( int m, int n, int rank, int x[] );
//****************************************************************************80
int main ( void )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for LCVT_DATASET.
//
// Discussion:
//
// LCVT_DATASET computes a Latinized CVT dataset and writes it to a file.
//
// This program is meant to be used interactively. It's also
// possible to prepare a simple input file beforehand and use it
// in batch mode.
//
// The program requests input values from the user:
//
// * DIM_NUM, the spatial dimension;
// * N, the number of points to generate;
// * SEED_INIT, a seed to use for random number generation;
// * INIT, initialize the points:
// ** file, by reading data from file;
// ** GRID, picking points from a grid;
// ** HALTON, from a Halton sequence;
// ** RANDOM, using C++ RANDOM function;
// ** UNIFORM, using a simple uniform RNG;
// ** USER, call the "user" routine;
// * CVT_IT_NUM, the maximum number of iterations;
// * SAMPLE, how to conduct the sampling:
// ** GRID, picking points from a grid;
// ** HALTON, from a Halton sequence;
// ** RANDOM, using C++ RANDOM function;
// ** UNIFORM, using a simple uniform RNG;
// ** USER, call the "user" routine.
// * SAMPLE_NUM, the number of sampling points;
// * BATCH, the number of sampling points to create at one time.
// * LAT_IT_NUM, the maximum number of iterations;
// * OUTPUT, a file in which to store the data.
//
// To indicate that no further computations are desired, it is
// enough to input a nonsensical value, such as -1.
//
// Modified:
//
// 12 April 2007
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// double CELL_GENERATOR(M,N), the Voronoi cell generators
// of the Voronoi tessellation, as approximated by the CVT algorithm. This
// is the output quantity of most interest.
//
// int CVT_IT, the number of iterations used in the Centroidal
// Voronoi Tesselation calculation. The default value is 10.
//
// int LATIN_IT, the number of Latin hypercube iterations to carry out.
// This defaults to 5.
//
// int M, the spatial dimension.
//
// int N, the number of Voronoi cells to generate.
//
// int SAMPLE_FUNCTION_CVT, specifies how the region is sampled:
// -1, the sampling function is C++ RANDOM_NUMBER,
// 0, the sampling function is UNIFORM,
// 1, the sampling function is HALTON,
// 2, the sampling function is GRID.
//
// int SAMPLE_FUNCTION_INIT, specifies how the initial
// generators are chosen:
// -1, the initialization function is C++ RANDOM,
// 0, the initialization function is UNIFORM,
// 1, the initialization function is HALTON,
// 2, the initialization function is GRID,
// 3, the initial values are read in from a file.
//
// int SAMPLE_NUM, the number of sampling points used on
// each CVT iteration. A typical value is 5000 * N.
//
// int SEED, determines how to initialize the random number routine.
// If SEED is zero, then RANDOM_INITIALIZE will make up a seed
// from the current real time clock reading.
// If SEED is nonzero, then a reproducible sequence of random numbers
// defined by SEED will be chosen.
// By default, SEED initially has a value chosen by RANDOM_INITIALIZE,
// but the user can reset SEED at any time.
//
{
int batch;
double cvt_energy;
double cvt_it_diff;
int cvt_it;
int cvt_it_num ;
bool debug = true;
int dim_num;
char input_file_name[80];
int init;
char init_string[80];
double lat_energy;
int lat_it;
int lat_it_num;
int n;
int n_total;
char output_file_name[80];
double *r;
bool reset;
int sample;
int sample_num;
char sample_string[80];
int seed;
int seed_init;
//
// Print introduction and options.
//
timestamp ( );
cout << "\n";
cout << "LCVT_DATASET\n";
cout << " C++ version\n";
cout << " Create a \"Latinized\" CVT datasets.\n";
cout << "\n";
cout << " Compiled on " << __DATE__ << " at " << __TIME__ << ".\n";
cout << "\n";
cout << " This program is meant to be used interactively.\n";
cout << " It is also possible to prepare a simple input \n";
cout << " file beforehand and use it in batch mode.\n";
cout << "\n";
cout << " The program requests input values from the user:\n";
cout << "\n";
cout << " * DIM_NUM, the spatial dimension,\n";
cout << " * N, the number of points to generate,\n";
cout << " * SEED_INIT, a seed to use for random number generation,\n";
cout << " * INIT, initialize the points:\n";
cout << " ** file, read data from a file;\n";
cout << " ** GRID, by picking points from a grid;\n";
cout << " ** HALTON, from a Halton sequence;\n";
cout << " ** RANDOM, using C++ RANDOM function;\n";
cout << " ** UNIFORM, using a simple uniform RNG;\n";
cout << " ** USER, call the \"user\" routine;\n";
cout << " * CVT_IT_NUM, the maximum number of CVT iterations.\n";
cout << " * SAMPLE, how to conduct the sampling.\n";
cout << " ** GRID, by picking points from a grid;\n";
cout << " ** HALTON, from a Halton sequence;\n";
cout << " ** RANDOM, using C++ RANDOM function;\n";
cout << " ** UNIFORM, using a simple uniform RNG;\n";
cout << " ** USER, call the \"user\" routine;\n";
cout << " * SAMPLE_NUM, the number of sample points.\n";
cout << " * BATCH, number of sample points to create at one time.\n";
cout << " * LAT_IT_NUM, the number of Latinizing iterations.\n";
cout << " * OUTPUT, a file in which to store the data.\n";
cout << "\n";
cout << " To indicate that no further computations are\n";
cout << " desired, it is enough to input a nonsensical value,\n";
cout << " such as -1.\n";
cout << " *\n";
cout << " *\n";
cout << "* Ready to generate a new dataset:\n";
cout << " *\n";
cout << " *\n";
cout << " Enter DIM_NUM, the spatial dimension:\n";
cout << " (Try \"2\" if you do not have a preference.)\n";
cout << " (0 or any negative value terminates execution).\n";
cin >> dim_num;
if ( cin.rdstate ( ) )
{
cin.clear ( );
cout << "\n";
cout << "LCVT_DATASET - Warning!\n";
cout << " Terminating abnormally because of an I/O error\n";
cout << " while expecting input for DIM_NUM.\n";
exit ( 1 );
}
cout << " User input DIM_NUM = " << dim_num << "\n";
if ( dim_num < 1 )
{
cout << "\n";
cout << "LCVT_DATASET\n";
cout << " The input value of DIM_NUM = " << dim_num << "\n";
cout << " is interpreted as a request for termination.\n";
cout << " Normal end of execution.\n";
exit ( 1 );
}
cout << "\n";
cout << " Enter N, the number of points to generate:\n";
cout << " (Try \"25\" if you do not have a preference.)\n";
cout << " (0 or any negative value terminates execution).\n";
cin >> n;
if ( cin.rdstate ( ) )
{
cin.clear ( );
cout << "\n";
cout << "LCVT_DATASET - Warning!\n";
cout << " Terminating abnormally because of an I/O error\n";
cout << " while expecting input for N.\n";
exit ( 1 );
}
cout << " User input N = " << n << "\n";
if ( n < 1 )
{
cout << "\n";
cout << "LCVT_DATASET\n";
cout << " The input value of N = " << n << "\n";
cout << " is interpreted as a request for termination.\n";
cout << " Normal end of execution.\n";
exit ( 1 );
}
cout << "\n";
cout << " Enter SEED_INIT, a seed for the random number generator:\n";
cout << " (Try \"123456789\" if you do not have a preference.)\n";
cout << " (Any negative value terminates execution).\n";
cin >> seed_init;
if ( cin.rdstate ( ) )
{
cin.clear ( );
cout << "\n";
cout << "LCVT_DATASET - Warning!\n";
cout << " Terminating abnormally because of an I/O error\n";
cout << " while expecting input for SEED_INIT.\n";
exit ( 1 );
}
cout << " User input SEED_INIT = " << seed_init << "\n";
if ( seed_init < 0 )
{
cout << "\n";
cout << "LCVT_DATASET\n";
cout << " The input value of SEED_INIT = " << seed_init << "\n";
cout << " is interpreted as a request for termination.\n";
cout << " Normal end of execution.\n";
exit ( 1 );
}
seed = seed_init;
cout << "\n";
cout << " INIT is the method of initializing the data:\n";
cout << "\n";
cout << " file read data from a file;\n";
cout << " GRID by picking points from a grid;\n";
cout << " HALTON from a Halton sequence;\n";
cout << " RANDOM using C++ RANDOM function;\n";
cout << " UNIFORM using a simple uniform RNG;\n";
cout << " USER call the \"user\" routine;\n";
cout << "\n";
cout << " (Try \"RANDOM\" if you do not have a preference.)\n";
cout << " (A blank value terminates execution).\n";
cout << "\n";
cout << " Enter INIT:\n";
cin >> init_string;
if ( cin.rdstate ( ) )
{
cin.clear ( );
cout << "\n";
cout << "LCVT_DATASET - Warning!\n";
cout << " Terminating abnormally because of an I/O error\n";
cout << " while expecting input for INIT\n";
exit ( 1 );
}
if ( s_eqi ( init_string, "RANDOM" ) )
{
init = -1;
cout << " User input INIT = \"RANDOM\"\n";
}
else if ( s_eqi ( init_string, "UNIFORM" ) )
{
init = 0;
cout << " User input INIT = \"UNIFORM\"\n";
}
else if ( s_eqi ( init_string, "HALTON" ) )
{
init = 1;
cout << " User input INIT = \"HALTON\".\n";
}
else if ( s_eqi ( init_string, "GRID" ) )
{
init = 2;
cout << " User input INIT = \"GRID\".\n";
}
else if ( s_eqi ( init_string, "USER" ) )
{
init = 3;
cout << " User input INIT = \"USER\".\n";
}
else if ( 0 < s_len_trim ( init_string ) )
{
init = 4;
cout << " User input INIT = FILE_NAME = \"" << init_string << "\".\n";
strcpy ( input_file_name, init_string );
}
else
{
cout << "n";
cout << "LCVT_DATASET\n";
cout << " The input value of INIT \n";
cout << " is interpreted as a request for termination.\n";
cout << " Normal end of execution.\n";
exit ( 1 );
}
cout << "\n";
cout << " CVT_IT_NUM is the number of CVT iterations.\n";
cout << "\n";
cout << " A CVT iteration carries out the following steps:\n";
cout << " * the Voronoi region associated with each\n";
cout << " generator is estimated by sampling;\n";
cout << " * the centroid of each Voronoi region is estimated.\n";
cout << " * the generator is replaced by the centroid.\n";
cout << "\n";
cout << " If \"enough\" sampling points are used,\n";
cout << " and \"enough\" iterations are taken, this process\n";
cout << " will converge\n";
cout << "\n";
cout << " (Try \"50\" if you do not have a preference.)\n";
cout << " (A negative value terminates execution).\n";
cout << "\n";
cout << " Enter CVT_IT_NUM:\n";
cin >> cvt_it_num;
if ( cin.rdstate ( ) )
{
cin.clear ( );
cout << "\n";
cout << "LCVT_DATASET - Warning!\n";
cout << " Terminating abnormally because of an I/O error\n";
cout << " while expecting input for CVT_IT_NUM.\n";
exit ( 1 );
}
cout << " User input CVT_IT_NUM = " << cvt_it_num << "\n";
if ( cvt_it_num < 0 )
{
cout << "\n";
cout << "LCVT_DATASET\n";
cout << " The input value of CVT_IT_NUM = " << cvt_it_num << "\n";
cout << " is interpreted as a request for termination.\n";
cout << " Normal end of execution.\n";
exit ( 1 );
}
cout << "\n";
cout << " SAMPLE is the method of sampling the region:\n";
cout << "\n";
cout << " GRID by picking points from a grid;\n";
cout << " HALTON from a Halton sequence;\n";
cout << " RANDOM using C++ RANDOM function;\n";
cout << " UNIFORM using a simple uniform RNG;\n";
cout << " USER call the \"user\" routine;\n";
cout << "\n";
cout << " (Try \"RANDOM\" if you do not have a preference.)\n";
cout << " (A blank value terminates execution).\n";
cout << "\n";
cout << " Enter SAMPLE:\n";
cin >> sample_string;
if ( cin.rdstate ( ) )
{
cin.clear ( );
cout << "\n";
cout << "LCVT_DATASET - Warning!\n";
cout << " Terminating abnormally because of an I/O error\n";
cout << " while expecting input for SAMPLE.\n";
exit ( 1 );
}
if ( s_eqi ( sample_string, "RANDOM" ) )
{
cout << " User input INIT = \"RANDOM\".\n";
sample = -1;
}
else if ( s_eqi ( sample_string, "UNIFORM" ) )
{
cout << " User input INIT = \"UNIFORM\".\n";
sample = 0;
}
else if ( s_eqi ( sample_string, "HALTON" ) )
{
cout << " User input INIT = \"HALTON\".\n";
sample = 1;
}
else if ( s_eqi ( sample_string, "GRID" ) )
{
cout << " User input INIT = \"GRID\".\n";
sample = 2;
}
else if ( s_eqi ( sample_string, "USER" ) )
{
cout << " User input INIT = \"USER\".\n";
sample = 3;
}
else
{
cout << "\n";
cout << "LCVT_DATASET\n";
cout << " The input value of SAMPLE \n";
cout << " is interpreted as a request for termination.\n";
cout << " Normal end of execution.\n";
exit ( 1 );
}
cout << "\n";
cout << " SAMPLE_NUM is the number of sample points for CVT.\n";
cout << "\n";
cout << " The Voronoi regions will be explored by generating\n";
cout << " SAMPLE_NUM points. For each sample point, the\n";
cout << " nearest generator is found. Using more points\n";
cout << " gives a better estimate of these regions.\n";
cout << "\n";
cout << " SAMPLE_NUM should be much larger than N, the\n";
cout << " number of generators.\n";
cout << "\n";
cout << " (Try \"10000\" if you do not have a preference.)\n";
cout << " (A zero or negative value terminates execution.)\n";
cout << "\n";
cout << " Enter SAMPLE_NUM:\n";
cin >> sample_num;
if ( cin.rdstate ( ) )
{
cin.clear ( );
cout << "\n";
cout << "LCVT_DATASET - Warning!\n";
cout << " Terminating abnormally because of an I/O error\n";
cout << " while expecting input for SAMPLE_NUM.\n";
exit ( 1 );
}
cout << " User input SAMPLE_NUM = " << sample_num << "\n";
if ( sample_num <= 0 )
{
cout << "\n";
cout << "LCVT_DATASET\n";
cout << " The input value of SAMPLE_NUM = " << sample_num << "\n";
cout << " is interpreted as a request for termination.\n";
cout << " Normal end of execution.\n";
exit ( 1 );
}
cout << "\n";
cout << " BATCH is the number of sample points to create\n";
cout << " at one time.\n";
cout << "\n";
cout << " BATCH should be between 1 and SAMPLE_NUM.\n";
cout << "\n";
cout << " It is FASTER to set BATCH to SAMPLE_NUM;\n";
cout << " setting BATCH to 1 requires the least memory.\n";
cout << "\n";
cout << " (Try \"" << i4_min ( sample_num, 1000 ) <<
"\" if you do not have a preference.)\n";
cout << " (A zero or negative value terminates execution.)\n";
cout << "\n";
cout << " Enter BATCH:\n";
cin >> batch;
if ( cin.rdstate ( ) )
{
cin.clear ( );
cout << "\n";
cout << "LCVT_DATASET - Warning!\n";
cout << " Terminating abnormally because of an I/O error\n";
cout << " while expecting input for SAMPLE_NUM.\n";
exit ( 1 );
}
cout << " User input BATCH = " << batch << "\n";
if ( batch <= 0 )
{
cout << "\n";
cout << "LCVT_DATASET\n";
cout << " The input value of BATCH = " << batch << "\n";
cout << " is interpreted as a request for termination.\n";
cout << " Normal end of execution.\n";
exit ( 1 );
}
cout << "\n";
cout << " LAT_IT_NUM is the number of Latinizing iterations.\n";
cout << "\n";
cout << " Each step of the latinizing iteration begins\n";
cout << " by carrying out CVT_IT_NUM steps of CVT iteration,\n";
cout << " after which the data is \"latinized\".\n";
cout << "\n";
cout << " Often, one latinizing step is enough.\n";
cout << "\n";
cout << " In some cases, it may be worth while to carry\n";
cout << " out several latinizing steps; that is, the\n";
cout << " Latinized data is smoothed by another series\n";
cout << " of CVT steps, then latinized, and so on.\n";
cout << "\n";
cout << " (Try \"1\" if you do not have a preference.)\n";
cout << " (A negative value terminates execution).\n";
cout << "\n";
cout << " Enter LAT_IT_NUM:\n";
cin >> lat_it_num;
if ( cin.rdstate ( ) )
{
cin.clear ( );
cout << "\n";
cout << "LCVT_DATASET - Warning!\n";
cout << " Terminating abnormally because of an I/O error\n";
cout << " while expecting input for LAT_IT_NUM.\n";
exit ( 1 );
}
cout << " User input LAT_IT_NUM = " << lat_it_num << "\n";
if ( cvt_it_num < 0 )
{
cout << "\n";
cout << "LCVT_DATASET\n";
cout << " The input value of LAT_IT_NUM = " << lat_it_num << "\n";
cout << " is interpreted as a request for termination.\n";
cout << " Normal end of execution.\n";
exit ( 1 );
}
cout << "\n";
cout << " OUTPUT is the name of a file into which\n";
cout << " the computed data may be stored.\n";
cout << "\n";
cout << " (Try \"lcvt.txt\" if you do not have a preference.)\n";
cout << " (A blank value terminates execution).\n";
cout << "\n";
cout << " Enter OUTPUT:\n";
cin >> output_file_name;
if ( cin.rdstate ( ) )
{
cin.clear ( );
cout << "\n";
cout << "LCVT_DATASET - Warning!\n";
cout << " Terminating abnormally because of an I/O error\n";
cout << " while expecting input for OUTPUT.\n";
exit ( 1 );
}
cout << " User input OUTPUT = \"" << output_file_name << "\".\n";
if ( s_len_trim ( output_file_name ) <= 0 )
{
cout << "\n";
cout << "LCVT_DATASET\n";
cout << " The input value of OUTPUT \n";
cout << " is interpreted as a request for termination.\n";
cout << " Normal end of execution.\n";
exit ( 1 );
}
//
// Initialize the data.
//
if ( init == 4 )
{
r = r8table_data_read ( input_file_name, dim_num, n );
}
else
{
n_total = n;
r = new double[dim_num*n];
reset = true;
region_sampler ( dim_num, n, n_total, r, init, reset, &seed );
}
if ( debug )
{
cout << "\n";
cout << " Latin IT CVT Energy Latin Energy\n";
cout << "\n";
}
for ( lat_it = 1; lat_it <= lat_it_num; lat_it++ )
{
if ( debug )
{
cout << "\n";
cout << " CVT IT Change\n";
cout << "\n";
}
for ( cvt_it = 1; cvt_it <= cvt_it_num; cvt_it++ )
{
cvt_iteration ( dim_num, n, r, sample_num, sample, &seed, &cvt_it_diff );
if ( debug )
{
cout << " " << setw(8) << cvt_it
<< " " << setw(14) << cvt_it_diff << "\n";
}
}
if ( debug )
{
cout << "\n";
}
cvt_energy = cluster_energy ( dim_num, n, r, sample_num, sample, &seed );
r8mat_latinize ( dim_num, n, r );
lat_energy = cluster_energy ( dim_num, n, r, sample_num, sample, &seed );
cout << " " << setw(8) << lat_it
<< " " << setw(14) << cvt_energy
<< " " << setw(14) << lat_energy << "\n";
}
//
// Write the data to a file.
//
lcvt_write ( dim_num, n, seed_init, init, input_file_name, sample,
sample_num, cvt_it_num, cvt_energy, lat_it_num, lat_energy,
r, output_file_name );
delete [] r;
cout << "\n";
cout << " The data was written to the file \""
<< output_file_name << "\".\n";
cout << "\n";
cout << "LCVT_DATASET:\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
}
//****************************************************************************80
char ch_cap ( char c )
//****************************************************************************80
//
// Purpose:
//
// CH_CAP capitalizes a single character.
//
// Discussion:
//
// This routine should be equivalent to the library "toupper" function.
//
// Modified:
//
// 19 July 1998
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, char C, the character to capitalize.
//
// Output, char CH_CAP, the capitalized character.
//
{
if ( 97 <= c && c <= 122 )
{
c = c - 32;
}
return c;
}
//****************************************************************************80
bool ch_eqi ( char c1, char c2 )
//****************************************************************************80
//
// Purpose:
//
// CH_EQI is true if two characters are equal, disregarding case.
//
// Modified:
//
// 13 June 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, char C1, char C2, the characters to compare.
//
// Output, bool CH_EQI, is true if the two characters are equal,
// disregarding case.
//
{
if ( 97 <= c1 && c1 <= 122 )
{
c1 = c1 - 32;
}
if ( 97 <= c2 && c2 <= 122 )
{
c2 = c2 - 32;
}
return ( c1 == c2 );
}
//****************************************************************************80
int ch_to_digit ( char c )
//****************************************************************************80
//
// Purpose:
//
// CH_TO_DIGIT returns the integer value of a base 10 digit.
//
// Example:
//
// C DIGIT
// --- -----
// '0' 0
// '1' 1
// ... ...
// '9' 9
// ' ' 0
// 'X' -1
//
// Modified:
//
// 13 June 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, char C, the decimal digit, '0' through '9' or blank are legal.
//
// Output, int CH_TO_DIGIT, the corresponding integer value. If C was
// 'illegal', then DIGIT is -1.
//
{
int digit;
if ( '0' <= c && c <= '9' )
{
digit = c - '0';
}
else if ( c == ' ' )
{
digit = 0;
}
else
{
digit = -1;
}
return digit;
}
//****************************************************************************80
double cluster_energy ( int dim_num, int n, double cell_generator[],
int sample_num, int sample_function_cvt, int *seed )
//****************************************************************************80
//
// Purpose:
//
// CLUSTER_ENERGY returns the energy of a dataset.
//
// Discussion:
//
// The energy is the integral of the square of the distance from each point
// in the region to its nearest generator.
//
// Modified:
//
// 08 September 2006
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int N, the number of generators.
//
// Input, double CELL_GENERATOR[DIM_NUM*N], the coordinates of the points.
//
// Input, int SAMPLE_NUM, the number of sample points to use.
//
// Input, int SAMPLE_FUNCTION_CVT, specifies how the sampling is done.
// -1, 'RANDOM', using C++ RANDOM function;
// 0, 'UNIFORM', using a simple uniform RNG;
// 1, 'HALTON', from a Halton sequence;
// 2, 'GRID', points from a grid;
// 3, 'USER', call "user" routine.
//
// Input/output, int *SEED, a seed for the random number generator.
//
// Output, double CLUSTER_ENERGY, the estimated energy.
//
{
double energy;
int i;
int j;
int nearest;
bool reset;
double *x;
x = new double [dim_num];
energy = 0.0;
reset = true;
for ( j = 0; j < sample_num; j++ )
{
//
// Generate a sampling point X.
//
region_sampler ( dim_num, 1, sample_num, x, sample_function_cvt,
reset, seed );
reset = false;
//
// Find the nearest cell generator.
//
nearest = find_closest ( dim_num, n, x, cell_generator );
for ( i = 0; i < dim_num; i++ )
{
energy = energy
+ pow ( x[i] - cell_generator[i+nearest*dim_num], 2 );
}
}
//
// Add the contribution to the energy.
//
energy = energy / ( double ) ( sample_num );
delete [] x;
return energy;
}
//****************************************************************************80
void cvt_iteration ( int m, int n, double generator[], int sample_num,
int sample_function_cvt, int *seed, double *change_l2 )
//****************************************************************************80
//
// Purpose:
//
// CVT_ITERATION takes one step of the CVT iteration.
//
// Discussion:
//
// The routine is given a set of points, called "generators", which
// define a tessellation of the region into Voronoi cells. Each point
// defines a cell. Each cell, in turn, has a centroid, but it is
// unlikely that the centroid and the generator coincide.
//
// Each time this CVT iteration is carried out, an attempt is made
// to modify the generators in such a way that they are closer and
// closer to being the centroids of the Voronoi cells they generate.
//
// A large number of sample points are generated, and the nearest generator
// is determined. A count is kept of how many points were nearest to each
// generator. Once the sampling is completed, the location of all the
// generators is adjusted. This step should decrease the discrepancy
// between the generators and the centroids.
//
// Modified:
//
// 05 May 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, the spatial dimension.
//
// Input, int N, the number of Voronoi cells.
//
// Input/output, double GENERATOR[M*N], the Voronoi
// cell generators. On output, these have been modified
//
// Input, int SAMPLE_NUM, the number of sample points.
//
// Input, int SAMPLE_FUNCTION_CVT, region sampling function:
// -1, sampling function is RANDOM (C++ STDLIB library function);
// 0, sampling function is UNIFORM;
// 1, sampling function is HALTON;
// 2, sampling function is GRID;
//
// Input/output, int *SEED, the random number seed.
//
// Output, double *CHANGE_L2, the L2 norm of the difference between
// the input and output data.
//
{
double *generator2;
int *counter;
int i;
int j;
int k;
int nearest;
bool reset;
double *x;
generator2 = new double[m*n];
for ( k = 0; k < m*n; k++ )
{
generator2[k] = 0.0;
}
counter = new int[n];
for ( i = 0; i < n; i++ )
{
counter[i] = 0;
}
x = new double[m];