00001
00002
00003
00004
00005
00006
00011 #include <df1b2fnl.h>
00012 #ifndef OPT_LIB
00013 #include <cassert>
00014 #include <climits>
00015 #endif
00016
00017 #define USE_BARD_PEN
00018 class newadkludge;
00019 newadkludge * newadkl=0;
00020
00021
00022 typedef funnel_init_var * PFUNNEL_INIT_VAR;
00023
00024 class laplace_approximation_calculator;
00025 laplace_approximation_calculator * funnel_init_var::lapprox=0;
00026 df1b2variable * funnel_init_var::funnel_constraints_penalty=0;
00027 unsigned int funnel_init_var::num_vars=0;
00028
00029 int funnel_init_var::num_inactive_vars=0;
00030 unsigned int funnel_init_var::num_active_parameters=0;
00031
00032 funnel_init_var ** funnel_init_var::list=new PFUNNEL_INIT_VAR[2000];
00033 funnel_init_var ** funnel_init_var::inactive_list=new PFUNNEL_INIT_VAR[2000];
00034 init_df1b2vector * funnel_init_var::py=0;
00035 imatrix * funnel_init_var::plist=0;
00036
00037 int funnel_check_flag=0;
00038
00043 void funnel_init_var::add_to_list(void)
00044 {
00045 #ifndef OPT_LIB
00046 assert(num_vars <= INT_MAX);
00047 #endif
00048 index = (int)num_vars;
00049 list[num_vars++]=this;
00050
00051 }
00052
00057 void funnel_init_var::delete_from_list(void)
00058 {
00059 #ifndef OPT_LIB
00060 assert(num_vars <= INT_MAX);
00061 #endif
00062 if (index != (int)(num_vars - 1))
00063 {
00064 cerr << "can only delete last member" << endl;
00065 ad_exit(1);
00066 }
00067 num_vars--;
00068 index=-1;
00069 }
00070
00075 void funnel_init_var::add_to_inactive_list(void)
00076 {
00077 index=-1;
00078 inactive_list[num_inactive_vars++]=this;
00079 }
00080
00085 void funnel_init_var::allocate(void)
00086 {
00087
00088 }
00089
00094 void check_pool_depths(void)
00095 {
00096 for (int i=0;i<df1b2variable::adpool_counter;i++)
00097 {
00098 cout << " Pool depth " << i << " "
00099 << df1b2variable::adpool_vector[i]->depth_check()
00100 << " " << df1b2variable::adpool_vector[i] << endl;
00101 }
00102 }
00103
00107 void funnel_init_var::deallocate_all(void)
00108 {
00109 if (plist)
00110 {
00111 #ifndef OPT_LIB
00112 assert(num_active_parameters <= INT_MAX);
00113 #endif
00114 if (plist->indexmax() != (int)num_active_parameters)
00115 {
00116 delete plist;
00117 plist = 0;
00118 }
00119 }
00120 if (py)
00121 {
00122 delete py;
00123 py = 0;
00124 }
00125 }
00126
00131 void funnel_init_var::allocate_all(void)
00132 {
00133 re_objective_function_value::pobjfun->deallocate();
00134 if (lapprox)
00135 {
00136 if (lapprox->calling_set)
00137 {
00138 (*lapprox->calling_set)(0,0)++;
00139 }
00140 }
00141 num_active_parameters=funnel_init_var::nvarcalc_all();
00142 #ifndef OPT_LIB
00143 assert(num_active_parameters <= INT_MAX);
00144 #endif
00145 if (py)
00146 {
00147 if (py->indexmax() != (int)num_active_parameters)
00148 {
00149 delete py;
00150 py=0;
00151 }
00152 }
00153 f1b2gradlist->reset();
00154
00155 adpool * tmppool=df1b2variable::pool;
00156 if (tmppool)
00157 {
00158
00159
00160 if (tmppool->nvar != num_active_parameters)
00161 {
00162
00163 int found_pool_flag=0;
00164 for (int i=0;i<df1b2variable::adpool_counter;i++)
00165 {
00166 if (df1b2variable::adpool_vector[i]->nvar == num_active_parameters)
00167 {
00168 df1b2variable::pool=df1b2variable::adpool_vector[i];
00169 found_pool_flag=1;
00170 break;
00171 }
00172 }
00173 if (!found_pool_flag)
00174 {
00175 df1b2variable::pool=new adpool();
00176 if (!df1b2variable::pool)
00177 {
00178 cerr << "Memory allocation error" << endl;
00179 ad_exit(1);
00180 }
00181 if (df1b2variable::adpool_counter>df1b2variable::adpool_vectorsize)
00182 {
00183
00184
00185
00186 int offset=1;
00187 adpool * tmp= df1b2variable::adpool_vector[offset];
00188 delete tmp;
00189 df1b2variable::adpool_vector[offset]=df1b2variable::pool;
00190 df1b2variable::nvar_vector[offset]=num_active_parameters;
00191 df1b2variable::adpool_use_index[offset]=
00192 df1b2variable::current_allocation_index;
00193 }
00194 else
00195 {
00196 df1b2variable::adpool_vector[df1b2variable::adpool_counter]=
00197 df1b2variable::pool;
00198 df1b2variable::nvar_vector[df1b2variable::adpool_counter]=
00199 num_active_parameters;
00200
00201 df1b2variable::increment_adpool_counter();
00202 }
00203 }
00204 }
00205 }
00206 else
00207 {
00208 df1b2variable::pool=new adpool();
00209 if (!df1b2variable::pool)
00210 {
00211 cerr << "Memory allocation error" << endl;
00212 ad_exit(1);
00213 }
00214 if (df1b2variable::adpool_counter>df1b2variable::adpool_vectorsize)
00215 {
00216 int offset=1;
00217 adpool * tmp= df1b2variable::adpool_vector[offset];
00218 delete tmp;
00219 df1b2variable::adpool_vector[offset]=df1b2variable::pool;
00220 df1b2variable::nvar_vector[offset]=num_active_parameters;
00221 df1b2variable::adpool_use_index[offset]=
00222 df1b2variable::current_allocation_index;
00223 }
00224 else
00225 {
00226 df1b2variable::adpool_use_index[df1b2variable::adpool_counter]=
00227 df1b2variable::current_allocation_index;
00228 df1b2variable::adpool_vector[df1b2variable::adpool_counter]=
00229 df1b2variable::pool;
00230 df1b2variable::nvar_vector[df1b2variable::adpool_counter]=
00231 num_active_parameters;
00232
00233 df1b2variable::increment_adpool_counter();
00234 }
00235 }
00236 df1b2variable::current_allocation_index++;
00237 df1b2variable::nvar=num_active_parameters;
00238 df1b2variable::set_blocksize();
00239
00240 re_objective_function_value::pobjfun->allocate();
00241
00242
00243 df1b2variable::minder=1;
00244 int maxdersave=df1b2variable::maxder;
00245 df1b2variable::maxder=(int)num_active_parameters;
00246 if (!py)
00247 {
00248 py = new init_df1b2vector(1,(int)num_active_parameters);
00249 }
00250
00251
00252 if (!py)
00253 {
00254 cerr << "memory allocation error" << endl;
00255 ad_exit(1);
00256 }
00257
00258
00259
00260 if (plist)
00261 {
00262 if (plist->indexmax() != (int)num_active_parameters)
00263 {
00264 delete plist;
00265 plist=0;
00266 }
00267 }
00268 if (!plist)
00269 {
00270 plist = new imatrix(1,(int)num_active_parameters,1,2);
00271 }
00272 if (!plist)
00273 {
00274 cerr << "memory allocation error" << endl;
00275 ad_exit(1);
00276 }
00277
00278 int ii=1;
00279 for(unsigned int i=0;i<num_vars;i++)
00280 {
00281 list[i]->xinit(*py,ii);
00282 }
00283
00284 ii=1;
00285 for(unsigned int i=0;i<num_vars;i++)
00286 {
00287 list[i]->set_index(*plist,ii);
00288 }
00289
00290 for(int i=0;i<num_inactive_vars;i++)
00291 {
00292 inactive_list[i]->allocate();
00293 }
00294
00295 funnel_init_var::reset(*py);
00296
00297
00298 df1b2variable::maxder=maxdersave;
00299 }
00300
00305 funnel_init_df1b2variable::funnel_init_df1b2variable
00306 (const df1b2_init_number & _x) : df1b2variable(newadkl)
00307
00308 {
00309 ADUNCONST(df1b2_init_number,x)
00310 type=0;
00311 pointer=0;
00312 ind_index=x.get_ind_index();
00313 if (ind_index<0)
00314 {
00315 add_to_inactive_list();
00316 }
00317 else
00318 {
00319 add_to_list();
00320 #ifdef DIAG
00321 if (lapprox==0)
00322 {
00323 cout << "This can't happen" << endl;
00324 ad_exit(1);
00325 }
00326 #endif
00327 lapprox->used_flags(ind_index)+=1;
00328 }
00329
00330 xu=*(x.get_u());
00331 }
00332
00337 funnel_init_df1b2variable::funnel_init_df1b2variable
00338 (const random_effects_bounded_vector_info & _u)
00339 : df1b2variable(newadkl)
00340 {
00341 ADUNCONST(random_effects_bounded_vector_info,u)
00342 df1b2variable& x = (*(u.pv)).df1b2vector::operator () (u.i);
00343
00344 type=1;
00345 pointer=u.pv;
00346 ind_index = x.get_ind_index();
00347 if (ind_index<0)
00348 {
00349 add_to_inactive_list();
00350 }
00351 else
00352 {
00353 add_to_list();
00354 lapprox->used_flags(ind_index)+=1;
00355 }
00356 xu=*(x.get_u());
00357 }
00358
00363 void funnel_init_df1b2variable::allocate(const df1b2variable& x)
00364 {
00365 cerr << "Haven't defined htis yet" << endl;
00366 ad_exit(1);
00367 }
00368
00373 funnel_init_df1b2variable::funnel_init_df1b2variable
00374 (void) : df1b2variable(newadkl)
00375 {
00376 type=0;
00377 pointer=0;
00378 ind_index = -1;
00379 if (ind_index<0)
00380 {
00381 add_to_inactive_list();
00382 }
00383 else
00384 {
00385 add_to_list();
00386 }
00387 }
00388
00393 void funnel_init_df1b2variable::
00394 preallocate(const df1b2variable & _x)
00395 {
00396 ADUNCONST(df1b2variable,x)
00397 type=0;
00398 pointer=0;
00399 ind_index = x.get_ind_index();
00400 if (ind_index<0)
00401 {
00402 add_to_inactive_list();
00403 }
00404 else
00405 {
00406 add_to_list();
00407 lapprox->used_flags(ind_index)+=1;
00408 }
00409 xu=*(x.get_u());
00410 }
00411
00416 funnel_init_df1b2variable::funnel_init_df1b2variable
00417 (const funnel_init_df1b2variable& x):
00418 funnel_init_var(),
00419 df1b2variable(x)
00420 {}
00421
00426 funnel_init_df1b2variable::funnel_init_df1b2variable
00427 (const df1b2variable & _x) : df1b2variable(newadkl)
00428 {
00429 ADUNCONST(df1b2variable,x)
00430 type=0;
00431 pointer=0;
00432 ind_index = x.get_ind_index();
00433 get_ind_index() = x.get_ind_index();
00434 if (ind_index<0)
00435 {
00436 add_to_inactive_list();
00437 }
00438 else
00439 {
00440 add_to_list();
00441 lapprox->used_flags(ind_index)+=1;
00442 if (lapprox->calling_set)
00443 {
00444 int j=++(*lapprox->calling_set)(ind_index,0);
00445 int k=lapprox->used_flags(ind_index);
00446 if (j != k)
00447 {
00448 cerr << "This can't happen" << endl;
00449 ad_exit(1);
00450 }
00451 (*lapprox->calling_set)(ind_index,j)=(*lapprox->calling_set)(0,0);
00452 }
00453 }
00454 xu=*(x.get_u());
00455 }
00456
00461 void funnel_init_df1b2variable::allocate(void)
00462 {
00463 df1b2variable::allocate();
00464 *(get_u())=xu;
00465 if (index>=0)
00466 get_u_dot()[index]=1.0;
00467 }
00468
00473 funnel_dependent_df1b2variable::funnel_dependent_df1b2variable
00474 (const df1b2variable& x)
00475 {
00476 df1b2variable::operator = (x);
00477 if (!df1b2_gradlist::no_derivatives)
00478 {
00479
00480
00481 }
00482 df1b2_gradlist::set_no_derivatives();
00483 }
00484
00489 void funnel_init_df1b2variable::set_value(const init_df1b2vector& _x,
00490 const int& _ii)
00491 {
00492 df1b2variable pen=0.0;
00493 ADUNCONST(init_df1b2vector,x)
00494 ADUNCONST(int,ii)
00495 if (!pointer)
00496 {
00497 df1b2variable::operator = (x(ii++));
00498 }
00499 else
00500 {
00501 switch (type)
00502 {
00503 case 1:
00504 {
00505 df1b2_init_bounded_vector & b = *(df1b2_init_bounded_vector*)pointer;
00506 if (!initial_params::straight_through_flag)
00507 {
00508
00509
00510 df1b2variable::operator =
00511 (boundp(x(ii++),b.getminb(),b.getmaxb()));
00512 }
00513 else
00514 {
00515 df1b2variable::operator = (x(ii));
00516 *get_u()=boundp(*(x(ii++).get_u()),b.getminb(),b.getmaxb());
00517 }
00518 break;
00519 }
00520 case 2:
00521 default:
00522 {
00523 cerr << "the bounded matrix case in "
00524 " void funnel_init_df1b2variable::xinit has not bee implemented"
00525 << endl;
00526 ad_exit(1);
00527 }
00528 }
00529 }
00530 }
00531
00536 void funnel_init_df1b2variable::set_value(const init_df1b2vector& _x,
00537 const int& _ii,const df1b2variable& _pen)
00538 {
00539 ADUNCONST(init_df1b2vector,x)
00540 ADUNCONST(int,ii)
00541 ADUNCONST(df1b2variable,pen)
00542 if (!pointer)
00543 {
00544 df1b2variable::operator = (x(ii++));
00545 }
00546 else
00547 {
00548 switch (type)
00549 {
00550 case 1:
00551 {
00552 df1b2_init_bounded_vector & b = *(df1b2_init_bounded_vector*)pointer;
00553 laplace_approximation_calculator * l =lapprox;
00554 int uf=-1;
00555 if (ind_index>0)
00556 {
00557 uf=l->used_flags(ind_index);
00558 }
00559 if (uf > 1 && (l->hesstype ==2))
00560 {
00561 if (ind_index <=lapprox->xsize )
00562 {
00563 cout << " fixed effect used " << uf << " times" << endl;
00564 df1b2variable::operator = (boundp(x(ii++),b.getminb(),
00565 b.getmaxb()));
00566 }
00567 else
00568 {
00569 cout << lapprox->hesstype << endl;
00570 cout << " Error random effect used " << uf << " times" << endl;
00571 ad_exit(1);
00572 }
00573 }
00574 else
00575 {
00576 if (!initial_params::straight_through_flag)
00577 {
00578 df1b2variable::operator = (boundp(x(ii++),b.getminb(),b.getmaxb(),
00579 pen));
00580 }
00581 else
00582 {
00583 df1b2variable::operator = (x(ii++));
00584 *get_u()=boundp(*get_u(),b.getminb(),b.getmaxb());
00585 double diff=b.getmaxb()-b.getminb();
00586 df1b2variable ss=((*this)-b.getminb())/diff;
00587 # ifdef USE_BARD_PEN
00588 const double l4=log(4.0);
00589 double wght=.000001/diff;
00590 pen-=wght*(log(ss+double(1.e-40))+log((double(1.0)-ss)
00591 +double(1.e-40))+l4);
00592 # else
00593 XXXX
00594 # endif
00595 }
00596 }
00597 break;
00598 }
00599 case 2:
00600 default:
00601 {
00602 cerr << "the bounded matrix case in "
00603 " void funnel_init_df1b2variable::xinit has not bee implemented"
00604 << endl;
00605 ad_exit(1);
00606 }
00607 }
00608 }
00609 }
00610
00615 unsigned int funnel_init_var::nvarcalc_all(void)
00616 {
00617 int n = 0;
00618 for (unsigned int i=0;i<num_vars;i++)
00619 {
00620 n += list[i]->nvar_calc();
00621 }
00622 #ifndef OPT_LIB
00623 assert(n >= 0);
00624 #endif
00625 return (unsigned int)n;
00626 }
00627
00632 void funnel_init_df1b2variable::xinit(init_df1b2vector& y,int& ii)
00633 {
00634 if (!pointer)
00635 {
00636 y(ii)= xu;
00637 }
00638 else
00639 {
00640 switch (type)
00641 {
00642 case 1:
00643 {
00644 df1b2_init_bounded_vector & b = *(df1b2_init_bounded_vector*)pointer;
00645 y(ii)=boundpin(xu,b.getminb(),b.getmaxb());
00646
00647
00648 break;
00649 }
00650 case 2:
00651 default:
00652 {
00653 cerr << "the bounded matrix case in "
00654 " void funnel_init_df1b2variable::xinit has not bee implemented"
00655 << endl;
00656 ad_exit(1);
00657 }
00658 }
00659 }
00660 ii++;
00661 }
00662
00667 void funnel_init_df1b2variable::xinit(dvector& y,int& ii)
00668 {
00669 if (!pointer)
00670 {
00671 y(ii)= xu;
00672 }
00673 else
00674 {
00675 switch (type)
00676 {
00677 case 1:
00678 {
00679 df1b2_init_bounded_vector & b = *(df1b2_init_bounded_vector*)pointer;
00680 y(ii)=boundpin(xu,b.getminb(),b.getmaxb());
00681 break;
00682 }
00683 case 2:
00684 default:
00685 {
00686 cerr << "the bounded matrix case in "
00687 " void funnel_init_df1b2variable::xinit has not bee implemented"
00688 << endl;
00689 ad_exit(1);
00690 }
00691 }
00692 }
00693 ii++;
00694 }
00695
00696
00697
00698
00699
00700
00701
00702
00703
00708 void funnel_init_df1b2variable::set_index(imatrix& y,int& ii)
00709 {
00710
00711 y(ii,1)= ind_index;
00712 y(ii,2)= ii;
00713 ii++;
00714 }
00715
00720 void funnel_init_var::reset(init_df1b2vector& x)
00721 {
00722 int ii=1;
00723 df1b2variable pen=0.0;
00724 for (unsigned int i=0;i<num_vars;i++)
00725 {
00726 list[i]->set_value(x,ii,pen);
00727
00728 }
00729 if (funnel_init_var::funnel_constraints_penalty)
00730 {
00731 delete funnel_init_var::funnel_constraints_penalty;
00732 }
00733 funnel_init_var::funnel_constraints_penalty=new df1b2variable(pen);
00734 }
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00765 funnel_init_df1b2vector::funnel_init_df1b2vector(const df1b2vector & _x)
00766 {
00767
00768
00769 ADUNCONST(df1b2_init_vector,x)
00770 p=&_x;
00771 int mmin=p->indexmin();
00772 int mmax=p->indexmax();
00773 int ind_index = x(mmin).get_ind_index();
00774 if (ind_index<0)
00775 {
00776 add_to_inactive_list();
00777 }
00778 else
00779 {
00780 add_to_list();
00781 for (int i=mmin;i<=mmax;i++)
00782 {
00783 ind_index = x(i).get_ind_index();
00784 lapprox->used_flags(ind_index)+=1;
00785 }
00786 df1b2variable::noallocate=1;
00787 df1b2vector::allocate(mmin,mmax);
00788 df1b2variable::noallocate=0;
00789 }
00790 }
00794 funnel_init_df1b2vector::~funnel_init_df1b2vector()
00795 {
00796 df1b2vector::deallocate();
00797 }
00802 int funnel_init_df1b2vector::nvar_calc(void)
00803 {
00804 return p->indexmax()-p->indexmin()+1;
00805 }
00806
00811 void funnel_init_df1b2vector::xinit(init_df1b2vector& y,int& ii)
00812 {
00813
00814
00815
00816 int mmin=p->indexmin();
00817 int mmax=p->indexmax();
00818 int i;
00819 for (i=mmin;i<=mmax;i++)
00820 {
00821
00822 y(ii)= value((*p)(i));
00823 ii++;
00824 }
00825 }
00826
00831 void funnel_init_df1b2vector::set_index(imatrix& y,int& ii)
00832 {
00833 df1b2_init_vector * vp = (df1b2_init_vector *) p;
00834 int mmin=vp->indexmin();
00835 int mmax=vp->indexmax();
00836 int i;
00837 for (i=mmin;i<=mmax;i++)
00838 {
00839 y(ii,1)= ( *vp)(i).get_ind_index();
00840 y(ii,2)= ii;
00841 ii++;
00842 }
00843 }
00844
00849 void funnel_init_df1b2vector::set_value(const init_df1b2vector& _x,
00850 const int& _ii,const df1b2variable& _pen)
00851 {
00852 ADUNCONST(int,ii)
00853 ADUNCONST(init_df1b2vector,x)
00854 int mmin=p->indexmin();
00855 int mmax=p->indexmax();
00856 int i;
00857 for (i=mmin;i<=mmax;i++)
00858 {
00859 (*this)(i) = (x(ii++));
00860 }
00861 }
00862
00863
00864
00865
00870 funnel_init_bounded_df1b2vector::funnel_init_bounded_df1b2vector(
00871 const df1b2_init_bounded_vector& _x)
00872 {
00873 ADUNCONST(df1b2_init_bounded_vector,x)
00874
00875
00876 p=&_x;
00877 int mmin=x.indexmin();
00878 int mmax=x.indexmax();
00879 int ind_index = x(mmin).get_ind_index();
00880 if (ind_index<0)
00881 {
00882 add_to_inactive_list();
00883 }
00884 else
00885 {
00886 add_to_list();
00887 }
00888 df1b2variable::noallocate=1;
00889 df1b2vector::allocate(mmin,mmax);
00890 df1b2variable::noallocate=0;
00891 }
00892
00897 int funnel_init_bounded_df1b2vector::nvar_calc(void)
00898 {
00899 return p->indexmax()-p->indexmin()+1;
00900 }
00901
00906 void funnel_init_bounded_df1b2vector::xinit(init_df1b2vector& y,int& ii)
00907 {
00908 df1b2_init_bounded_vector * vp = (df1b2_init_bounded_vector *) p;
00909 int mmin=p->indexmin();
00910 int mmax=p->indexmax();
00911 int i;
00912 for (i=mmin;i<=mmax;i++)
00913 {
00914 y(ii)= value((*vp)(i));
00915 ii++;
00916 }
00917 }
00918
00923 void funnel_init_bounded_df1b2vector::set_index(imatrix& y,int& ii)
00924 {
00925 int mmin=p->indexmin();
00926 int mmax=p->indexmax();
00927 int i;
00928 for (i=mmin;i<=mmax;i++)
00929 {
00930 y(ii,1)= ( *(df1b2_init_bounded_vector *)(p) )(i).get_ind_index();
00931 y(ii,2)= ii;
00932 ii++;
00933 }
00934 }
00935
00940 void funnel_init_bounded_df1b2vector::set_value(const init_df1b2vector& _x,
00941 const int& _ii,const df1b2variable& _pen)
00942 {
00943 ADUNCONST(int,ii)
00944 ADUNCONST(init_df1b2vector,x)
00945 df1b2_init_bounded_vector * vp = (df1b2_init_bounded_vector *) p;
00946 int mmin=p->indexmin();
00947 int mmax=p->indexmax();
00948 int i;
00949 for (i=mmin;i<=mmax;i++)
00950 {
00951 (*this)(i) = (x(ii++));
00952 if (!initial_params::straight_through_flag)
00953 {
00954
00955
00956 (*this)(i) = (boundp(x(ii++),vp->getminb(),vp->getmaxb()));
00957 }
00958 else
00959 {
00960 (*this)(i) = (x(ii));
00961 *((*this)(i).get_u()) =
00962 boundp(*(x(ii++).get_u()),vp->getminb(),vp->getmaxb());
00963 }
00964 }
00965 }