void sbb_cf_word(struct emuctx *ctx, word val1, word val2) { /* word two_comp ; two_comp.w = (~val2.w) + 1 ; if (get_cf(ctx) == YES) { if ((val1.w + two_comp.w + 0xFFFF) & 0x10000) set_cf(ctx, YES) ; else set_cf(ctx, NO) ; } else { if ((val1.w + two_comp.w) & 0x10000) set_cf(ctx, NO) ; else set_cf(ctx, YES) ; } */ if (get_cf(ctx) == YES) { if ((val1.w - val2.w - 1) & 0x10000) set_cf(ctx, YES) ; else set_cf(ctx, NO) ; } else { if ((val1.w - val2.w) & 0x10000) set_cf(ctx, YES) ; else set_cf(ctx, NO) ; } }
void sbb_cf_byte(struct emuctx *ctx, byte val1, byte val2) { /* byte two_comp = (~val2)+1 ; if (get_cf(ctx)) { if ((val1 + two_comp + 0xff) & 0x100) set_cf(ctx, YES) ; else set_cf(ctx, NO) ; } else { if ((val1 + two_comp) & 0x100) set_cf(ctx, NO) ; else set_cf(ctx, YES) ; } */ if (get_cf(ctx)) { if ((val1 - val2 - 1) & 0x100) set_cf(ctx, YES) ; else set_cf(ctx, NO) ; } else { if ((val1 - val2) & 0x100) set_cf(ctx, YES) ; else set_cf(ctx, NO) ; } }
void sbb_af_byte(struct emuctx *ctx, byte val1, byte val2) { /* byte two_comp = (~val2)+1 ; if (get_cf(ctx)) { if (((val1 & 0x0F) + (two_comp & 0x0F) + 0x0F) & 0x10) set_af(ctx, YES); else set_af(ctx, NO) ; } else { if (((val1 & 0x0F) + (two_comp & 0x0F)) & 0x10) set_af(ctx, NO); else set_af(ctx, YES) ; } */ if (get_cf(ctx)) { if (((val1 & 0x0F) - (val1 & 0x0F) - 1) & 0x10) set_af(ctx, YES); else set_af(ctx, NO) ; } else { if (((val1 & 0x0F) - (val2 & 0x0F)) & 0x10) set_af(ctx, YES); else set_af(ctx, NO) ; } }
void sbb_af_word(struct emuctx *ctx, word val1, word val2) { /* word two_comp ; two_comp.w = (~val2.w) + 1 ; if (get_cf(ctx)) { if (((val1.w & 0x0F) + (two_comp.w & 0x0F) + 0x0F) & 0x10) set_af(ctx, YES) ; else set_af(ctx, NO) ; } else { if (((val1.w & 0x0F) + (two_comp.w & 0x0F)) & 0x10) set_af(ctx, NO) ; else set_af(ctx, YES) ; } */ if (get_cf(ctx)) { if (((val1.w & 0x0F) - (val2.w & 0x0F) - 1) & 0x10) set_af(ctx, YES) ; else set_af(ctx, NO) ; } else { if (((val1.w & 0x0F) - (val2.w & 0x0F)) & 0x10) set_af(ctx, YES) ; else set_af(ctx, NO) ; } }
void sbb_of_byte(struct emuctx *ctx, byte val1, byte val2) { byte two_comp = (~val2)+1 ; if (get_cf(ctx)) { byte result = val1 + two_comp + 0xFF ; two_comp = two_comp + 0xFF ; if (!signed_byte(val1) && !signed_byte(two_comp) && signed_byte(result)) set_of(ctx, YES) ; else if (signed_byte(val1) && signed_byte(two_comp) && !signed_byte(result)) set_of(ctx, YES) ; else set_of(ctx, NO) ; } else { byte result = val1 + two_comp + 0xFF ; if (!signed_byte(val1) && !signed_byte(two_comp) && signed_byte(result)) set_of(ctx, YES) ; else if (signed_byte(val1) && signed_byte(two_comp) && !signed_byte(result)) set_of(ctx, YES) ; else set_of(ctx, NO) ; } }
void sbb_of_word(struct emuctx *ctx, word val1, word val2) { word two_comp ; two_comp.w = (~val2.w) + 1 ; if (get_cf(ctx) == YES) { word result ; result.w = val1.w + two_comp.w + 0xFFFF ; two_comp.w = two_comp.w + 0xFFFF ; if (!signed_word(val1) && !signed_word(two_comp) && signed_word(result)) set_of(ctx, YES) ; else if (signed_word(val1) && signed_word(two_comp) && !signed_word(result)) set_of(ctx,YES) ; else set_of(ctx, NO) ; } else { word result; result.w = val1.w + two_comp.w ; if (!signed_word(val1) && !signed_word(two_comp) && signed_word(result)) set_of(ctx, YES) ; else if (signed_word(val1) && signed_word(two_comp) && !signed_word(result)) set_of(ctx,YES) ; else set_of(ctx, NO) ; } }
int main( int argc, char **argv ) { if ( argc < 2 ) { cout << "something wrong" << endl; exit(1); } else { read_parameters(argc,argv); } Timer initialization_timer; rnd = new Random((unsigned) time(&t)); graph = new UndirectedGraph(i_file); init_pheromone_trail(); register int i = 0; register int j = 0; register int k = 0; register int b = 0; cout << endl; for (int card_counter = cardb; card_counter <= carde; card_counter++) { cardinality = card_counter; if ((cardinality == cardb) || (cardinality == carde) || ((cardinality % cardmod) == 0)) { printf("begin cardinality %d\n",cardinality); if (tfile_is_given) { if (times.count(cardinality) == 1) { time_limit = times[cardinality]; } } vector<double> results; vector<double> times_best_found; double biar = 0.0; int n_of_ants = (int)(((double)((*graph).edges).size()) / ((double)cardinality)); if (n_of_ants < 15) { n_of_ants = 15; } if (n_of_ants > 50) { n_of_ants = 50; } if (ants.size() > 0) { for (list<KCT_Ant*>::iterator anAnt = ants.begin(); anAnt != ants.end(); anAnt++) { delete(*anAnt); } } ants.clear(); for (i = 0; i < n_of_ants; i++) { KCT_Ant* ant = new KCT_Ant(); ant->initialize(graph,rnd,pheromone,daemon_action); ants.push_back(ant); } for (int trial_counter = 1; trial_counter <= n_of_trials; trial_counter++) { printf("begin try %d\n",trial_counter); UndirectedGraph* best = NULL; UndirectedGraph* newSol = NULL; UndirectedGraph* restartBest = NULL; double ib_weight = 0.0; double rb_weight = 0.0; double gb_weight = 0.0; Timer timer; if ((!(card_counter == cardb)) || (!(trial_counter == 1))) { reset_pheromone_trail(); for (list<KCT_Ant*>::iterator ant = ants.begin(); ant != ants.end(); ant++) { (*ant)->restart_reset(); } } int iter = 1; double cf = 0.0; bool restart = false; bool program_stop = false; bool bs_update = false; while (program_stop == false) { for (list<KCT_Ant*>::iterator ant = ants.begin(); ant != ants.end(); ant++) { for (k = 0; k < cardinality; k++) { (*ant)->step(0.8); } (*ant)->evaluate(); } if (!(newSol == NULL)) { delete(newSol); } if (elite_action == "no") { newSol = getBestDaemonSolutionInIteration(); } else { KCT_Ant* bestAnt = getBestDaemonAntInIteration(); bestAnt->eliteAction(); newSol = new UndirectedGraph(); newSol->copy(bestAnt->getCurrentDaemonSolution()); } if (iter == 1) { best = new UndirectedGraph(); printf("best %f\ttime %f\n",newSol->weight(),timer.elapsed_time(Timer::VIRTUAL)); best->copy(newSol); restartBest = new UndirectedGraph(newSol); results.push_back(newSol->weight()); times_best_found.push_back(timer.elapsed_time(Timer::VIRTUAL)); if (trial_counter == 1) { biar = newSol->weight(); } else { if (newSol->weight() < biar) { biar = newSol->weight(); } } } else { if (restart) { restart = false; restartBest->copy(newSol); if (newSol->weight() < best->weight()) { printf("best %f\ttime %f\n",newSol->weight(),timer.elapsed_time(Timer::VIRTUAL)); best->copy(newSol); results[trial_counter-1] = newSol->weight(); times_best_found[trial_counter-1] = timer.elapsed_time(Timer::VIRTUAL); if (newSol->weight() < biar) { biar = newSol->weight(); } } } else { if (newSol->weight() < restartBest->weight()) { restartBest->copy(newSol); } if (newSol->weight() < best->weight()) { printf("best %f\ttime %f\n",newSol->weight(),timer.elapsed_time(Timer::VIRTUAL)); best->copy(newSol); results[trial_counter-1] = newSol->weight(); times_best_found[trial_counter-1] = timer.elapsed_time(Timer::VIRTUAL); if (newSol->weight() < biar) { biar = newSol->weight(); } } } } cf = get_cf(newSol); //cout << "cf: " << cf << endl; if (bs_update && (cf > 0.99)) { //cout << "doing restart" << endl; bs_update = false; restart = true; cf = 0.0; reset_pheromone_trail(); } else { if (cf > 0.99) { bs_update = true; } if (!bs_update) { if (cf < 0.7) { l_rate = 0.15; ib_weight = 2.0/3.0; rb_weight = 1.0/3.0; gb_weight = 0.0; } if ((cf >= 0.7) && (cf < 0.95)) { l_rate = 0.1; ib_weight = 1.0 / 3.0; rb_weight = 2.0 / 3.0; gb_weight = 0.0; } if (cf >= 0.95) { l_rate = 0.05; ib_weight = 0.0; rb_weight = 1.0; gb_weight = 0.0; } } else { // if bs_update = TRUE we use the best_so_far solution for updating the pheromone values l_rate = 0.1; ib_weight = 0.0; rb_weight = 0.0; gb_weight = 1.0; } map<Edge*,double>* trans_best = translate_solution(best); map<Edge*,double>* trans_newSol = translate_solution(newSol); map<Edge*,double>* trans_restartBest = translate_solution(restartBest); map<Edge*,double>* new_pd = new map<Edge*,double>; for (list<Edge*>::iterator e = (graph->edges).begin(); e != (graph->edges).end(); e++) { (*new_pd)[*e] = 0.0; (*new_pd)[*e] = (*new_pd)[*e] + (ib_weight * (*trans_newSol)[*e]) + (rb_weight * (*trans_restartBest)[*e]) + (gb_weight * (*trans_best)[*e]); } for (list<Edge*>::iterator e = (graph->edges).begin(); e != (graph->edges).end(); e++) { (*pheromone)[*e] = (*pheromone)[*e] + (l_rate * ((*new_pd)[*e] - (*pheromone)[*e])); if ((*pheromone)[*e] > tau_max) { (*pheromone)[*e] = tau_max; } if ((*pheromone)[*e] < tau_min) { (*pheromone)[*e] = tau_min; } } delete(trans_best); delete(trans_newSol); delete(trans_restartBest); delete(new_pd); } for (list<KCT_Ant*>::iterator i = ants.begin(); i != ants.end(); i++) { (*i)->reset(); } iter = iter + 1; if (tfile_is_given) { if (timer.elapsed_time(Timer::VIRTUAL) > time_limit) { program_stop = true; } } else { if (time_limit_given && iter_limit_given) { if ((timer.elapsed_time(Timer::VIRTUAL) > time_limit) || (iter > n_of_iter)) { program_stop = true; } } else { if (time_limit_given) { if (timer.elapsed_time(Timer::VIRTUAL) > time_limit) { program_stop = true; } } else { if (iter > n_of_iter) { program_stop = true; } } } } } printf("end try %d\n",trial_counter); // eturetken 18.09.09. Write the best MST for this cardinality to the file. //////////////////////////////////////////////////////////////////// if( mstfile_is_given ) { string MSTFile(mst_file); best->Write2File(concatIntToString(MSTFile, card_counter) + ".mst"); } //////////////////////////////////////////////////////////////////// delete(best); delete(restartBest); delete(newSol); } double r_mean = 0.0; double t_mean = 0.0; for (int i = 0; i < results.size(); i++) { r_mean = r_mean + results[i]; t_mean = t_mean + times_best_found[i]; } r_mean = r_mean / ((double)results.size()); t_mean = t_mean / ((double)times_best_found.size()); double rsd = 0.0; double tsd = 0.0; for (int i = 0; i < results.size(); i++) { rsd = rsd + pow(results[i]-r_mean,2.0); tsd = tsd + pow(times_best_found[i]-t_mean,2.0); } rsd = rsd / ((double)(results.size()-1.0)); if (rsd > 0.0) { rsd = sqrt(rsd); } tsd = tsd / ((double)(times_best_found.size()-1.0)); if (tsd > 0.0) { tsd = sqrt(tsd); } if (output_file_given == true) { fout << cardinality << "\t" << r_mean << "\t" << rsd << "\t" << t_mean << "\t" << tsd << endl; } else { printf("statistics\t%d\t%g\t%f\t%f\t%f\t%f\n",cardinality,biar,r_mean,rsd,t_mean,tsd); } printf("end cardinality %d\n",cardinality); } } if (output_file_given == true) { fout.close(); } delete(graph); delete rnd; }
void word_af_adc(struct emuctx *ctx, word left_reg, word right_reg) { if (((left_reg.w & 0x0F) + (right_reg.w & 0x0F) + get_cf(ctx)) & 0x10) set_af(ctx, YES) ; else set_af(ctx, NO) ; }