static void add_constraint(int col,int idx,int next_expr(int)) {int i,nextexpr; for(i=0;idx>=0;i++){ nextexpr=next_expr(i); xassert(nextexpr==0); if(entropy_expr.type == ent_Markov){ if(idx>entropy_expr.n-3){ idx -= entropy_expr.n-2; continue; } int v1,v2,v; int j; // add idx-th Markov equality v=v1=v2=0; for(j=0;j<entropy_expr.n;j++){ if(j<idx+1) v1 |= entropy_expr.item[j].var; else if(j>idx+1) v2 |= entropy_expr.item[j].var; else v=entropy_expr.item[j].var; } // (v1,v2|v)=0 row_idx[1]=varidx(v1|v); row_val[1]=1.0; row_idx[2]=varidx(v2|v); row_val[2]=1.0; row_idx[3]=varidx(v1|v2|v); row_val[3]=-1.0; row_idx[4]=varidx(v); row_val[4]=-1.0; add_column(col,4,GLP_FR); return; } else if(idx>0) { idx--; continue; } else { // add this constraint int j; for(j=0;j<entropy_expr.n;j++){ row_idx[j+1]=varidx(entropy_expr.item[j].var); row_val[j+1]=entropy_expr.item[j].coeff; } add_column(col,entropy_expr.n, entropy_expr.type==ent_eq?GLP_FR : GLP_LO); return; } } xassert(idx!=idx); }
BString VisionApp::ParseAlias(const char* cmd, const BString& channel) { BString command(GetWord(cmd, 1).ToUpper()); BString newcmd = fAliases[command]; const char* parse = newcmd.String(); int32 replidx(0); int32 varidx(0); newcmd.ReplaceAll("$C", channel.String()); while (*parse != '\0') { if (*parse != '$') { ++parse; continue; } else { replidx = parse - newcmd.String(); ++parse; if (isdigit(*parse)) { varidx = atoi(parse); BString replString = "$"; replString << varidx; if (*(parse + replString.Length() - 1) != '-') { newcmd.ReplaceAll(replString.String(), GetWord(cmd, varidx + 1).String()); } else { replString += "-"; newcmd.ReplaceAll(replString.String(), RestOfString(cmd, varidx + 1).String()); } } } } return newcmd; }
char *call_lp(int next_expr(int)) {int i,constraints; expr_type_t goal_type; char *retval, *retval2; /* initially the expression to be checked is in entropy_expr. determine first the variables */ init_var_assignment(); /* start collecting variables */ add_expr_variables(); /* variables in the expression to be checked */ constraints=0; for(i=0;next_expr(i)==0;i++){ /* go over all constraints */ constraints++; // Markov constraints give several cols if(entropy_expr.type==ent_Markov){ constraints+=entropy_expr.n-3; } add_expr_variables(); } /* figure out final variables, rows, cols, number of Shannon */ if(do_variable_assignment()){ // number of variables is less than 2 return "number of final random variables is less than 2"; } /* get memory for row and column permutation */ cols += constraints; rowperm=malloc((rows+1)*sizeof(int)); colperm=malloc((cols+1)*sizeof(int)); if(!rowperm || !colperm){ if(rowperm){ free(rowperm); rowperm=NULL; } if(colperm){ free(colperm); colperm=NULL; } return "the problem is too large, not enough memory"; } for(i=0;i<=rows;i++) rowperm[i]=i; perm_array(rows+1,rowperm); for(i=0;i<=cols;i++) colperm[i]= i-1; perm_array(cols+1,colperm); /* the expression to be checked, this will be the goal */ if(constraints) next_expr(-1); goal_type=entropy_expr.type; // ent_eq, ent_ge create_glp(); // create a new glp instance for(i=0;i<entropy_expr.n;i++){ row_idx[i+1]=varidx(entropy_expr.item[i].var); row_val[i+1]=entropy_expr.item[i].coeff; } add_goal(entropy_expr.n); // right hand side value // go over the columns add them to the lp instance for(i=1;i<=cols;i++){ int colct=colperm[i]; if(add_shannon(i,colct)){ // this is a constraint add_constraint(i,colct-(shannon+var_no),next_expr); } } /* call the lp */ init_glp_parameters(); if(parm.presolve!=GLP_ON) // generate the first basis glp_adv_basis(P,0); retval=invoke_lp(); // call again with -1.0 when checking for ent_eq if(goal_type==ent_eq && (retval==EXPR_TRUE || retval==EXPR_FALSE)){ next_expr(-1); // reload the problem for(i=0;i<entropy_expr.n;i++){ row_idx[i+1]=varidx(entropy_expr.item[i].var); row_val[i+1]=-entropy_expr.item[i].coeff; } add_goal(entropy_expr.n); // right hand side value retval2=invoke_lp(); if(retval2==EXPR_TRUE){ if(retval==EXPR_FALSE) retval=EQ_LE_ONLY; } else if(retval2==EXPR_FALSE){ if(retval==EXPR_TRUE) retval=EQ_GE_ONLY; } else { retval=retval2; } } /* release allocated memory */ release_glp(); if(rowperm){ free(rowperm); rowperm=NULL; } if(colperm){ free(colperm); colperm=NULL; } return retval; }