/* Take a step in migration by choosing optimal bucket to transfer */ static double migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, const PRICEMAT *pm) { const double maxamt = 1./(double)pm->ncols; const double minamt = maxamt*5e-6; double *src_cost; struct { int s, d; /* source and destination */ double price; /* price estimate per amount moved */ double amt; /* amount we can move */ } cur, best; int i; /* allocate cost array */ src_cost = (double *)malloc(sizeof(double)*pm->nrows); if (src_cost == NULL) { fprintf(stderr, "%s: Out of memory in migration_step()\n", progname); exit(1); } for (i = pm->nrows; i--; ) /* starting costs for diff. */ src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i); /* find best source & dest. */ best.s = best.d = -1; best.price = FHUGE; best.amt = 0; for (cur.s = pm->nrows; cur.s--; ) { double cost_others = 0; if (src_rem[cur.s] <= minamt) continue; /* examine cheapest dest. */ for (i = 0; i < pm->ncols; i++) if (dst_rem[ cur.d = psortrow(pm,cur.s)[i] ] > minamt) break; if (i >= pm->ncols) break; if ((cur.price = pricerow(pm,cur.s)[cur.d]) >= best.price) continue; /* no point checking further */ cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ? src_rem[cur.s] : dst_rem[cur.d]; if (cur.amt > maxamt) cur.amt = maxamt; dst_rem[cur.d] -= cur.amt; /* add up differential costs */ for (i = pm->nrows; i--; ) if (i != cur.s) cost_others += min_cost(src_rem[i], dst_rem, pm, i) - src_cost[i]; dst_rem[cur.d] += cur.amt; /* undo trial move */ cur.price += cost_others/cur.amt; /* adjust effective price */ if (cur.price < best.price) /* are we better than best? */ best = cur; } free(src_cost); /* finish up */ if ((best.s < 0) | (best.d < 0)) /* nothing left to move? */ return(.0); /* else make the actual move */ mtx_coef(mig,best.s,best.d) += best.amt; src_rem[best.s] -= best.amt; dst_rem[best.d] -= best.amt; return(best.amt); }
int min_cost(int i,int j){ if(i==m-1 && j==n-1) return a[i][j]; if(i>m-1 || j>n-1) return INT_MAX; return a[i][j]+min(min_cost(i+1,j),min(min_cost(i,j+1),min_cost(i+1,j+1))); }
void main(){ printf("Minimum cost path is %d",min_cost(0,0));; }
int main(){ int n; // char a[]="0101110"; int a[]={1,3,7}; n=strlen(a); printf("%.04f",min_cost(a,3,0,2)); return 0; }
double min_cost(int a[],int n,int p,int q){ int i; double mint; if(p>n-1 || q>n-1)return 9999.0000; if(p>q)return 9999.0000; if(q-p==1)return sqrt(a[q]-a[p]+1); if(p==q)return sqrt(1.0000); mint=9999.0000; for(i=p;i<n-1;i++) mint=minp(mint,minp(sqrt((double)a[q]-a[p]+1),min_cost(a,n,p,i)+min_cost(a,n,i+1,q))); return mint; }
double min_cost_util(char *a,int n){ int i,j; int *b=(int*)malloc(sizeof(int)*n); j=0; for(i=0;i<n;i++) { if(*(a+i)=='0'){ // printf("%d",i); *(b+j)=i+1; j++;} } // return 2.0000; return min_cost(b,j,0,j-1); }
void mipb_frac::load() { int min_tone,min_user; FILE *lead_lag_fp; FILE *p_fp; lead_lag_fp=fopen("../data/mipb_lag_lead.txt","w"); p_fp=fopen("../data/mipb_p_against_b.txt","w"); if (p_fp == NULL || lead_lag_fp == NULL) { printf("Boom!\n"); exit(2); } init_cost_matrix(); while(1) { min_cost(&min_tone,&min_user); //printf("Min tone/user was %d/%d\n",min_tone,min_user); //getchar(); //if (min_tone == 115 && min_user == 1) // getchar(); if (_all_tones_full) { printf("All tones full\n"); if (_graph_loading) { write_current_stats(min_tone,min_user); } //for (int tone=0;tone<DMTCHANNELS;tone++) { // print_vector(_F[tone],"F"); //} //for (int tone=0;tone<DMTCHANNELS;tone++) { // print_vector(_cost[tone],"cost"); //} //print_vector(_wp,"wp"); break; } _b[min_tone][min_user]+=_bit_inc;; // add bit to min cos tone _b_total[min_user]+=_bit_inc; _total_bits+=_bit_inc; update_power(min_tone,min_user); if (_b[min_tone][min_user] >= MAXBITSPERTONE) { _F[min_tone][min_user] = 1; } //print_vector(_p[min_tone],"p on tone"); //getchar(); //print_vector(_b_total,"b_total"); //printf("total bits = %d\n",_total_bits); //print_vector(_p_used,"p_used"); if (!_greedy) update_wp(); //print_vector(_wp,"wp"); //getchar(); fprintf(lead_lag_fp,"%lf\t",_total_bits); fprintf(p_fp,"%lf\t",_total_bits); for (int user=0;user<lines;user++) { fprintf(lead_lag_fp,"%6.4g\t",_p_used[user]-_p_ave); fprintf(p_fp,"%6.4g\t",_p_used[user]); } fprintf(lead_lag_fp,"\n"); fprintf(p_fp,"\n"); if ((int)_total_bits % 5 == 0) { if (_graph_loading) { write_current_stats(min_tone,min_user); } } for (int user=0;user<lines;user++) { calc_delta_p(min_tone,user); } for (int tone=0;tone<DMTCHANNELS;tone++) { for (int user=0;user<lines;user++) { _cost[tone][user] = cf(tone,user); //_cost[tone][user] = var_cf(tone,user); } } dump_cost_matrix(); //print_vector(_cost[min_tone],"New costs"); //getchar(); } fclose(p_fp); fclose(lead_lag_fp); if (_graph_loading) { write_stats_totals(); } }
int multiuser_new::run() { int tone,user,t_line,runs=0; FILE* fp[lines]; bool last_run=false; reset_data(); //fp[0]=fopen("line0_order.txt","w"); //fp[1]=fopen("line1_order.txt","w"); //calc_normalised_cost_matrix(); //print_cost_matrix(0,10); //calc_delta_p_debug=true; for (int user=0;user<lines;user++) { printf("rate target on line %d is %d\n",user,b_target[user]); if (b_target[user]!=NOT_SET) { t_line=user; } } while((abs(b_target[t_line] - b_total[t_line]) > 5) || last_run) { reset_data(); for (tone=0;tone<DMTCHANNELS;tone++) { for(user=0;user<lines;user++) { calc_delta_p_cost(tone,user); } } //print_cost_matrix(0,DMTCHANNELS-1); while(1) { min_cost(&tone,&user); if(all_tones_full) { break; } b[tone][user]++; // add bit to min cos tone b_total[user]++; total_bits++; update_power(tone,user); //update power on tone //fprintf(fp[user],"%d %d\n",tone,total_bits); if (last_run && graph_loading) write_current_stats(tone,user); if (b[tone][user] == MAXBITSPERTONE) { F[tone][user]=1; } if (b_total[user] == b_target[user]) { freeze_user(user); if (last_run) printf("just froze user %d cos he got his target\n",user); } for (int user=0;user<lines;user++) calc_delta_p_cost(tone,user); // update delta_p on all lines for this tone } /*for (int user=0;user<lines;user++) { printf("b_%d = %d\n",user,b_total[user]); }*/ //printf("w = %lf\n",w[t_line]); //getchar(); if (last_run) { printf("this is the last run so setting last_run to false\n"); last_run=false; break; } if (abs(b_target[t_line] - b_total[t_line]) < 5) { printf("Met the rate target, setting the last_run to true\n"); last_run=true; continue; } if (b_total[t_line] > b_target[t_line]) { // if rate > target_rate then this is the min weight // printf("Rate is greater than target, so increase the weight and we have found the min weight\n"); w_min[t_line]=w[t_line]; // set the min weight if (w_max[t_line] < 0) { // dont yet know the max weight // printf("dont yet know the max weight so increase the weight\n"); w[t_line]*=2; } } else { // this is the max weight // printf("Rate is less than target so decrease the weight and we have found the max weight\n"); w_max[t_line]=w[t_line]; if (w_min[t_line] < 0) { // printf("dont yet know the min weight so decrease the weight\n"); w[t_line]/=2; } } if (w_max[t_line] > 0 && w_min[t_line] > 0) { w[t_line] = (w_max[t_line]+w_min[t_line])/2; // printf("Currently bisecting, new weight is %lf\n",w[t_line]); } } //for (int user=0;user<lines;user++) // fclose(fp[user]); init_lines(); calculate_snr(); set_loading_algo(); return 0; }
int multiuser_new::run_a() { int tone,user,t_line,runs=0; FILE* fp; bool last_run=false; reset_data(); //calc_ave_bits(); //calc_ave_xtalk(); /* for (int user=0;user<lines;user++) { double max=0; int line=0; for (int xtalker=0;xtalker<lines;xtalker++) { if (xtalker==user) continue; if (ave_xtalk[user][xtalker] > max) { max = ave_xtalk[user][xtalker]; line = xtalker; } } printf("Worst xtalker into line %d = line %d\n",user,line); } */ //exit(0); //for (int user=0;user<lines;user++) { // printf("rate target on line %d is %d\n",user,b_target[user]); //} do { iter_count++; reset_data(); init_cost_matrix(); //for (int user=0;user<lines;user++) { // printf("w[%d] = %4.2lf\n",user,w[user]); //} while(1) { min_cost(&tone,&user); printf("Min tone/user was %d/%d\n",tone,user); //getchar(); //if (tone == 115 && user == 1) { // getchar(); //} if(all_tones_full) { printf("All tones are full on iteration number %d\n",iter_count); break; } b[tone][user]++; // add bit to min cos tone b_total[user]++; total_bits++; //printf("Added a bit to user %d tone %d\n",user,tone); //print_vector(cost[tone],"Costs on current tone"); update_power(tone,user); //update power on tone //print_vector(p[tone],"p on tone"); //getchar(); print_vector(b_total,"b_total"); printf("total bits = %d\n",total_bits); print_vector(p_used,"p_used"); if (!greedy) update_wp(); //print_vector(wp,"wp"); //getchar(); if (graph_loading) write_current_stats(tone,user); /* if (b[tone][user] == MAXBITSPERTONE) { F[tone][user]=1; } if (!rate_targets_off) { if (b_total[user] == b_target[user]) { freeze_user(user); printf("just froze user %d cos he got his target\n",user); printf("Power used by user %d when frozen = %lf\n",user,p_used[user]); init_cost_matrix(); user_re_init[user]=true; } } */ /*for (int user=0;user<lines;user++) { if (user_frozen[user] == true && user_re_init[user] == false) { init_cost_matrix(); user_re_init[user]=true; } }*/ /* bool update=false; for (int user1=0;user1<lines;user1++) { if (wp[user1] > 1.1) { update=true; break; } } */ for (int user=0;user<lines;user++) { calc_delta_p(tone,user); // update delta_p on all lines for this tone //cost[tone][user] = cf(tone,user); } //print_vector(_delta_p[tone][user],"Delta of adding another bit"); for (int tone=0;tone<DMTCHANNELS;tone++) { for (int user=0;user<lines;user++) { cost[tone][user] = cf(tone,user); } } //print_vector(cost[tone],"New costs"); //getchar(); } for (int user=0;user<lines;user++) { printf("rate on line %d is %d\n",user,b_total[user]); } //getchar(); calc_lost_bits(); sort_lost_bits(); print_lost_bits(); if (0) { char tag[100]; sprintf(tag,"iteration%d",iter_count); init_lines(); calculate_snr(); for (int user=0;user<lines;user++) { write_mw_stats(user,tag); } } update_w(); //getchar(); } while(!finished()); if (all_rates_good()) { printf("All lines met their rate target\n"); } else { double sum=0; for (int user=0;user<lines;user++) { sum+=pow(b_total[user]-b_target[user],2); } printf("Total distance from target = %lf\n",sqrt(sum)); } //isb_optimise_p(); init_lines(); calculate_snr(); set_loading_algo(); printf("max lead = %6.4lf max lag = %6.4lf\n",max_lead,max_lag); if (graph_loading) { write_stats_totals(); } return 0; }
/* * optimally partition an input sequence with random ks */ optimal_partition(const std::vector<element_t>& seq, cost_t fixedCost = 64) { FILE* stat = fopen("./share/oploops", "a"); ASSERT(seq.size() != 0); //所有元素均使用32bit表示 std::vector<element_t>::const_iterator begin = seq.begin(); cost_t single_blcok_cost = seq.size() * 32; std::vector<cost_t> min_cost(seq.size() + 1, single_blcok_cost); min_cost[0] = 0; // create the required window: one for each power of approx_factor std::vector<cost_window> windows; cost_t cost_lower_bound = fixedCost; cost_t cost_bound = cost_lower_bound; while (/*eps1 == 0 ||*/cost_bound < cost_lower_bound / eps1) { //FIXME 由于begin作为每个窗口的滑动指针,如果是引用传递,那么操作一个begin++ // 会不会导致所有的begin都向前移动? windows.emplace_back(begin, cost_bound); if (cost_bound >= single_blcok_cost) break; cost_bound = cost_bound * (1 + eps2); } // step 2: calculate optimal path std::vector<posIndex_t> path(seq.size() + 1, 0); std::vector<element_t> bPath(seq.size() + 1, 0); //XXX 1 in 3 int ind = 0; bool firsttime; uint32_t firsttimeB = 0; for (posIndex_t i = 0; i < seq.size(); i++) { posIndex_t last_end = i + 1; element_t maxB = firsttimeB; firsttime = true; for (auto& window : windows) { assert(window.start == i); while (window.end < last_end) { window.advance_end(); } cost_t window_cost; //从当前window的begin到endIndex之间计算maximum bitwidth if (UNLIKELY(maxB == 0)) { for (posIndex_t i = 0; i < window.size(); i++) maxB = maxB > *(window.start_it + i) ? maxB : *(window.start_it + i); } while (true) { //XXX 2 in 3 ind++; maxB = maxB > window.end_p ? maxB : window.end_p; window_cost = window.size() * maxB; if (min_cost[i] + window_cost < min_cost[window.end]) { min_cost[window.end] = min_cost[i] + window_cost; path[window.end] = i; bPath[window.end] = maxB; } last_end = window.end; if (window.end == seq.size()) break; if (window_cost >= window.cost_upper_bound) break; window.advance_end(); } if (firsttime) { firsttime = false; if (maxB == window.start_p) firsttimeB = 0; else firsttimeB = maxB; } window.advance_start(); } } /* for (int i = 0; i < bPath.size(); i++) std::cout <<i<<":"<< bPath[i] << std::endl;*/ //XXX 3 in 3 // std::cout << "times of loop: " << ind << std::endl; fwrite(&ind, 4, 1, stat); fflush(stat); fclose(stat); posIndex_t curr_pos = seq.size(); posIndex_t last_pos = curr_pos; while (curr_pos != 0) { partition.emplace_back(curr_pos); Bs.emplace_back(bPath[curr_pos]); last_pos = curr_pos; curr_pos = path[curr_pos]; Ks.emplace_back(last_pos - curr_pos); } partition.emplace_back(0); std::reverse(partition.begin(), partition.end()); std::reverse(Bs.begin(), Bs.end()); std::reverse(Ks.begin(), Ks.end()); cost_opt = min_cost[seq.size()]; }
/* * Optimally partition an input sequence with limited options for k * seq represents the input integer sequence * lens represents the array containning the options */ optimal_partition(const std::vector<element_t>& seq, const element_t *lens, const uint32_t size, cost_t fixedCost = 64) { ASSERT(seq.size() != 0); //所有元素均使用32bit表示 std::vector<element_t>::const_iterator begin = seq.begin(); cost_t single_blcok_cost = seq.size() * 32; std::vector<cost_t> min_cost(seq.size() + 1, single_blcok_cost); min_cost[0] = 0; // create the required window: one for each power of approx_factor std::vector<cost_window> windows; cost_t cost_lower_bound = fixedCost; cost_t cost_bound = cost_lower_bound; while (/*eps1 == 0 ||*/cost_bound < cost_lower_bound / eps1) { //FIXME 由于begin作为每个窗口的滑动指针,如果是引用传递,那么操作一个begin++ // 会不会导致所有的begin都向前移动? windows.emplace_back(begin, cost_bound); if (cost_bound >= single_blcok_cost) break; cost_bound = cost_bound * (1 + eps2); } // step 2: calculate optimal path std::vector<posIndex_t> path(seq.size() + 1, 0); std::vector<element_t> bPath(seq.size() + 1, 0); for (posIndex_t i = 0; i < seq.size(); i++) { posIndex_t last_end = i + 1; // for each window search for minimum cost path for (auto& window : windows) { assert(window.start == i); while (window.end < last_end) { window.advance_end(); } if (window.size() <= lens[size - 1]) { cost_t window_cost; //从当前window的begin到endIndex之间计算maximum bitwidth element_t maxB = 0; for (posIndex_t g = 0; g < window.size(); g++) maxB = maxB > *(window.start_it + g) ? maxB : *(window.start_it + g); int l = 0; while (l < size) { if (window.size() != lens[l]) if (window.end != seq.size()) { last_end = window.end; window.advance_end(); maxB = maxB > window.end_p ? maxB : window.end_p; continue; } else break; else l++; window_cost = window.size() * maxB; if (min_cost[i] + window_cost < min_cost[window.end]) { min_cost[window.end] = min_cost[i] + window_cost; path[window.end] = i; bPath[window.end] = maxB; } if (window_cost >= window.cost_upper_bound) break; } } window.advance_start(); } } // for (int i = 0; i < bPath.size(); i++) // std::cout <<i<<":"<< bPath[i] << std::endl; posIndex_t curr_pos = seq.size(); posIndex_t last_pos = curr_pos; while (curr_pos != 0) { partition.emplace_back(curr_pos); Bs.emplace_back(bPath[curr_pos]); last_pos = curr_pos; curr_pos = path[curr_pos]; Ks.emplace_back(last_pos - curr_pos); } partition.emplace_back(0); std::reverse(partition.begin(), partition.end()); std::reverse(Bs.begin(), Bs.end()); std::reverse(Ks.begin(), Ks.end()); cost_opt = min_cost[seq.size()]; }
int main(){ int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 }; int n = sizeof(arr)/sizeof(arr[0]); printf("Length of LIS is %d\n", longest_increasing_subsequence( arr, n ) ); { char X[] = "AGGTAB"; char Y[] = "GXTXAYB"; int m = strlen(X); int n = strlen(Y); printf("Length of LCS is %d\n", longest_common_subs( X,m, Y, n) ); } { int cost[5][5] = { {1, 2, 3}, {4, 8, 2}, {1, 5, 3} }; printf("\n%d ", min_cost(cost, 2, 2)); } { int arr[] = {1, 2, 5}; int m = sizeof(arr)/sizeof(arr[0]); int n = 11; printf("\ncoin change : %d ", coin_change(arr, m, n)); } { int val[] = {60, 100, 120}; int wt[] = {10, 20, 30}; int W = 50; int n = sizeof(val)/sizeof(val[0]); printf("\n%d", knapsack(wt, W, val, n)); } { char seq[] = "GEEKS FOR GEEKS"; printf ("\nThe lnegth of the LPS is %d", longest_palindromic_subs(seq)); } { int arr[] = {1, 5, 8, 9, 10, 17, 17, 20}; int size = sizeof(arr)/sizeof(arr[0]); printf("\nMaximum Obtainable Value is %d\n", cut_rod(arr, size)); } { int arr[] = {1, 101, 2, 3, 100, 4, 5}; int n = sizeof(arr)/sizeof(arr[0]); printf("\nSum of maximum sum increasing subsequence is %d\n", max_increasing_subs( arr, n ) ); } { int arr[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}; int n = sizeof(arr)/sizeof(arr[0]); printf("\nLength of LBS is %d\n", bitonic_subs( arr, n ) ); } { char str[] = "ababbbabbababa"; printf("Min cuts needed for Palindrome Partitioning is %d", min_pal_partition(str)); } { int arr[] = {1, 3, 6, 1, 0, 9}; int size = sizeof(arr)/sizeof(int); printf("\nMinimum number of jumps to reach end is %d ", min_jumps(arr,size)); } { int n = 150; printf("\n%d ugly number is : %d",n,find_nth_ugly(n)); } { int M[6][5] = {{0, 1, 1, 0, 1}, {1, 1, 0, 1, 0}, {0, 1, 1, 1, 0}, {1, 1, 1, 1, 0}, {1, 1, 1, 1, 1}, {0, 0, 0, 0, 0}}; print_max_square(M,6,5); } { char str[] = "forgeeksskeegfor"; printf("\nLength is: %d\n", print_longest_pal( str ) ); } return 0; }