VIS

src/mark/markFPSolve.c

Go to the documentation of this file.
00001 
00028 #include "markInt.h"
00029 
00030 /*---------------------------------------------------------------------------*/
00031 /* Constant declarations                                                     */
00032 /*---------------------------------------------------------------------------*/
00033 
00034 
00035 /*---------------------------------------------------------------------------*/
00036 /* Stucture declarations                                                     */
00037 /*---------------------------------------------------------------------------*/
00038 
00039 
00040 /*---------------------------------------------------------------------------*/
00041 /* Type declarations                                                         */
00042 /*---------------------------------------------------------------------------*/
00043 
00044 
00045 /*---------------------------------------------------------------------------*/
00046 /* Variable declarations                                                     */
00047 /*---------------------------------------------------------------------------*/
00048 
00049 
00050 /*---------------------------------------------------------------------------*/
00051 /* Macro declarations                                                        */
00052 /*---------------------------------------------------------------------------*/
00053 
00054 
00057 /*---------------------------------------------------------------------------*/
00058 /* Static function prototypes                                                */
00059 /*---------------------------------------------------------------------------*/
00060 
00061 
00065 /*---------------------------------------------------------------------------*/
00066 /* Definition of exported functions                                          */
00067 /*---------------------------------------------------------------------------*/
00068 
00069 
00070 /*---------------------------------------------------------------------------*/
00071 /* Definition of internal functions                                          */
00072 /*---------------------------------------------------------------------------*/
00073 
00088 bdd_node ** 
00089 MarkAddFPSolve(
00090   CK *ck)
00091 {
00092     bdd_manager *manager;
00093     bdd_node **xAddVars, **yAddVars;
00094     bdd_node *Scaling;
00095     bdd_node *InitG, *G, *NewG;
00096     bdd_node *p, *q, *newTr;
00097     bdd_node *xCube, *ddTemp, *guess; 
00098     bdd_node *zero;
00099     bdd_node **result;
00100     bdd_node *probMatrix;
00101     int nVars, iter = 0;
00102     int converged;
00103     double max;
00104 
00105     manager = ck->manager;
00106     nVars = ck->nVars;
00107     xAddVars = ck->xAddVars;
00108     yAddVars = ck->yAddVars;
00109 
00110     result = ALLOC(bdd_node *, 2);
00111     if (result == NULL)
00112         return NULL;
00113     zero = bdd_read_zero(manager);
00114 
00115     /* Build an ADD for one step transition probability matrix */
00116     bdd_ref(probMatrix = MarkAddBuildCoeff(manager,ck->coeff, ck->piAddVars,
00117                                             ck->inputProb, ck->scale, nVars,
00118                                             ck->nPi));
00119     result[1] = probMatrix;
00120 
00121     /* create initial guess and print it;
00122      * equiprobability to all the states and probability 1 to one of the
00123      * reset states are currently supported
00124      */
00125     switch(ck->start) {
00126     case Start_EquiProb_c:
00127         (void)printf("Initial guess: equiprob\n");
00128         bdd_ref(ck->init_guess = ck->term_scc);
00129         p = bdd_add_const(manager,
00130                           (double) (1/(double)(ck->term_SCC_states)));
00131         bdd_ref(p);
00132         bdd_ref(q = bdd_add_ite(manager,ck->init_guess,p,zero)); 
00133         bdd_recursive_deref(manager,p);
00134         InitG = bdd_add_swap_variables(manager,q,xAddVars,yAddVars,nVars);
00135         bdd_ref(InitG);
00136         bdd_recursive_deref(manager,q);
00137         bdd_recursive_deref(manager,ck->init_guess);
00138         break;
00139     case Start_Reset_c:
00140     default:
00141         /* Pick one of the states in the TSCC as the initial guess */
00142         bdd_ref(ddTemp = bdd_add_bdd_threshold(manager,ck->term_scc,1));
00143         bdd_ref(guess = bdd_bdd_pick_one_minterm(manager, ddTemp,
00144                                              ck->xVars, nVars));
00145         bdd_recursive_deref(manager,ddTemp);
00146         bdd_ref(ck->init_guess = bdd_bdd_to_add(manager,guess));
00147         bdd_recursive_deref(manager,guess);
00148         bdd_ref(InitG = bdd_add_swap_variables(manager,ck->init_guess,
00149                                                xAddVars, yAddVars,nVars));
00150         bdd_recursive_deref(manager,ck->init_guess);
00151         break;
00152     }
00153 
00154     /* put prob. transition matrix in appropriate form (transpose)*/
00155     newTr = bdd_add_swap_variables(manager,probMatrix,xAddVars,yAddVars,nVars);
00156     bdd_ref(newTr);
00157 
00158     /* calculate the x-cube for abstraction */
00159     bdd_ref(xCube = bdd_add_compute_cube(manager,xAddVars,NULL,nVars));
00160  
00161     do {
00162         iter++;
00163         G = bdd_add_matrix_multiply(manager,newTr,InitG,yAddVars,nVars);
00164         bdd_ref(G);
00165         bdd_ref(Scaling = bdd_add_exist_abstract(manager,G,xCube));
00166 
00167         bdd_ref(NewG = bdd_add_apply(manager,bdd_add_divide,G,Scaling));
00168         bdd_recursive_deref(manager,G);
00169         G = NewG; 
00170         bdd_recursive_deref(manager,Scaling);
00171         q = bdd_add_swap_variables(manager,G,xAddVars,yAddVars,nVars);
00172         bdd_ref(q);
00173         max = bdd_add_value(bdd_add_find_max(manager,InitG));
00174         converged = bdd_equal_sup_norm(manager,q,InitG,ck->reltol*max,0);
00175         bdd_recursive_deref( manager,InitG);
00176         if (converged) {
00177             (void) fprintf(vis_stdout,"Iteration = %d\n",iter);
00178             bdd_recursive_deref( manager,newTr);
00179             bdd_recursive_deref( manager,q);
00180             bdd_recursive_deref(manager,xCube);
00181             result[0] = G;
00182             return result;
00183         }
00184         bdd_recursive_deref( manager,G);
00185         InitG = q;
00186     } while (1);
00187 
00188 } /* end of addFPSolve */
00189 
00190 
00204 bdd_node *
00205 MarkAddBuildCoeff(
00206   bdd_manager *manager,
00207   bdd_node *func,
00208   bdd_node **piAddVars,
00209   st_table *inputProb,
00210   double scale,
00211   int nVars,
00212   int nPi)
00213 {
00214     /* Given func, the coefficient matrix is built */
00215     /* this function is used first to build the collapsed coeff matrix */
00216     /* and then the matrix for every TSCC */
00217 
00218     bdd_node *Correction;
00219     bdd_node *q;
00220     bdd_node *matrix;
00221     bdd_node *piAddCube;
00222 
00223     bdd_ref(piAddCube = bdd_add_compute_cube(manager,piAddVars,NULL,nPi));
00224 
00225     /* Create the transition matrix either with equiprobable
00226        or specific input probs. */
00227 
00228     if (inputProb != NULL) {
00229       bdd_ref(matrix = Mark_addInProb(manager,func,piAddCube,inputProb));
00230     }
00231     else {
00232       /* create correction ADD and print it */
00233       bdd_ref(Correction = bdd_add_const(manager,
00234                                           (scale/(double)(1 << nPi))));
00235       
00236       bdd_ref(q = bdd_add_exist_abstract(manager,func,piAddCube));
00237       
00238       /* apply correction to the transition relation matrix and print it */
00239       bdd_ref(matrix = bdd_add_apply(manager, bdd_add_times,q, Correction));
00240       bdd_recursive_deref( manager,Correction);
00241       bdd_recursive_deref(manager,q);
00242     }
00243     bdd_recursive_deref(manager,piAddCube);
00244     bdd_deref(matrix);
00245     return(matrix);
00246 }
00247 
00248