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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
|
/*
$Id: rep.cc,v 1.21 2014/06/12 01:44:08 mp Exp $
AutoDock
Copyright (C) 2009 The Scripps Research Institute. All rights reserved.
AutoDock is a Trade Mark of The Scripps Research Institute.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/********************************************************************
The methods associated with the Representation class hierarchy.
rsh 9/95
********************************************************************/
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <assert.h>
#include "rep.h"
#include "ranlib.h"
#include "structs.h"
#include "stop.h"
extern FILE *logFile; // DEBUG and bug check messages only
extern int debug;
//______________________________________________________________________________
//
// Initializations
int IntVector::low = -INT_MAX/4;
int IntVector::high = INT_MAX/4;
/* A nonstatic data member cannot be defined outside its class:
* Real RealVector::low = REALV_LOW;
* Real RealVector::high = REALV_HIGH;
*/
// For now assume that normalize handles this constraint
Real ConstrainedRealVector::low = REALV_LOW;
Real ConstrainedRealVector::high = REALV_HIGH;
double ConstrainedRealVector::sum = 1.0;
Real BitVector::one_prob = 0.5;
//______________________________________________________________________________
//
// The member functions for the canonical base classes
//______________________________________________________________________________
//______________________________________________________________________________
//
// This constructor is used to generate the initial (random) instances
// of an integer vector.
IntVector::IntVector(const int number_of_els)
: Representation(number_of_els)
{
register int i;
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/IntVector::IntVector(int number_of_els=%d) \n",number_of_els);
#endif /* DEBUG */
mytype = T_IntV;
vector = new int[number_of_els];
for (i=0; i<number_of_els; i++) {
vector[i] = ignuin(low, high);
}
}
//______________________________________________________________________________
//
IntVector::IntVector(const int num_els, const int init_low, const int init_high)
: Representation(num_els)
{
register int i;
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/IntVector::IntVector(int num_els=%d, int init_low=%d, int init_high=%d) \n",num_els,init_low,init_high);
#endif /* DEBUG */
mytype = T_IntV;
vector = new int[num_els];
for (i=0; i<num_els; i++) {
vector[i] = ignuin(init_low, init_high);
}
}
//______________________________________________________________________________
//
// This constructor does an actual copy of the vector.
// We could make gains by doing reference counting, but
// that's for the future.
IntVector::IntVector(const IntVector &original)
: Representation(original.number_of_pts)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/IntVector::IntVector(const IntVector &original) \n");
#endif /* DEBUG */
mytype = T_IntV;
if (original.vector!=NULL) {
vector = new int[number_of_pts];
} else {
vector = NULL;
}
for (register unsigned int i=0; i<number_of_pts; i++) {
vector[i] = original.vector[i];
}
}
//______________________________________________________________________________
//
void IntVector::write(const unsigned char& value, const int gene)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void IntVector::write(unsigned char value=%c, int gene=%d) \n",value,gene);
#endif /* DEBUG */
(void)fprintf(logFile,"Writing a Bit to an Int!\n"); // used to be "stderr"
(void)fprintf(logFile,"value= \"%c\", gene= %d\n", value, gene); // used to be "stderr"
}
//______________________________________________________________________________
//
void IntVector::write(const int& value, const int gene) /* not const */
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void IntVector::write(int value=%d, int gene=%d)` \n",value,gene);
#endif /* DEBUG */
if (value<low) {
vector[gene] = low;
} else if (value>high) {
vector[gene] = high;
} else {
vector[gene] = value;
}
}
//______________________________________________________________________________
//
void IntVector::write(ConstDouble value, const int gene)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void IntVector::write(double value=%lf, int gene=%d) \n",value,gene);
#endif /* DEBUG */
(void)fprintf(logFile,"Writing a Real to an Int!\n"); // used to be "stderr"
(void)fprintf(logFile,"value= %lf, gene= %d\n", value, gene); // used to be "stderr"
}
//______________________________________________________________________________
//
void IntVector::write(const Element& value, const int gene) /* not const */
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void IntVector::write(const Element value, int gene=%d) \n",gene);
#endif /* DEBUG */
if (value.integer<low) {
(void)fprintf(logFile,"Writing out-of-bounds Int!\n"); // used to be "stderr"
vector[gene] = low;
} else if (value.integer>high) {
(void)fprintf(logFile,"Writing out-of-bounds Int!\n"); // used to be "stderr"
vector[gene] = high;
} else {
vector[gene] = value.integer;
}
}
//______________________________________________________________________________
//
const Element IntVector::gene(const unsigned int gene_number) const
{
Element retval;
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/const Element IntVector::gene(unsigned int gene_number=%d) const \n",gene_number);
#endif /* DEBUG */
if (gene_number>=number_of_pts) {
char error_message[200];
(void)sprintf(error_message, "ERROR: BUGCHECK: Trying to access an out-of-bounds IntVector gene! (gene_number=%d >= number_of_pts=%d)\n", gene_number, number_of_pts); // used to be "stderr"
stop(error_message);
return(retval); // NOTREACHED
} else {
retval.integer = vector[gene_number];
return(retval); // typecast int as Element
}
}
//______________________________________________________________________________
//
const void *IntVector::internals(void) const
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/const void *IntVector::internals(void) const \n");
#endif /* DEBUG */
return((void *)(&vector[0]));
}
//______________________________________________________________________________
//
Representation &IntVector::operator=(const Representation &original)
{
register unsigned int i;
#ifdef DEBUG
(void)fprintf(logFile, "\nrep.cc/Representation &IntVector::operator=(const Representation &original) \n");
#endif /* DEBUG */
const int *const array = (int *)original.internals();
if (original.type()==T_IntV) {
number_of_pts = original.number_of_points();
if (vector!=NULL) {
delete [] vector;
}
if (array!=NULL) {
vector = new int[number_of_pts];
} else {
vector = NULL;
}
for (i=0; i<number_of_pts; i++) {
vector[i] = array[i];
}
} else {
stop("Unable to invoke Representation &IntVector operator= because Representations don't match!\n");
}
return(*this);
}
//______________________________________________________________________________
//
// This constructor is used to initialize the starting population,
// with random values between REALV_LOW and REALV_HIGH.
//
RealVector::RealVector(/* not const */ int num_els)
: Representation(num_els)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(int num_els=%d) \n",num_els);
#endif /* DEBUG */
mytype = T_RealV;
low = REALV_LOW;
high = REALV_HIGH;
vector = new double[num_els];
for (; --num_els>=0;) {
vector[num_els] = double(genunf(low, high));
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(num_els) vector[num_els] = %.3f\n", vector[num_els] );
#endif /* DEBUG */
}
} // RealVector::RealVector(int num_els)
//______________________________________________________________________________
//
// This constructor is used to initialize the starting population,
// with elements of the vector set to random values between
// the user-specified values, init_low and init_high.
//
RealVector::RealVector(/* not const */ int num_els, ConstDouble init_low, ConstDouble init_high)
: Representation(num_els)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(int num_els=%d, double init_low=%lf, double init_high=%lf) \n",num_els,init_low,init_high);
#endif /* DEBUG */
mytype = T_RealV;
low = init_low;
high = init_high;
vector = new double[num_els];
for (; --num_els>=0;) {
vector[num_els] = double(genunf(init_low, init_high));
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(num_els, init_low, init_high) vector[num_els] = %.3f\n", vector[num_els] );
#endif /* DEBUG */
}
} // RealVector::RealVector(int num_els, double init_low, double init_high)
//______________________________________________________________________________
//
// This constructor is used to initialize the starting population,
// setting the value of the second and all remaining elements in the
// vector (if any) to a uniformly-distributed random number between
// the user-specified bounds, init_low and init_high;
// but the first value in this vector is set to the value supplied as the last argument.
// This is useful for specifying an initial axis-angle rotation angle.
//
RealVector::RealVector(const int num_els, ConstDouble init_low, ConstDouble init_high, ConstDouble init_first_value)
: Representation(num_els)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(int num_els=%d, double init_low=%lf, double init_high=%lf, double init_first_value=%lf) \n",num_els,init_low,init_high,init_first_value);
#endif /* DEBUG */
register int i=0;
mytype = T_RealV;
low = init_low;
high = init_high;
vector = new double[num_els];
// Set the first element in the vector to "init_first_value":
vector[i] = init_first_value;
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(i, init_low, init_high, init_first_value) vector[i] = %.3f\n", vector[i] );
#endif /* DEBUG */
// Set the second and remaining elements in the vector, if any:
for (i=1; i<num_els; i++) {
vector[i] = double(genunf(init_low, init_high));
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(i, init_low, init_high, init_first_value) vector[i] = %.3f\n", vector[i] );
#endif /* DEBUG */
}
} // RealVector::RealVector(int num_els, double init_low, double init_high, double init_first_value)
//______________________________________________________________________________
//
// This constructor is used to initialize the starting population,
// with elements of a vector of length 3 being set to the user specified values
// nx, ny, and nz.
// This is useful for specifying an initial orientation's axis components
//
RealVector::RealVector( const int num_els, ConstDouble init_low, ConstDouble init_high, ConstDouble nx, ConstDouble ny, ConstDouble nz )
: Representation(num_els)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(double nx=%lf, double ny=%lf, double nz=%lf, int num_els=%d) \n", nx, ny, nz, num_els);
#endif /* DEBUG */
mytype = T_RealV;
low = init_low;
high = init_high;
vector = new double[3];
// Set the unit vector.
vector[0] = nx;
vector[1] = ny;
vector[2] = nz;
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(nx,ny,nz,num_els) vector[0] = %.3f\n", vector[0] );
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(nx,ny,nz,num_els) vector[1] = %.3f\n", vector[1] );
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(nx,ny,nz,num_els) vector[2] = %.3f\n", vector[2] );
#endif /* DEBUG */
} // RealVector::RealVector(double nx, double ny, double nz, int num_els)
//______________________________________________________________________________
//
// This constructor is used to initialize the starting population,
// with elements of a vector of length 4 being set to the user specified values
// w, x, y, z
// This is useful for specifying an initial quaternion rotation' unit vector.
//
RealVector::RealVector( const int num_els, ConstDouble init_low, ConstDouble init_high, ConstDouble x, ConstDouble y, ConstDouble z, ConstDouble w)
: Representation(num_els)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(int num_els=%d, double x=%lf, double y=%lf, double z=%lf, double w=%lf) \n", num_els, x, y, z, w );
#endif /* DEBUG */
mytype = T_RealV;
low = init_low;
high = init_high;
vector = new double[4];
// Set the x,y,z,w components of the quaternion.
vector[0] = x;
vector[1] = y;
vector[2] = z;
vector[3] = w;
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(num_els,x,y,z,w) vector[0] = %.3f\n", vector[0] );
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(num_els,x,y,z,w) vector[1] = %.3f\n", vector[1] );
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(num_els,x,y,z,w) vector[2] = %.3f\n", vector[2] );
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(num_els,x,y,z,w) vector[3] = %.3f\n", vector[3] );
#endif /* DEBUG */
}
//______________________________________________________________________________
//
// Do a deep copy of the original
//
RealVector::RealVector(const RealVector &original)
: Representation(original.number_of_pts)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/RealVector::RealVector(const RealVector &original) \n");
#endif /* DEBUG */
mytype = T_RealV;
low = original.low;
high = original.high;
if (original.vector!=NULL) {
vector = new double[original.number_of_pts];
} else {
vector = NULL;
}
for (register unsigned int i=0; i<original.number_of_pts; i++) {
vector[i] = original.vector[i];
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/i=%d, original.number_of_pts=%d, vector[%d]= %.3f\n",i, original.number_of_pts, i, vector[i]);
#endif /* DEBUG */
}
}
//______________________________________________________________________________
//
void RealVector::write(const unsigned char& value, const int gene) /* not const ... in sibling classes */
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void RealVector::write(unsigned char value=%c, int gene=%d) \n",value,gene);
#endif /* DEBUG */
(void)fprintf(logFile,"Writing a Bit to a Real!\n"); // used to be "stderr"
(void)fprintf(logFile,"value= \"%c\", gene= %d\n", value, gene); // used to be "stderr"
}
//______________________________________________________________________________
//
void RealVector::write(const int& value, const int gene) /* not const ... in sibling classes */
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void RealVector::write(int value=%d, int gene=%d) \n",value,gene);
#endif /* DEBUG */
(void)fprintf(logFile,"Writing an Int to a Real!\n"); // used to be "stderr"
(void)fprintf(logFile,"value= %ld, gene= %d\n", (long)value, gene); // used to be "stderr"
}
//______________________________________________________________________________
//
void RealVector::write(ConstDouble value, const int gene) /* not const */
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void RealVector::write(double value=%lf, int gene=%d) \n",value,gene);
#endif /* DEBUG */
if (value<low) {
// if (debug > 0) {
// (void)fprintf(logFile,"WARNING: Writing out of bounds Real! value (%lf) too low (%lf)\n",value,low); // used to be "stderr"
// }
vector[gene] = low;
} else if (value>high) {
// if (debug > 0) {
// (void)fprintf(logFile,"WARNING: Writing out of bounds Real! value (%lf) too high (%lf)\n",value,high); // used to be "stderr"
// }
vector[gene] = high;
} else {
vector[gene] = value;
}
}
//______________________________________________________________________________
//
void RealVector::write(const Element& value, const int gene) /* not const */
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void RealVector::write(const Element value, int gene=%d) \n",gene);
#endif /* DEBUG */
if (value.real<low) {
vector[gene] = low;
} else if (value.real>high) {
vector[gene] = high;
} else {
vector[gene] = value.real;
}
}
//______________________________________________________________________________
//
const Element RealVector::gene(const unsigned int gene_number) const
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/const Element RealVector::gene(unsigned int gene_number=%d) const \n",gene_number);
#endif /* DEBUG */
Element retval;
if (gene_number>=number_of_pts) {
(void)fprintf(logFile,"ERROR: Trying to access an out-of-bounds RealVector gene (gene_number=%d >= number_of_pts=%d)\n", gene_number, number_of_pts); // used to be "stderr"
retval.real = 0.0;
return(retval);
} else {
#ifdef DEBUG
pr( logFile, "rep.cc / retval.real = vector[gene_number=%d] = %lf\n", gene_number, vector[gene_number] );
#endif
retval.real = vector[gene_number];
#ifdef DEBUG
pr( logFile, "rep.cc / retval.real = %lf\n", retval.real );
#endif
return(retval);
}
}
//______________________________________________________________________________
//
const void *RealVector::internals(void) const
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/const void *RealVector::internals(void) const \n");
#endif /* DEBUG */
return((void *)(&vector[0]));
}
//______________________________________________________________________________
//
Representation &RealVector::operator=(const Representation &original)
{
#ifdef DEBUG
(void)fprintf(logFile, "\nrep.cc/Representation &RealVector::operator=(const Representation &original) \n");
#endif /* DEBUG */
register unsigned int i;
double *array;
if (original.type()==T_RealV) {
low = ((const RealVector &)original).low;
high = ((const RealVector &)original).high;
array = (double *)original.internals();
number_of_pts = original.number_of_points();
if (vector!=NULL) {
delete [] vector;
}
if (array!=NULL) {
vector = new double[number_of_pts];
} else {
vector = NULL;
}
for (i=0; i<number_of_pts; i++) {
vector[i] = array[i];
}
} else {
(void)fprintf(logFile,"Unable to invoke operator= because Representations don't match!\n"); // used to be "stderr"
}
return(*this);
}
//______________________________________________________________________________
//
ConstrainedRealVector::ConstrainedRealVector(/* not const */ int num_els)
: Representation(num_els)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/ConstrainedRealVector::ConstrainedRealVector(int num_els) \n");
#endif /* DEBUG */
mytype = T_CRealV;
normalized = 0;
vector = new double[num_els];
for (; --num_els>=0;) {
vector[num_els] = double(genunf(low, high));
}
normalize();
}
//______________________________________________________________________________
//
ConstrainedRealVector::ConstrainedRealVector(/* not const */ int num_els, ConstDouble init_low, ConstDouble init_high)
: Representation(num_els)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/ConstrainedRealVector::ConstrainedRealVector(int num_els=%d, double init_low=%lf, double init_high=%lf) \n",num_els,init_low,init_high);
#endif /* DEBUG */
mytype = T_CRealV;
normalized = 0;
low = init_low;
high = init_high;
vector = new double[num_els];
for (; --num_els>=0;) {
vector[num_els] = double(genunf(init_low, init_high));
}
normalize();
}
//______________________________________________________________________________
//
ConstrainedRealVector::ConstrainedRealVector(const ConstrainedRealVector &original)
: Representation(original.number_of_pts)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/ConstrainedRealVector::ConstrainedRealVector(const ConstrainedRealVector &original) \n");
#endif /* DEBUG */
mytype = T_CRealV;
normalized = original.normalized;
if (original.vector != NULL) {
vector = new double[original.number_of_pts];
} else {
vector = NULL;
}
for (register unsigned int i=0; i<original.number_of_pts; i++) {
vector[i] = original.vector[i];
}
}
//______________________________________________________________________________
//
void ConstrainedRealVector::write(const unsigned char& value, const int gene) /* not const, inherited */
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void ConstrainedRealVector::write(unsigned char value=%c, int gene=%d) \n",value,gene);
#endif /* DEBUG */
(void)fprintf(logFile,"Writing a Bit to a Constrained Real\n"); // used to be "stderr"
(void)fprintf(logFile,"value= \"%c\", gene= %d\n", value, gene); // used to be "stderr"
}
//______________________________________________________________________________
//
void ConstrainedRealVector::write(const int& value, const int gene)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void ConstrainedRealVector::write(int value=%ld, int gene=%d) \n",(long)value,gene);
#endif /* DEBUG */
(void)fprintf(logFile,"Writing an Integer to a Constrained Real\n"); // used to be "stderr"
(void)fprintf(logFile,"value= %ld, gene= %d\n",(long)value,gene); // used to be "stderr"
}
//______________________________________________________________________________
//
void ConstrainedRealVector::write(ConstDouble value, const int gene) /* not const */
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void ConstrainedRealVector::write(double value=%lf, int gene=%d) \n",value,gene);
#endif /* DEBUG */
if (value<low) {
(void)fprintf(logFile,"Writing out-of-bounds Constrained Real\n"); // used to be "stderr"
vector[gene] = low;
} else if (value>high) {
(void)fprintf(logFile,"Writing out-of-bounds Constrained Real\n"); // used to be "stderr"
vector[gene] = high;
} else {
vector[gene] = value;
}
normalized = 0;
}
//______________________________________________________________________________
//
void ConstrainedRealVector::write(ConstDouble a, ConstDouble b, ConstDouble c, ConstDouble d)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void ConstrainedRealVector::write( double a=%lf, double b=%lf, double c=%lf, double d=%lf ) \n", a, b, c, d );
#endif /* DEBUG */
#define clamp_and_set_vector( value, gene ) \
if (value<low) { \
(void)fprintf(logFile,"Writing out-of-bounds Constrained Real\n"); \
vector[gene] = low; \
} else if (value>high) { \
(void)fprintf(logFile,"Writing out-of-bounds Constrained Real\n"); \
vector[gene] = high; \
} else { \
vector[gene] = value; \
}
assert( number_of_pts >= 4 );
clamp_and_set_vector( a, 0 );
clamp_and_set_vector( b, 1 );
clamp_and_set_vector( c, 2 );
clamp_and_set_vector( d, 3 );
normalize();
}
//______________________________________________________________________________
//
void ConstrainedRealVector::write(const Element& value, const int gene) /* not const */
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void ConstrainedRealVector::write(const Element value, int gene=%d) \n",gene);
#endif /* DEBUG */
if (value.real<low) {
vector[gene] = low;
} else if (value.real>high) {
vector[gene] = high;
} else {
vector[gene] = value.real;
}
normalized = 0;
}
//______________________________________________________________________________
//
void ConstrainedRealVector::normalize(void) /* not const */
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void ConstrainedRealVector::normalize(void) const \n");
#endif /* DEBUG */
if (!normalized) {
register unsigned int i;
register double tempsum = 0.0, hypotenuse;
for (i=0; i<number_of_pts; i++) {
tempsum += vector[i] * vector[i];
}
if ((tempsum - sum > ACCURACY) || (sum - tempsum > ACCURACY)) {
hypotenuse = sqrt(tempsum);
// normalize the vector[]
for (i=0; i<number_of_pts; i++) {
vector[i] /= hypotenuse;
}
}
//normalized = 1;
set_normalized_true();
}
}
//______________________________________________________________________________
//
const Element ConstrainedRealVector::gene(const unsigned int gene_number) const
{
Element retval;
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/const Element ConstrainedRealVector::gene(unsigned int gene_number=%d) const \n",gene_number);
#endif /* DEBUG */
if (gene_number>=number_of_pts) {
(void)fprintf(logFile,"ERROR: Trying to access an out-of-bounds ConstrainedRealVector gene (gene_number=%d >= number_of_pts=%d)\n", gene_number, number_of_pts); // used to be "stderr"
retval.real = 0.0;
return(retval);
} else {
// normalize(); // cannot normalize because gene(int) is const
retval.real = vector[gene_number];
return(retval);
}
}
//______________________________________________________________________________
//
const void *ConstrainedRealVector::internals(void) const
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/const void *ConstrainedRealVector::internals(void) const \n");
#endif /* DEBUG */
return((void *)(&vector[0]));
}
//______________________________________________________________________________
//
Representation &ConstrainedRealVector::operator=(const Representation &original)
{
register unsigned int i;
double *array;
#ifdef DEBUG
(void)fprintf(logFile, "\nrep.cc/Representation &ConstrainedRealVector::operator=(const Representation &original) \n");
#endif /* DEBUG */
array = (double *)original.internals();
if (original.type()==T_CRealV) {
number_of_pts = original.number_of_points();
normalized = original.is_normalized();
if (vector!=NULL) {
delete [] vector;
}
if (array!=NULL) {
vector = new double[number_of_pts];
} else {
vector = NULL;
}
for (i=0; i<number_of_pts; i++) {
vector[i] = array[i];
}
} else {
(void)fprintf(logFile,"Unable to invoke operator= because Representations don't match!\n"); // used to be "stderr"
}
return(*this);
}
//______________________________________________________________________________
//
// This constructor is used to initialize the first
// generation of any particular bitvector. Right
// now bits are assumed to be unsigned chars.
BitVector::BitVector(/* not const */ int num_els)
: Representation(num_els)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/BitVector::BitVector(int num_els=%d) \n",num_els);
#endif /* DEBUG */
mytype = T_BitV;
vector = new unsigned char[num_els];
for (; --num_els>=0;) {
vector[num_els] = ((ranf()<one_prob)? 1 : 0);
}
}
//______________________________________________________________________________
//
BitVector::BitVector(/* not const */ int num_els, ConstReal prob)
: Representation(num_els)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/BitVector::BitVector(int num_els=%d, Real prob=%f) \n",num_els,prob);
#endif /* DEBUG */
mytype = T_BitV;
vector = new unsigned char[num_els];
for (; --num_els>=0;) {
vector[num_els] = ((ranf()<prob)? 1 : 0);
}
}
//______________________________________________________________________________
//
// There are probably better ways of doing this, e.g.
// using memcpy()
BitVector::BitVector(const BitVector &original)
: Representation(original.number_of_pts)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/BitVector::BitVector(const BitVector &original) \n");
#endif /* DEBUG */
mytype = T_BitV;
if (original.vector!=NULL) {
vector = new unsigned char[number_of_pts];
} else {
vector = NULL;
}
for (register unsigned int i=0; i<number_of_pts; i++) {
vector[i] = original.vector[i];
}
}
//______________________________________________________________________________
//
void BitVector::write(const unsigned char& value, const int gene) /* not const */
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void BitVector::write(unsigned char value=%c, int gene=%d) \n",value,gene);
#endif /* DEBUG */
vector[gene] = value;
}
//______________________________________________________________________________
//
void BitVector::write(const int& value, const int gene)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void BitVector::write(int value=%d, int gene=%d) \n",value,gene);
#endif /* DEBUG */
(void)fprintf(logFile,"Writing Int to Bit!\n"); // used to be "stderr"
(void)fprintf(logFile,"value= %ld, gene= %d\n",(long)value,gene); // used to be "stderr"
}
//______________________________________________________________________________
//
void BitVector::write(ConstDouble value, const int gene)
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void BitVector::write(double value=%lf, int gene=%d) \n",value,gene);
#endif /* DEBUG */
(void)fprintf(logFile,"Writing Real to Bit!\n"); // used to be "stderr"
(void)fprintf(logFile,"value= %lf, gene= %d\n",value,gene); // used to be "stderr"
}
//______________________________________________________________________________
//
void BitVector::write(const Element& value, const int gene) /* not const */
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/void BitVector::write(const Element value, int gene=%d) \n",gene);
#endif /* DEBUG */
vector[gene] = value.bit;
}
//______________________________________________________________________________
//
const Element BitVector::gene(const unsigned int gene_number) const
{
Element retval;
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/const Element BitVector::gene(unsigned int gene_number=%d) const \n",gene_number);
#endif /* DEBUG */
if (gene_number>=number_of_pts) {
(void)fprintf(logFile,"ERROR: Trying to access an out-of-bounds BitVector gene (gene_number=%d >= number_of_pts=%d)\n", gene_number, number_of_pts); // used to be "stderr"
retval.bit = 0;
return(retval);
} else {
retval.bit = vector[gene_number];
return(retval);
}
}
//______________________________________________________________________________
//
const void *BitVector::internals(void) const
{
#ifdef DEBUG
(void)fprintf(logFile, "rep.cc/const void *BitVector::internals(void) const \n");
#endif /* DEBUG */
return((void *)(&vector[0]));
}
//______________________________________________________________________________
//
Representation &BitVector::operator=(const Representation &original)
{
register unsigned int i;
unsigned char *array;
#ifdef DEBUG
(void)fprintf(logFile, "\nrep.cc/Representation &BitVector::operator=(const Representation &original) \n");
#endif /* DEBUG */
array = (unsigned char *)original.internals();
if (original.type()==T_BitV) {
if (vector!=NULL) {
delete [] vector;
}
number_of_pts = original.number_of_points();
if (array!=NULL) {
vector = new unsigned char[number_of_pts];
} else {
vector = NULL;
}
for (i=0; i<number_of_pts; i++) {
vector[i] = array[i];
}
} else {
(void)fprintf(logFile,"Unable to invoke operator= because Representations don't match!\n"); // used to be "stderr"
}
return(*this);
}
//______________________________________________________________________________
//
|