wolfe_condition.c 4.03 KB
Newer Older
1
/*-----------------------------------------------------------------------------------------
2
 * Copyright (C) 2016  For the list of authors, see file AUTHORS.
3
 *
Florian Wittkamp's avatar
Florian Wittkamp committed
4
 * This file is part of IFOS.
5
 *
Florian Wittkamp's avatar
Florian Wittkamp committed
6
 * IFOS is free software: you can redistribute it and/or modify
7 8 9
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
 *
Florian Wittkamp's avatar
Florian Wittkamp committed
10
 * IFOS is distributed in the hope that it will be useful,
11 12 13 14 15
 * 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
Florian Wittkamp's avatar
Florian Wittkamp committed
16
 * along with IFOS. See file COPYING and/or <http://www.gnu.org/licenses/gpl-2.0.html>.
17 18 19 20 21 22 23 24 25 26 27 28 29
 -----------------------------------------------------------------------------------------*/
/* Florian Wittkamp */
/* Return values:
 * 1: condition 1 not fullfilled
 * 2: condition 2 not fullfilled
 * 0: Wolfe Condition fullfilled
 */
#include "fd.h"
int check_wolfe(float steplength, float misfit_old, float misfit_new, float ** grad_old_vs, float ** grad_new_vs, float ** update_vs, float ** grad_old_rho, float ** grad_new_rho, float ** update_rho, float ** grad_old_vp, float ** grad_new_vp, float ** update_vp, float c1, float c2, int NPAR_LBFGS){
    
    /* global */
    extern int NX,NY,MYID;
    extern FILE *FP;
30
    extern int ACOUSTIC;
31 32 33 34 35 36 37
    
    /* local */
    float left=0.0, right=0.0;
    float grad_dot_p_old=0.0,grad_dot_p_old_norm=0.0, grad_dot_p_new=0.0;
    float temp;
    float temp2;
    
38 39 40 41 42 43 44
    if(!ACOUSTIC){
        temp=matrix_product(grad_old_vs, update_vs);
        grad_dot_p_old+=temp;
        temp2=global_maximum(grad_old_vs);
        if(fabs(temp2)>0) grad_dot_p_old_norm+=temp/temp2;
        grad_dot_p_new+=matrix_product(grad_new_vs, update_vs);
    }
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    
    if(NPAR_LBFGS>1) {
        temp=matrix_product(grad_old_rho, update_rho);
        grad_dot_p_old+=temp;
        temp2=global_maximum(grad_old_rho);
        if(fabs(temp2)>0) grad_dot_p_old_norm+=temp/temp2;
        grad_dot_p_new+=matrix_product(grad_new_rho, update_rho);
    }
    
    if(NPAR_LBFGS>2) {
        temp=matrix_product(grad_old_vp, update_vp);
        grad_dot_p_old+=temp;
        temp2=global_maximum(grad_old_vp);
        if(fabs(temp2)>0) grad_dot_p_old_norm+=temp/temp2;
        grad_dot_p_new+=matrix_product(grad_new_vp, update_vp);
    }
    
62
    fprintf(FP,"\n\n ------- Wolfe condition -------- ");
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
    
    /*---------------------------------*/
    /*       Check condition 1         */
    /* f(x+a*p) <= f(x)+c1*a*grad(x)*p */
    /*---------------------------------*/
    
    left  = misfit_new;
    right = (misfit_old-c1*steplength*grad_dot_p_old_norm*1e-3);
    
    if(left<=right) {
        fprintf(FP,"\n Condition 1 OK: (%f <= %f) ",left,right);
    } else {
        fprintf(FP,"\n Condition 1 NOT OK: (%f <= %f) ",left,right);
        return 1;
    }
    
    /*-------------------------------*/
    /*       Check condition 2       */
    /* grad(x+a*p)*p >= c2*grad(x)*p */
    /*-------------------------------*/
    
    left  = -grad_dot_p_new;
    right = -c2*grad_dot_p_old;
    
    if(left>=right) {
        fprintf(FP,"\n Condition 2 OK: (%.1f>= %.1f)", left, right);
    } else {
        fprintf(FP,"\n Condition 2 NOT OK: (%.1f>= %.1f)", left, right);
        return 2;
    }
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
    fprintf(FP,"\n Steplength %.3f satisfy wolfe condition",steplength);
    return 0;
}


void wolfe_linesearch(int wolfe_status, float *alpha_SL_min, float *alpha_SL_max, float *alpha_SL){
    
    switch (wolfe_status) {
        case 1:
            *alpha_SL_max=*alpha_SL;
            *alpha_SL=0.5*(*alpha_SL_max+*alpha_SL_min);
            break;
            
        case 2:
            *alpha_SL_min=*alpha_SL;
            if(*alpha_SL_max==0) *alpha_SL=10*(*alpha_SL);
            if(*alpha_SL_max!=0) *alpha_SL=0.5*(*alpha_SL_max+*alpha_SL_min);
            break;
            
        default:
            err("\n wolfe_status not known to wolfe_linesearch");
            break;
    }
    
}