Changeset 1334 for applications/robust

Show
Ignore:
Timestamp:
04/18/11 09:35:41 (13 years ago)
Author:
sindj
Message:

Dalsi pokracovani samplingu, rozdelan sampling z exp. rozdeleni v polyhedronu. JS

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/robustlib.h

    r1331 r1334  
    17911791                if(conditions.size()-2-number_of_parameters>=0) 
    17921792                { 
     1793                         
     1794                         
    17931795                        mat sample_mat; 
    17941796                        map<double,toprow*> ordered_toprows;                     
     
    18051807                        while(sample_mat.cols()<n) 
    18061808                        { 
     1809                                cout << "*************************************" << endl; 
     1810                                 
    18071811                                double rnumber = randu()*sum_a; 
    18081812 
     
    18101814 
    18111815                                // This could be more efficient (log n), but map::upper_bound() doesn't let me dereference returned iterator 
     1816                                int toprow_count = 0; 
    18121817                                toprow* sampled_toprow;                          
    18131818                                for(map<double,toprow*>::iterator top_ref = ordered_toprows.begin();top_ref!=ordered_toprows.end();top_ref++) 
    18141819                                { 
    18151820                                        // cout << "CDF:"<< (*top_ref).first << endl; 
     1821                                        toprow_count++; 
    18161822 
    18171823                                        if((*top_ref).first >= rnumber) 
     
    18221828                                }                                
    18231829 
     1830                                cout << "Toprow/Count: " << toprow_count << "/" << ordered_toprows.size() << endl; 
    18241831                                // cout << &sampled_toprow << ";"; 
    18251832 
     
    18281835                                set<simplex*>::iterator s_ref; 
    18291836                                double sum_b = 0; 
     1837                                int simplex_count = 0; 
    18301838                                for(s_ref = sampled_toprow->triangulation.begin();s_ref!=sampled_toprow->triangulation.end();s_ref++) 
    18311839                                { 
     1840                                        simplex_count++; 
     1841                                         
    18321842                                        sum_b += (*s_ref)->probability; 
    18331843 
     
    18361846                                } 
    18371847 
     1848                                cout << "Simplex/Count: " << simplex_count << "/" << sampled_toprow->triangulation.size() << endl; 
    18381849                                // cout << &(*tri_ref) << endl; 
    18391850 
     
    19031914                                for(set<vertex*>::iterator vert_ref = ++(*s_ref)->vertices.begin();vert_ref!=(*s_ref)->vertices.end();vert_ref++) 
    19041915                                { 
    1905                                         jacobian.set_row(row_count,(*vert_ref)->get_coordinates()-base_vert->get_coordinates()); 
     1916                                        vec relative_coords = (*vert_ref)->get_coordinates()-base_vert->get_coordinates();                               
     1917 
     1918                                        jacobian.set_row(row_count,relative_coords); 
    19061919 
    19071920                                        row_count++; 
    19081921                                } 
    1909  
     1922                                 
     1923                                cout << "Jacobian: " << jacobian << endl; 
     1924 
     1925                                cout << "Gradient before trafo:" << gradient << endl; 
     1926                                                                 
     1927                                gradient = jacobian*gradient;    
     1928 
     1929                                // vec normal_gradient = gradient/sqrt(gradient*gradient); 
    19101930                                // cout << gradient << endl; 
    1911                                 // cout << jacobian << endl; 
    1912  
    1913                                 gradient = jacobian*gradient;    
    1914  
    1915                                 //cout << gradient << endl; 
    1916  
    1917                                 mat rotation_matrix = eye(dimension); 
    1918  
    1919                                 double squared_length = 1; 
     1931                                // cout << normal_gradient << endl; 
     1932                                // cout << sqrt(gradient*gradient) << endl; 
     1933 
     1934                                mat rotation_matrix = eye(dimension);                            
     1935 
     1936                                                                 
    19201937 
    19211938                                for(int i = 1;i<dimension;i++) 
    19221939                                { 
    1923                                         double t = gradient[i]/sqrt(squared_length); 
    1924  
    1925                                         double sin_theta = t/sqrt(1+pow(t,2)); 
    1926                                         double cos_theta = 1/sqrt(1+pow(t,2)); 
     1940                                        vec x_axis = zeros(dimension); 
     1941                                        x_axis.set(0,1); 
     1942 
     1943                                        x_axis = rotation_matrix*x_axis; 
     1944 
     1945                                        double t = gradient[i]/gradient*x_axis; 
     1946 
     1947                                        double sin_theta = sign(gradient[i])*t/sqrt(1+pow(t,2)); 
     1948                                        double cos_theta = sign(gradient*x_axis)/sqrt(1+pow(t,2)); 
    19271949 
    19281950                                        mat partial_rotation = eye(dimension); 
     
    19341956                                        partial_rotation.set(i,0,-sin_theta); 
    19351957                                         
    1936                                         rotation_matrix = rotation_matrix*partial_rotation; 
    1937                                          
    1938                                         squared_length += pow(gradient[i],2); 
     1958                                        rotation_matrix = rotation_matrix*partial_rotation;                              
     1959                                         
    19391960                                } 
    19401961 
    19411962                                // cout << rotation_matrix << endl; 
    1942                                 // cout << gradient << endl; 
     1963                                cout << "Gradient after trafo:" << gradient << endl; 
    19431964 
    19441965                                vec minima = numeric_limits<double>::max()*ones(number_of_parameters); 
     
    19471968                                vertex* min_grad; 
    19481969                                vertex* max_grad; 
    1949                                  
     1970                                                                 
    19501971                                for(set<vertex*>::iterator vert_ref = (*s_ref)->vertices.begin();vert_ref!=(*s_ref)->vertices.end();vert_ref++) 
    19511972                                { 
     
    19781999                                } 
    19792000 
    1980                                 vec sample_coordinates; 
     2001                                vec sample_coordinates;                          
    19812002 
    19822003                                for(int j = 0;j<number_of_parameters;j++) 
     
    19902011                                                double pos_value = sampled_toprow->condition_sum.right(sampled_toprow->condition_sum.size()-1)*max_grad->get_coordinates()-sampled_toprow->condition_sum[0]; 
    19912012                                                 
     2013                                                vec new_gradient = rotation_matrix*gradient; 
     2014 
     2015                                                cout << "New gradient(should have only first component nonzero):" << new_gradient << endl; 
     2016 
     2017                                                cout << "Max: " << maxima[0] << "  Min: " << minima[0] << "  Grad:" << new_gradient[0] << endl; 
    19922018                                                 
    1993                                                 //coordinate = log(rnumber*(exp(sampled_toprow->condition_sum.size()-1)*max_grad->get_coordinates()-sampled_toprow->condition_sum[0])-exp(sampled_toprow->condition_sum.right(sampled_toprow->condition_sum.size()-1)*min_grad->get_coordinates()-sampled_toprow->condition_sum[0]))+ 
     2019                                                double log_bracket = rnumber*exp(new_gradient[0]*maxima[0]/sigma)+(1-rnumber)*exp(new_gradient[0]*minima[0]/sigma); 
     2020                                                 
     2021                                                coordinate = log(log_bracket); 
    19942022                                        } 
    19952023                                }